Package org.apache.geronimo.tomcat.security

Examples of org.apache.geronimo.tomcat.security.AuthResult


            UserIdentity cachedIdentity) throws ServerAuthException {
        try {
            HttpServletRequest httpRequest = request.getRequest();
            UserIdentity userIdentity = loginService.login(new RequestCallbackHandler(httpRequest));
            if (userIdentity != null) {
                return new AuthResult(TomcatAuthStatus.SUCCESS, userIdentity, false);
            } else {
                response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
                return new AuthResult(TomcatAuthStatus.FAILURE, unauthenticatedIdentity, false);
            }
        } catch (IOException e) {
            throw new ServerAuthException(e);
        }
    }
View Full Code Here


    @Override
    public AuthResult login(String username, String password, Request request) throws ServletException {
        UserIdentity userIdentity = loginService.login(username, password);
        if (userIdentity != null) {
            return new AuthResult(TomcatAuthStatus.SUCCESS, userIdentity, true);
        }
        return new AuthResult(TomcatAuthStatus.FAILURE, null, false);
    }
View Full Code Here

public class NoneAuthenticator implements Authenticator {

    private final AuthResult unauthenticated;

    public NoneAuthenticator(UserIdentity unauthenticatedIdentity) {
        unauthenticated = new AuthResult(TomcatAuthStatus.SUCCESS, unauthenticatedIdentity, false);
    }
View Full Code Here

            ServerAuthContext authContext = serverAuthConfig.getAuthContext(authContextId, serviceSubject, authProperties);
            Subject clientSubject = new Subject();

            AuthStatus authStatus = authContext.validateRequest(messageInfo, clientSubject, serviceSubject);
            if (authStatus == AuthStatus.SEND_CONTINUE)
                return new AuthResult(TomcatAuthStatus.SEND_CONTINUE, null, false);
            if (authStatus == AuthStatus.SEND_FAILURE)
                return new AuthResult(TomcatAuthStatus.SEND_FAILURE, null, false);

            if (authStatus == AuthStatus.SUCCESS) {
                Set<UserIdentity> ids = clientSubject.getPrivateCredentials(UserIdentity.class);
                UserIdentity userIdentity;
                if (ids.size() > 0) {
                    userIdentity = ids.iterator().next();
                } else {
                    CallerPrincipalCallback principalCallback = callbackHandler.getThreadCallerPrincipalCallback();
                    if (principalCallback == null) throw new NullPointerException("No CallerPrincipalCallback");
                    Principal principal = principalCallback.getPrincipal();
                    if (principal == null) {
                        String principalName = principalCallback.getName();
                        Set<Principal> principals = principalCallback.getSubject().getPrincipals();
                        for (Principal p : principals) {
                            if (p.getName().equals(principalName)) {
                                principal = p;
                                break;
                            }
                        }
                        if (principal == null) {
                            //TODO not clear what to do here.
                            return new AuthResult(TomcatAuthStatus.SUCCESS, null, false);
                        }
                    }
                    GroupPrincipalCallback groupPrincipalCallback = callbackHandler.getThreadGroupPrincipalCallback();
                    String[] groups = groupPrincipalCallback == null ? null : groupPrincipalCallback.getGroups();
                    userIdentity = identityService.newUserIdentity(clientSubject, principal, groups == null ? Collections.<String>emptyList() : Arrays.asList(groups));
                }
                return new AuthResult(TomcatAuthStatus.SUCCESS, userIdentity, containerCaching);
            }
            if (authStatus == AuthStatus.SEND_SUCCESS) {
                //we are processing a message in a secureResponse dialog.
                return new AuthResult(TomcatAuthStatus.SEND_SUCCESS, null, false);
            }
            //should not happen
            throw new NullPointerException("No AuthStatus returned");
        } catch (AuthException e) {
            throw new ServerAuthException(e);
View Full Code Here

        PasswordValidationCallback passwordValidationCallback = new PasswordValidationCallback(new Subject(), username, password.toCharArray());
        try {
            callbackHandler.handle(new Callback[] {passwordValidationCallback});
            if (passwordValidationCallback.getResult()) {
                UserIdentity userIdentity = passwordValidationCallback.getSubject().getPrivateCredentials(UserIdentity.class).iterator().next();
                return new AuthResult(TomcatAuthStatus.SUCCESS, userIdentity, containerCaching);
            }
            return new AuthResult(TomcatAuthStatus.FAILURE, null, false);
        } catch (UnsupportedCallbackException e) {
            throw new ServletException("internal server error");
        } catch (IOException e) {
            throw new ServletException("Unsuccessful login");
        }
View Full Code Here

                authorizationBC.setOffset(authorizationBC.getOffset() - 6);
            }

            UserIdentity userIdentity = loginService.login(username, password);
            if (userIdentity != null) {
                return new AuthResult(TomcatAuthStatus.SUCCESS, userIdentity, false);
            }
        }


        // Send an "unauthorized" response and an appropriate challenge
        if (isAuthMandatory) {
            try {
                StringBuilder authenticateCC = new StringBuilder();
                authenticateCC.append("Basic realm=\"");
                authenticateCC.append((realmName == null) ? "<unspecified>" : realmName);
                authenticateCC.append('\"');
                response.addHeader(new String(AUTHENTICATE_BYTES), authenticateCC.toString());
                response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
                return new AuthResult(TomcatAuthStatus.SEND_CONTINUE, null, false);
            } catch (IOException e) {
                throw new ServerAuthException(e);
            }
        }
        return new AuthResult(TomcatAuthStatus.SUCCESS, unauthenticatedIdentity, false);
    }
View Full Code Here

    @Override
    public AuthResult login(String username, String password, Request request) throws ServletException {
        UserIdentity userIdentity = loginService.login(username, password);
        if (userIdentity != null) {
            return new AuthResult(TomcatAuthStatus.SUCCESS, userIdentity, false);
        }
        return new AuthResult(TomcatAuthStatus.FAILURE, null, false);
    }
View Full Code Here

    public AuthResult validateRequest(Request request, HttpServletResponse response, boolean isAuthMandatory, UserIdentity cachedIdentity) throws ServerAuthException {
        String authorization = request.getHeader("authorization");
        if (authorization != null) {
            UserIdentity userIdentity = findPrincipal(request, authorization);
            if (userIdentity != null) {
                return new AuthResult(TomcatAuthStatus.SUCCESS, userIdentity, false);
            }
        }



        // Send an "unauthorized" response and an appropriate challenge

        // Next, generate a nOnce token (that is a token which is supposed
        // to be unique).
        if (isAuthMandatory) {
            String nOnce = generateNOnce(request);

            setAuthenticateHeader(response, nOnce);
            try {
                response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
            } catch (IOException e) {
                throw new ServerAuthException(e);
            }
            return new AuthResult(TomcatAuthStatus.SEND_CONTINUE, null, false);
        }
        return new AuthResult(TomcatAuthStatus.SUCCESS, unauthenticatedIdentity, false);

    }
View Full Code Here

    @Override
    public AuthResult login(String username, String password, Request request) throws ServletException {
        UserIdentity userIdentity = loginService.login(username, password);
        if (userIdentity != null) {
            return new AuthResult(TomcatAuthStatus.SUCCESS, userIdentity, false);
        }
        return new AuthResult(TomcatAuthStatus.FAILURE, null, false);
    }
View Full Code Here

    public AuthResult validateRequest(Request request, HttpServletResponse response, boolean isAuthMandatory, UserIdentity cachedIdentity) throws ServerAuthException {
        try {
            Session session = request.getSessionInternal(isAuthMandatory);
            if (session == null) {
                //default identity??
                return new AuthResult(TomcatAuthStatus.SUCCESS, null, false);
            }
            if (matchRequest(request, session)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Restore request from session '" + session.getIdInternal() + "'");
                }
                if (!restoreRequest(request, session)) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Proceed to restored request");
                    }
                    response.sendError(HttpServletResponse.SC_BAD_REQUEST);
                    return new AuthResult(TomcatAuthStatus.SEND_FAILURE, null, false);
                }
            }
            if (cachedIdentity != null) {
                return new AuthResult(TomcatAuthStatus.SUCCESS, cachedIdentity, true);
            }

            //we have not yet completed authentication.
            // Acquire references to objects we will need to evaluate
            MessageBytes uriMB = MessageBytes.newInstance();
            CharChunk uriCC = uriMB.getCharChunk();
            uriCC.setLimit(-1);
            String contextPath = request.getContextPath();
            String requestURI = request.getDecodedRequestURI();

            // Is this the action request from the login page?
            boolean loginAction =
                    requestURI.startsWith(contextPath) &&
                            requestURI.endsWith(Constants.FORM_ACTION);

            // No -- Save this request and redirect to the form login page
            if (!loginAction) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Save request in session '" + session.getIdInternal() + "'");
                }
                if (!isAuthMandatory) {
                    return new AuthResult(TomcatAuthStatus.SUCCESS, null, false);
                }
                try {
                    saveRequest(request, session);
                } catch (IOException ioe) {
                    logger.debug("Request body too big to save during authentication");
                    response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                            sm.getString("authenticator.requestBodyTooBig"));
                    return new AuthResult(TomcatAuthStatus.SEND_FAILURE, null, false);
                }
                forwardToLoginPage(request, response);
                return new AuthResult(TomcatAuthStatus.SEND_CONTINUE, unauthenticatedIdentity, false);
            }

            // Yes -- Validate the specified credentials and redirect
            // to the error page if they are not correct
//            if (characterEncoding != null) {
//                request.setCharacterEncoding(characterEncoding);
//            }
            String username = request.getParameter(Constants.FORM_USERNAME);
            String password = request.getParameter(Constants.FORM_PASSWORD);
            if (logger.isDebugEnabled()) {
                logger.debug("Authenticating username '" + username + "'");
            }
            UserIdentity userIdentity = loginService.login(username, password);
            if (userIdentity == null) {
                forwardToErrorPage(request, response);
                //TODO right status?
                return new AuthResult(TomcatAuthStatus.SEND_FAILURE, unauthenticatedIdentity, false);
            }

            if (logger.isDebugEnabled()) {
                logger.debug("Authentication of '" + username + "' was successful");
            }

            session = request.getSessionInternal(false);
            if (session == null) {
                if (logger.isDebugEnabled()) {
                    logger.debug("User took so long to log on the session expired");
                }
                response.sendError(HttpServletResponse.SC_REQUEST_TIMEOUT,
                        sm.getString("authenticator.sessionExpired"));
                return new AuthResult(TomcatAuthStatus.SEND_FAILURE, unauthenticatedIdentity, false);
            }

            // Redirect the user to the original request URI (which will cause
            // the original request to be restored)
            requestURI = savedRequestURL(session);
            if (logger.isDebugEnabled()) {
                logger.debug("Redirecting to original '" + requestURI + "'");
            }
            if (requestURI == null) {
                response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                        sm.getString("authenticator.formlogin"));
                return new AuthResult(TomcatAuthStatus.SEND_FAILURE, null, false);
            } else {
                response.sendRedirect(response.encodeRedirectURL(requestURI));
                return new AuthResult(TomcatAuthStatus.SEND_CONTINUE, userIdentity, true);
            }
        } catch (IOException e) {
            throw new ServerAuthException(e);
        }
View Full Code Here

TOP

Related Classes of org.apache.geronimo.tomcat.security.AuthResult

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.