Search in sources :

Example 16 with ServerAuthorizationCodeGrant

use of org.apache.cxf.rs.security.oauth2.grants.code.ServerAuthorizationCodeGrant in project cxf by apache.

the class AuthorizationCodeGrantService method createGrant.

protected Response createGrant(OAuthRedirectionState state, Client client, List<String> requestedScope, List<String> approvedScope, UserSubject userSubject, ServerAccessToken preauthorizedToken) {
    // in this flow the code is still created, the preauthorized token
    // will be retrieved by the authorization code grant handler
    ServerAuthorizationCodeGrant grant = null;
    try {
        grant = getGrantRepresentation(state, client, requestedScope, approvedScope, userSubject, preauthorizedToken);
    } catch (OAuthServiceException ex) {
        return createErrorResponse(state.getState(), state.getRedirectUri(), OAuthConstants.ACCESS_DENIED);
    }
    String grantCode = processCodeGrant(client, grant.getCode(), grant.getSubject());
    if (state.getRedirectUri() == null) {
        OOBAuthorizationResponse bean = new OOBAuthorizationResponse();
        bean.setClientId(client.getClientId());
        bean.setClientDescription(client.getApplicationDescription());
        bean.setAuthorizationCode(grantCode);
        bean.setUserId(userSubject.getLogin());
        bean.setExpiresIn(grant.getExpiresIn());
        return deliverOOBResponse(bean);
    } else if (isFormResponse(state)) {
        FormAuthorizationResponse bean = new FormAuthorizationResponse();
        bean.setAuthorizationCode(grantCode);
        bean.setExpiresIn(grant.getExpiresIn());
        bean.setState(state.getState());
        bean.setRedirectUri(state.getRedirectUri());
        return createHtmlResponse(bean);
    } else {
        // return the code by appending it as a query parameter to the redirect URI
        UriBuilder ub = getRedirectUriBuilder(state.getState(), state.getRedirectUri());
        ub.queryParam(OAuthConstants.AUTHORIZATION_CODE_VALUE, grantCode);
        return Response.seeOther(ub.build()).build();
    }
}
Also used : OOBAuthorizationResponse(org.apache.cxf.rs.security.oauth2.common.OOBAuthorizationResponse) OAuthServiceException(org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException) ServerAuthorizationCodeGrant(org.apache.cxf.rs.security.oauth2.grants.code.ServerAuthorizationCodeGrant) FormAuthorizationResponse(org.apache.cxf.rs.security.oauth2.common.FormAuthorizationResponse) UriBuilder(javax.ws.rs.core.UriBuilder)

Aggregations

ServerAuthorizationCodeGrant (org.apache.cxf.rs.security.oauth2.grants.code.ServerAuthorizationCodeGrant)10 Client (org.apache.cxf.rs.security.oauth2.common.Client)6 Test (org.junit.Test)6 OAuthServiceException (org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException)3 ServerAccessToken (org.apache.cxf.rs.security.oauth2.common.ServerAccessToken)2 AuthorizationCodeRegistration (org.apache.cxf.rs.security.oauth2.grants.code.AuthorizationCodeRegistration)2 Ignore (org.junit.Ignore)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 URI (java.net.URI)1 CacheException (javax.cache.CacheException)1 UriBuilder (javax.ws.rs.core.UriBuilder)1 JSONProvider (org.apache.cxf.jaxrs.provider.json.JSONProvider)1 AbstractFormImplicitResponse (org.apache.cxf.rs.security.oauth2.common.AbstractFormImplicitResponse)1 AccessTokenRegistration (org.apache.cxf.rs.security.oauth2.common.AccessTokenRegistration)1 FormAuthorizationResponse (org.apache.cxf.rs.security.oauth2.common.FormAuthorizationResponse)1 OOBAuthorizationResponse (org.apache.cxf.rs.security.oauth2.common.OOBAuthorizationResponse)1 AuthorizationCodeDataProvider (org.apache.cxf.rs.security.oauth2.grants.code.AuthorizationCodeDataProvider)1 RefreshToken (org.apache.cxf.rs.security.oauth2.tokens.refresh.RefreshToken)1