Created
October 9, 2019 11:04
-
-
Save Tiryoh/47f99ef2b8fbba4368a252ebea22441f to your computer and use it in GitHub Desktop.
pyotpでTOTPを使うサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import pyotp | |
import qrcode | |
def verify_user(key, otp): | |
# base32のシークレットキーに基づいてOTP認証 | |
totp = pyotp.TOTP(key) | |
return totp.verify(otp) | |
if __name__ == '__main__': | |
sec_key = input('input OPT secret key (leave empty to re-generate): ') | |
if sec_key == '': | |
sec_key = pyotp.random_base32() # base32のシークレットキーを発行 | |
print('generated OTP secret: ', sec_key) | |
# Google Authenticatorに登録する用のQRコードを表示 | |
img = qrcode.make(pyotp.totp.TOTP(sec_key).provisioning_uri('user.email@gmail.com', issuer_name='TEST WEB SERVICE')) | |
img.show() | |
otp = input('input pass: ') | |
print(verify_user(sec_key, otp)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment