MfaAuthenticationProvider.java 5.7 KB
Newer Older
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
1
/*
M
220413  
MaxKey 已提交
2
 * Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * 
 * 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.
 */
 

M
MaxKey 已提交
18
package org.maxkey.authn.provider;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
19

M
MaxKey 已提交
20 21 22
import org.maxkey.authn.AbstractAuthenticationProvider;
import org.maxkey.authn.LoginCredential;
import org.maxkey.authn.jwt.AuthJwtService;
M
MaxKey 已提交
23
import org.maxkey.authn.online.OnlineTicketService;
M
MaxKey 已提交
24 25
import org.maxkey.authn.realm.AbstractAuthenticationRealm;
import org.maxkey.configuration.ApplicationConfig;
M
sso  
MaxKey 已提交
26
import org.maxkey.constants.ConstsLoginType;
M
MaxKey 已提交
27
import org.maxkey.entity.Institutions;
M
MaxKey 已提交
28
import org.maxkey.entity.UserInfo;
M
MaxKey 已提交
29
import org.maxkey.persistence.MomentaryService;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
30 31 32 33
import org.maxkey.web.WebConstants;
import org.maxkey.web.WebContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
M
MaxKey 已提交
34
import org.springframework.security.authentication.BadCredentialsException;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
35 36
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
M
sso  
MaxKey 已提交
37
import org.springframework.security.core.AuthenticationException;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
38 39 40


/**
MaxKey单点登录官方's avatar
v1.3 RC  
MaxKey单点登录官方 已提交
41
 * database Authentication provider.
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
42 43 44
 * @author Crystal.Sea
 *
 */
M
MaxKey 已提交
45
public class MfaAuthenticationProvider extends AbstractAuthenticationProvider {
MaxKey单点登录官方's avatar
v1.3 RC  
MaxKey单点登录官方 已提交
46
    private static final Logger _logger =
M
MaxKey 已提交
47
            LoggerFactory.getLogger(MfaAuthenticationProvider.class);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
48

M
MaxKey 已提交
49 50
    public String getProviderName() {
        return "normal" + PROVIDER_SUFFIX;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
51
    }
M
MaxKey 已提交
52
    
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
53

M
MaxKey 已提交
54
    public MfaAuthenticationProvider() {
M
MaxKey 已提交
55 56 57
		super();
	}

M
MaxKey 已提交
58
    public MfaAuthenticationProvider(
M
MaxKey 已提交
59 60
    		AbstractAuthenticationRealm authenticationRealm,
    		ApplicationConfig applicationConfig,
M
MaxKey 已提交
61 62 63
    	    OnlineTicketService onlineTicketServices,
    	    AuthJwtService authJwtService,
    	    MomentaryService momentaryService) {
M
MaxKey 已提交
64 65 66
		this.authenticationRealm = authenticationRealm;
		this.applicationConfig = applicationConfig;
		this.onlineTicketServices = onlineTicketServices;
M
MaxKey 已提交
67 68
		this.authJwtService = authJwtService;
		this.momentaryService = momentaryService;
M
MaxKey 已提交
69 70
	}

M
sso  
MaxKey 已提交
71
    @Override
M
MaxKey 已提交
72
	public Authentication doAuthenticate(LoginCredential loginCredential) {
M
sso  
MaxKey 已提交
73 74 75 76 77 78 79 80
		UsernamePasswordAuthenticationToken authenticationToken = null;
		_logger.debug("Trying to authenticate user '{}' via {}", 
                loginCredential.getPrincipal(), getProviderName());
        try {
        	
	        _logger.debug("authentication " + loginCredential);
	        
	        Institutions inst = (Institutions)WebContext.getAttribute(WebConstants.CURRENT_INST);
M
MaxKey 已提交
81

M
sso  
MaxKey 已提交
82 83 84 85 86 87 88 89 90 91
	        emptyPasswordValid(loginCredential.getPassword());
	
	        UserInfo userInfo = null;
	
	        emptyUsernameValid(loginCredential.getUsername());
	
	        userInfo =  loadUserInfo(loginCredential.getUsername(),loginCredential.getPassword());
	
	        statusValid(loginCredential , userInfo);
	        //mfa 
M
MaxKey 已提交
92
	        mfacaptchaValid(loginCredential.getOtpCaptcha(),userInfo);
M
sso  
MaxKey 已提交
93 94 95
	        
	        //Validate PasswordPolicy
	        authenticationRealm.getPasswordPolicyValidator().passwordPolicyValid(userInfo);
M
MaxKey 已提交
96 97 98 99
	             
	        //Match password 
	        authenticationRealm.passwordMatches(userInfo, loginCredential.getPassword());

M
sso  
MaxKey 已提交
100 101 102
	        //apply PasswordSetType and resetBadPasswordCount
	        authenticationRealm.getPasswordPolicyValidator().applyPasswordPolicy(userInfo);
	        
M
MaxKey 已提交
103
	        authenticationToken = createOnlineTicket(loginCredential,userInfo);
M
sso  
MaxKey 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
	        // user authenticated
	        _logger.debug("'{}' authenticated successfully by {}.", 
	        		loginCredential.getPrincipal(), getProviderName());
	        
	        authenticationRealm.insertLoginHistory(userInfo, 
							        				ConstsLoginType.LOCAL, 
									                "", 
									                "xe00000004", 
									                WebConstants.LOGIN_RESULT.SUCCESS);
        } catch (AuthenticationException e) {
            _logger.error("Failed to authenticate user {} via {}: {}",
                    new Object[] {  loginCredential.getPrincipal(),
                                    getProviderName(),
                                    e.getMessage() });
            WebContext.setAttribute(
                    WebConstants.LOGIN_ERROR_SESSION_MESSAGE, e.getMessage());
        } catch (Exception e) {
            _logger.error("Login error Unexpected exception in {} authentication:\n{}" ,
                            getProviderName(), e.getMessage());
M
MaxKey 已提交
123
        }
M
sso  
MaxKey 已提交
124
       
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
125
        return  authenticationToken;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
126
    }
M
MaxKey 已提交
127 128
    
    
MaxKey单点登录官方's avatar
CAS FIX  
MaxKey单点登录官方 已提交
129

M
MaxKey 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    /**
     * captcha validate.
     * 
     * @param otpCaptcha String
     * @param authType   String
     * @param userInfo   UserInfo
     */
    protected void mfacaptchaValid(String otpCaptcha, UserInfo userInfo) {
        // for one time password 2 factor
        if (applicationConfig.getLoginConfig().isMfa()) {
            UserInfo validUserInfo = new UserInfo();
            validUserInfo.setUsername(userInfo.getUsername());
            validUserInfo.setSharedSecret(userInfo.getSharedSecret());
            validUserInfo.setSharedCounter(userInfo.getSharedCounter());
            validUserInfo.setId(userInfo.getId());
            if (otpCaptcha == null || !tfaOtpAuthn.validate(validUserInfo, otpCaptcha)) {
                String message = WebContext.getI18nValue("login.error.captcha");
                _logger.debug("login captcha valid error.");
                throw new BadCredentialsException(message);
149 150
            }
        }
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
151
    }
M
MaxKey 已提交
152

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
153
}