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

org.pac4j.http.client.indirect.IndirectBasicAuthClient Maven / Gradle / Ivy

There is a newer version: 6.1.0
Show newest version
package org.pac4j.http.client.indirect;

import org.pac4j.core.client.IndirectClientV2;
import org.pac4j.core.client.RedirectAction;
import org.pac4j.core.context.WebContext;
import org.pac4j.core.credentials.authenticator.Authenticator;
import org.pac4j.core.exception.CredentialsException;
import org.pac4j.core.exception.HttpAction;
import org.pac4j.core.profile.CommonProfile;
import org.pac4j.core.profile.creator.ProfileCreator;
import org.pac4j.core.util.CommonHelper;
import org.pac4j.core.credentials.UsernamePasswordCredentials;
import org.pac4j.core.credentials.authenticator.UsernamePasswordAuthenticator;
import org.pac4j.core.credentials.extractor.BasicAuthExtractor;

/**
 * 

This class is the client to authenticate users through HTTP basic auth. It was previously named: BasicAuthClient.

*

For authentication, the user is redirected to the callback url. If the user is not authenticated by basic auth, a * specific exception : {@link HttpAction} is returned which must be handled by the application to force * authentication.

* * @author Jerome Leleu * @since 1.8.0 */ public class IndirectBasicAuthClient extends IndirectClientV2 { private String realmName = "authentication required"; public IndirectBasicAuthClient() {} public IndirectBasicAuthClient(final Authenticator usernamePasswordAuthenticator) { setAuthenticator(usernamePasswordAuthenticator); } public IndirectBasicAuthClient(final String realmName, final Authenticator usernamePasswordAuthenticator) { this.realmName = realmName; setAuthenticator(usernamePasswordAuthenticator); } public IndirectBasicAuthClient(final Authenticator usernamePasswordAuthenticator, final ProfileCreator profileCreator) { setAuthenticator(usernamePasswordAuthenticator); setProfileCreator(profileCreator); } @Override protected void internalInit(final WebContext context) { CommonHelper.assertNotBlank("realmName", this.realmName); setRedirectActionBuilder(webContext -> RedirectAction.redirect(computeFinalCallbackUrl(webContext))); setCredentialsExtractor(new BasicAuthExtractor(getName())); super.internalInit(context); assertAuthenticatorTypes(UsernamePasswordAuthenticator.class); } @Override protected UsernamePasswordCredentials retrieveCredentials(final WebContext context) throws HttpAction { final UsernamePasswordCredentials credentials; try { // retrieve credentials credentials = getCredentialsExtractor().extract(context); logger.debug("credentials : {}", credentials); if (credentials == null) { throw HttpAction.unauthorized("Requires authentication", context, this.realmName); } // validate credentials getAuthenticator().validate(credentials); } catch (final CredentialsException e) { throw HttpAction.unauthorized("Requires authentication", context, this.realmName); } return credentials; } public String getRealmName() { return realmName; } public void setRealmName(String realmName) { this.realmName = realmName; } @Override public String toString() { return CommonHelper.toString(this.getClass(), "callbackUrl", this.callbackUrl, "name", getName(), "realmName", this.realmName, "extractor", getCredentialsExtractor(), "authenticator", getAuthenticator(), "profileCreator", getProfileCreator()); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy