Exemplo n.º 1
0
  @Override
  protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
      throws AuthenticationException {

    UsernamePasswordToken upToken = (UsernamePasswordToken) token;
    String username = upToken.getUsername().trim();
    String password = "";
    if (upToken.getPassword() != null) {
      password = new String(upToken.getPassword());
    }

    User user = null;
    try {
      user = userService.login(username, password);
    } catch (UserNotExistsException e) {
      throw new UnknownAccountException(e.getMessage(), e);
    } catch (UserPasswordNotMatchException e) {
      throw new AuthenticationException(e.getMessage(), e);
    } catch (UserPasswordRetryLimitExceedException e) {
      throw new ExcessiveAttemptsException(e.getMessage(), e);
    } catch (UserBlockedException e) {
      throw new LockedAccountException(e.getMessage(), e);
    } catch (Exception e) {
      log.error("login error", e);
      throw new AuthenticationException(new UserException("user.unknown.error", null));
    }

    SimpleAuthenticationInfo info =
        new SimpleAuthenticationInfo(user.getUsername(), password.toCharArray(), getName());
    return info;
  }
 @Override
 protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authToken)
     throws AuthenticationException {
   UsernamePasswordToken token = (UsernamePasswordToken) authToken;
   if (StringUtils.isBlank(token.getUsername())) {
     throw new AccountException("Empty usernames are not allowed by this realm.");
   }
   String loginPayload = createLoginPayload(token.getUsername(), token.getPassword());
   User user = authenticateUser(loginPayload);
   LOG.debug("{} successfully login via ZeppelinHub", user.login);
   return new SimpleAuthenticationInfo(user.login, token.getPassword(), name);
 }
Exemplo n.º 3
0
  @Override
  protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
      throws AuthenticationException {
    // only allow jcool/jcool
    UsernamePasswordToken userpass = (UsernamePasswordToken) token;
    if ("jcool".equals(userpass.getUsername())
        && "jcool".equals(new String(userpass.getPassword()))) {
      return new SimpleAuthenticationInfo(
          userpass.getUsername(), new String(userpass.getPassword()), this.getName());
    }

    return null;
  }
Exemplo n.º 4
0
  @Override
  protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
      throws AuthenticationException {
    /** WHEN USER LOGS IN !!! */
    logger.info("doGetAuthorizationInfo(token)...");
    UsernamePasswordToken upToken = (UsernamePasswordToken) token;
    String login = upToken.getUsername();
    logger.info("doGetAuthorizationInfo(token) : login = "******"doGetAuthorizationInfo(token) : password = "******"OK")) {
      // Authentication OK

      User user = new User(UserType.TELOSYS_USER, login);

      //			Constructor that takes in a single 'primary' principal of the account and
      //			its corresponding credentials, associated with the specified realm.
      //
      //			This is a convenience constructor and will construct a PrincipalCollection
      //			based on the principal and realmName argument.
      //			Parameters:principal the 'primary' principal associated with the specified
      // realm.credentials
      //			the credentials that verify the given principal.realmName the realm from where the
      // principal and credentials were acquired.
      return new SimpleAuthenticationInfo(user, password, REALM_NAME);
    } else {
      // Authentication INVALID
      throw new AuthenticationException("Invalid user/password");
    }
  }
  @Override
  protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)
      throws AuthenticationException {
    if (!(authenticationToken instanceof UsernamePasswordToken)) {
      throw new UnsupportedTokenException(
          "Token of type "
              + authenticationToken.getClass().getName()
              + " is not supported.  A "
              + UsernamePasswordToken.class.getName()
              + " is required.");
    }
    UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;

    String password = new String(token.getPassword());

    try {
      crowdClientHolder.getAuthenticationManager().authenticate(token.getUsername(), password);
      return new SimpleAuthenticationInfo(token.getPrincipal(), token.getCredentials(), getName());
    } catch (RemoteException e) {
      throw new AuthenticationException(DEFAULT_MESSAGE, e);
    } catch (com.atlassian.crowd.exception.InactiveAccountException e) {
      throw new AuthenticationException(DEFAULT_MESSAGE, e);
    } catch (com.atlassian.crowd.exception.ExpiredCredentialException e) {
      throw new AuthenticationException(DEFAULT_MESSAGE, e);
    } catch (com.atlassian.crowd.exception.InvalidAuthenticationException e) {
      throw new AuthenticationException(DEFAULT_MESSAGE, e);
    } catch (com.atlassian.crowd.exception.InvalidAuthorizationTokenException e) {
      throw new AuthenticationException(DEFAULT_MESSAGE, e);
    } catch (com.atlassian.crowd.exception.ApplicationAccessDeniedException e) {
      throw new AuthenticationException(DEFAULT_MESSAGE, e);
    }
  }
Exemplo n.º 6
0
  private boolean authenticateViaUrl(final UsernamePasswordToken usernamePasswordToken) {
    final HttpClient client = getHttpClient(null);

    try {
      final String url =
          kenaiRealmConfiguration.getConfiguration().getBaseUrl() + "api/login/authenticate.json";
      final List<NameValuePair> nameValuePairs = Lists.newArrayListWithCapacity(2);
      nameValuePairs.add(new BasicNameValuePair("username", usernamePasswordToken.getUsername()));
      nameValuePairs.add(
          new BasicNameValuePair("password", new String(usernamePasswordToken.getPassword())));
      final HttpPost post = new HttpPost(url);
      post.setEntity(new UrlEncodedFormEntity(nameValuePairs, Consts.UTF_8));
      final HttpResponse response = client.execute(post);

      try {
        logger.debug(
            "Kenai Realm user \"{}\" validated against URL={} as {}",
            usernamePasswordToken.getUsername(),
            url,
            response.getStatusLine());
        final boolean success =
            response.getStatusLine().getStatusCode() >= 200
                && response.getStatusLine().getStatusCode() <= 299;
        return success;
      } finally {
        HttpClientUtils.closeQuietly(response);
      }
    } catch (IOException e) {
      logger.info("Kenai Realm was unable to perform authentication", e);
      return false;
    }
  }
 @RequestMapping(value = "/auth", method = POST)
 public void authenticate(@RequestBody final UsernamePasswordToken credentials) {
   log.info(
       "Authenticating {} with password {}", credentials.getUsername(), credentials.getPassword());
   final Subject subject = SecurityUtils.getSubject();
   subject.login(credentials);
   // set attribute that will allow session querying
   subject.getSession().setAttribute("email", credentials.getUsername());
 }
Exemplo n.º 8
0
 private HttpClient getHttpClient(final UsernamePasswordToken usernamePasswordToken) {
   // risky, but we must blindly assume it is
   final DefaultHttpClient client = (DefaultHttpClient) hc4Provider.createHttpClient();
   if (usernamePasswordToken != null) {
     final List<String> authorisationPreference = new ArrayList<String>(2);
     authorisationPreference.add(AuthPolicy.DIGEST);
     authorisationPreference.add(AuthPolicy.BASIC);
     final Credentials credentials =
         new UsernamePasswordCredentials(
             usernamePasswordToken.getUsername(),
             String.valueOf(usernamePasswordToken.getPassword()));
     client.getCredentialsProvider().setCredentials(AuthScope.ANY, credentials);
     client.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, authorisationPreference);
   }
   return client;
 }
 /*
  * 用户验证。
  * @see org.apache.shiro.realm.AuthenticatingRealm#doGetAuthenticationInfo(org.apache.shiro.authc.AuthenticationToken)
  */
 @Override
 protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
     throws AuthenticationException {
   logger.debug(String.format("token:[%s]", token.getClass()));
   UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
   String username = usernamePasswordToken.getUsername(),
       pwd =
           new SimpleHash(
                   "md5",
                   new String(usernamePasswordToken.getPassword()),
                   ByteSource.Util.bytes(username),
                   2)
               .toHex();
   // 交给AuthenticatingRealm使用CredentialsMatcher进行密码匹配。
   return new SimpleAuthenticationInfo(
       username, pwd, ByteSource.Util.bytes(username), this.getName());
 }
Exemplo n.º 10
0
  @Override
  public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token)
      throws AuthenticationException {
    UsernamePasswordToken usernameToken = (UsernamePasswordToken) token;

    String email = usernameToken.getUsername();
    String senha = new String(usernameToken.getPassword());

    participanteDao = getParticipanteDao();

    Participante participante = participanteDao.getParticipante(email, senha);

    if (participante != null) {
      AuthenticationInfo info = new SimpleAuthenticationInfo(email, senha, getName());
      return info;
    }
    throw new AuthenticationException();
  }