use of hudson.security.UserMayOrMayNotExistException2 in project jenkins by jenkinsci.
the class User method getUserDetailsForImpersonation2.
/**
* This method checks with {@link SecurityRealm} if the user is a valid user that can login to the security realm.
* If {@link SecurityRealm} is a kind that does not support querying information about other users, this will
* use {@link LastGrantedAuthoritiesProperty} to pick up the granted authorities as of the last time the user has
* logged in.
*
* @return userDetails for the user, in case he's not found but seems legitimate, we provide a userDetails with minimum access
* @throws UsernameNotFoundException If this user is not a valid user in the backend {@link SecurityRealm}.
* @since 2.266
*/
@NonNull
public UserDetails getUserDetailsForImpersonation2() throws UsernameNotFoundException {
ImpersonatingUserDetailsService2 userDetailsService = new ImpersonatingUserDetailsService2(Jenkins.get().getSecurityRealm().getSecurityComponents().userDetails2);
try {
UserDetails userDetails = userDetailsService.loadUserByUsername(id);
LOGGER.log(Level.FINE, "Impersonation of the user {0} was a success", id);
return userDetails;
} catch (UserMayOrMayNotExistException2 e) {
LOGGER.log(Level.FINE, "The user {0} may or may not exist in the SecurityRealm, so we provide minimum access", id);
} catch (UsernameNotFoundException e) {
if (ALLOW_NON_EXISTENT_USER_TO_LOGIN) {
LOGGER.log(Level.FINE, "The user {0} was not found in the SecurityRealm but we are required to let it pass, due to ALLOW_NON_EXISTENT_USER_TO_LOGIN", id);
} else {
LOGGER.log(Level.FINE, "The user {0} was not found in the SecurityRealm", id);
throw e;
}
}
return new LegitimateButUnknownUserDetails(id);
}
use of hudson.security.UserMayOrMayNotExistException2 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;
});
}
use of hudson.security.UserMayOrMayNotExistException2 in project azure-ad-plugin by jenkinsci.
the class AzureSecurityRealm method loadGroupByGroupname2.
/**
* {@inheritDoc}
*/
@Override
public GroupDetails loadGroupByGroupname2(String groupName, boolean fetchMembers) {
if (isDisableGraphIntegration()) {
throw new UserMayOrMayNotExistException2("Can't lookup a group if graph integration is disabled");
}
GraphServiceClient<Request> azureClient = getAzureClient();
String groupId = ObjId2FullSidMap.extractObjectId(groupName);
if (groupId == null) {
// just an object id on it's own?
groupId = groupName;
}
Group group;
if (UUIDValidator.isValidUUID(groupId)) {
group = azureClient.groups(groupId).buildRequest().get();
} else {
group = loadGroupByDisplayName(groupName);
}
if (group == null || group.id == null) {
throw new UsernameNotFoundException("Group: " + groupName + " not found");
}
return new AzureAdGroupDetails(group.id, group.displayName);
}
use of hudson.security.UserMayOrMayNotExistException2 in project crowd2-plugin by jenkinsci.
the class CrowdMailAddressResolverImpl method findMailAddressFor.
/**
* {@inheritDoc}
*
* @see hudson.tasks.MailAddressResolver#findMailAddressFor(hudson.model.User)
*/
@Override
public String findMailAddressFor(User u) {
String mail = null;
SecurityRealm realm = getSecurityRealm();
if (realm instanceof CrowdSecurityRealm) {
try {
String userId = u.getId();
LOG.log(Level.FINE, "Looking up mail address for user: {0}", userId);
CrowdUser details = (CrowdUser) realm.loadUserByUsername2(userId);
mail = details.getEmailAddress();
} catch (UserMayOrMayNotExistException2 ex) {
LOG.log(Level.SEVERE, "User do not exist, unable to look up email address", ex);
} catch (UsernameNotFoundException ex) {
LOG.log(Level.INFO, "Failed to look up email address in Crowd");
}
}
return mail;
}
Aggregations