com.github.lontime.extpac4j.config.PropertiesConfigExtFactory Maven / Gradle / Ivy
The newest version!
package com.github.lontime.extpac4j.config;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.github.lontime.base.commonj.components.ComponentInterface;
import com.github.lontime.base.commonj.components.ComponentInterfaceHelper;
import com.github.lontime.base.commonj.utils.CollectionHelper;
import com.github.lontime.extpac4j.config.builder.DbAuthenticatorExtBuilder;
import com.github.lontime.extpac4j.config.builder.DirectClientExtBuilder;
import com.github.lontime.extpac4j.config.builder.OAuthExtBuilder;
import com.github.lontime.extpac4j.provider.AuthenticatorInterface;
import org.pac4j.config.builder.CasClientBuilder;
import org.pac4j.config.builder.DbAuthenticatorBuilder;
import org.pac4j.config.builder.IndirectHttpClientBuilder;
import org.pac4j.config.builder.LdapAuthenticatorBuilder;
import org.pac4j.config.builder.OidcClientBuilder;
import org.pac4j.config.builder.RestAuthenticatorBuilder;
import org.pac4j.config.builder.Saml2ClientBuilder;
import org.pac4j.config.builder.ShiroEncoderBuilder;
import org.pac4j.config.builder.SpringEncoderBuilder;
import org.pac4j.config.client.PropertiesConfigFactory;
import org.pac4j.core.client.Client;
import org.pac4j.core.config.Config;
import org.pac4j.core.credentials.authenticator.Authenticator;
import org.pac4j.core.credentials.password.PasswordEncoder;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.BODY_JSONCLIENT_AUTHENTICATOR;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.COOKIECLIENT_AUTHENTICATOR;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.DIRECTBEARERAUTHCLIENT_AUTHENTICATOR;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.DIRECTDIGESTAUTHCLIENT_AUTHENTICATOR;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.DIRECT_FORMCLIENT_AUTHENTICATOR;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.HEADERCLIENT_AUTHENTICATOR;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.IPCLIENT_AUTHENTICATOR;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.JDBI_NAME;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.PARAMETERCLIENT_AUTHENTICATOR;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.WECHAT_KEY;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.WECHAT_SECRET;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.X509CLIENT_AUTHENTICATOR;
import static org.pac4j.core.util.CommonHelper.isNotBlank;
/**
* PropertiesConfigExtFactory.
* @author lontime
* @since 1.0
*/
public class PropertiesConfigExtFactory extends PropertiesConfigFactory {
private final String callbackUrl2;
public PropertiesConfigExtFactory(Map properties) {
this(null, properties);
}
public PropertiesConfigExtFactory(String callbackUrl, Map properties) {
super(callbackUrl, properties);
this.callbackUrl2 = callbackUrl;
}
@Override
public Config build(Object... parameters) {
final List clients = new ArrayList<>();
final Map authenticators = new HashMap<>();
final Map encoders = new HashMap<>();
final List authenticatorInterfaces =
ComponentInterfaceHelper.getAll(AuthenticatorInterface.class);
if (CollectionHelper.isNotEmpty(authenticatorInterfaces)) {
authenticators.putAll(authenticatorInterfaces.stream()
.collect(Collectors.toMap(ComponentInterface::getName, k -> k, (m1, m2) -> m1)));
}
// spring-security-crypto dependency required
if (hasSpringEncoder()) {
final SpringEncoderBuilder springEncoderBuilder = new SpringEncoderBuilder(properties);
springEncoderBuilder.tryCreatePasswordEncoder(encoders);
}
// shiro-core dependency required
if (hasShiroEncoder()) {
final ShiroEncoderBuilder shiroEncoderBuilder = new ShiroEncoderBuilder(properties);
shiroEncoderBuilder.tryCreatePasswordEncoder(encoders);
}
// pac4j-ldap dependency required
if (hasLdapAuthenticator()) {
final LdapAuthenticatorBuilder ldapAuthenticatorBuilder = new LdapAuthenticatorBuilder(properties);
ldapAuthenticatorBuilder.tryBuildLdapAuthenticator(authenticators);
}
// pac4j-sql dependency required
if (hasDbAuthenticator()) {
final DbAuthenticatorBuilder dbAuthenticatorBuilder = new DbAuthenticatorBuilder(properties);
dbAuthenticatorBuilder.tryBuildDbAuthenticator(authenticators, encoders);
final DbAuthenticatorExtBuilder dbAuthenticatorExtBuilder = new DbAuthenticatorExtBuilder(properties);
dbAuthenticatorExtBuilder.tryBuildDbAuthenticator(authenticators, encoders);
}
// pac4j-oauth dependency required
if (hasOAuthClients()) {
final OAuthExtBuilder oAuthBuilder = new OAuthExtBuilder(properties);
oAuthBuilder.tryCreateFacebookClient(clients);
oAuthBuilder.tryCreateTwitterClient(clients);
oAuthBuilder.tryCreateDropboxClient(clients);
oAuthBuilder.tryCreateGithubClient(clients);
oAuthBuilder.tryCreateYahooClient(clients);
oAuthBuilder.tryCreateGoogleClient(clients);
oAuthBuilder.tryCreateFoursquareClient(clients);
oAuthBuilder.tryCreateWindowsLiveClient(clients);
oAuthBuilder.tryCreateLinkedInClient(clients);
oAuthBuilder.tryCreateGenericOAuth2Clients(clients);
oAuthBuilder.tryCreateWechatClient(clients);
}
// pac4j-saml dependency required
if (hasSaml2Clients()) {
final Saml2ClientBuilder saml2ClientBuilder = new Saml2ClientBuilder(properties);
saml2ClientBuilder.tryCreateSaml2Client(clients);
}
// pac4j-cas dependency required
if (hasCasClients()) {
final CasClientBuilder casClientBuilder = new CasClientBuilder(properties);
casClientBuilder.tryCreateCasClient(clients);
}
// pac4j-oidc dependency required
if (hasOidcClients()) {
final OidcClientBuilder oidcClientBuilder = new OidcClientBuilder(properties);
oidcClientBuilder.tryCreateOidcClient(clients);
}
// pac4j-http dependency required
if (hasHttpAuthenticatorsOrClients()) {
final RestAuthenticatorBuilder restAuthenticatorBuilder = new RestAuthenticatorBuilder(properties);
restAuthenticatorBuilder.tryBuildRestAuthenticator(authenticators);
final IndirectHttpClientBuilder indirectHttpClientBuilder = new IndirectHttpClientBuilder(properties, authenticators);
indirectHttpClientBuilder.tryCreateLoginFormClient(clients);
indirectHttpClientBuilder.tryCreateIndirectBasicAuthClient(clients);
final DirectClientExtBuilder directClientBuilder = new DirectClientExtBuilder(properties, authenticators);
directClientBuilder.tryCreateAnonymousClient(clients);
directClientBuilder.tryCreateDirectBasciAuthClient(clients);
directClientBuilder.tryCreateDirectFormClient(clients);
directClientBuilder.tryCreateHeaderClient(clients);
directClientBuilder.tryCreateParameterClient(clients);
directClientBuilder.tryCreateIpClient(clients);
directClientBuilder.tryCreateDirectDigestAuthClient(clients);
directClientBuilder.tryCreateDirectBearerAuthClient(clients);
directClientBuilder.tryCreateCookieClient(clients);
directClientBuilder.tryCreateX509Client(clients);
directClientBuilder.tryCreateBodyJsonClient(clients);
}
return new Config(callbackUrl2, clients);
}
@Override
protected boolean hasDbAuthenticator() {
if (super.hasDbAuthenticator()) {
return true;
}
for (int i = 0; i <= MAX_NUM_AUTHENTICATORS; i++) {
final String dsName = getProperty(JDBI_NAME, i);
if (isNotBlank(dsName)) {
return true;
}
}
return false;
}
@Override
protected boolean hasHttpAuthenticatorsOrClients() {
if (super.hasHttpAuthenticatorsOrClients()) {
return true;
}
for (int i = 0; i <= MAX_NUM_CLIENTS; i++) {
if (isNotBlank(getProperty(DIRECT_FORMCLIENT_AUTHENTICATOR, i))) {
return true;
}
}
for (int i = 0; i <= MAX_NUM_CLIENTS; i++) {
if (isNotBlank(getProperty(HEADERCLIENT_AUTHENTICATOR, i))) {
return true;
}
}
for (int i = 0; i <= MAX_NUM_CLIENTS; i++) {
if (isNotBlank(getProperty(PARAMETERCLIENT_AUTHENTICATOR, i))) {
return true;
}
}
for (int i = 0; i <= MAX_NUM_CLIENTS; i++) {
if (isNotBlank(getProperty(IPCLIENT_AUTHENTICATOR, i))) {
return true;
}
}
for (int i = 0; i <= MAX_NUM_CLIENTS; i++) {
if (isNotBlank(getProperty(DIRECTDIGESTAUTHCLIENT_AUTHENTICATOR, i))) {
return true;
}
}
for (int i = 0; i <= MAX_NUM_CLIENTS; i++) {
if (isNotBlank(getProperty(DIRECTBEARERAUTHCLIENT_AUTHENTICATOR, i))) {
return true;
}
}
for (int i = 0; i <= MAX_NUM_CLIENTS; i++) {
if (isNotBlank(getProperty(COOKIECLIENT_AUTHENTICATOR, i))) {
return true;
}
}
for (int i = 0; i <= MAX_NUM_CLIENTS; i++) {
if (isNotBlank(getProperty(X509CLIENT_AUTHENTICATOR, i))) {
return true;
}
}
for (int i = 0; i <= MAX_NUM_CLIENTS; i++) {
if (isNotBlank(getProperty(BODY_JSONCLIENT_AUTHENTICATOR, i))) {
return true;
}
}
return false;
}
@Override
protected boolean hasOAuthClients() {
if (super.hasOAuthClients()) {
return true;
}
if (isNotBlank(getProperty(WECHAT_KEY)) && isNotBlank(getProperty(WECHAT_SECRET))) {
return true;
}
return false;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy