Exemple #1
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;
    }
  }
Exemple #2
0
 @Override
 protected AuthorizationInfo doGetAuthorizationInfo(final PrincipalCollection principals) {
   // only if authenticated with this realm too
   if (!principals.getRealmNames().contains(getName())) {
     return null;
   }
   // add the default role
   final SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
   authorizationInfo.addRole(kenaiRealmConfiguration.getConfiguration().getDefaultRole());
   return authorizationInfo;
 }