All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.dropwizard.bundles.apikey.BasicCredentialsAuthenticator Maven / Gradle / Ivy

The newest version!
package io.dropwizard.bundles.apikey;

import com.google.common.base.Optional;
import io.dropwizard.auth.AuthenticationException;
import io.dropwizard.auth.Authenticator;
import io.dropwizard.auth.basic.BasicCredentials;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * An Authenticator that converts HTTP basic authentication credentials into an API key.
 */
public class BasicCredentialsAuthenticator implements Authenticator {
  private final ApiKeyProvider provider;

  BasicCredentialsAuthenticator(ApiKeyProvider provider) {
    this.provider = checkNotNull(provider);
  }

  @Override
  public Optional authenticate(BasicCredentials credentials)
      throws AuthenticationException {
    checkNotNull(credentials);

    String username = credentials.getUsername();
    String secret = credentials.getPassword();

    ApiKey key = provider.get(username);
    if (key == null) {
      return Optional.absent();
    }

    if (!secret.equals(key.getSecret())) {
      return Optional.absent();
    }

    return Optional.of(key.getUsername());
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy