Search in sources :

Example 1 with UserInfoResponse

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

the class OAuth2Resource method getUserInfo.

@POST
@Path(OAUTH2_API_USER_INFO)
@Produces(MediaType.APPLICATION_JSON)
public Response getUserInfo(@Valid @NotNull UserInfoRequest userInfoRequest) {
    try {
        log.info("Get User-Info request to Auth Server.");
        UserInfoResponse userInfoResponse = oAuth2Service.getUserInfo(userInfoRequest);
        log.info("Get User-Info received from Auth Server.");
        return Response.ok(userInfoResponse).build();
    } catch (ApplicationException e) {
        log.error(ErrorResponse.GET_USER_INFO_ERROR.getDescription(), e);
        return Response.status(e.getErrorCode()).entity(e.getMessage()).build();
    } catch (Exception e) {
        log.error(ErrorResponse.GET_USER_INFO_ERROR.getDescription(), e);
        return Response.serverError().entity(e.getMessage()).build();
    }
}
Also used : ApplicationException(io.jans.ca.plugin.adminui.model.exception.ApplicationException) UserInfoResponse(io.jans.ca.plugin.adminui.model.auth.UserInfoResponse) ApplicationException(io.jans.ca.plugin.adminui.model.exception.ApplicationException)

Example 2 with UserInfoResponse

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

the class OAuth2Service method getUserInfo.

public UserInfoResponse getUserInfo(UserInfoRequest userInfoRequest) throws ApplicationException {
    ApacheHttpClient43Engine engine = new ApacheHttpClient43Engine();
    try {
        log.debug("Getting User-Info from auth-server: {}", userInfoRequest.getAccessToken());
        AUIConfiguration auiConfiguration = auiConfigurationService.getAUIConfiguration();
        String accessToken = org.apache.logging.log4j.util.Strings.isNotBlank(userInfoRequest.getAccessToken()) ? userInfoRequest.getAccessToken() : null;
        if (Strings.isNullOrEmpty(userInfoRequest.getCode()) && Strings.isNullOrEmpty(accessToken)) {
            log.error(ErrorResponse.CODE_OR_TOKEN_REQUIRED.getDescription());
            throw new ApplicationException(Response.Status.BAD_REQUEST.getStatusCode(), ErrorResponse.CODE_OR_TOKEN_REQUIRED.getDescription());
        }
        if (org.apache.logging.log4j.util.Strings.isNotBlank(userInfoRequest.getCode()) && org.apache.logging.log4j.util.Strings.isBlank(accessToken)) {
            TokenResponse tokenResponse = getAccessToken(userInfoRequest.getCode());
            accessToken = tokenResponse.getAccessToken();
        }
        log.debug("Access Token : {}", accessToken);
        MultivaluedMap<String, String> body = new MultivaluedHashMap<>();
        body.putSingle("access_token", accessToken);
        ResteasyClient client = ((ResteasyClientBuilder) ClientBuilder.newBuilder()).httpEngine(engine).build();
        ResteasyWebTarget target = client.target(UriBuilder.fromPath(auiConfiguration.getAuthServerUserInfoEndpoint()));
        Response response = target.request().header("Authorization", "Bearer " + accessToken).post(Entity.form(body));
        log.debug("User-Info response status code: {}", response.getStatus());
        if (response.getStatus() == 200) {
            String entity = response.readEntity(String.class);
            log.debug("User-Info response entity: {}", entity);
            final Jwt jwtUserInfo = Jwt.parse(entity);
            log.debug("User-Info response jwtUserInfo: {}", jwtUserInfo);
            UserInfoResponse userInfoResponse = new UserInfoResponse();
            userInfoResponse.setClaims(getClaims(jwtUserInfo));
            userInfoResponse.setJwtUserInfo(entity);
            log.debug("User-Info response userInfoResponse: {}", userInfoResponse);
            return userInfoResponse;
        }
    } catch (ApplicationException e) {
        log.error(ErrorResponse.GET_USER_INFO_ERROR.getDescription());
        throw e;
    } catch (Exception e) {
        log.error(ErrorResponse.GET_USER_INFO_ERROR.getDescription(), e);
        throw new ApplicationException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ErrorResponse.GET_USER_INFO_ERROR.getDescription());
    } finally {
        if (engine != null) {
            engine.close();
        }
    }
    return null;
}
Also used : ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) Jwt(io.jans.as.model.jwt.Jwt) 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) 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) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget) UserInfoResponse(io.jans.ca.plugin.adminui.model.auth.UserInfoResponse)

Aggregations

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