Search in sources :

Example 26 with OAuth2Request

use of io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request in project gravitee-access-management by gravitee-io.

the class ResourceOwnerPasswordCredentialsTokenGranterTest method shouldGenerateAnAccessToken.

@Test
public void shouldGenerateAnAccessToken() {
    LinkedMultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
    parameters.set(Parameters.USERNAME, "my-username");
    parameters.set(Parameters.PASSWORD, "my-password");
    Client client = new Client();
    client.setClientId("my-client-id");
    client.setAuthorizedGrantTypes(Arrays.asList(new String[] { "password" }));
    Token accessToken = new AccessToken("test-token");
    when(tokenRequest.parameters()).thenReturn(parameters);
    when(tokenRequest.createOAuth2Request()).thenReturn(new OAuth2Request());
    when(tokenRequestResolver.resolve(any(), any(), any())).thenReturn(Single.just(tokenRequest));
    when(tokenService.create(any(), any(), any())).thenReturn(Single.just(accessToken));
    when(userAuthenticationManager.authenticate(any(Client.class), any(Authentication.class))).thenReturn(Single.just(new User()));
    TestObserver<Token> testObserver = granter.grant(tokenRequest, client).test();
    testObserver.assertComplete();
    testObserver.assertNoErrors();
    testObserver.assertValue(token -> token.getValue().equals("test-token"));
}
Also used : OAuth2Request(io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request) User(io.gravitee.am.model.User) LinkedMultiValueMap(io.gravitee.common.util.LinkedMultiValueMap) AccessToken(io.gravitee.am.gateway.handler.oauth2.service.token.impl.AccessToken) Authentication(io.gravitee.am.identityprovider.api.Authentication) AccessToken(io.gravitee.am.gateway.handler.oauth2.service.token.impl.AccessToken) Token(io.gravitee.am.gateway.handler.oauth2.service.token.Token) Client(io.gravitee.am.model.oidc.Client) Test(org.junit.Test)

Example 27 with OAuth2Request

use of io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request in project gravitee-access-management by gravitee-io.

the class RefreshTokenGranterTest method shouldGenerateAnAccessToken.

@Test
public void shouldGenerateAnAccessToken() {
    String refreshToken = "refresh-token";
    LinkedMultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
    parameters.set("refresh_token", refreshToken);
    Client client = new Client();
    client.setClientId("my-client-id");
    client.setAuthorizedGrantTypes(Arrays.asList(new String[] { "refresh_token" }));
    OAuth2Request oAuth2Request = new OAuth2Request();
    oAuth2Request.setClientId(client.getClientId());
    oAuth2Request.setGrantType(GrantType.REFRESH_TOKEN);
    Token accessToken = new AccessToken("test-token");
    when(tokenRequest.parameters()).thenReturn(parameters);
    when(tokenRequest.createOAuth2Request()).thenReturn(oAuth2Request);
    when(tokenService.create(any(), any(), any())).thenReturn(Single.just(accessToken));
    when(tokenService.refresh(refreshToken, tokenRequest, client)).thenReturn(Single.just(new RefreshToken(refreshToken)));
    TestObserver<Token> testObserver = granter.grant(tokenRequest, client).test();
    testObserver.assertComplete();
    testObserver.assertNoErrors();
    testObserver.assertValue(token -> token.getValue().equals("test-token"));
}
Also used : OAuth2Request(io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request) RefreshToken(io.gravitee.am.gateway.handler.oauth2.service.token.impl.RefreshToken) LinkedMultiValueMap(io.gravitee.common.util.LinkedMultiValueMap) AccessToken(io.gravitee.am.gateway.handler.oauth2.service.token.impl.AccessToken) AccessToken(io.gravitee.am.gateway.handler.oauth2.service.token.impl.AccessToken) Token(io.gravitee.am.gateway.handler.oauth2.service.token.Token) RefreshToken(io.gravitee.am.gateway.handler.oauth2.service.token.impl.RefreshToken) Client(io.gravitee.am.model.oidc.Client) Test(org.junit.Test)

Example 28 with OAuth2Request

use of io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request in project gravitee-access-management by gravitee-io.

the class UmaTokenGranterTest method grant_user_nominalCase_accessPolicy_grant.

@Test
public void grant_user_nominalCase_accessPolicy_grant() {
    AccessPolicy policy = mock(AccessPolicy.class);
    when(policy.getType()).thenReturn(AccessPolicyType.GROOVY);
    ExecutionContext executionContext = mock(ExecutionContext.class);
    when(resourceService.findAccessPoliciesByResources(anyList())).thenReturn(Flowable.just(policy));
    when(executionContextFactory.create(any())).thenReturn(executionContext);
    when(rulesEngine.fire(any(), any())).thenReturn(Completable.complete());
    TestObserver<Token> testObserver = umaTokenGranter.grant(tokenRequest, client).test();
    testObserver.assertComplete().assertNoErrors().assertValue(token -> "success".equals(token.getValue()));
    OAuth2Request result = oauth2RequestCaptor.getValue();
    assertTrue(USER_ID.equals(result.getSubject()));
    assertTrue(assertNominalPermissions(result.getPermissions()));
    assertTrue(result.isSupportRefreshToken());
}
Also used : OAuth2Request(io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) Token(io.gravitee.am.gateway.handler.oauth2.service.token.Token) AccessToken(io.gravitee.am.gateway.handler.oauth2.service.token.impl.AccessToken) AccessPolicy(io.gravitee.am.model.uma.policy.AccessPolicy) Test(org.junit.Test)

Example 29 with OAuth2Request

use of io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request in project gravitee-access-management by gravitee-io.

the class UmaTokenGranterTest method grant_client_nominalCase.

@Test
public void grant_client_nominalCase() {
    parameters.remove(CLAIM_TOKEN);
    parameters.remove(CLAIM_TOKEN_FORMAT);
    TestObserver<Token> testObserver = umaTokenGranter.grant(tokenRequest, client).test();
    testObserver.assertComplete().assertNoErrors().assertValue(token -> "success".equals(token.getValue()));
    OAuth2Request result = oauth2RequestCaptor.getValue();
    assertNull(result.getSubject());
    assertTrue(assertNominalPermissions(result.getPermissions()));
    assertFalse(result.isSupportRefreshToken());
}
Also used : OAuth2Request(io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request) Token(io.gravitee.am.gateway.handler.oauth2.service.token.Token) AccessToken(io.gravitee.am.gateway.handler.oauth2.service.token.impl.AccessToken) Test(org.junit.Test)

Example 30 with OAuth2Request

use of io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request in project gravitee-access-management by gravitee-io.

the class UmaTokenGranterTest method grant_client_additionalCase.

@Test
public void grant_client_additionalCase() {
    parameters.remove(CLAIM_TOKEN);
    parameters.remove(CLAIM_TOKEN_FORMAT);
    tokenRequest.setScopes(new HashSet<>(Arrays.asList("scopeB", "scopeC")));
    TestObserver<Token> testObserver = umaTokenGranter.grant(tokenRequest, client).test();
    testObserver.assertComplete().assertNoErrors().assertValue(token -> "success".equals(token.getValue()));
    OAuth2Request result = oauth2RequestCaptor.getValue();
    assertNull(result.getSubject());
    assertTrue(assertAdditionalScopePermissions(result.getPermissions()));
    assertFalse(result.isSupportRefreshToken());
}
Also used : OAuth2Request(io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request) Token(io.gravitee.am.gateway.handler.oauth2.service.token.Token) AccessToken(io.gravitee.am.gateway.handler.oauth2.service.token.impl.AccessToken) Test(org.junit.Test)

Aggregations

OAuth2Request (io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request)32 Test (org.junit.Test)27 Client (io.gravitee.am.model.oidc.Client)21 ExecutionContext (io.gravitee.gateway.api.ExecutionContext)17 AccessToken (io.gravitee.am.gateway.handler.oauth2.service.token.impl.AccessToken)15 CertificateProvider (io.gravitee.am.certificate.api.CertificateProvider)12 JWT (io.gravitee.am.common.jwt.JWT)12 Token (io.gravitee.am.gateway.handler.oauth2.service.token.Token)12 User (io.gravitee.am.model.User)10 LinkedMultiValueMap (io.gravitee.common.util.LinkedMultiValueMap)5 ReactableExecutionContext (io.gravitee.am.gateway.handler.context.ReactableExecutionContext)3 IDTokenServiceImpl (io.gravitee.am.gateway.handler.oidc.service.idtoken.impl.IDTokenServiceImpl)3 TokenClaim (io.gravitee.am.model.TokenClaim)3 PermissionRequest (io.gravitee.am.model.uma.PermissionRequest)3 RefreshToken (io.gravitee.am.repository.oauth2.model.RefreshToken)3 InvalidTokenException (io.gravitee.am.common.exception.oauth2.InvalidTokenException)2 JWTService (io.gravitee.am.gateway.handler.common.jwt.JWTService)2 ExecutionContextFactory (io.gravitee.am.gateway.handler.context.ExecutionContextFactory)2 InvalidGrantException (io.gravitee.am.gateway.handler.oauth2.exception.InvalidGrantException)2 TokenRequest (io.gravitee.am.gateway.handler.oauth2.service.request.TokenRequest)2