Search in sources :

Example 11 with JWTResponse

use of it.unibo.arces.wot.sepa.commons.response.JWTResponse in project SEPA by arces-wot.

the class DefaultAuthenticationService method requestToken.

public Response requestToken(String authorization, int timeout) {
    logger.log(Level.getLevel("oauth"), "TOKEN_REQUEST: " + authorization);
    CloseableHttpResponse response = null;
    long start = Timings.getTime();
    try {
        URI uri = new URI(oauthProperties.getTokenRequestUrl());
        HttpPost httpRequest = new HttpPost(uri);
        httpRequest.setHeader("Content-Type", "application/json");
        httpRequest.setHeader("Accept", "application/json");
        httpRequest.setHeader("Authorization", authorization);
        // Set timeout
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout).build();
        httpRequest.setConfig(requestConfig);
        try {
            response = httpClient.execute(httpRequest);
        // break;
        } catch (IOException e) {
            logger.error("HTTP EXECUTE: " + e.getMessage());
            return new ErrorResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "HttpExecute", e.getMessage());
        }
        logger.log(Level.getLevel("oauth"), "Response: " + response);
        HttpEntity entity = response.getEntity();
        String jsonResponse = EntityUtils.toString(entity, Charset.forName("UTF-8"));
        EntityUtils.consume(entity);
        // Parse response
        JsonObject json = new JsonParser().parse(jsonResponse).getAsJsonObject();
        if (json.has("error")) {
            Timings.log("TOKEN_REQUEST", start, Timings.getTime());
            ErrorResponse error = new ErrorResponse(json.get("status_code").getAsInt(), json.get("error").getAsString(), json.get("error_description").getAsString());
            return error;
        }
        return new JWTResponse(json);
    } catch (Exception e) {
        logger.error(e.getMessage());
        Timings.log("TOKEN_REQUEST", start, Timings.getTime());
        return new ErrorResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Exception", e.getMessage());
    } finally {
        try {
            if (response != null)
                response.close();
        } catch (IOException e) {
            logger.error(e.getMessage());
            Timings.log("TOKEN_REQUEST", start, Timings.getTime());
            return new ErrorResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "IOException", e.getMessage());
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) RequestConfig(org.apache.http.client.config.RequestConfig) HttpEntity(org.apache.http.HttpEntity) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) URI(java.net.URI) ParseException(org.apache.http.ParseException) SEPASecurityException(it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ErrorResponse(it.unibo.arces.wot.sepa.commons.response.ErrorResponse) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) JsonParser(com.google.gson.JsonParser) JWTResponse(it.unibo.arces.wot.sepa.commons.response.JWTResponse)

Aggregations

ErrorResponse (it.unibo.arces.wot.sepa.commons.response.ErrorResponse)11 JWTResponse (it.unibo.arces.wot.sepa.commons.response.JWTResponse)11 SEPASecurityException (it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException)6 Response (it.unibo.arces.wot.sepa.commons.response.Response)5 Date (java.util.Date)5 JsonObject (com.google.gson.JsonObject)4 JsonParser (com.google.gson.JsonParser)4 RegistrationResponse (it.unibo.arces.wot.sepa.commons.response.RegistrationResponse)4 JOSEException (com.nimbusds.jose.JOSEException)3 BadJOSEException (com.nimbusds.jose.proc.BadJOSEException)3 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)3 SignedJWT (com.nimbusds.jwt.SignedJWT)3 SEPAPropertiesException (it.unibo.arces.wot.sepa.commons.exceptions.SEPAPropertiesException)3 IOException (java.io.IOException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 URISyntaxException (java.net.URISyntaxException)3 ParseException (java.text.ParseException)3 HttpEntity (org.apache.http.HttpEntity)3 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)3 JWSHeader (com.nimbusds.jose.JWSHeader)2