AbstractOtpAuthn.java 4.0 KB
Newer Older
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright [2020] [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.
 */
 

M
MaxKey 已提交
18
package org.maxkey.password.onetimepwd;
MaxKey单点登录官方's avatar
format  
MaxKey单点登录官方 已提交
19

M
MaxKey 已提交
20
import org.maxkey.entity.UserInfo;
M
MaxKey 已提交
21 22
import org.maxkey.password.onetimepwd.token.AbstractOtpTokenStore;
import org.maxkey.password.onetimepwd.token.InMemoryOtpTokenStore;
MaxKey单点登录官方's avatar
format  
MaxKey单点登录官方 已提交
23 24 25 26 27 28 29 30 31
import org.maxkey.util.StringGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * AbstractOTPAuthn.
 * @author Administrator
 *
 */
M
MaxKey 已提交
32 33
public abstract class AbstractOtpAuthn {
    private static final  Logger logger = LoggerFactory.getLogger(AbstractOtpAuthn.class);
MaxKey单点登录官方's avatar
format  
MaxKey单点登录官方 已提交
34

M
MaxKey 已提交
35
    protected AbstractOtpTokenStore optTokenStore = new InMemoryOtpTokenStore();
36 37
    
    //验证码有效間隔
MaxKey单点登录官方's avatar
format  
MaxKey单点登录官方 已提交
38
    protected int interval = 30;
39 40
    
    // 验证码长度,范围4~10,默认为6
MaxKey单点登录官方's avatar
format  
MaxKey单点登录官方 已提交
41 42 43 44
    protected int digits = 6;

    protected String crypto = "HmacSHA1";

M
http  
MaxKey 已提交
45 46
    protected String defaultEncoding ="utf-8";
    
MaxKey单点登录官方's avatar
format  
MaxKey单点登录官方 已提交
47
    StringGenerator stringGenerator;
48
    
M
otpType  
MaxKey 已提交
49
    protected String otpType = OtpTypes.TIMEBASED_OTP;
MaxKey单点登录官方's avatar
format  
MaxKey单点登录官方 已提交
50

M
otpType  
MaxKey 已提交
51
    public static final class OtpTypes {
MaxKey单点登录官方's avatar
format  
MaxKey单点登录官方 已提交
52
        // 手机
53
        public static String  MOBILE = "MOBILE";
MaxKey单点登录官方's avatar
format  
MaxKey单点登录官方 已提交
54
        // 短信
55
        public static String SMS = "SMS";
MaxKey单点登录官方's avatar
format  
MaxKey单点登录官方 已提交
56
        // 邮箱
57 58
        public static String EMAIL = "EMAIL";
        //TIMEBASED_OPT
M
otpType  
MaxKey 已提交
59
        public static String TIMEBASED_OTP = "TOPT";
60
        // HmacOTP
M
otpType  
MaxKey 已提交
61
        public static String HOTP_OTP = "HOTP";
MaxKey单点登录官方's avatar
format  
MaxKey单点登录官方 已提交
62

M
otpType  
MaxKey 已提交
63
        public static String RSA_OTP = "RSA";
64
        
M
otpType  
MaxKey 已提交
65
        public static String CAP_OTP = "CAP";
MaxKey单点登录官方's avatar
format  
MaxKey单点登录官方 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

    }

    public abstract boolean produce(UserInfo userInfo);

    public abstract boolean validate(UserInfo userInfo, String token);

    protected String defaultProduce(UserInfo userInfo) {
        return genToken(userInfo);
    }

    /**
     * genToken.
     * @param userInfo UserInfo
     * @return
     */
    public String genToken(UserInfo userInfo) {
        if (stringGenerator == null) {
            stringGenerator = new StringGenerator(StringGenerator.DEFAULT_CODE_NUMBER, digits);
        }
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
86 87 88
        String token = stringGenerator.randomGenerate();
        logger.debug("Generator token " + token);
        return token;
MaxKey单点登录官方's avatar
format  
MaxKey单点登录官方 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    }

    /**
     *  the interval.
     * @return the interval
     */
    public int getInterval() {
        return interval;
    }

    /**
     * interval the interval to set.
     * @param interval the interval to set
     */
    public void setInterval(int interval) {
        this.interval = interval;
    }

    /**
     * digits.
     * @return the digits
     */
    public int getDigits() {
        return digits;
    }

    /**
     * digits the digits to set.
     * @param digits the digits to set
     */
    public void setDigits(int digits) {
        this.digits = digits;
    }

    /**
     * crypto.
     * @return the crypto
     */
    public String getCrypto() {
        return crypto;
    }

    /**
     * crypto the crypto to set.
     * @param crypto the crypto to set
     */
    public void setCrypto(String crypto) {
        this.crypto = crypto;
    }

M
otpType  
MaxKey 已提交
139 140
    public String getOtpType() {
        return otpType;
141 142
    }

M
otpType  
MaxKey 已提交
143 144
    public void setOtpType(String optType) {
        this.otpType = optType;
145 146
    }

M
MaxKey 已提交
147
    public void setOptTokenStore(AbstractOtpTokenStore optTokenStore) {
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
148 149 150 151 152 153
        this.optTokenStore = optTokenStore;
    }

    public void initPropertys() {
        
    }
M
http  
MaxKey 已提交
154 155 156 157 158 159 160 161

    public String getDefaultEncoding() {
        return defaultEncoding;
    }

    public void setDefaultEncoding(String defaultEncoding) {
        this.defaultEncoding = defaultEncoding;
    }
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
162
 
MaxKey单点登录官方's avatar
format  
MaxKey单点登录官方 已提交
163
}