Search in sources :

Example 1 with TokenResponse

use of io.jans.ca.plugin.adminui.model.auth.TokenResponse in project jans by JanssenProject.

the class OAuth2Service method getToken.

public io.jans.as.client.TokenResponse getToken(TokenRequest tokenRequest, String tokenEndpoint, String userInfoJwt) {
    ApacheHttpClient43Engine engine = new ApacheHttpClient43Engine();
    try {
        engine.setFollowRedirects(false);
        MultivaluedMap<String, String> body = new MultivaluedHashMap<>();
        if (!Strings.isNullOrEmpty(tokenRequest.getCode())) {
            body.putSingle("code", tokenRequest.getCode());
        }
        if (!Strings.isNullOrEmpty(tokenRequest.getScope())) {
            body.putSingle("scope", tokenRequest.getScope());
        }
        if (!Strings.isNullOrEmpty(userInfoJwt)) {
            body.putSingle("ujwt", userInfoJwt);
        }
        body.putSingle("grant_type", tokenRequest.getGrantType().getValue());
        body.putSingle("redirect_uri", tokenRequest.getRedirectUri());
        body.putSingle("client_id", tokenRequest.getAuthUsername());
        ResteasyClient client = ((ResteasyClientBuilder) ClientBuilder.newBuilder()).httpEngine(engine).build();
        ResteasyWebTarget target = client.target(UriBuilder.fromPath(tokenEndpoint));
        Response response = target.request().header("Authorization", "Basic " + tokenRequest.getEncodedCredentials()).post(Entity.form(body));
        log.debug("Get Access Token status code: {}", response.getStatus());
        if (response.getStatus() == 200) {
            String entity = response.readEntity(String.class);
            io.jans.as.client.TokenResponse tokenResponse = new io.jans.as.client.TokenResponse();
            tokenResponse.setEntity(entity);
            tokenResponse.injectDataFromJson(entity);
            return tokenResponse;
        }
    } catch (Exception e) {
        log.error("Problems processing token call");
        throw e;
    } finally {
        engine.close();
    }
    return null;
}
Also used : ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) ApacheHttpClient43Engine(org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient43Engine) ApplicationException(io.jans.ca.plugin.adminui.model.exception.ApplicationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ErrorResponse(io.jans.ca.plugin.adminui.utils.ErrorResponse) TokenResponse(io.jans.ca.plugin.adminui.model.auth.TokenResponse) UserInfoResponse(io.jans.ca.plugin.adminui.model.auth.UserInfoResponse) Response(javax.ws.rs.core.Response) TokenResponse(io.jans.ca.plugin.adminui.model.auth.TokenResponse) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget)

Example 2 with TokenResponse

use of io.jans.ca.plugin.adminui.model.auth.TokenResponse in project jans by JanssenProject.

the class OAuth2Resource method getApiProtectionToken.

@GET
@Path(OAUTH2_API_PROTECTION_TOKEN)
@Produces(MediaType.APPLICATION_JSON)
public Response getApiProtectionToken(@QueryParam("ujwt") String ujwt) {
    try {
        log.info("Api protection token request to Auth Server.");
        TokenResponse tokenResponse = oAuth2Service.getApiProtectionToken(ujwt);
        log.info("Api protection token received from Auth Server.");
        return Response.ok(tokenResponse).build();
    } catch (ApplicationException e) {
        log.error(ErrorResponse.GET_API_PROTECTION_TOKEN_ERROR.getDescription(), e);
        return Response.status(e.getErrorCode()).entity(e.getMessage()).build();
    } catch (Exception e) {
        log.error(ErrorResponse.GET_API_PROTECTION_TOKEN_ERROR.getDescription(), e);
        return Response.serverError().entity(e.getMessage()).build();
    }
}
Also used : ApplicationException(io.jans.ca.plugin.adminui.model.exception.ApplicationException) TokenResponse(io.jans.ca.plugin.adminui.model.auth.TokenResponse) ApplicationException(io.jans.ca.plugin.adminui.model.exception.ApplicationException)

Example 3 with TokenResponse

use of io.jans.ca.plugin.adminui.model.auth.TokenResponse in project jans by JanssenProject.

the class OAuth2Resource method getAccessToken.

@GET
@Path(OAUTH2_ACCESS_TOKEN)
@Produces(MediaType.APPLICATION_JSON)
public Response getAccessToken(@QueryParam("code") String code) {
    try {
        log.info("Access token request to Auth Server.");
        TokenResponse tokenResponse = oAuth2Service.getAccessToken(code);
        log.info("Access token received from Auth Server.");
        return Response.ok(tokenResponse).build();
    } catch (ApplicationException e) {
        log.error(ErrorResponse.GET_ACCESS_TOKEN_ERROR.getDescription(), e);
        return Response.status(e.getErrorCode()).entity(e.getMessage()).build();
    } catch (Exception e) {
        log.error(ErrorResponse.GET_ACCESS_TOKEN_ERROR.getDescription(), e);
        return Response.serverError().entity(e.getMessage()).build();
    }
}
Also used : ApplicationException(io.jans.ca.plugin.adminui.model.exception.ApplicationException) TokenResponse(io.jans.ca.plugin.adminui.model.auth.TokenResponse) ApplicationException(io.jans.ca.plugin.adminui.model.exception.ApplicationException)

Example 4 with TokenResponse

use of io.jans.ca.plugin.adminui.model.auth.TokenResponse in project jans by JanssenProject.

the class OAuth2Service method getAccessToken.

/**
 * Calls token endpoint from the Identity Provider and returns a valid Access Token.
 */
public TokenResponse getAccessToken(String code) throws ApplicationException {
    try {
        log.debug("Getting access token with code");
        if (Strings.isNullOrEmpty(code)) {
            log.error(ErrorResponse.AUTHORIZATION_CODE_BLANK.getDescription());
            throw new ApplicationException(Response.Status.BAD_REQUEST.getStatusCode(), ErrorResponse.AUTHORIZATION_CODE_BLANK.getDescription());
        }
        AUIConfiguration auiConfiguration = auiConfigurationService.getAUIConfiguration();
        TokenRequest tokenRequest = new TokenRequest(GrantType.AUTHORIZATION_CODE);
        tokenRequest.setCode(code);
        tokenRequest.setAuthUsername(auiConfiguration.getAuthServerClientId());
        tokenRequest.setAuthPassword(auiConfiguration.getAuthServerClientSecret());
        tokenRequest.setGrantType(GrantType.AUTHORIZATION_CODE);
        tokenRequest.setRedirectUri(auiConfiguration.getAuthServerRedirectUrl());
        tokenRequest.setScope(auiConfiguration.getAuthServerScope());
        io.jans.as.client.TokenResponse tokenResponse = getToken(tokenRequest, auiConfiguration.getAuthServerTokenEndpoint());
        TokenResponse tokenResp = new TokenResponse();
        tokenResp.setAccessToken(tokenResponse.getAccessToken());
        tokenResp.setIdToken(tokenResponse.getIdToken());
        tokenResp.setRefreshToken(tokenResponse.getRefreshToken());
        return tokenResp;
    } catch (ApplicationException e) {
        log.error(ErrorResponse.GET_ACCESS_TOKEN_ERROR.getDescription());
        throw e;
    } catch (Exception e) {
        log.error(ErrorResponse.GET_ACCESS_TOKEN_ERROR.getDescription(), e);
        throw new ApplicationException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ErrorResponse.GET_ACCESS_TOKEN_ERROR.getDescription());
    }
}
Also used : ApplicationException(io.jans.ca.plugin.adminui.model.exception.ApplicationException) TokenResponse(io.jans.ca.plugin.adminui.model.auth.TokenResponse) AUIConfiguration(io.jans.ca.plugin.adminui.model.config.AUIConfiguration) TokenRequest(io.jans.as.client.TokenRequest) ApplicationException(io.jans.ca.plugin.adminui.model.exception.ApplicationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 5 with TokenResponse

use of io.jans.ca.plugin.adminui.model.auth.TokenResponse in project jans by JanssenProject.

the class OAuth2Service method getApiProtectionToken.

/**
 * Calls token endpoint from the Identity Provider and returns a valid Access Token.
 */
public TokenResponse getApiProtectionToken(String userInfoJwt) throws ApplicationException {
    try {
        log.debug("Getting api-protection token");
        AUIConfiguration auiConfiguration = auiConfigurationService.getAUIConfiguration();
        TokenRequest tokenRequest = new TokenRequest(GrantType.CLIENT_CREDENTIALS);
        tokenRequest.setAuthUsername(auiConfiguration.getTokenServerClientId());
        tokenRequest.setAuthPassword(auiConfiguration.getTokenServerClientSecret());
        tokenRequest.setGrantType(GrantType.CLIENT_CREDENTIALS);
        tokenRequest.setRedirectUri(auiConfiguration.getTokenServerRedirectUrl());
        if (Strings.isNullOrEmpty(userInfoJwt)) {
            log.warn(ErrorResponse.USER_INFO_JWT_BLANK.getDescription());
            tokenRequest.setScope(scopeAsString(Arrays.asList(OAuth2Resource.SCOPE_OPENID)));
        }
        io.jans.as.client.TokenResponse tokenResponse = getToken(tokenRequest, auiConfiguration.getTokenServerTokenEndpoint(), userInfoJwt);
        final Jwt tokenJwt = Jwt.parse(tokenResponse.getAccessToken());
        Map<String, Object> claims = getClaims(tokenJwt);
        TokenResponse tokenResp = new TokenResponse();
        tokenResp.setAccessToken(tokenResponse.getAccessToken());
        tokenResp.setIdToken(tokenResponse.getIdToken());
        tokenResp.setRefreshToken(tokenResponse.getRefreshToken());
        final String SCOPE = "scope";
        if (claims.get(SCOPE) instanceof List) {
            tokenResp.setScopes((List) claims.get(SCOPE));
        }
        if (claims.get("iat") != null) {
            tokenResp.setIat(Long.valueOf(claims.get("iat").toString()));
        }
        if (claims.get("exp") != null) {
            tokenResp.setExp(Long.valueOf(claims.get("exp").toString()));
        }
        if (claims.get("iss") != null) {
            tokenResp.setIssuer(claims.get("iss").toString());
        }
        return tokenResp;
    } catch (Exception e) {
        log.error(ErrorResponse.GET_API_PROTECTION_TOKEN_ERROR.getDescription(), e);
        throw new ApplicationException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ErrorResponse.GET_API_PROTECTION_TOKEN_ERROR.getDescription());
    }
}
Also used : Jwt(io.jans.as.model.jwt.Jwt) ApplicationException(io.jans.ca.plugin.adminui.model.exception.ApplicationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ApplicationException(io.jans.ca.plugin.adminui.model.exception.ApplicationException) TokenResponse(io.jans.ca.plugin.adminui.model.auth.TokenResponse) AUIConfiguration(io.jans.ca.plugin.adminui.model.config.AUIConfiguration) TokenRequest(io.jans.as.client.TokenRequest) JSONObject(org.json.JSONObject) List(java.util.List)

Aggregations

TokenResponse (io.jans.ca.plugin.adminui.model.auth.TokenResponse)6 ApplicationException (io.jans.ca.plugin.adminui.model.exception.ApplicationException)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 AUIConfiguration (io.jans.ca.plugin.adminui.model.config.AUIConfiguration)3 TokenRequest (io.jans.as.client.TokenRequest)2 Jwt (io.jans.as.model.jwt.Jwt)2 UserInfoResponse (io.jans.ca.plugin.adminui.model.auth.UserInfoResponse)2 ErrorResponse (io.jans.ca.plugin.adminui.utils.ErrorResponse)2 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)2 Response (javax.ws.rs.core.Response)2 ResteasyClient (org.jboss.resteasy.client.jaxrs.ResteasyClient)2 ResteasyWebTarget (org.jboss.resteasy.client.jaxrs.ResteasyWebTarget)2 ApacheHttpClient43Engine (org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient43Engine)2 List (java.util.List)1 JSONObject (org.json.JSONObject)1