使用 PHP 的 Google 身份验证器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26539649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:57:11  来源:igfitidea点击:

Google Authenticator using PHP

phpauthenticationgoogle-authenticationgoogle-authenticator

提问by Abhishek Salian

I have used https://github.com/chregu/GoogleAuthenticator.phpto built a 2 factor authentication for a web application i am working on. Everything works generating secret and even the code worked. Now i set up the same code in a different server and generated new secret key and added it to google authenticator mobile app now the code generated in mobile doesnt match.

我已经使用https://github.com/chregu/GoogleAuthenticator.php为我正在处理的 Web 应用程序构建了一个 2 因素身份验证。一切正常,生成秘密,甚至代码也有效。现在我在不同的服务器中设置了相同的代码并生成了新的密钥并将其添加到谷歌身份验证器移动应用程序中,现在移动端生成的代码不匹配。

I digging up by comparing results on both server and noticed the time() function returns different time (1 hr difference) then i forced my 2nd server(where google code didnt work) to have same time as first one and it worked. So i am really confused is this some sort of time zone issue? Coz i really need these servers to have their own time zone.

我通过比较两台服务器上的结果进行挖掘,并注意到 time() 函数返回不同的时间(1 小时差异)然后我强迫我的第二台服务器(谷歌代码不起作用)与第一台服务器具有相同的时间并且它工作了。所以我真的很困惑这是某种时区问题吗?因为我真的需要这些服务器有自己的时区。

Is there any work around?

有什么解决办法吗?

Also I followed https://support.google.com/accounts/answer/185834?hl=enand synced my google authenticator app still doesnt work. the code generated in mobile app works on my 2nd server after an hour. Can anyone please help me or suggest me a different approach.

我也跟着https://support.google.com/accounts/answer/185834?hl=en并同步了我的谷歌身份验证器应用程序仍然无法正常工作。一个小时后,移动应用程序中生成的代码在我的第二台服务器上运行。任何人都可以帮助我或建议我采用不同的方法。

here is the code i am using to connect to the above library

这是我用来连接到上述库的代码

class googleAuthLibrary extends GoogleAuthenticator
{
    public function getSecretKey()
    {
        $secretKey = $this->generateSecret();

        return $secretKey;
    }

    public function getQRLink($username, $hostname, $secretKey)
    {
        $url = 'https://chart.googleapis.com/chart?cht=qr&chs=300x300&chl=';

        $qrCode = 'otpauth://totp/'.$username.'@'.$hostname.'?secret='.$secretKey;

        $url = $url.$qrCode;

        return $url;
    }

    public function getAuthCode($secretKey)
    {
        $authCode =$this->getCode($secretKey);

        return $authCode;
    }
}

回答by Abhishek Salian

Found out that my server time was not synced with internet time. After syncing time in web server the issue got resolved. So timezone doesnt affect the authenticator as long as both mobile phone and the server have synced to have correct time (NTP servers).

发现我的服务器时间没有与互联网时间同步。在 Web 服务器中同步时间后,问题得到解决。因此,只要手机和服务器同步到正确的时间(NTP 服务器),时区就不会影响验证器。

If anyone having same issue check server time and mobile phone time, make sure they are showing right time even a minute slow or fast can end up generating wrong codes.

如果有人遇到同样的问题,请检查服务器时间和手机时间,确保他们显示正确的时间,即使慢一分钟或快一分钟也可能最终生成错误的代码。