Search in sources :

Example 41 with AccessToken

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

the class TokenResponseType method createToken.

public CoreToken createToken(Token accessToken, Map<String, Object> data) throws NotFoundException {
    final String tokenType = (String) data.get(OAuth2Constants.CoreTokenParams.TOKEN_TYPE);
    final Set<String> scope = (Set<String>) data.get(OAuth2Constants.CoreTokenParams.SCOPE);
    final OAuth2Request request = requestFactory.create(Request.getCurrent());
    final ResourceOwner resourceOwner = ownerAuthenticator.authenticate(request, true);
    final String clientId = (String) data.get(OAuth2Constants.CoreTokenParams.CLIENT_ID);
    final String redirectUri = (String) data.get(OAuth2Constants.CoreTokenParams.REDIRECT_URI);
    final String codeChallenge = (String) data.get(OAuth2Constants.Custom.CODE_CHALLENGE);
    final String codeChallengeMethod = (String) data.get(OAuth2Constants.Custom.CODE_CHALLENGE_METHOD);
    try {
        final Map.Entry<String, Token> tokenEntry = handler.handle(tokenType, scope, resourceOwner, clientId, redirectUri, null, requestFactory.create(Request.getCurrent()), codeChallenge, codeChallengeMethod);
        return new LegacyAccessTokenAdapter((AccessToken) tokenEntry.getValue());
    } catch (ServerException e) {
        throw OAuthProblemException.OAuthError.SERVER_ERROR.handle(Request.getCurrent(), e.getMessage());
    }
}
Also used : OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) Set(java.util.Set) ServerException(org.forgerock.oauth2.core.exceptions.ServerException) LegacyAccessTokenAdapter(org.forgerock.openam.oauth2.legacy.LegacyAccessTokenAdapter) ResourceOwner(org.forgerock.oauth2.core.ResourceOwner) Token(org.forgerock.oauth2.core.Token) CoreToken(org.forgerock.openam.oauth2.legacy.CoreToken) AccessToken(org.forgerock.oauth2.core.AccessToken) Map(java.util.Map)

Example 42 with AccessToken

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

the class OpenAMScopeValidatorTest method shouldReturnScopesWithoutValues.

@Test
public void shouldReturnScopesWithoutValues() throws Exception {
    // given
    String scopeKey1 = "mail";
    String scopeKey2 = "phone";
    AccessToken accessToken = mock(AccessToken.class);
    when(accessToken.getScope()).thenReturn(new HashSet<>(Arrays.asList(scopeKey1, scopeKey2)));
    when(accessToken.getResourceOwnerId()).thenReturn(anyString());
    when(identity.getAttribute(scopeKey1)).thenReturn(Collections.emptySet());
    when(identity.getAttribute(scopeKey2)).thenReturn(null);
    // when
    Map<String, Object> result = validator.evaluateScope(accessToken);
    // then
    assertThat(result).isNotNull();
    assertThat(result).isNotEmpty();
    assertThat(result.get(scopeKey1)).isEqualTo("");
    assertThat(result.get(scopeKey2)).isEqualTo("");
}
Also used : AccessToken(org.forgerock.oauth2.core.AccessToken) Test(org.testng.annotations.Test)

Example 43 with AccessToken

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

the class OpenAMScopeValidatorTest method shouldReturnScopesWithValues.

@Test
public void shouldReturnScopesWithValues() throws Exception {
    // given
    String scopeKey1 = "mail";
    String scopeKey2 = "phone";
    String scopeValue1 = "test@example.com";
    String scopeValue2 = "1234567890";
    AccessToken accessToken = mock(AccessToken.class);
    when(accessToken.getScope()).thenReturn(new HashSet<>(Arrays.asList(scopeKey1, scopeKey2)));
    when(accessToken.getResourceOwnerId()).thenReturn(anyString());
    when(identity.getAttribute(scopeKey1)).thenReturn(Collections.singleton(scopeValue1));
    when(identity.getAttribute(scopeKey2)).thenReturn(Collections.singleton(scopeValue2));
    // when
    Map<String, Object> result = validator.evaluateScope(accessToken);
    // then
    assertThat(result).isNotNull();
    assertThat(result).isNotEmpty();
    assertThat(result.get(scopeKey1)).isEqualTo(scopeValue1);
    assertThat(result.get(scopeKey2)).isEqualTo(scopeValue2);
}
Also used : AccessToken(org.forgerock.oauth2.core.AccessToken) Test(org.testng.annotations.Test)

Example 44 with AccessToken

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

the class OAuth2AuditAccessTokenContextProvider method retrieveAccessTokenFromRequest.

private AccessToken retrieveAccessTokenFromRequest(Request request) {
    AccessToken token;
    token = requestFactory.create(request).getToken(AccessToken.class);
    return token;
}
Also used : AccessToken(org.forgerock.oauth2.core.AccessToken)

Example 45 with AccessToken

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

the class OAuth2AuditAccessTokenContextProvider method getUserIdFromAccessTokenFromAuthorizationHeader.

private String getUserIdFromAccessTokenFromAuthorizationHeader(Request request) {
    String userId = null;
    AccessToken accessToken = retrieveAccessTokenFromChallengeResponse(request);
    if (accessToken != null) {
        userId = getUserIdFromToken(accessToken);
    }
    return userId;
}
Also used : AccessToken(org.forgerock.oauth2.core.AccessToken)

Aggregations

AccessToken (org.forgerock.oauth2.core.AccessToken)37 OAuth2Request (org.forgerock.oauth2.core.OAuth2Request)22 Test (org.testng.annotations.Test)17 ServerException (org.forgerock.oauth2.core.exceptions.ServerException)14 JsonValue (org.forgerock.json.JsonValue)13 Request (org.restlet.Request)12 ChallengeResponse (org.restlet.data.ChallengeResponse)10 OAuth2ProviderSettings (org.forgerock.oauth2.core.OAuth2ProviderSettings)9 NotFoundException (org.forgerock.oauth2.core.exceptions.NotFoundException)7 AccessTokenVerifier (org.forgerock.oauth2.core.AccessTokenVerifier)6 Response (org.restlet.Response)6 Map (java.util.Map)5 InvalidClientException (org.forgerock.oauth2.core.exceptions.InvalidClientException)5 InvalidGrantException (org.forgerock.oauth2.core.exceptions.InvalidGrantException)5 UnauthorizedClientException (org.forgerock.oauth2.core.exceptions.UnauthorizedClientException)5 BeforeMethod (org.testng.annotations.BeforeMethod)5 HashMap (java.util.HashMap)4 ResourceOwner (org.forgerock.oauth2.core.ResourceOwner)4 InvalidTokenException (org.forgerock.oauth2.core.exceptions.InvalidTokenException)4 JSONObject (org.json.JSONObject)4