use of javax.security.auth.message.AuthException in project wildfly by wildfly.
the class HTTPSchemeServerAuthModule method validateRequest.
@Override
public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject, Subject serviceSubject) throws AuthException {
HttpServerExchange exchange = (HttpServerExchange) messageInfo.getMap().get(JASPICAuthenticationMechanism.HTTP_SERVER_EXCHANGE_ATTACHMENT_KEY);
SecurityContext securityContext = (SecurityContext) messageInfo.getMap().get(JASPICAuthenticationMechanism.SECURITY_CONTEXT_ATTACHMENT_KEY);
ServletRequestContext src = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
List<AuthenticationMechanism> mechanisms = src.getDeployment().getAuthenticationMechanisms();
try {
boolean success = false;
for (AuthenticationMechanism mechanism : mechanisms) {
AuthenticationMechanism.AuthenticationMechanismOutcome result = mechanism.authenticate(exchange, securityContext);
if (result == AUTHENTICATED) {
success = true;
break;
} else if (result == NOT_AUTHENTICATED) {
break;
}
}
if (!success) {
String mandatory = (String) messageInfo.getMap().get("javax.security.auth.message.MessagePolicy.isMandatory");
if (mandatory != null && mandatory.toLowerCase().equals("false")) {
return SUCCESS;
} else {
for (AuthenticationMechanism mechanism : mechanisms) {
AuthenticationMechanism.ChallengeResult challengeResult = mechanism.sendChallenge(exchange, securityContext);
if (challengeResult.getDesiredResponseCode() != null) {
exchange.setResponseCode(challengeResult.getDesiredResponseCode());
}
if (exchange.isResponseComplete()) {
break;
}
}
return SEND_CONTINUE;
}
}
} catch (Exception e) {
UndertowLogger.ROOT_LOGGER.debug(e);
throw new AuthException("Could not validateRequest using mechanisms [" + mechanisms + ".");
}
return SUCCESS;
}
use of javax.security.auth.message.AuthException in project tomee by apache.
the class TheServerAuthModule method cdi.
private void cdi(final MessageInfo messageInfo, final String msg) throws AuthException {
final HttpServletRequest request = HttpServletRequest.class.cast(messageInfo.getRequestMessage());
final HttpServletResponse response = HttpServletResponse.class.cast(messageInfo.getResponseMessage());
if (request.getParameter("bean") != null) {
final TheBean cdiBean = CDI.current().select(TheBean.class).get();
cdiBean.set(msg);
try {
response.getWriter().write(String.valueOf(request.getAttribute("cdi")));
} catch (final IOException e) {
throw new AuthException(e.getMessage());
}
}
}
use of javax.security.auth.message.AuthException in project tomee by apache.
the class TheServerAuthModule method validateRequest.
@Override
public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject, Subject serviceSubject) throws AuthException {
HttpServletRequest request = (HttpServletRequest) messageInfo.getRequestMessage();
Callback[] callbacks;
if (request.getParameter("doLogin") != null) {
callbacks = new Callback[] { new CallerPrincipalCallback(clientSubject, "test"), new GroupPrincipalCallback(clientSubject, new String[] { "architect" }) };
} else {
callbacks = new Callback[] { new CallerPrincipalCallback(clientSubject, (Principal) null) };
}
try {
handler.handle(callbacks);
} catch (IOException | UnsupportedCallbackException e) {
throw (AuthException) new AuthException().initCause(e);
}
cdi(messageInfo, "vr");
return SUCCESS;
}
use of javax.security.auth.message.AuthException in project Payara by payara.
the class SimpleSAMConfig method getAuthContext.
@Override
public ServerAuthContext getAuthContext(String authContextID, Subject serviceSubject, Map properties) throws AuthException {
// combine constructed properties with passed in properties
if (constructedProperties != null)
properties.putAll(constructedProperties);
ServerAuthModule localSam = sam;
if (localSam == null || properties.containsKey(JASPICWebListenerHelper.SAM_PER_REQUEST_PROPERTY)) {
try {
localSam = (ServerAuthModule) samClass.newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
Logger.getLogger(SimpleSAMConfig.class.getName()).log(Level.SEVERE, null, ex);
AuthException ae = new AuthException("Unable to instantiate an instance of the provided SAM class");
ae.initCause(ex);
throw ae;
}
}
ServerAuthModule sam = this.sam;
if (sam == null) {
synchronized (this) {
this.sam = localSam;
}
}
return new SimpleSAMAuthContext(authContextID, serviceSubject, properties, handler, localSam);
}
use of javax.security.auth.message.AuthException in project Payara by payara.
the class RestMonitoringAuthModule method validateRequest.
@Override
public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject, Subject serviceSubject) throws AuthException {
if (securityEnabled) {
HttpServletRequest request = (HttpServletRequest) messageInfo.getRequestMessage();
HttpServletResponse response = (HttpServletResponse) messageInfo.getResponseMessage();
HttpSession session = request.getSession();
// Check if our session has already been authenticated
Principal userPrincipal = request.getUserPrincipal();
if (userPrincipal != null) {
try {
handler.handle(new Callback[] { new CallerPrincipalCallback(clientSubject, userPrincipal) });
return AuthStatus.SUCCESS;
} catch (IOException | UnsupportedCallbackException ex) {
AuthException ae = new AuthException();
ae.initCause(ex);
throw ae;
}
}
// See if the username / password has been passed in...
String username = request.getParameter("j_username");
String password = request.getParameter("j_password");
if ((username == null) || (password == null) || !request.getMethod().equalsIgnoreCase("post")) {
// Not passed in, show the login page...
String origPath = request.getRequestURI();
String queryString = request.getQueryString();
if ((queryString != null) && (!queryString.isEmpty())) {
origPath += "?" + queryString;
}
session.setAttribute(ORIG_REQUEST_PATH, origPath);
RequestDispatcher rd = request.getRequestDispatcher(LOGIN_PAGE);
try {
rd.forward(request, response);
} catch (Exception ex) {
AuthException authException = new AuthException();
authException.initCause(ex);
throw authException;
}
return AuthStatus.SEND_CONTINUE;
}
// Authenticate the details
PasswordValidationCallback pvCallback = new PasswordValidationCallback(clientSubject, username, password.toCharArray());
try {
handler.handle(new Callback[] { pvCallback });
} catch (Exception ex) {
AuthException ae = new AuthException();
ae.initCause(ex);
throw ae;
}
// Register the session as authenticated
messageInfo.getMap().put("javax.servlet.http.registerSession", Boolean.TRUE.toString());
// Redirect to original path
try {
String origRequest = (String) session.getAttribute(ORIG_REQUEST_PATH);
if ((origRequest == null)) {
origRequest = contextRoot;
}
response.sendRedirect(response.encodeRedirectURL(origRequest));
} catch (Exception ex) {
AuthException ae = new AuthException();
ae.initCause(ex);
throw ae;
}
// Continue...
return AuthStatus.SUCCESS;
} else {
Callback[] callbacks = new Callback[] { new CallerPrincipalCallback(clientSubject, DEFAULT_USER_NAME) };
try {
handler.handle(callbacks);
} catch (IOException | UnsupportedCallbackException ex) {
Logger.getLogger(RestMonitoringAuthModule.class.getName()).log(Level.SEVERE, null, ex);
}
return AuthStatus.SUCCESS;
}
}
Aggregations