Search in sources :

Example 26 with UserIdentity

use of org.eclipse.jetty.server.UserIdentity in project zm-mailbox by Zimbra.

the class SpnegoAuthenticator method authenticate.

/* =========================================================
     *
     * Based on org.eclipse.jetty.security.SpnegoAuthenticator
     *
     * =========================================================
     */
private ZimbraPrincipal authenticate(LoginService realm, Request request, HttpServletResponse response) throws ServiceException, IOException {
    Principal user = null;
    String header = request.getHeader(HttpHeader.AUTHORIZATION.toString());
    /*
         * if the header is null then we need to challenge...this is after the error page check
         */
    if (header == null) {
        sendChallenge(realm, request, response);
        throw SSOAuthenticatorServiceException.SENT_CHALLENGE();
    } else if (header != null && header.startsWith(HttpHeader.NEGOTIATE.toString())) {
        /*
             * we have gotten a negotiate header to try and authenticate
             */
        // skip over "Negotiate "
        String token = header.substring(10);
        UserIdentity identity = realm.login(null, token, request);
        if (identity == null) {
            throw AuthFailedServiceException.AUTH_FAILED("SpengoAuthenticator: unable to login", (Throwable) null);
        }
        user = identity.getUserPrincipal();
        if (user != null) {
            ZimbraLog.account.debug("SpengoAuthenticator: obtained principal: " + user.getName());
            Account acct = getAccountByPrincipal(user);
            ZimbraPrincipal zimbraPrincipal = new ZimbraPrincipal(user.getName(), acct);
            String clientName = ((SpnegoUserPrincipal) user).getName();
            String role = clientName.substring(clientName.indexOf('@') + 1);
            SpnegoUserIdentity spnegoUserIdentity = new SpnegoUserIdentity(identity.getSubject(), zimbraPrincipal, Arrays.asList(role));
            Authentication authentication = new UserAuthentication(getAuthType(), spnegoUserIdentity);
            request.setAuthentication(authentication);
            response.addHeader(HttpHeader.WWW_AUTHENTICATE.toString(), HttpHeader.NEGOTIATE.toString() + " " + ((SpnegoUserPrincipal) user).getToken());
            return zimbraPrincipal;
        } else {
            /*
                 * no user was returned from the authentication which means something failed
                 * so process error logic
                 */
            ZimbraLog.account.debug("SpengoAuthenticator: no user found, authentication failed");
            throw AuthFailedServiceException.AUTH_FAILED("SpengoAuthenticator: no user found, authentication failed", (Throwable) null);
        }
    } else {
        /*
             * the header was not null, but we didn't get a negotiate so process error logic
             */
        throw AuthFailedServiceException.AUTH_FAILED("SpengoAuthenticator: authentication failed, unknown header (browser is likely misconfigured for SPNEGO)", (Throwable) null);
    }
}
Also used : SpnegoUserIdentity(org.eclipse.jetty.security.SpnegoUserIdentity) GuestAccount(com.zimbra.cs.account.GuestAccount) Account(com.zimbra.cs.account.Account) UserAuthentication(org.eclipse.jetty.security.UserAuthentication) Authentication(org.eclipse.jetty.server.Authentication) UserIdentity(org.eclipse.jetty.server.UserIdentity) SpnegoUserIdentity(org.eclipse.jetty.security.SpnegoUserIdentity) UserAuthentication(org.eclipse.jetty.security.UserAuthentication) Krb5Principal(com.zimbra.cs.account.krb5.Krb5Principal) SpnegoUserPrincipal(org.eclipse.jetty.security.SpnegoUserPrincipal) Principal(java.security.Principal)

Aggregations

UserIdentity (org.eclipse.jetty.server.UserIdentity)26 UserAuthentication (org.eclipse.jetty.security.UserAuthentication)17 HttpServletRequest (javax.servlet.http.HttpServletRequest)15 HttpServletResponse (javax.servlet.http.HttpServletResponse)11 ServerAuthException (org.eclipse.jetty.security.ServerAuthException)11 IOException (java.io.IOException)10 Principal (java.security.Principal)7 HttpSession (javax.servlet.http.HttpSession)6 Authentication (org.eclipse.jetty.server.Authentication)6 Constraint (org.eclipse.jetty.util.security.Constraint)6 Subject (javax.security.auth.Subject)4 ServletRequest (javax.servlet.ServletRequest)4 Request (org.eclipse.jetty.server.Request)4 ArrayList (java.util.ArrayList)3 SessionAuthentication (org.eclipse.jetty.security.authentication.SessionAuthentication)3 Account (com.zimbra.cs.account.Account)2 KeyStore (java.security.KeyStore)2 MessageDigest (java.security.MessageDigest)2 X509Certificate (java.security.cert.X509Certificate)2 CallerPrincipalCallback (javax.security.auth.message.callback.CallerPrincipalCallback)2