} catch (IllegalArgumentException ex) {
getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
return null;
}
}
getResponse().setChallengeRequests(Collections.singletonList(new ChallengeRequest(ChallengeScheme.HTTP_BASIC,realm==null ? "users" : "realm "+realm.getName())));
getResponse().setStatus(Status.CLIENT_ERROR_UNAUTHORIZED);
return null;
}
String authid = authorization.getIdentifier().trim();
String password = new String(authorization.getSecret());
User user = null;
if (realm!=null) {
RealmUser ruser = findRealmUser(db,realm,authid);
if (ruser!=null) {
user = ruser.getUser();
}
if (user==null) {
// see if the user is a super user across realms
user = findUser(db,authid);
if (user!=null) {
// The user must either be a superuser or have the cross-realm permission
Permission superuser = db.getPermission(AuthDB.SUPERUSER_PERMISSION);
Permission crossrealm = db.getPermission(AuthDB.ACROSS_REALM_PERMISSION);
if (!user.hasPermission(superuser) && !user.hasPermission(crossrealm)) {
user = null;
}
}
}
} else {
user = findUser(db,authid);
}
if (user!=null) {
try {
String seconds = requestForm.getFirstValue("expiration");
long requestExpiration = seconds==null ? expiration : Long.parseLong(seconds)*1000;
if ("false".equals(requestForm.getFirstValue("session"))) {
requestExpiration = 0;
}
User.Authenticated authd = user.authenticate(realm,password,requestExpiration);
if (authd!=null) {
String userAlias = authd.getUser().getAlias();
UUID userId = authd.getUser().getUUID();
String name = authd.getUser().getName();
String email = authd.getUser().getEmail();
Iterator<Group> groups = null;
Iterator<Role> roles = authd.getUser().getRoles();
// If we have a realm, we need the realm user to get the right alias
if (realm!=null) {
RealmUser ruser = db.findRealmUser(realm,authd.getUser());
if (ruser!=null) {
userAlias = ruser.getAlias();
name = ruser.getName();
email = ruser.getEmail();
groups = ruser.getGroups();
}
}
getResponse().setStatus(Status.SUCCESS_OK);
return getSessionEntity(authd.getSession(),userId,userAlias,name,email,roles,groups);
}
} catch (Exception ex) {
getContext().getLogger().log(Level.SEVERE,"Failed to authenticate due to exception.",ex);
getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
return null;
}
}
getResponse().setStatus(Status.CLIENT_ERROR_UNAUTHORIZED);
getResponse().setChallengeRequests(Collections.singletonList(new ChallengeRequest(ChallengeScheme.HTTP_BASIC,realm==null ? "Realm Users" : "Users")));
} catch (SQLException ex) {
getContext().getLogger().log(Level.SEVERE,"Cannot get user data from database.",ex);
getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
}
return null;