
io.dropwizard.bundles.apikey.BasicCredentialsAuthenticator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dropwizard-api-key-bundle Show documentation
Show all versions of dropwizard-api-key-bundle Show documentation
Dropwizard bundle that allows your application to support API keys.
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