use of io.jans.ca.common.introspection.CorrectRptIntrospectionResponse in project jans by JanssenProject.
the class UmaTokenService method getRpt.
public RpGetRptResponse getRpt(RpGetRptParams params) throws Exception {
Rp rp = rpSyncService.getRp(params.getRpId());
UmaMetadata discovery = discoveryService.getUmaDiscoveryByRpId(params.getRpId());
if (!Strings.isNullOrEmpty(rp.getRpt()) && rp.getRptExpiresAt() != null) {
if (!CoreUtils.isExpired(rp.getRptExpiresAt())) {
LOG.debug("RPT from rp, RPT: " + rp.getRpt() + ", rp: " + rp);
RpGetRptResponse result = new RpGetRptResponse();
result.setRpt(rp.getRpt());
result.setTokenType(rp.getRptTokenType());
result.setPct(rp.getRptPct());
result.setUpdated(rp.getRptUpgraded());
return result;
}
}
Builder client = opClientFactory.createClientRequest(discovery.getTokenEndpoint(), httpService.getClientEngine());
client.header("Authorization", "Basic " + Utils.encodeCredentials(rp.getClientId(), rp.getClientSecret()));
Form formRequest = new Form();
formRequest.param("grant_type", GrantType.OXAUTH_UMA_TICKET.getValue());
formRequest.param("ticket", params.getTicket());
if (params.getClaimToken() != null) {
formRequest.param("claim_token", params.getClaimToken());
}
if (params.getClaimTokenFormat() != null) {
formRequest.param("claim_token_format", params.getClaimTokenFormat());
}
if (params.getPct() != null) {
formRequest.param("pct", params.getPct());
}
if (params.getRpt() != null) {
formRequest.param("rpt", params.getRpt());
}
if (params.getScope() != null) {
formRequest.param("scope", Utils.joinAndUrlEncode(params.getScope()));
}
if (params.getParams() != null && !params.getParams().isEmpty()) {
for (Map.Entry<String, String> p : params.getParams().entrySet()) {
formRequest.param(p.getKey(), p.getValue());
}
}
Response response = null;
try {
response = client.buildPost(Entity.form(formRequest)).invoke();
} catch (Exception e) {
LOG.error("Failed to receive RPT response for rp: " + rp, e);
throw new HttpException(ErrorResponseCode.FAILED_TO_GET_RPT);
}
String entityResponse = null;
try {
entityResponse = response.readEntity(String.class);
} catch (Exception e) {
LOG.error("Failed to read RPT response for rp: " + rp, e);
throw new HttpException(ErrorResponseCode.FAILED_TO_GET_RPT);
} finally {
response.close();
}
UmaTokenResponse tokenResponse = asTokenResponse(entityResponse);
if (tokenResponse != null && StringUtils.isNotBlank(tokenResponse.getAccessToken())) {
final IntrospectionService introspectionService = ServerLauncher.getInjector().getInstance(IntrospectionService.class);
CorrectRptIntrospectionResponse status = introspectionService.introspectRpt(params.getRpId(), tokenResponse.getAccessToken());
LOG.debug("RPT " + tokenResponse.getAccessToken() + ", status: " + status);
if (status.getActive()) {
LOG.debug("RPT is successfully obtained from AS. RPT: {}", tokenResponse.getAccessToken());
rp.setRpt(tokenResponse.getAccessToken());
rp.setRptTokenType(tokenResponse.getTokenType());
rp.setRptPct(tokenResponse.getPct());
rp.setRptUpgraded(tokenResponse.getUpgraded());
rp.setRptCreatedAt(new Date(status.getIssuedAt() * 1000));
rp.setRptExpiresAt(new Date(status.getExpiresAt() * 1000));
rpService.updateSilently(rp);
RpGetRptResponse result = new RpGetRptResponse();
result.setRpt(rp.getRpt());
result.setTokenType(rp.getRptTokenType());
result.setPct(rp.getRptPct());
result.setUpdated(rp.getRptUpgraded());
return result;
}
} else {
RpGetRptOperation.handleRptError(response.getStatus(), entityResponse);
}
LOG.error("Failed to get RPT for rp: " + rp);
throw new HttpException(ErrorResponseCode.FAILED_TO_GET_RPT);
}
Aggregations