Example #1
0
  /**
   * Start the authentication process.
   *
   * @param scheme scheme
   * @param request request
   * @throws Exception on any error
   */
  public static void authenticate(AuthenticationScheme scheme, HttpServletRequest request)
      throws Exception {
    AuthenticationModule module = scheme.currentAuthenticationModule();
    if (module == null) {
      throw new Exception("No current authentication module");
    }
    RequestParameterMap params = new RequestParameterMap(new ServletRequestAdapter(request));
    User currentUser = scheme.getUser();
    LogonStateAndCache logonStateMachine =
        (LogonStateAndCache)
            request.getSession().getAttribute(LogonStateAndCache.LOGON_STATE_MACHINE);

    if (logonStateMachine == null) {
      logonStateMachine =
          new LogonStateAndCache(LogonStateAndCache.STATE_STARTED, request.getSession());
    }

    if (logonStateMachine.getState()
        == LogonStateAndCache.STATE_KNOWN_USERNAME_NO_SCHEME_SPOOF_PASSWORD_ENTRY) {
      scheme.addCredentials(new PasswordCredentials("", "".toCharArray()));
    } else if (logonStateMachine.getState()
        == LogonStateAndCache.STATE_UNKNOWN_USERNAME_PROMPT_FOR_PASSWORD) {
      Credentials creds = module.authenticate(request, params);
      if (creds != null) scheme.addCredentials(creds);
    } else {
      Credentials creds = module.authenticate(request, params);
      if (creds != null) {
        scheme.addCredentials(creds);
        logonStateMachine.setState(LogonStateAndCache.STATE_VALID_LOGON);
      }
      // Check we have a user object
      if (currentUser == null && scheme.getUser() == null) {
        throw new Exception("The first authentication did not provide a user.");
      }
    }

    PolicyUtil.checkLogin(scheme.getUser());
  }