Search in sources :

Example 11 with TokenStore

use of org.forgerock.oauth2.core.TokenStore 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 12 with TokenStore

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

the class OpenAMTokenStoreTest method shouldFailToReadAccessToken.

@Test(expectedExceptions = ServerException.class)
public void shouldFailToReadAccessToken() throws Exception {
    //Given
    doThrow(CoreTokenException.class).when(tokenStore).read("TOKEN_ID");
    OAuth2Request request = oAuth2RequestFactory.create(this.request);
    //When
    openAMtokenStore.readAccessToken(request, "TOKEN_ID");
//Then
//Expected ServerException
}
Also used : RestletOAuth2Request(org.forgerock.oauth2.restlet.RestletOAuth2Request) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) Test(org.testng.annotations.Test)

Example 13 with TokenStore

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

the class OpenAMTokenStoreTest method shouldUpdateDeviceCode.

@Test
public void shouldUpdateDeviceCode() throws Exception {
    // Given
    DeviceCode code = new DeviceCode(json(object(field("tokenName", asSet("device_code")), field("id", asSet("123")), field("user_code", asSet("456")), field("realm", asSet("/")), field("clientID", asSet("CLIENT_ID")))));
    given(tokenStore.read("123")).willReturn(code);
    final RestletOAuth2Request oauth2Request = oAuth2RequestFactory.create(this.request);
    given(request.getAttributes()).willReturn(new ConcurrentHashMap<>(singletonMap("realm", (Object) "/")));
    given(realmNormaliser.normalise("/")).willReturn("/");
    // When
    openAMtokenStore.updateDeviceCode(code, oauth2Request);
    // Then
    verify(tokenStore).update(code);
}
Also used : DeviceCode(org.forgerock.oauth2.core.DeviceCode) RestletOAuth2Request(org.forgerock.oauth2.restlet.RestletOAuth2Request) Test(org.testng.annotations.Test)

Example 14 with TokenStore

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

the class RestletFormBodyAccessTokenVerifierTest method shouldLookupValue.

@Test
public void shouldLookupValue() throws Exception {
    // Given
    Form form = new Form();
    form.add("access_token", "freddy");
    Request request = new Request();
    request.setEntity(form.getWebRepresentation());
    OAuth2Request req = new RestletOAuth2Request(null, request);
    // When
    AccessTokenVerifier.TokenState result = verifier.verify(req);
    // Then
    assertThat(result.isValid()).isFalse();
    verify(tokenStore).readAccessToken(req, "freddy");
}
Also used : OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) Form(org.restlet.data.Form) Request(org.restlet.Request) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) AccessTokenVerifier(org.forgerock.oauth2.core.AccessTokenVerifier) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)12 OAuth2Request (org.forgerock.oauth2.core.OAuth2Request)11 Request (org.restlet.Request)8 AccessTokenVerifier (org.forgerock.oauth2.core.AccessTokenVerifier)7 AccessToken (org.forgerock.oauth2.core.AccessToken)6 RestletOAuth2Request (org.forgerock.oauth2.restlet.RestletOAuth2Request)5 ChallengeResponse (org.restlet.data.ChallengeResponse)4 HttpRequest (org.restlet.engine.adapter.HttpRequest)4 Form (org.restlet.data.Form)3 SecureRandom (java.security.SecureRandom)2 DeviceCode (org.forgerock.oauth2.core.DeviceCode)2 ClientAuthenticationFailureFactory (org.forgerock.oauth2.core.exceptions.ClientAuthenticationFailureFactory)2 InvalidClientException (org.forgerock.oauth2.core.exceptions.InvalidClientException)2 BDDMockito.anyString (org.mockito.BDDMockito.anyString)2 BeforeMethod (org.testng.annotations.BeforeMethod)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SSOTokenManager (com.iplanet.sso.SSOTokenManager)1 Debug (com.sun.identity.shared.debug.Debug)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 JsonValue (org.forgerock.json.JsonValue)1