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;
}
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();
}
}
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();
}
}
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());
}
}
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());
}
}
Aggregations