Search in sources :

Example 6 with CallerPrincipalCallback

use of javax.security.auth.message.callback.CallerPrincipalCallback 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;
    }
}
Also used : HttpSession(javax.servlet.http.HttpSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) AuthException(javax.security.auth.message.AuthException) IOException(java.io.IOException) RequestDispatcher(javax.servlet.RequestDispatcher) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) IOException(java.io.IOException) AuthException(javax.security.auth.message.AuthException) HttpServletRequest(javax.servlet.http.HttpServletRequest) CallerPrincipalCallback(javax.security.auth.message.callback.CallerPrincipalCallback) PasswordValidationCallback(javax.security.auth.message.callback.PasswordValidationCallback) CallerPrincipalCallback(javax.security.auth.message.callback.CallerPrincipalCallback) Callback(javax.security.auth.callback.Callback) PasswordValidationCallback(javax.security.auth.message.callback.PasswordValidationCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) Principal(java.security.Principal)

Example 7 with CallerPrincipalCallback

use of javax.security.auth.message.callback.CallerPrincipalCallback in project Payara by payara.

the class ConnectorCallbackHandler method handleCallerPrincipalCallbackWithMapping.

public Callback handleCallerPrincipalCallbackWithMapping(CallerPrincipalCallback cpc) {
    CallerPrincipalCallback asCPC;
    Principal eisPrincipal = cpc.getPrincipal();
    String eisName = cpc.getName();
    Principal asPrincipal = getMappedPrincipal(eisPrincipal, eisName);
    asCPC = new CallerPrincipalCallback(cpc.getSubject(), asPrincipal);
    return asCPC;
/*
        Set<Principal> principals = cpc.getSubject().getPrincipals();
        for (Principal p : principals) {
            Principal mappedPrincipal = (Principal) securityMap.get(p);
            if (mappedPrincipal != null) {
                DistinguishedPrincipalCredential dpc = new DistinguishedPrincipalCredential(mappedPrincipal);
                cpc.getSubject().getPublicCredentials().add(dpc);
            }
        }
        SecurityContext.setCurrent(new SecurityContext(cpc.getSubject()));
*/
}
Also used : CallerPrincipalCallback(javax.security.auth.message.callback.CallerPrincipalCallback) Principal(java.security.Principal)

Example 8 with CallerPrincipalCallback

use of javax.security.auth.message.callback.CallerPrincipalCallback in project Payara by payara.

the class BaseContainerCallbackHandler method processCallback.

/**
 * gets the appropriate callback processor and hands the callback to
 * processor to process the callback.
 */
protected void processCallback(Callback callback) throws UnsupportedCallbackException {
    if (callback instanceof CallerPrincipalCallback) {
        processCallerPrincipal((CallerPrincipalCallback) callback);
    } else if (callback instanceof GroupPrincipalCallback) {
        processGroupPrincipal((GroupPrincipalCallback) callback);
    } else if (callback instanceof PasswordValidationCallback) {
        processPasswordValidation((PasswordValidationCallback) callback);
    } else if (callback instanceof PrivateKeyCallback) {
        processPrivateKey((PrivateKeyCallback) callback);
    } else if (callback instanceof TrustStoreCallback) {
        TrustStoreCallback tstoreCallback = (TrustStoreCallback) callback;
        if (_logger.isLoggable(Level.FINE)) {
            _logger.log(Level.FINE, "JMAC: In TrustStoreCallback Processor");
        }
        tstoreCallback.setTrustStore(sslUtils.getMergedTrustStore());
    } else if (callback instanceof CertStoreCallback) {
        processCertStore((CertStoreCallback) callback);
    } else if (callback instanceof SecretKeyCallback) {
        processSecretKey((SecretKeyCallback) callback);
    } else {
        // the isSupportedCallback method already takes care of this case
        if (_logger.isLoggable(Level.FINE)) {
            _logger.log(Level.FINE, "JMAC: UnsupportedCallback : " + callback.getClass().getName());
        }
        throw new UnsupportedCallbackException(callback);
    }
}
Also used : CallerPrincipalCallback(javax.security.auth.message.callback.CallerPrincipalCallback) GroupPrincipalCallback(javax.security.auth.message.callback.GroupPrincipalCallback) SecretKeyCallback(javax.security.auth.message.callback.SecretKeyCallback) TrustStoreCallback(javax.security.auth.message.callback.TrustStoreCallback) CertStoreCallback(javax.security.auth.message.callback.CertStoreCallback) PasswordValidationCallback(javax.security.auth.message.callback.PasswordValidationCallback) PrivateKeyCallback(javax.security.auth.message.callback.PrivateKeyCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 9 with CallerPrincipalCallback

use of javax.security.auth.message.callback.CallerPrincipalCallback in project javaee7-samples by javaee-samples.

the class TestServerAuthModule 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) {
        // For the test perform a login by directly "returning" the details of the authenticated user.
        // Normally credentials would be checked and the details fetched from some repository
        callbacks = new Callback[] { // The name of the authenticated user
        new CallerPrincipalCallback(clientSubject, "test"), // the roles of the authenticated user
        new GroupPrincipalCallback(clientSubject, new String[] { "architect" }) };
    } else {
        // The JASPIC protocol for "do nothing"
        callbacks = new Callback[] { new CallerPrincipalCallback(clientSubject, (Principal) null) };
    }
    try {
        // Communicate the details of the authenticated user to the container. In many
        // cases the handler will just store the details and the container will actually handle
        // the login after we return from this method.
        handler.handle(callbacks);
    } catch (IOException | UnsupportedCallbackException e) {
        throw (AuthException) new AuthException().initCause(e);
    }
    return SUCCESS;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) CallerPrincipalCallback(javax.security.auth.message.callback.CallerPrincipalCallback) GroupPrincipalCallback(javax.security.auth.message.callback.GroupPrincipalCallback) GroupPrincipalCallback(javax.security.auth.message.callback.GroupPrincipalCallback) CallerPrincipalCallback(javax.security.auth.message.callback.CallerPrincipalCallback) Callback(javax.security.auth.callback.Callback) AuthException(javax.security.auth.message.AuthException) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) Principal(java.security.Principal)

Example 10 with CallerPrincipalCallback

use of javax.security.auth.message.callback.CallerPrincipalCallback in project javaee7-samples by javaee-samples.

the class TestServerAuthModule method validateRequest.

@Override
public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject, Subject serviceSubject) throws AuthException {
    HttpServletRequest request = (HttpServletRequest) messageInfo.getRequestMessage();
    Callback[] callbacks;
    if (request.getAttribute("doLogin") != null) {
        // notice "getAttribute" here, this is set by the Servlet
        // For the test perform a login by directly "returning" the details of the authenticated user.
        // Normally credentials would be checked and the details fetched from some repository
        callbacks = new Callback[] { // The name of the authenticated user
        new CallerPrincipalCallback(clientSubject, "test"), // the roles of the authenticated user
        new GroupPrincipalCallback(clientSubject, new String[] { "architect" }) };
    } else {
        // The JASPIC protocol for "do nothing"
        callbacks = new Callback[] { new CallerPrincipalCallback(clientSubject, (Principal) null) };
    }
    try {
        // Communicate the details of the authenticated user to the container. In many
        // cases the handler will just store the details and the container will actually handle
        // the login after we return from this method.
        handler.handle(callbacks);
    } catch (IOException | UnsupportedCallbackException e) {
        throw (AuthException) new AuthException().initCause(e);
    }
    return SUCCESS;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) CallerPrincipalCallback(javax.security.auth.message.callback.CallerPrincipalCallback) GroupPrincipalCallback(javax.security.auth.message.callback.GroupPrincipalCallback) GroupPrincipalCallback(javax.security.auth.message.callback.GroupPrincipalCallback) CallerPrincipalCallback(javax.security.auth.message.callback.CallerPrincipalCallback) Callback(javax.security.auth.callback.Callback) AuthException(javax.security.auth.message.AuthException) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) Principal(java.security.Principal)

Aggregations

CallerPrincipalCallback (javax.security.auth.message.callback.CallerPrincipalCallback)30 GroupPrincipalCallback (javax.security.auth.message.callback.GroupPrincipalCallback)24 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)21 IOException (java.io.IOException)18 Principal (java.security.Principal)17 AuthException (javax.security.auth.message.AuthException)16 HttpServletRequest (javax.servlet.http.HttpServletRequest)16 Callback (javax.security.auth.callback.Callback)14 PasswordValidationCallback (javax.security.auth.message.callback.PasswordValidationCallback)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 Subject (javax.security.auth.Subject)4 CertStoreCallback (javax.security.auth.message.callback.CertStoreCallback)3 PrivateKeyCallback (javax.security.auth.message.callback.PrivateKeyCallback)3 SecretKeyCallback (javax.security.auth.message.callback.SecretKeyCallback)3 TrustStoreCallback (javax.security.auth.message.callback.TrustStoreCallback)3 Map (java.util.Map)2 HttpSession (javax.servlet.http.HttpSession)2 LoginCallbackImpl (org.eclipse.jetty.security.authentication.LoginCallbackImpl)2 CredentialValidationCallback (org.eclipse.jetty.security.jaspi.callback.CredentialValidationCallback)2 UserIdentity (org.eclipse.jetty.server.UserIdentity)2