ServerAuthContext authContext = serverAuthConfig.getAuthContext(authContextId, serviceSubject, authProperties);
Subject clientSubject = new Subject();
AuthStatus authStatus = authContext.validateRequest(messageInfo, clientSubject, serviceSubject);
if (authStatus == AuthStatus.SEND_CONTINUE)
return new AuthResult(TomcatAuthStatus.SEND_CONTINUE, null);
if (authStatus == AuthStatus.SEND_FAILURE)
return new AuthResult(TomcatAuthStatus.SEND_FAILURE, null);
if (authStatus == AuthStatus.SUCCESS) {
Set<UserIdentity> ids = clientSubject.getPrivateCredentials(UserIdentity.class);
UserIdentity userIdentity;
if (ids.size() > 0) {
userIdentity = ids.iterator().next();
} else {
CallerPrincipalCallback principalCallback = callbackHandler.getThreadCallerPrincipalCallback();
if (principalCallback == null) throw new NullPointerException("No CallerPrincipalCallback");
Principal principal = principalCallback.getPrincipal();
if (principal == null) {
String principalName = principalCallback.getName();
Set<Principal> principals = principalCallback.getSubject().getPrincipals();
for (Principal p : principals) {
if (p.getName().equals(principalName)) {
principal = p;
break;
}
}
if (principal == null) {
//TODO not clear what to do here.
return new AuthResult(TomcatAuthStatus.SUCCESS, null);
}
}
GroupPrincipalCallback groupPrincipalCallback = callbackHandler.getThreadGroupPrincipalCallback();
String[] groups = groupPrincipalCallback == null ? null : groupPrincipalCallback.getGroups();
userIdentity = identityService.newUserIdentity(clientSubject, principal, Arrays.asList(groups));
}
return new AuthResult(TomcatAuthStatus.SUCCESS, userIdentity);
}
if (authStatus == AuthStatus.SEND_SUCCESS) {
//we are processing a message in a secureResponse dialog.
return new AuthResult(TomcatAuthStatus.SEND_SUCCESS, null);
}
//should not happen
throw new NullPointerException("No AuthStatus returned");
} catch (AuthException e) {
throw new ServerAuthException(e);