use of com.nexblocks.authguard.jwt.oauth.config.ImmutableOAuthClientConfiguration in project AuthGuard by AuthGuard.
the class OAuthService method getOrCreateAccount.
private AccountBO getOrCreateAccount(final OAuthServiceClient serviceClient, final String authorizationCode, final String idToken) {
final ImmutableOAuthClientConfiguration configuration = serviceClient.getConfiguration();
final DecodedJWT decoded = JWT.decode(idToken);
final String externalId = decoded.getSubject();
final Optional<AccountBO> account = accountsService.getByExternalId(externalId);
if (account.isPresent()) {
return account.get();
}
final AccountBO.Builder newAccount = AccountBO.builder().externalId(externalId).social(true).identityProvider(configuration.getProvider());
if (configuration.getEmailField() != null) {
final Claim emailClaim = decoded.getClaim(configuration.getEmailField());
if (!emailClaim.isNull()) {
newAccount.email(AccountEmailBO.builder().email(emailClaim.asString()).build());
}
}
final RequestContextBO requestContext = RequestContextBO.builder().source(configuration.getProvider()).idempotentKey(authorizationCode).build();
return accountsService.create(newAccount.build(), requestContext);
}
Aggregations