use of com.microsoft.graph.requests.GraphServiceClient in project azure-ad-plugin by jenkinsci.
the class AzureSecurityRealm method createSecurityComponents.
@Override
public SecurityComponents createSecurityComponents() {
return new SecurityComponents((AuthenticationManager) authentication -> {
if (authentication instanceof AzureAuthenticationToken) {
return authentication;
}
throw new BadCredentialsException("Unexpected authentication type: " + authentication);
}, username -> {
if (username == null) {
throw new UserMayOrMayNotExistException2("Can't find a user with no username");
}
if (isDisableGraphIntegration()) {
throw new UserMayOrMayNotExistException2("Can't lookup a user if graph integration is disabled");
}
AzureAdUser azureAdUser = caches.get(username, (cacheKey) -> {
GraphServiceClient<Request> azureClient = getAzureClient();
String userId = ObjId2FullSidMap.extractObjectId(username);
if (userId == null) {
userId = username;
}
// as we look up by object id we don't know if it's a user or a group :(
try {
com.microsoft.graph.models.User activeDirectoryUser = azureClient.users(userId).buildRequest().get();
if (activeDirectoryUser != null & activeDirectoryUser.id == null) {
// known to happen when subject is a group with display name only and starts with a #
return null;
}
AzureAdUser user = requireNonNull(AzureAdUser.createFromActiveDirectoryUser(activeDirectoryUser));
List<AzureAdGroup> groups = AzureCachePool.get(azureClient).getBelongingGroupsByOid(user.getObjectID());
user.setAuthorities(groups);
return user;
} catch (GraphServiceException e) {
if (e.getResponseCode() == NOT_FOUND) {
return null;
} else if (e.getResponseCode() == BAD_REQUEST) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "Failed to lookup user with userid '" + userId, e);
} else {
LOGGER.log(Level.WARNING, "Failed to lookup user with userid '" + userId + "'." + " Enable 'Fine' Logging for more information.");
}
return null;
}
throw e;
}
});
if (azureAdUser == null) {
throw new UsernameNotFoundException("Cannot find user: " + username);
}
return azureAdUser;
});
}
Aggregations