AuthenticationProvider

This commit is contained in:
MaxKey 2022-04-25 10:51:46 +08:00
parent d3b2f4da64
commit e24b598257
3 changed files with 88 additions and 41 deletions

View File

@ -18,7 +18,6 @@
package org.maxkey.authn; package org.maxkey.authn;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import org.maxkey.authn.jwt.AuthJwtService; import org.maxkey.authn.jwt.AuthJwtService;
import org.maxkey.authn.online.OnlineTicket; import org.maxkey.authn.online.OnlineTicket;
@ -52,6 +51,8 @@ public abstract class AbstractAuthenticationProvider {
private static final Logger _logger = private static final Logger _logger =
LoggerFactory.getLogger(AbstractAuthenticationProvider.class); LoggerFactory.getLogger(AbstractAuthenticationProvider.class);
public static String PROVIDER_SUFFIX = "AuthenticationProvider";
public class AuthType{ public class AuthType{
public final static String NORMAL = "normal"; public final static String NORMAL = "normal";
public final static String TFA = "tfa"; public final static String TFA = "tfa";
@ -59,11 +60,6 @@ public abstract class AbstractAuthenticationProvider {
public final static String TRUSTED = "trusted"; public final static String TRUSTED = "trusted";
} }
protected static String PROVIDER_SUFFIX = "AuthenticationProvider";
private static HashMap<String,AbstractAuthenticationProvider> providers =
new HashMap<String,AbstractAuthenticationProvider>();
protected ApplicationConfig applicationConfig; protected ApplicationConfig applicationConfig;
protected AbstractAuthenticationRealm authenticationRealm; protected AbstractAuthenticationRealm authenticationRealm;
@ -94,22 +90,11 @@ public abstract class AbstractAuthenticationProvider {
} }
public Authentication authenticate(LoginCredential authentication){ public Authentication authenticate(LoginCredential authentication){
if(authentication.getAuthType().equalsIgnoreCase("trusted")) {
//risk remove
return null; return null;
} }
AbstractAuthenticationProvider provider = providers.get(authentication.getAuthType() + PROVIDER_SUFFIX);
return provider == null ? null : provider.doAuthenticate(authentication); public Authentication authenticate(LoginCredential authentication,boolean trusted) {
} return null;
public Authentication authenticate(LoginCredential authentication,boolean trusted){
AbstractAuthenticationProvider provider = providers.get(AuthType.TRUSTED + PROVIDER_SUFFIX);
return provider.doAuthenticate(authentication);
}
public void addAuthenticationProvider(AbstractAuthenticationProvider provider) {
providers.put(provider.getProviderName(), provider);
} }
/** /**

View File

@ -0,0 +1,60 @@
/*
* Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.maxkey.authn.provider;
import java.util.HashMap;
import org.maxkey.authn.AbstractAuthenticationProvider;
import org.maxkey.authn.LoginCredential;
import org.springframework.security.core.Authentication;
public class AuthenticationProviderFactory extends AbstractAuthenticationProvider {
private static HashMap<String,AbstractAuthenticationProvider> providers =
new HashMap<String,AbstractAuthenticationProvider>();
@Override
public Authentication authenticate(LoginCredential authentication){
if(authentication.getAuthType().equalsIgnoreCase("trusted")) {
//risk remove
return null;
}
AbstractAuthenticationProvider provider = providers.get(authentication.getAuthType() + PROVIDER_SUFFIX);
return provider == null ? null : provider.doAuthenticate(authentication);
}
@Override
public Authentication authenticate(LoginCredential authentication,boolean trusted){
AbstractAuthenticationProvider provider = providers.get(AuthType.TRUSTED + PROVIDER_SUFFIX);
return provider.doAuthenticate(authentication);
}
public void addAuthenticationProvider(AbstractAuthenticationProvider provider) {
providers.put(provider.getProviderName(), provider);
}
@Override
public String getProviderName() {
return "AuthenticationProviderFactory";
}
@Override
public Authentication doAuthenticate(LoginCredential authentication) {
//AuthenticationProvider Factory do nothing
return null;
}
}

View File

@ -25,6 +25,7 @@ import org.maxkey.authn.jwt.InMemoryCongressService;
import org.maxkey.authn.jwt.RedisCongressService; import org.maxkey.authn.jwt.RedisCongressService;
import org.maxkey.authn.online.OnlineTicketService; import org.maxkey.authn.online.OnlineTicketService;
import org.maxkey.authn.online.OnlineTicketServiceFactory; import org.maxkey.authn.online.OnlineTicketServiceFactory;
import org.maxkey.authn.provider.AuthenticationProviderFactory;
import org.maxkey.authn.provider.MobileAuthenticationProvider; import org.maxkey.authn.provider.MobileAuthenticationProvider;
import org.maxkey.authn.provider.NormalAuthenticationProvider; import org.maxkey.authn.provider.NormalAuthenticationProvider;
import org.maxkey.authn.provider.TrustedAuthenticationProvider; import org.maxkey.authn.provider.TrustedAuthenticationProvider;
@ -69,24 +70,34 @@ public class AuthenticationAutoConfiguration implements InitializingBean {
@Bean(name = "authenticationProvider") @Bean(name = "authenticationProvider")
public AbstractAuthenticationProvider authenticationProvider( public AbstractAuthenticationProvider authenticationProvider(
AbstractAuthenticationProvider normalAuthenticationProvider,
AbstractAuthenticationProvider mobileAuthenticationProvider,
AbstractAuthenticationProvider trustedAuthenticationProvider
) {
AuthenticationProviderFactory authenticationProvider = new AuthenticationProviderFactory();
authenticationProvider.addAuthenticationProvider(normalAuthenticationProvider);
authenticationProvider.addAuthenticationProvider(mobileAuthenticationProvider);
authenticationProvider.addAuthenticationProvider(trustedAuthenticationProvider);
return authenticationProvider;
}
@Bean
public AbstractAuthenticationProvider normalAuthenticationProvider(
AbstractAuthenticationRealm authenticationRealm, AbstractAuthenticationRealm authenticationRealm,
ApplicationConfig applicationConfig, ApplicationConfig applicationConfig,
OnlineTicketService onlineTicketServices, OnlineTicketService onlineTicketServices,
AuthJwtService authJwtService, AuthJwtService authJwtService,
MomentaryService momentaryService MomentaryService momentaryService
) { ) {
_logger.debug("init authentication Provider ."); _logger.debug("init authentication Provider .");
NormalAuthenticationProvider normal = new NormalAuthenticationProvider( return new NormalAuthenticationProvider(
authenticationRealm, authenticationRealm,
applicationConfig, applicationConfig,
onlineTicketServices, onlineTicketServices,
authJwtService, authJwtService,
momentaryService momentaryService
); );
normal.addAuthenticationProvider(normal);
return normal;
} }
@Bean(name = "mobileAuthenticationProvider") @Bean(name = "mobileAuthenticationProvider")
@ -94,38 +105,29 @@ public class AuthenticationAutoConfiguration implements InitializingBean {
AbstractAuthenticationRealm authenticationRealm, AbstractAuthenticationRealm authenticationRealm,
ApplicationConfig applicationConfig, ApplicationConfig applicationConfig,
OtpAuthnService otpAuthnService, OtpAuthnService otpAuthnService,
OnlineTicketService onlineTicketServices, OnlineTicketService onlineTicketServices
AbstractAuthenticationProvider authenticationProvider
) { ) {
MobileAuthenticationProvider mobile = new MobileAuthenticationProvider( _logger.debug("init Mobile authentication Provider .");
return new MobileAuthenticationProvider(
authenticationRealm, authenticationRealm,
applicationConfig, applicationConfig,
otpAuthnService, otpAuthnService,
onlineTicketServices onlineTicketServices
); );
authenticationProvider.addAuthenticationProvider(mobile);
_logger.debug("init Mobile authentication Provider .");
return mobile;
} }
@Bean(name = "trustedAuthenticationProvider") @Bean(name = "trustedAuthenticationProvider")
public AbstractAuthenticationProvider trustedAuthenticationProvider( public AbstractAuthenticationProvider trustedAuthenticationProvider(
AbstractAuthenticationRealm authenticationRealm, AbstractAuthenticationRealm authenticationRealm,
ApplicationConfig applicationConfig, ApplicationConfig applicationConfig,
OnlineTicketService onlineTicketServices, OnlineTicketService onlineTicketServices
AbstractAuthenticationProvider authenticationProvider
) { ) {
TrustedAuthenticationProvider trusted = new TrustedAuthenticationProvider( _logger.debug("init Mobile authentication Provider .");
return new TrustedAuthenticationProvider(
authenticationRealm, authenticationRealm,
applicationConfig, applicationConfig,
onlineTicketServices onlineTicketServices
); );
authenticationProvider.addAuthenticationProvider(trusted);
_logger.debug("init Mobile authentication Provider .");
return trusted;
} }
@Bean(name = "authJwtService") @Bean(name = "authJwtService")