Search in sources :

Example 1 with TokensResponse

use of com.nexblocks.authguard.jwt.oauth.TokensResponse in project AuthGuard by AuthGuard.

the class OAuthServiceTest method exchangeAuthorizationCodeAndGetAccount.

@Test
void exchangeAuthorizationCodeAndGetAccount() {
    Mockito.when(sessionsService.getByToken(Mockito.any())).thenAnswer(invocation -> {
        final SessionBO session = SessionBO.builder().sessionToken(invocation.getArgument(0)).expiresAt(OffsetDateTime.now().plus(Duration.ofMinutes(2))).build();
        return Optional.of(session);
    });
    Mockito.when(accountsService.getByExternalId("1")).thenReturn(Optional.of(AccountBO.builder().id("1").build()));
    final TokensResponse actual = oAuthService.exchangeAuthorizationCode("account_test", "random", "code").join();
    final TokensResponse expected = testIdentityServer.getSuccessResponse();
    expected.setAccountId("1");
    assertThat(actual).isEqualTo(expected);
}
Also used : TokensResponse(com.nexblocks.authguard.jwt.oauth.TokensResponse) SessionBO(com.nexblocks.authguard.service.model.SessionBO)

Example 2 with TokensResponse

use of com.nexblocks.authguard.jwt.oauth.TokensResponse in project AuthGuard by AuthGuard.

the class OAuthServiceTest method exchangeAuthorizationCode.

@Test
void exchangeAuthorizationCode() {
    Mockito.when(sessionsService.getByToken(Mockito.any())).thenAnswer(invocation -> {
        final SessionBO session = SessionBO.builder().sessionToken(invocation.getArgument(0)).expiresAt(OffsetDateTime.now().plus(Duration.ofMinutes(2))).build();
        return Optional.of(session);
    });
    final TokensResponse actual = oAuthService.exchangeAuthorizationCode("test", "random", "code").join();
    final TokensResponse expected = testIdentityServer.getSuccessResponse();
    assertThat(actual).isEqualTo(expected);
}
Also used : TokensResponse(com.nexblocks.authguard.jwt.oauth.TokensResponse) SessionBO(com.nexblocks.authguard.service.model.SessionBO)

Example 3 with TokensResponse

use of com.nexblocks.authguard.jwt.oauth.TokensResponse in project AuthGuard by AuthGuard.

the class OAuthService method exchangeAuthorizationCode.

/**
 * Exchanges an authorization code with OAuth tokens. It'll verify that
 * a session containing that state exists before performing the exchange.
 * If the state has expired or no record of it existed then the future
 * will complete with {@link ServiceAuthorizationException}.
 *
 * @param provider The name of a provider as stated in the configuration.
 * @param state The state the identity provider returned.
 * @param authorizationCode The authorization code generated by the identity provider.
 */
public CompletableFuture<TokensResponse> exchangeAuthorizationCode(final String provider, final String state, final String authorizationCode) {
    final OAuthServiceClient client = Optional.ofNullable(providersClients.get(provider)).orElseThrow(() -> new ServiceException(ErrorCode.GENERIC_AUTH_FAILURE, "Invalid identity provider"));
    return CompletableFuture.supplyAsync(() -> sessionsService.getByToken(state)).thenCompose(sessionOptional -> sessionOptional.map(session -> doExchange(client, authorizationCode, session)).orElseThrow(() -> new ServiceAuthorizationException(ErrorCode.TOKEN_EXPIRED_OR_DOES_NOT_EXIST, "The provided state is either invalid or has expired"))).thenApply(tokensResponse -> {
        if (client.getConfiguration().isAccountProvider()) {
            if (tokensResponse.getIdToken() == null) {
                LOG.warn("Provider {} was set as an account provider but no ID was found in the response", provider);
            } else {
                final AccountBO account = getOrCreateAccount(client, authorizationCode, tokensResponse.getIdToken());
                tokensResponse.setAccountId(account.getId());
            }
        }
        return tokensResponse;
    });
}
Also used : JWT(com.auth0.jwt.JWT) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) ImmutableOAuthConfiguration(com.nexblocks.authguard.jwt.oauth.config.ImmutableOAuthConfiguration) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) ServiceException(com.nexblocks.authguard.service.exceptions.ServiceException) CompletableFuture(java.util.concurrent.CompletableFuture) SessionBO(com.nexblocks.authguard.service.model.SessionBO) AccountsService(com.nexblocks.authguard.service.AccountsService) Duration(java.time.Duration) Map(java.util.Map) ResponseType(com.nexblocks.authguard.jwt.oauth.ResponseType) Claim(com.auth0.jwt.interfaces.Claim) SessionsService(com.nexblocks.authguard.service.SessionsService) ServiceAuthorizationException(com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException) ErrorCode(com.nexblocks.authguard.service.exceptions.codes.ErrorCode) Logger(org.slf4j.Logger) AccountBO(com.nexblocks.authguard.service.model.AccountBO) ImmutableOAuthClientConfiguration(com.nexblocks.authguard.jwt.oauth.config.ImmutableOAuthClientConfiguration) TokensResponse(com.nexblocks.authguard.jwt.oauth.TokensResponse) AccountEmailBO(com.nexblocks.authguard.service.model.AccountEmailBO) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Try(io.vavr.control.Try) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) OAuthServiceClient(com.nexblocks.authguard.jwt.oauth.OAuthServiceClient) Optional(java.util.Optional) ConfigContext(com.nexblocks.authguard.config.ConfigContext) Named(com.google.inject.name.Named) AccountBO(com.nexblocks.authguard.service.model.AccountBO) ServiceException(com.nexblocks.authguard.service.exceptions.ServiceException) OAuthServiceClient(com.nexblocks.authguard.jwt.oauth.OAuthServiceClient) ServiceAuthorizationException(com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException)

Example 4 with TokensResponse

use of com.nexblocks.authguard.jwt.oauth.TokensResponse in project AuthGuard by AuthGuard.

the class OAuthServiceTest method exchangeAuthorizationCodeAndCreateAccount.

@Test
void exchangeAuthorizationCodeAndCreateAccount() {
    final RequestContextBO expectedContext = RequestContextBO.builder().idempotentKey("code").source("account_test").build();
    Mockito.when(sessionsService.getByToken(Mockito.any())).thenAnswer(invocation -> {
        final SessionBO session = SessionBO.builder().sessionToken(invocation.getArgument(0)).expiresAt(OffsetDateTime.now().plus(Duration.ofMinutes(2))).build();
        return Optional.of(session);
    });
    Mockito.when(accountsService.create(Mockito.any(), Mockito.eq(expectedContext))).thenAnswer(invocation -> invocation.getArgument(0, AccountBO.class).withId("1"));
    final TokensResponse actual = oAuthService.exchangeAuthorizationCode("account_test", "random", "code").join();
    final TokensResponse expected = testIdentityServer.getSuccessResponse();
    expected.setAccountId("1");
    assertThat(actual).isEqualTo(expected);
}
Also used : RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) TokensResponse(com.nexblocks.authguard.jwt.oauth.TokensResponse) SessionBO(com.nexblocks.authguard.service.model.SessionBO)

Aggregations

TokensResponse (com.nexblocks.authguard.jwt.oauth.TokensResponse)4 SessionBO (com.nexblocks.authguard.service.model.SessionBO)4 RequestContextBO (com.nexblocks.authguard.service.model.RequestContextBO)2 JWT (com.auth0.jwt.JWT)1 Claim (com.auth0.jwt.interfaces.Claim)1 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)1 Maps (com.google.common.collect.Maps)1 Inject (com.google.inject.Inject)1 Named (com.google.inject.name.Named)1 ConfigContext (com.nexblocks.authguard.config.ConfigContext)1 OAuthServiceClient (com.nexblocks.authguard.jwt.oauth.OAuthServiceClient)1 ResponseType (com.nexblocks.authguard.jwt.oauth.ResponseType)1 ImmutableOAuthClientConfiguration (com.nexblocks.authguard.jwt.oauth.config.ImmutableOAuthClientConfiguration)1 ImmutableOAuthConfiguration (com.nexblocks.authguard.jwt.oauth.config.ImmutableOAuthConfiguration)1 AccountsService (com.nexblocks.authguard.service.AccountsService)1 SessionsService (com.nexblocks.authguard.service.SessionsService)1 ServiceAuthorizationException (com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException)1 ServiceException (com.nexblocks.authguard.service.exceptions.ServiceException)1 ErrorCode (com.nexblocks.authguard.service.exceptions.codes.ErrorCode)1 AccountBO (com.nexblocks.authguard.service.model.AccountBO)1