/** {@inheritDoc} */
 @Override
 public void process(final AuthenticationResponse response) {
   if (response.getMessage() != null) {
     final ActiveDirectoryAccountState.Error adError =
         ActiveDirectoryAccountState.Error.parse(response.getMessage());
     if (adError != null) {
       response.setAccountState(new ActiveDirectoryAccountState(adError));
     }
   }
 }
  @Override
  public List<Message> handle(
      final AuthenticationResponse response, final LdapPasswordPolicyConfiguration configuration)
      throws LoginException {

    final AccountState state = response.getAccountState();
    if (state == null) {
      logger.debug("Account state not defined. Returning empty list of messages.");
      return Collections.emptyList();
    }

    final List<Message> messages = new ArrayList<>();
    handleError(state.getError(), response, configuration, messages);
    handleWarning(state.getWarning(), response, configuration, messages);

    return messages;
  }
Example #3
0
  @Override
  protected boolean login(final NameCallback nameCb, final PasswordCallback passCb)
      throws LoginException {
    try {
      getCredentials(nameCb, passCb, false);
      authRequest.setUser(new User(nameCb.getName()));
      authRequest.setCredential(new Credential(passCb.getPassword()));

      AuthenticationResponse response = auth.authenticate(authRequest);
      LdapEntry entry = null;
      if (response.getResult()) {
        entry = response.getLdapEntry();
        if (entry != null) {
          roles.addAll(LdapRole.toRoles(entry));
          if (defaultRole != null && !defaultRole.isEmpty()) {
            roles.addAll(defaultRole);
          }
        }
        loginSuccess = true;
      } else {
        if (tryFirstPass) {
          getCredentials(nameCb, passCb, true);
          response = auth.authenticate(authRequest);
          if (response.getResult()) {
            entry = response.getLdapEntry();
            if (entry != null) {
              roles.addAll(LdapRole.toRoles(entry));
            }
            if (defaultRole != null && !defaultRole.isEmpty()) {
              roles.addAll(defaultRole);
            }
            loginSuccess = true;
          } else {
            loginSuccess = false;
          }
        } else {
          loginSuccess = false;
        }
      }

      if (!loginSuccess) {
        logger.debug("Authentication failed: " + response);
        throw new LoginException("Authentication failed: " + response);
      } else {
        if (setLdapPrincipal) {
          principals.add(new LdapPrincipal(nameCb.getName(), entry));
        }

        final String loginDn = response.getResolvedDn();
        if (loginDn != null && setLdapDnPrincipal) {
          principals.add(new LdapDnPrincipal(loginDn, entry));
        }

        if (setLdapCredential) {
          credentials.add(new LdapCredential(passCb.getPassword()));
        }
        storeCredentials(nameCb, passCb, loginDn);
      }
    } catch (LdapException e) {
      logger.debug("Error occurred attempting authentication", e);
      loginSuccess = false;
      throw new LoginException(e != null ? e.getMessage() : "Authentication Error");
    }
    return true;
  }