package com.jeequan.jeepay.core.utils.google;

import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.utils.DateKit;
import org.apache.commons.lang3.StringUtils;

/**
 * google身份验证器 工具类
 */
public class GoogleAuthUtil {

    /**
     * @author: xiaoyu
     * @date: 2022/5/16 18:29
     * @describe: 验证谷歌验证码是否正确， 默认时间偏移量 5
     */
    public static Boolean checkByCode(String googleCode, String googleAuthSecretKey){

        if (StringUtils.isEmpty(googleCode)) {
            throw new BizException("验证码不能为空");
        }

        if(StringUtils.isBlank(googleAuthSecretKey)) {
            throw new BizException("验证密码为空");
        }

        // 谷歌验证
        long t = DateKit.currentTimeMillis();
        GoogleAuth ga = new GoogleAuth();
        ga.setWindowSize(5);    // 最多可偏移的时间
        boolean checkResult = ga.check_code(googleAuthSecretKey, Long.parseLong(googleCode), t);
        if (!checkResult) {
            return false;
        }
        return true;
    }

}
