Search in sources :

Example 1 with UmaTokenResponse

use of org.gluu.oxauth.model.uma.UmaTokenResponse in project oxAuth by GluuFederation.

the class UmaTokenService method requestRpt.

public Response requestRpt(String grantType, String ticket, String claimToken, String claimTokenFormat, String pctCode, String rptCode, String scope, HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
    try {
        log.trace("requestRpt grant_type: {}, ticket: {}, claim_token: {}, claim_token_format: {}, pct: {}, rpt: {}, scope: {}", grantType, ticket, claimToken, claimTokenFormat, pctCode, rptCode, scope);
        umaValidationService.validateGrantType(grantType);
        List<UmaPermission> permissions = umaValidationService.validateTicket(ticket);
        Jwt idToken = umaValidationService.validateClaimToken(claimToken, claimTokenFormat);
        UmaPCT pct = umaValidationService.validatePct(pctCode);
        UmaRPT rpt = umaValidationService.validateRPT(rptCode);
        Client client = umaValidationService.validate(identity.getSessionClient().getClient());
        Map<Scope, Boolean> scopes = umaValidationService.validateScopes(scope, permissions, client);
        // creates new pct if pct is null in request
        pct = pctService.updateClaims(pct, idToken, client.getClientId(), permissions);
        Claims claims = new Claims(idToken, pct, claimToken);
        Map<UmaScriptByScope, UmaAuthorizationContext> scriptMap = umaNeedsInfoService.checkNeedsInfo(claims, scopes, permissions, pct, httpRequest, client);
        if (!scriptMap.isEmpty()) {
            expressionService.evaluate(scriptMap, permissions);
        } else {
            log.warn("There are no any policies that protects scopes. Scopes: " + UmaScopeService.asString(scopes.keySet()) + ". Configuration property umaGrantAccessIfNoPolicies: " + appConfiguration.getUmaGrantAccessIfNoPolicies());
            if (appConfiguration.getUmaGrantAccessIfNoPolicies() != null && appConfiguration.getUmaGrantAccessIfNoPolicies()) {
                log.warn("Access granted because there are no any protection. Make sure it is intentional behavior.");
            } else {
                log.warn("Access denied because there are no any protection. Make sure it is intentional behavior.");
                throw errorResponseFactory.createWebApplicationException(Response.Status.FORBIDDEN, UmaErrorResponseType.FORBIDDEN_BY_POLICY, "Access denied because there are no any protection. Make sure it is intentional behavior.");
            }
        }
        log.trace("Access granted.");
        updatePermissionsWithClientRequestedScope(permissions, scopes);
        addPctToPermissions(permissions, pct);
        boolean upgraded = false;
        if (rpt == null) {
            ExecutionContext executionContext = new ExecutionContext(httpRequest, httpResponse);
            executionContext.setClient(client);
            rpt = rptService.createRPTAndPersist(executionContext, permissions);
            rptCode = rpt.getNotHashedCode();
        } else if (rptService.addPermissionToRPT(rpt, permissions)) {
            upgraded = true;
        }
        UmaTokenResponse response = new UmaTokenResponse();
        response.setAccessToken(rptCode);
        response.setUpgraded(upgraded);
        response.setTokenType("Bearer");
        response.setPct(pct.getCode());
        return Response.ok(ServerUtil.asJson(response)).build();
    } catch (Exception ex) {
        log.error("Exception happened", ex);
        if (ex instanceof WebApplicationException) {
            throw (WebApplicationException) ex;
        }
    }
    log.error("Failed to handle request to UMA Token Endpoint.");
    throw errorResponseFactory.createWebApplicationException(Response.Status.INTERNAL_SERVER_ERROR, UmaErrorResponseType.SERVER_ERROR, "Failed to handle request to UMA Token Endpoint.");
}
Also used : UmaTokenResponse(org.gluu.oxauth.model.uma.UmaTokenResponse) WebApplicationException(javax.ws.rs.WebApplicationException) Jwt(org.gluu.oxauth.model.jwt.Jwt) WebApplicationException(javax.ws.rs.WebApplicationException) ExecutionContext(org.gluu.oxauth.model.common.ExecutionContext) Scope(org.oxauth.persistence.model.Scope) UmaPermission(org.gluu.oxauth.model.uma.persistence.UmaPermission) Client(org.gluu.oxauth.model.registration.Client)

Example 2 with UmaTokenResponse

use of org.gluu.oxauth.model.uma.UmaTokenResponse in project oxAuth by GluuFederation.

the class AccessProtectedResourceFlowHttpTest method repeatRptRequest.

@Test(dependsOnMethods = { "successfulRptRequest" })
@Parameters({ "umaPatClientId", "umaPatClientSecret" })
public void repeatRptRequest(String umaPatClientId, String umaPatClientSecret) throws Exception {
    showTitle("repeatRptRequest");
    rsRegisterPermissions();
    requestRptAndGetNeedsInfo(umaPatClientId, umaPatClientSecret);
    claimsGathering(umaPatClientId);
    showTitle("Request RPT with existing RPT (upgrade case) ... ");
    UmaTokenResponse response = tokenService.requestRpt("Basic " + encodeCredentials(umaPatClientId, umaPatClientSecret), GrantType.OXAUTH_UMA_TICKET.getValue(), claimsGatheringTicket, null, null, null, this.rpt, "oxd");
    assert_(response);
    assertTrue(response.getUpgraded());
    this.rpt = response.getAccessToken();
}
Also used : UmaTokenResponse(org.gluu.oxauth.model.uma.UmaTokenResponse) Parameters(org.testng.annotations.Parameters) BaseTest(org.gluu.oxauth.BaseTest) Test(org.testng.annotations.Test)

Example 3 with UmaTokenResponse

use of org.gluu.oxauth.model.uma.UmaTokenResponse in project oxAuth by GluuFederation.

the class AccessProtectedResourceFlowHttpTest method successfulRptRequest.

/**
 * Request RPT with all claims provided
 */
@Test(dependsOnMethods = { "claimsGathering" })
@Parameters({ "umaPatClientId", "umaPatClientSecret" })
public void successfulRptRequest(String umaPatClientId, String umaPatClientSecret) throws Exception {
    showTitle("successfulRptRequest");
    UmaTokenResponse response = tokenService.requestRpt("Basic " + encodeCredentials(umaPatClientId, umaPatClientSecret), GrantType.OXAUTH_UMA_TICKET.getValue(), claimsGatheringTicket, null, null, null, null, null);
    assert_(response);
    this.rpt = response.getAccessToken();
}
Also used : UmaTokenResponse(org.gluu.oxauth.model.uma.UmaTokenResponse) Parameters(org.testng.annotations.Parameters) BaseTest(org.gluu.oxauth.BaseTest) Test(org.testng.annotations.Test)

Example 4 with UmaTokenResponse

use of org.gluu.oxauth.model.uma.UmaTokenResponse in project oxTrust by GluuFederation.

the class BaseApiTest method getAuthorizedRpt.

private void getAuthorizedRpt(String asUri, String ticket) {
    try {
        UmaMetadata umaMetadata = UmaClientFactory.instance().createMetadataService(asUri).getMetadata();
        if (umaMetadata == null) {
            throw new IllegalArgumentException(String.format("Failed to load valid UMA metadata configuration from: %s", asUri));
        }
        TokenRequest tokenRequest = getAuthorizationTokenRequest(umaMetadata);
        UmaTokenService tokenService = UmaClientFactory.instance().createTokenService(umaMetadata);
        UmaTokenResponse rptResponse = tokenService.requestJwtAuthorizationRpt(ClientAssertionType.JWT_BEARER.toString(), tokenRequest.getClientAssertion(), GrantType.OXAUTH_UMA_TICKET.getValue(), ticket, null, null, null, null, null);
        if (rptResponse == null) {
            throw new IllegalArgumentException("UMA RPT token response is invalid");
        }
        if (StringUtils.isBlank(rptResponse.getAccessToken())) {
            throw new IllegalArgumentException("UMA RPT is invalid");
        }
        this.rpt = rptResponse.getAccessToken();
        System.out.println("RPT IS:" + this.rpt);
    } catch (Exception ex) {
        throw new IllegalArgumentException(ex.getMessage(), ex);
    }
}
Also used : UmaMetadata(org.gluu.oxauth.model.uma.UmaMetadata) UmaTokenResponse(org.gluu.oxauth.model.uma.UmaTokenResponse) UmaTokenService(org.gluu.oxauth.client.uma.UmaTokenService) TokenRequest(org.gluu.oxauth.client.TokenRequest) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 5 with UmaTokenResponse

use of org.gluu.oxauth.model.uma.UmaTokenResponse in project oxAuth by GluuFederation.

the class UmaSpontaneousScopeHttpTest method successfulRptRequest.

@Test(dependsOnMethods = { "registerPermissions" })
public void successfulRptRequest() throws Exception {
    showTitle("successfulRptRequest");
    UmaTokenResponse response = tokenService.requestRpt("Basic " + encodeCredentials(clientResponse.getClientId(), clientResponse.getClientSecret()), GrantType.OXAUTH_UMA_TICKET.getValue(), permissionFlowTest.ticket, null, null, null, null, null);
    assert_(response);
    this.rpt = response.getAccessToken();
}
Also used : UmaTokenResponse(org.gluu.oxauth.model.uma.UmaTokenResponse) BaseTest(org.gluu.oxauth.BaseTest) Test(org.testng.annotations.Test)

Aggregations

UmaTokenResponse (org.gluu.oxauth.model.uma.UmaTokenResponse)5 BaseTest (org.gluu.oxauth.BaseTest)3 Test (org.testng.annotations.Test)3 Parameters (org.testng.annotations.Parameters)2 IOException (java.io.IOException)1 KeyManagementException (java.security.KeyManagementException)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 TokenRequest (org.gluu.oxauth.client.TokenRequest)1 UmaTokenService (org.gluu.oxauth.client.uma.UmaTokenService)1 ExecutionContext (org.gluu.oxauth.model.common.ExecutionContext)1 Jwt (org.gluu.oxauth.model.jwt.Jwt)1 Client (org.gluu.oxauth.model.registration.Client)1 UmaMetadata (org.gluu.oxauth.model.uma.UmaMetadata)1 UmaPermission (org.gluu.oxauth.model.uma.persistence.UmaPermission)1 Scope (org.oxauth.persistence.model.Scope)1