Java Code Examples for org.pac4j.core.profile.CommonProfile#setId()

The following examples show how to use org.pac4j.core.profile.CommonProfile#setId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SecurityContext.java    From NNAnalytics with Apache License 2.0 6 votes vote down vote up
private boolean localLogin(
    HttpServletRequest request, HttpServletResponse response, String username, String password)
    throws AuthenticationException {
  if (localOnlyUsers.allows(username)) {
    if (localOnlyUsers.authenticate(username, password)) {
      LOG.debug("Login success via [LOCAL] for: {} at {}", username, request.getRemoteAddr());
      CommonProfile profile = new CommonProfile();
      profile.setId(username);
      String generate = jwtGenerator.generate(profile);
      response.addHeader("Set-Cookie", "nna-jwt-token=" + generate);
      currentUser.set(username);
      return true;
    } else {
      LOG.info("Login failed via [LOCAL] for: {}", request.getRemoteAddr());
      throw new BadCredentialsException("Invalid credentials for: " + username);
    }
  }
  return false;
}
 
Example 2
Source File: SecurityContext.java    From NNAnalytics with Apache License 2.0 5 votes vote down vote up
private boolean ldapLogin(
    HttpServletRequest request, HttpServletResponse response, String username, String password)
    throws HttpAction {
  if (ldapAuthenticator != null) {
    RuntimeException authFailedEx = null;
    Set<String> ldapBaseDns = applicationConfiguration.getLdapBaseDn();
    for (String ldapBaseDn : ldapBaseDns) {
      String ldapDnRegexd = ldapBaseDn.replaceAll("%u", username);
      ldapAuthenticator.getLdapAuthenticator().setDnResolver(new FormatDnResolver(ldapDnRegexd));
      UsernamePasswordCredentials credentials =
          new UsernamePasswordCredentials(username, password, request.getRemoteAddr());
      try {
        ldapAuthenticator.validate(credentials, new J2EContext(request, response));
      } catch (RuntimeException e) {
        authFailedEx = e;
        continue;
      }
      LOG.debug("Login success via [LDAP] for: {} at {}", username, request.getRemoteAddr());
      CommonProfile profile = credentials.getUserProfile();
      profile.setId(username);
      String generate = jwtGenerator.generate(profile);
      response.addHeader("Set-Cookie", "nna-jwt-token=" + generate);
      currentUser.set(username);
      return true;
    }

    if (authFailedEx != null) {
      LOG.info("Login failed via [LDAP] for: {}", request.getRemoteAddr());
      throw authFailedEx;
    }
  }
  return false;
}