List<String> authHeaders = messageContext.getHttpHeaders()
.getRequestHeader(HttpHeaders.AUTHORIZATION);
if (authHeaders.size() != 1) {
LOG.fine("No Authorization header is available");
throw new NotAuthorizedException(getFaultResponse());
}
String[] authPair = StringUtils.split(authHeaders.get(0), " ");
if (authPair.length != 2 || !NEGOTIATE_SCHEME.equalsIgnoreCase(authPair[0])) {
LOG.fine("Negotiate Authorization scheme is expected");
throw new NotAuthorizedException(getFaultResponse());
}
byte[] serviceTicket = getServiceTicket(authPair[1]);
try {
Subject serviceSubject = loginAndGetSubject();
GSSContext gssContext = createGSSContext();
Subject.doAs(serviceSubject, new ValidateServiceTicketAction(gssContext, serviceTicket));
GSSName srcName = gssContext.getSrcName();
if (srcName == null) {
throw new NotAuthorizedException(getFaultResponse());
}
String complexUserName = srcName.toString();
String simpleUserName = complexUserName;
int index = simpleUserName.lastIndexOf('@');
if (index > 0) {
simpleUserName = simpleUserName.substring(0, index);
}
if (!gssContext.getCredDelegState()) {
gssContext.dispose();
gssContext = null;
}
Message m = JAXRSUtils.getCurrentMessage();
m.put(SecurityContext.class,
new KerberosSecurityContext(new KerberosPrincipal(simpleUserName,
complexUserName),
gssContext));
} catch (LoginException e) {
LOG.fine("Unsuccessful JAAS login for the service principal");
throw new NotAuthorizedException(getFaultResponse());
} catch (GSSException e) {
LOG.fine("GSS API exception: " + e.getMessage());
throw new NotAuthorizedException(getFaultResponse());
} catch (PrivilegedActionException e) {
LOG.fine("PrivilegedActionException: " + e.getMessage());
throw new NotAuthorizedException(getFaultResponse());
}
}