Search in sources :

Example 1 with TokenStore

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

the class OpenAMTokenStoreTest method setUp.

@BeforeMethod
public void setUp() {
    tokenStore = mock(OAuthTokenStore.class);
    providerSettingsFactory = mock(OAuth2ProviderSettingsFactory.class);
    oAuth2UrisFactory = mock(OAuth2UrisFactory.class);
    clientRegistrationStore = mock(OpenIdConnectClientRegistrationStore.class);
    realmNormaliser = mock(RealmNormaliser.class);
    ssoTokenManager = mock(SSOTokenManager.class);
    request = mock(Request.class);
    cookieExtractor = mock(CookieExtractor.class);
    auditLogger = mock(OAuth2AuditLogger.class);
    debug = mock(Debug.class);
    failureFactory = mock(ClientAuthenticationFailureFactory.class);
    oAuth2RequestFactory = new RestletOAuth2RequestFactory(new JacksonRepresentationFactory(new ObjectMapper()));
    ClientAuthenticationFailureFactory failureFactory = mock(ClientAuthenticationFailureFactory.class);
    InvalidClientException expectedResult = mock(InvalidClientException.class);
    when(expectedResult.getError()).thenReturn(new String("invalid_client"));
    when(failureFactory.getException()).thenReturn(expectedResult);
    when(failureFactory.getException(anyString())).thenReturn(expectedResult);
    when(failureFactory.getException(any(OAuth2Request.class), anyString())).thenReturn(expectedResult);
    openAMtokenStore = new OpenAMTokenStore(tokenStore, providerSettingsFactory, oAuth2UrisFactory, clientRegistrationStore, realmNormaliser, ssoTokenManager, cookieExtractor, auditLogger, debug, new SecureRandom(), failureFactory);
}
Also used : OAuth2UrisFactory(org.forgerock.oauth2.core.OAuth2UrisFactory) SSOTokenManager(com.iplanet.sso.SSOTokenManager) JacksonRepresentationFactory(org.forgerock.openam.rest.representations.JacksonRepresentationFactory) RestletOAuth2Request(org.forgerock.oauth2.restlet.RestletOAuth2Request) Request(org.restlet.Request) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) SecureRandom(java.security.SecureRandom) BDDMockito.anyString(org.mockito.BDDMockito.anyString) OpenIdConnectClientRegistrationStore(org.forgerock.openidconnect.OpenIdConnectClientRegistrationStore) ClientAuthenticationFailureFactory(org.forgerock.oauth2.core.exceptions.ClientAuthenticationFailureFactory) RealmNormaliser(org.forgerock.openam.utils.RealmNormaliser) RestletOAuth2Request(org.forgerock.oauth2.restlet.RestletOAuth2Request) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) OAuth2ProviderSettingsFactory(org.forgerock.oauth2.core.OAuth2ProviderSettingsFactory) InvalidClientException(org.forgerock.oauth2.core.exceptions.InvalidClientException) RestletOAuth2RequestFactory(org.forgerock.oauth2.restlet.RestletOAuth2RequestFactory) Debug(com.sun.identity.shared.debug.Debug) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 2 with TokenStore

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

the class AccessTokenProtectionFilterTest method testBeforeHandleWithoutNeedingScope.

@Test
public void testBeforeHandleWithoutNeedingScope() throws Exception {
    //Given
    filter = new AccessTokenProtectionFilter(null, tokenStore, requestFactory, null);
    Request req = mock(Request.class);
    Response resp = mock(Response.class);
    OAuth2Request oAuth2Request = mock(OAuth2Request.class);
    when(requestFactory.create(req)).thenReturn(oAuth2Request);
    ChallengeResponse challengeResponse = new ChallengeResponse(ChallengeScheme.HTTP_BASIC);
    challengeResponse.setRawValue("tokenId");
    when(req.getChallengeResponse()).thenReturn(challengeResponse);
    AccessToken accessToken = new AccessToken(json(object(field("id", "tokenId"), field("tokenName", "access_token"), field("scope", asSet("a")), field("expireTime", System.currentTimeMillis() + 5000))));
    when(tokenStore.readAccessToken(oAuth2Request, "tokenId")).thenReturn(accessToken);
    //When
    int result = filter.beforeHandle(req, resp);
    //Then
    assertThat(result).isEqualTo(Filter.CONTINUE);
}
Also used : ChallengeResponse(org.restlet.data.ChallengeResponse) Response(org.restlet.Response) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) AccessToken(org.forgerock.oauth2.core.AccessToken) Request(org.restlet.Request) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) ChallengeResponse(org.restlet.data.ChallengeResponse) Test(org.testng.annotations.Test)

Example 3 with TokenStore

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

the class RestletFormBodyAccessTokenVerifierTest method shouldCheckValid.

@Test
public void shouldCheckValid() 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);
    AccessToken token = new AccessToken(json(object()), "access_token", "freddy") {

        @Override
        public boolean isExpired() {
            return false;
        }
    };
    when(tokenStore.readAccessToken(req, "freddy")).thenReturn(token);
    // When
    AccessTokenVerifier.TokenState result = verifier.verify(req);
    // Then
    assertThat(result.isValid()).isTrue();
    assertThat(result.getTokenId()).isEqualTo("freddy");
    verify(tokenStore).readAccessToken(req, "freddy");
}
Also used : OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) Form(org.restlet.data.Form) AccessToken(org.forgerock.oauth2.core.AccessToken) Request(org.restlet.Request) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) AccessTokenVerifier(org.forgerock.oauth2.core.AccessTokenVerifier) Test(org.testng.annotations.Test)

Example 4 with TokenStore

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

the class RestletFormBodyAccessTokenVerifierTest method shouldCheckExpired.

@Test
public void shouldCheckExpired() 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);
    AccessToken token = new AccessToken(json(object()), "access_token", "freddy") {

        @Override
        public boolean isExpired() {
            return true;
        }
    };
    when(tokenStore.readAccessToken(req, "freddy")).thenReturn(token);
    // 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) AccessToken(org.forgerock.oauth2.core.AccessToken) Request(org.restlet.Request) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) AccessTokenVerifier(org.forgerock.oauth2.core.AccessTokenVerifier) Test(org.testng.annotations.Test)

Example 5 with TokenStore

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

the class RestletHeaderAccessTokenVerifierTest method shouldCheckHttpHeader.

@Test
public void shouldCheckHttpHeader() throws Exception {
    // Given
    ServerCall serverCall = mock(ServerCall.class);
    HttpRequest request = mock(HttpRequest.class);
    OAuth2Request req = new RestletOAuth2Request(null, request);
    when(request.getHttpCall()).thenReturn(serverCall);
    Series<Header> requestHeaders = new Series<Header>(Header.class);
    requestHeaders.add(new Header(HeaderConstants.HEADER_AUTHORIZATION, "Bearer freddy"));
    when(request.getHttpCall().getRequestHeaders()).thenReturn(requestHeaders);
    // When
    AccessTokenVerifier.TokenState result = verifier.verify(req);
    // Then
    assertThat(result.isValid()).isFalse();
    verify(tokenStore).readAccessToken(req, "freddy");
}
Also used : HttpRequest(org.restlet.engine.adapter.HttpRequest) Series(org.restlet.util.Series) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) Header(org.restlet.data.Header) ServerCall(org.restlet.engine.adapter.ServerCall) 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