use of java.security.Principal in project tomcat by apache.
the class CombinedRealm method authenticate.
/**
* Return the Principal associated with the specified chain of X509
* client certificates. If there is none, return <code>null</code>.
*
* @param certs Array of client certificates, with the first one in
* the array being the certificate of the client itself.
*/
@Override
public Principal authenticate(X509Certificate[] certs) {
Principal authenticatedUser = null;
String username = null;
if (certs != null && certs.length > 0) {
username = certs[0].getSubjectDN().getName();
}
for (Realm realm : realms) {
if (log.isDebugEnabled()) {
log.debug(sm.getString("combinedRealm.authStart", username, realm.getClass().getName()));
}
authenticatedUser = realm.authenticate(certs);
if (authenticatedUser == null) {
if (log.isDebugEnabled()) {
log.debug(sm.getString("combinedRealm.authFail", username, realm.getClass().getName()));
}
} else {
if (log.isDebugEnabled()) {
log.debug(sm.getString("combinedRealm.authSuccess", username, realm.getClass().getName()));
}
break;
}
}
return authenticatedUser;
}
use of java.security.Principal in project storm by apache.
the class DefaultHttpCredentialsPlugin method populateContext.
/**
* Populates a given context with a new Subject derived from the
* credentials in a servlet request.
* @param context the context to be populated
* @param req the servlet request
* @return the context
*/
@Override
public ReqContext populateContext(ReqContext context, HttpServletRequest req) {
String userName = getUserName(req);
String doAsUser = req.getHeader("doAsUser");
if (doAsUser == null) {
doAsUser = req.getParameter("doAsUser");
}
if (doAsUser != null) {
context.setRealPrincipal(new SingleUserPrincipal(userName));
userName = doAsUser;
} else {
context.setRealPrincipal(null);
}
Set<Principal> principals = new HashSet<>();
if (userName != null) {
Principal p = new SingleUserPrincipal(userName);
principals.add(p);
}
Subject s = new Subject(true, principals, new HashSet(), new HashSet());
context.setSubject(s);
return context;
}
use of java.security.Principal in project storm by apache.
the class NimbusClient method withConfiguredClient.
public static void withConfiguredClient(WithNimbus cb, Map conf) throws Exception {
ReqContext context = ReqContext.context();
Principal principal = context.principal();
String user = principal == null ? null : principal.getName();
try (NimbusClient client = getConfiguredClientAs(conf, user)) {
cb.run(client.getClient());
}
}
use of java.security.Principal in project tomcat by apache.
the class ApplicationFilterChain method internalDoFilter.
private void internalDoFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
// Call the next filter if there is one
if (pos < n) {
ApplicationFilterConfig filterConfig = filters[pos++];
try {
Filter filter = filterConfig.getFilter();
if (request.isAsyncSupported() && "false".equalsIgnoreCase(filterConfig.getFilterDef().getAsyncSupported())) {
request.setAttribute(Globals.ASYNC_SUPPORTED_ATTR, Boolean.FALSE);
}
if (Globals.IS_SECURITY_ENABLED) {
final ServletRequest req = request;
final ServletResponse res = response;
Principal principal = ((HttpServletRequest) req).getUserPrincipal();
Object[] args = new Object[] { req, res, this };
SecurityUtil.doAsPrivilege("doFilter", filter, classType, args, principal);
} else {
filter.doFilter(request, response, this);
}
} catch (IOException | ServletException | RuntimeException e) {
throw e;
} catch (Throwable e) {
e = ExceptionUtils.unwrapInvocationTargetException(e);
ExceptionUtils.handleThrowable(e);
throw new ServletException(sm.getString("filterChain.filter"), e);
}
return;
}
// We fell off the end of the chain -- call the servlet instance
try {
if (ApplicationDispatcher.WRAP_SAME_OBJECT) {
lastServicedRequest.set(request);
lastServicedResponse.set(response);
}
if (request.isAsyncSupported() && !servletSupportsAsync) {
request.setAttribute(Globals.ASYNC_SUPPORTED_ATTR, Boolean.FALSE);
}
// Use potentially wrapped request from this point
if ((request instanceof HttpServletRequest) && (response instanceof HttpServletResponse) && Globals.IS_SECURITY_ENABLED) {
final ServletRequest req = request;
final ServletResponse res = response;
Principal principal = ((HttpServletRequest) req).getUserPrincipal();
Object[] args = new Object[] { req, res };
SecurityUtil.doAsPrivilege("service", servlet, classTypeUsedInService, args, principal);
} else {
servlet.service(request, response);
}
} catch (IOException | ServletException | RuntimeException e) {
throw e;
} catch (Throwable e) {
e = ExceptionUtils.unwrapInvocationTargetException(e);
ExceptionUtils.handleThrowable(e);
throw new ServletException(sm.getString("filterChain.servlet"), e);
} finally {
if (ApplicationDispatcher.WRAP_SAME_OBJECT) {
lastServicedRequest.set(null);
lastServicedResponse.set(null);
}
}
}
use of java.security.Principal in project tomcat by apache.
the class TestJNDIRealm method testAuthenticateWithoutUserPassword.
@Test
public void testAuthenticateWithoutUserPassword() throws Exception {
// GIVEN
JNDIRealm realm = buildRealm(PASSWORD);
// WHEN
String expectedResponse = MD5Encoder.encode(md5Helper.digest((ha1() + ":" + NONCE + ":" + HA2).getBytes()));
Principal principal = realm.authenticate(USER, expectedResponse, NONCE, null, null, null, REALM, HA2);
// THEN
Assert.assertNull(principal);
}
Aggregations