use of com.nexblocks.authguard.jwt.oauth.OAuthServiceClient 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;
});
}
Aggregations