TimeBasedOtpAuthn.java 3.2 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.impl;
MaxKey单点登录官方's avatar
format  
MaxKey单点登录官方 已提交
19 20 21 22 23 24 25

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import org.apache.commons.codec.binary.Hex;
import org.maxkey.crypto.Base32Utils;
M
MaxKey 已提交
26
import org.maxkey.crypto.password.PasswordReciprocal;
M
MaxKey 已提交
27
import org.maxkey.entity.UserInfo;
M
MaxKey 已提交
28 29
import org.maxkey.password.onetimepwd.AbstractOtpAuthn;
import org.maxkey.password.onetimepwd.algorithm.TimeBasedOTP;
MaxKey单点登录官方's avatar
format  
MaxKey单点登录官方 已提交
30 31 32
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

M
MaxKey 已提交
33
public class TimeBasedOtpAuthn extends AbstractOtpAuthn {
MaxKey单点登录官方's avatar
format  
MaxKey单点登录官方 已提交
34 35 36
    private static final  Logger _logger = LoggerFactory.getLogger(TimeBasedOtpAuthn.class);
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
37
    public TimeBasedOtpAuthn() {
MaxKey单点登录官方's avatar
format  
MaxKey单点登录官方 已提交
38 39 40
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    }

41 42 43 44 45 46
    public TimeBasedOtpAuthn(int digits , int interval) {
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        super.digits = digits;
        super.interval = interval;
    }
    
MaxKey单点登录官方's avatar
format  
MaxKey单点登录官方 已提交
47 48 49 50 51 52 53 54 55
    @Override
    public boolean produce(UserInfo userInfo) {
        return true;
    }

    @Override
    public boolean validate(UserInfo userInfo, String token) {
        _logger.debug("utcTime : " + dateFormat.format(new Date()));
        long currentTimeSeconds = System.currentTimeMillis() / 1000;
M
MaxKey 已提交
56 57 58
        String sharedSecret = 
                PasswordReciprocal.getInstance().decoder(userInfo.getSharedSecret());
        byte[] byteSharedSecret = Base32Utils.decode(sharedSecret);
MaxKey单点登录官方's avatar
format  
MaxKey单点登录官方 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        String hexSharedSecret = Hex.encodeHexString(byteSharedSecret);
        String timeBasedToken = "";
        if (crypto.equalsIgnoreCase("HmacSHA1")) {
            timeBasedToken = TimeBasedOTP.genOTP(
                    hexSharedSecret,
                    Long.toHexString(currentTimeSeconds / interval).toUpperCase() + "",
                    digits + "");
        } else if (crypto.equalsIgnoreCase("HmacSHA256")) {
            timeBasedToken = TimeBasedOTP.genOTPHmacSHA256(
                    hexSharedSecret,
                    Long.toHexString(currentTimeSeconds / interval).toUpperCase() + "", 
                    digits + "");
        } else if (crypto.equalsIgnoreCase("HmacSHA512")) {
            timeBasedToken = TimeBasedOTP.genOTPHmacSHA512(
                    hexSharedSecret,
                    Long.toHexString(currentTimeSeconds / interval).toUpperCase() + "", 
                    digits + "");
        }
        _logger.debug("token : " + token);
        _logger.debug("timeBasedToken : " + timeBasedToken);
        if (token.equalsIgnoreCase(timeBasedToken)) {
            return true;
        }
        return false;

    }

}