use of io.jans.as.model.uma.UmaMetadata 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);
}
use of io.jans.as.model.uma.UmaMetadata in project jans by JanssenProject.
the class RsModifyOperation method execute.
@Override
public IOpResponse execute(final RsModifyParams params) throws Exception {
validate(params);
Rp rp = getRp();
PatProvider patProvider = new PatProvider() {
@Override
public String getPatToken() {
return getUmaTokenService().getPat(params.getRpId()).getToken();
}
@Override
public void clearPat() {
// do nothing
}
};
io.jans.ca.server.model.UmaResource umaResource = rp.umaResource(params.getPath(), params.getHttpMethod());
if (umaResource == null) {
final ErrorResponse error = new ErrorResponse("invalid_request");
error.setErrorDescription("Resource is not protected with path: " + params.getPath() + " and httpMethod: " + params.getHttpMethod() + ". Please protect your resource first with uma_rs_modify command. Check details on " + CoreUtils.DOC_URL);
LOG.error(error.getErrorDescription());
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON_TYPE).entity(Jackson2.asJson(error)).build());
}
UmaMetadata discovery = getDiscoveryService().getUmaDiscoveryByRpId(params.getRpId());
UmaResourceService resourceService = UmaClientFactory.instance().createResourceService(discovery, getHttpService().getClientEngine());
UmaResource opUmaResource = getResource(resourceService, params, umaResource.getId());
try {
String pat = getUmaTokenService().getPat(params.getRpId()).getToken();
return update(pat, umaResource.getId(), rp, resourceService, opUmaResource);
} catch (ClientErrorException e) {
LOG.debug("Failed to update resource. Entity: " + e.getResponse().readEntity(String.class) + ", status: " + e.getResponse().getStatus(), e);
if (e.getResponse().getStatus() == 400 || e.getResponse().getStatus() == 401) {
LOG.debug("Try maybe PAT is lost on AS, force refresh PAT and re-try ...");
return update(getUmaTokenService().obtainPat(params.getRpId()).getToken(), umaResource.getId(), rp, resourceService, opUmaResource);
} else {
throw e;
}
} catch (Exception e) {
LOG.error(e.getMessage(), e);
throw e;
}
}
Aggregations