use of io.gravitee.am.gateway.handler.oauth2.exception.InvalidGrantException in project gravitee-access-management by gravitee-io.
the class ExtensionGrantGranter method resolveResourceOwner.
@Override
protected Maybe<User> resolveResourceOwner(TokenRequest tokenRequest, Client client) {
return extensionGrantProvider.grant(convert(tokenRequest)).flatMap(endUser -> {
if (extensionGrant.isCreateUser()) {
Map<String, Object> additionalInformation = endUser.getAdditionalInformation() == null ? new HashMap<>() : new HashMap<>(endUser.getAdditionalInformation());
// set source provider
additionalInformation.put("source", extensionGrant.getIdentityProvider() != null ? extensionGrant.getIdentityProvider() : extensionGrant.getId());
additionalInformation.put("client_id", client.getId());
((DefaultUser) endUser).setAdditionalInformation(additionalInformation);
return userAuthenticationManager.connect(endUser, false).toMaybe();
} else {
// Check that the user is existing from the identity provider
if (extensionGrant.isUserExists()) {
if (extensionGrant.getIdentityProvider() == null) {
return Maybe.error(new InvalidGrantException("No identity_provider provided"));
}
return identityProviderManager.get(extensionGrant.getIdentityProvider()).flatMap(prov -> retrieveUserByUsernameFromIdp(prov, tokenRequest, convert(endUser)).switchIfEmpty(Maybe.defer(() -> {
LOGGER.debug("User name '{}' not found, try as the userId", endUser.getUsername());
if (endUser.getId() != null) {
// so the search by ID should be done with the username...
return userService.findById(endUser.getUsername()).flatMap(user -> retrieveUserByUsernameFromIdp(prov, tokenRequest, user));
}
return Maybe.empty();
}))).map(idpUser -> {
User user = new User();
user.setId(endUser.getId());
user.setExternalId(idpUser.getId());
user.setUsername(endUser.getUsername());
Map<String, Object> extraInformation = new HashMap<>(idpUser.getAdditionalInformation());
if (endUser.getAdditionalInformation() != null) {
extraInformation.putAll(endUser.getAdditionalInformation());
}
if (user.getLoggedAt() != null) {
extraInformation.put(Claims.auth_time, user.getLoggedAt().getTime() / 1000);
}
extraInformation.put(StandardClaims.PREFERRED_USERNAME, user.getUsername());
user.setAdditionalInformation(extraInformation);
user.setCreatedAt(idpUser.getCreatedAt());
user.setUpdatedAt(idpUser.getUpdatedAt());
user.setDynamicRoles(idpUser.getRoles());
return user;
}).switchIfEmpty(Maybe.error(new InvalidGrantException("Unknown user: " + endUser.getId())));
} else {
User user = new User();
// we do not router AM user, user id is the idp user id
user.setId(endUser.getId());
user.setUsername(endUser.getUsername());
user.setAdditionalInformation(endUser.getAdditionalInformation());
return Maybe.just(user);
}
}
}).onErrorResumeNext(ex -> {
return Maybe.error(new InvalidGrantException(ex.getMessage()));
});
}
Aggregations