Search in sources :

Example 6 with RefreshToken

use of org.forgerock.oauth2.core.RefreshToken in project OpenAM by OpenRock.

the class OpenAMTokenStore method createAccessToken.

/**
     * {@inheritDoc}
     */
public AccessToken createAccessToken(String grantType, String accessTokenType, String authorizationCode, String resourceOwnerId, String clientId, String redirectUri, Set<String> scope, RefreshToken refreshToken, String nonce, String claims, OAuth2Request request) throws ServerException, NotFoundException {
    OpenIdConnectClientRegistration clientRegistration = getClientRegistration(clientId, request);
    final OAuth2ProviderSettings providerSettings = providerSettingsFactory.get(request);
    final String id = UUID.randomUUID().toString();
    final String auditId = UUID.randomUUID().toString();
    String realm = realmNormaliser.normalise(request.<String>getParameter(REALM));
    long expiryTime = 0;
    if (clientRegistration == null) {
        expiryTime = providerSettings.getAccessTokenLifetime() + System.currentTimeMillis();
    } else {
        expiryTime = clientRegistration.getAccessTokenLifeTime(providerSettings) + System.currentTimeMillis();
    }
    final AccessToken accessToken;
    if (refreshToken == null) {
        accessToken = new OpenAMAccessToken(id, authorizationCode, resourceOwnerId, clientId, redirectUri, scope, expiryTime, null, OAuth2Constants.Token.OAUTH_ACCESS_TOKEN, grantType, nonce, realm, claims, auditId);
    } else {
        accessToken = new OpenAMAccessToken(id, authorizationCode, resourceOwnerId, clientId, redirectUri, scope, expiryTime, refreshToken.getTokenId(), OAuth2Constants.Token.OAUTH_ACCESS_TOKEN, grantType, nonce, realm, claims, auditId);
    }
    try {
        tokenStore.create(accessToken);
        if (auditLogger.isAuditLogEnabled()) {
            String[] obs = { "CREATED_TOKEN", accessToken.toString() };
            auditLogger.logAccessMessage("CREATED_TOKEN", obs, null);
        }
    } catch (CoreTokenException e) {
        logger.error("Could not create token in CTS: " + e.getMessage());
        if (auditLogger.isAuditLogEnabled()) {
            String[] obs = { "FAILED_CREATE_TOKEN", accessToken.toString() };
            auditLogger.logErrorMessage("FAILED_CREATE_TOKEN", obs, null);
        }
        throw new ServerException("Could not create token in CTS: " + e.getMessage());
    }
    request.setToken(AccessToken.class, accessToken);
    return accessToken;
}
Also used : OpenIdConnectClientRegistration(org.forgerock.openidconnect.OpenIdConnectClientRegistration) ServerException(org.forgerock.oauth2.core.exceptions.ServerException) AccessToken(org.forgerock.oauth2.core.AccessToken) CoreTokenException(org.forgerock.openam.cts.exceptions.CoreTokenException) OAuth2ProviderSettings(org.forgerock.oauth2.core.OAuth2ProviderSettings)

Example 7 with RefreshToken

use of org.forgerock.oauth2.core.RefreshToken in project OpenAM by OpenRock.

the class DeviceCodeGrantTypeHandlerTest method setup.

@BeforeMethod
public void setup() throws Exception {
    initMocks(this);
    OAuth2ProviderSettingsFactory providerSettingsFactory = mock(OAuth2ProviderSettingsFactory.class);
    when(providerSettingsFactory.get(request)).thenReturn(providerSettings);
    when(providerSettings.getDeviceCodePollInterval()).thenReturn(5);
    when(providerSettings.validateRequestedClaims(anyString())).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            return (String) invocation.getArguments()[0];
        }
    });
    OAuth2UrisFactory oAuth2UrisFactory = mock(OAuth2UrisFactory.class);
    when(oAuth2UrisFactory.get(request)).thenReturn(oAuth2Uris);
    ClientAuthenticator clientAuthenticator = mock(ClientAuthenticator.class);
    ClientRegistration clientRegistration = mock(ClientRegistration.class);
    when(clientAuthenticator.authenticate(eq(request), anyString())).thenReturn(clientRegistration);
    accessTokenGenerator = new GrantTypeAccessTokenGenerator(tokenStore);
    when(tokenStore.createAccessToken(anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anySetOf(String.class), any(RefreshToken.class), anyString(), anyString(), any(OAuth2Request.class))).thenReturn(accessToken);
    when(tokenStore.createRefreshToken(anyString(), anyString(), anyString(), anyString(), anySetOf(String.class), any(OAuth2Request.class), anyString())).thenReturn(refreshToken);
    ClientAuthenticationFailureFactory failureFactory = mock(ClientAuthenticationFailureFactory.class);
    InvalidClientException expectedResult = mock(InvalidClientException.class);
    when(expectedResult.getError()).thenReturn("invalid_client");
    when(failureFactory.getException()).thenReturn(expectedResult);
    when(failureFactory.getException(anyString())).thenReturn(expectedResult);
    when(failureFactory.getException(any(OAuth2Request.class), anyString())).thenReturn(expectedResult);
    grantTypeHandler = new DeviceCodeGrantTypeHandler(providerSettingsFactory, clientAuthenticator, tokenStore, clientRegistrationStore, failureFactory, oAuth2UrisFactory, accessTokenGenerator);
}
Also used : ClientAuthenticationFailureFactory(org.forgerock.oauth2.core.exceptions.ClientAuthenticationFailureFactory) InvocationOnMock(org.mockito.invocation.InvocationOnMock) InvalidClientException(org.forgerock.oauth2.core.exceptions.InvalidClientException) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 8 with RefreshToken

use of org.forgerock.oauth2.core.RefreshToken in project OpenAM by OpenRock.

the class OAuth2AuditRefreshTokenContextProvider method getUserIdFromRefreshTokenFromRequest.

private String getUserIdFromRefreshTokenFromRequest(Request request) {
    String userId = null;
    RefreshToken refreshToken = retrieveRefreshTokenFromRequest(request);
    if (refreshToken != null) {
        userId = getUserIdFromToken(refreshToken);
    }
    return userId;
}
Also used : RefreshToken(org.forgerock.oauth2.core.RefreshToken)

Example 9 with RefreshToken

use of org.forgerock.oauth2.core.RefreshToken in project OpenAM by OpenRock.

the class OAuth2AuditRefreshTokenContextProvider method retrieveRefreshTokenFromChallengeResponse.

private RefreshToken retrieveRefreshTokenFromChallengeResponse(Request request) {
    RefreshToken refreshToken;
    ChallengeResponse challengeResponse = request.getChallengeResponse();
    if (challengeResponse == null) {
        return null;
    }
    String bearerToken = challengeResponse.getRawValue();
    if ("undefined".equals(bearerToken)) {
        return null;
    }
    OAuth2Request oAuth2Request = requestFactory.create(request);
    try {
        refreshToken = tokenStore.readRefreshToken(oAuth2Request, bearerToken);
    } catch (ServerException | InvalidGrantException | NotFoundException e) {
        return null;
    }
    return refreshToken;
}
Also used : OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) RefreshToken(org.forgerock.oauth2.core.RefreshToken) ServerException(org.forgerock.oauth2.core.exceptions.ServerException) NotFoundException(org.forgerock.oauth2.core.exceptions.NotFoundException) InvalidGrantException(org.forgerock.oauth2.core.exceptions.InvalidGrantException) ChallengeResponse(org.restlet.data.ChallengeResponse)

Example 10 with RefreshToken

use of org.forgerock.oauth2.core.RefreshToken in project OpenAM by OpenRock.

the class OAuth2AuditRefreshTokenContextProvider method getTrackingIdFromRefreshTokenFromAuthorizationHeader.

private String getTrackingIdFromRefreshTokenFromAuthorizationHeader(Request request) {
    String trackingId = null;
    RefreshToken refreshToken = retrieveRefreshTokenFromChallengeResponse(request);
    if (refreshToken != null) {
        trackingId = getTrackingIdFromToken(refreshToken);
    }
    return trackingId;
}
Also used : RefreshToken(org.forgerock.oauth2.core.RefreshToken)

Aggregations

RefreshToken (org.forgerock.oauth2.core.RefreshToken)8 ServerException (org.forgerock.oauth2.core.exceptions.ServerException)5 OAuth2ProviderSettings (org.forgerock.oauth2.core.OAuth2ProviderSettings)3 CoreTokenException (org.forgerock.openam.cts.exceptions.CoreTokenException)3 JsonValue (org.forgerock.json.JsonValue)2 InvalidGrantException (org.forgerock.oauth2.core.exceptions.InvalidGrantException)2 OpenIdConnectClientRegistration (org.forgerock.openidconnect.OpenIdConnectClientRegistration)2 SSOException (com.iplanet.sso.SSOException)1 SMSException (com.sun.identity.sm.SMSException)1 Date (java.util.Date)1 InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)1 NotFoundException (org.forgerock.json.resource.NotFoundException)1 AccessToken (org.forgerock.oauth2.core.AccessToken)1 AuthorizationCode (org.forgerock.oauth2.core.AuthorizationCode)1 OAuth2Request (org.forgerock.oauth2.core.OAuth2Request)1 ClientAuthenticationFailureFactory (org.forgerock.oauth2.core.exceptions.ClientAuthenticationFailureFactory)1 InvalidClientException (org.forgerock.oauth2.core.exceptions.InvalidClientException)1 NotFoundException (org.forgerock.oauth2.core.exceptions.NotFoundException)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 ChallengeResponse (org.restlet.data.ChallengeResponse)1