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"));
}
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"));
}
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());
}
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());
}
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());
}
Aggregations