Ver Fonte

AuthenticationProvider

MaxKey há 3 anos atrás
pai
commit
e24b598257

+ 5 - 20
maxkey-authentications/maxkey-authentication-core/src/main/java/org/maxkey/authn/AbstractAuthenticationProvider.java

@@ -18,7 +18,6 @@
 package org.maxkey.authn;
 
 import java.util.ArrayList;
-import java.util.HashMap;
 
 import org.maxkey.authn.jwt.AuthJwtService;
 import org.maxkey.authn.online.OnlineTicket;
@@ -52,6 +51,8 @@ public abstract class AbstractAuthenticationProvider {
     private static final Logger _logger = 
             LoggerFactory.getLogger(AbstractAuthenticationProvider.class);
 
+    public static String PROVIDER_SUFFIX = "AuthenticationProvider";
+    
     public class AuthType{
     	public final static String NORMAL 	= "normal";
     	public final static String TFA 		= "tfa";
@@ -59,11 +60,6 @@ public abstract class AbstractAuthenticationProvider {
     	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 AbstractAuthenticationRealm authenticationRealm;
@@ -94,24 +90,13 @@ public abstract class AbstractAuthenticationProvider {
     }
 
     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);
+    	return null;
     }
     
-    public Authentication authenticate(LoginCredential authentication,boolean trusted){
-    	AbstractAuthenticationProvider provider = providers.get(AuthType.TRUSTED + PROVIDER_SUFFIX);
-    	return provider.doAuthenticate(authentication);
+    public Authentication authenticate(LoginCredential authentication,boolean trusted) {
+    	return null;
     }
     
-    public void addAuthenticationProvider(AbstractAuthenticationProvider provider) {
-    	providers.put(provider.getProviderName(), provider);
-    }
-
     /**
      * createOnlineSession 
      * @param credential

+ 60 - 0
maxkey-authentications/maxkey-authentication-core/src/main/java/org/maxkey/authn/provider/AuthenticationProviderFactory.java

@@ -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;
+	}
+}

+ 23 - 21
maxkey-authentications/maxkey-authentication-core/src/main/java/org/maxkey/autoconfigure/AuthenticationAutoConfiguration.java

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