Search in sources :

Example 1 with RestletOAuth2Request

use of org.forgerock.oauth2.restlet.RestletOAuth2Request in project OpenAM by OpenRock.

the class OpenAMTokenStoreTest method shouldCreateDeviceCode.

@Test
public void shouldCreateDeviceCode() throws Exception {
    // Given
    OAuth2ProviderSettings providerSettings = mock(OAuth2ProviderSettings.class);
    given(providerSettingsFactory.get(any(OAuth2Request.class))).willReturn(providerSettings);
    given(providerSettings.getDeviceCodeLifetime()).willReturn(10);
    given(tokenStore.query(any(QueryFilter.class))).willReturn(json(array()));
    final RestletOAuth2Request oauth2Request = oAuth2RequestFactory.create(this.request);
    given(request.getAttributes()).willReturn(new ConcurrentHashMap<>(singletonMap("realm", (Object) "MY_REALM")));
    given(realmNormaliser.normalise("MY_REALM")).willReturn("MY_REALM");
    ResourceOwner resourceOwner = mock(ResourceOwner.class);
    given(resourceOwner.getId()).willReturn("RESOURCE_OWNER_ID");
    // When
    DeviceCode code = openAMtokenStore.createDeviceCode(asSet("one", "two"), resourceOwner, "CLIENT ID", "NONCE", "RESPONSE TYPE", "STATE", "ACR VALUES", "PROMPT", "UI LOCALES", "LOGIN HINT", 55, "CLAIMS", oauth2Request, "CODE CHALLENGE", "CODE METHOD");
    // Then
    assertThat(code.getScope()).containsOnly("one", "two");
    assertThat(code.getClientId()).isEqualTo("CLIENT ID");
    assertThat(code.getNonce()).isEqualTo("NONCE");
    assertThat(code.getResponseType()).isEqualTo("RESPONSE TYPE");
    assertThat(code.getState()).isEqualTo("STATE");
    assertThat(code.getAcrValues()).isEqualTo("ACR VALUES");
    assertThat(code.getPrompt()).isEqualTo("PROMPT");
    assertThat(code.getUiLocales()).isEqualTo("UI LOCALES");
    assertThat(code.getLoginHint()).isEqualTo("LOGIN HINT");
    assertThat(code.getClaims()).isEqualTo("CLAIMS");
    assertThat(code.getCodeChallenge()).isEqualTo("CODE CHALLENGE");
    assertThat(code.getCodeChallengeMethod()).isEqualTo("CODE METHOD");
    assertThat(code.getMaxAge()).isEqualTo(55);
    assertThat(code.getTokenName()).isEqualTo("device_code");
    assertThat(code.getExpiryTime()).isCloseTo(System.currentTimeMillis() + 10000, offset(1000L));
    assertThat(code.getTokenId()).matches("[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}");
    assertThat(code.getUserCode()).matches("[" + OpenAMTokenStore.ALPHABET + "]{8}");
    assertThat(code.getRealm()).isEqualTo("MY_REALM");
}
Also used : RestletOAuth2Request(org.forgerock.oauth2.restlet.RestletOAuth2Request) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) QueryFilter(org.forgerock.util.query.QueryFilter) ResourceOwner(org.forgerock.oauth2.core.ResourceOwner) DeviceCode(org.forgerock.oauth2.core.DeviceCode) OAuth2ProviderSettings(org.forgerock.oauth2.core.OAuth2ProviderSettings) RestletOAuth2Request(org.forgerock.oauth2.restlet.RestletOAuth2Request) Test(org.testng.annotations.Test)

Example 2 with RestletOAuth2Request

use of org.forgerock.oauth2.restlet.RestletOAuth2Request in project OpenAM by OpenRock.

the class OpenAMTokenStoreTest method shouldReadValidDeviceCode.

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

Example 3 with RestletOAuth2Request

use of org.forgerock.oauth2.restlet.RestletOAuth2Request in project OpenAM by OpenRock.

the class RestletFormBodyAccessTokenVerifierTest method shouldCheckBodyType.

@Test
public void shouldCheckBodyType() throws Exception {
    // Given
    Request request = new Request();
    request.setEntity(new EmptyRepresentation());
    OAuth2Request req = new RestletOAuth2Request(null, request);
    // When
    AccessTokenVerifier.TokenState result = verifier.verify(req);
    // Then
    assertThat(result.isValid()).isFalse();
}
Also used : OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) Request(org.restlet.Request) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) AccessTokenVerifier(org.forgerock.oauth2.core.AccessTokenVerifier) Test(org.testng.annotations.Test)

Example 4 with RestletOAuth2Request

use of org.forgerock.oauth2.restlet.RestletOAuth2Request 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 5 with RestletOAuth2Request

use of org.forgerock.oauth2.restlet.RestletOAuth2Request 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)

Aggregations

Test (org.testng.annotations.Test)15 OAuth2Request (org.forgerock.oauth2.core.OAuth2Request)12 AccessTokenVerifier (org.forgerock.oauth2.core.AccessTokenVerifier)11 Request (org.restlet.Request)10 HttpRequest (org.restlet.engine.adapter.HttpRequest)5 AccessToken (org.forgerock.oauth2.core.AccessToken)4 DeviceCode (org.forgerock.oauth2.core.DeviceCode)4 RestletOAuth2Request (org.forgerock.oauth2.restlet.RestletOAuth2Request)4 Form (org.restlet.data.Form)4 ChallengeResponse (org.restlet.data.ChallengeResponse)3 OAuth2ProviderSettings (org.forgerock.oauth2.core.OAuth2ProviderSettings)1 ResourceOwner (org.forgerock.oauth2.core.ResourceOwner)1 QueryFilter (org.forgerock.util.query.QueryFilter)1 Header (org.restlet.data.Header)1 ServerCall (org.restlet.engine.adapter.ServerCall)1 EmptyRepresentation (org.restlet.representation.EmptyRepresentation)1 Series (org.restlet.util.Series)1