Skip to content

Instantly share code, notes, and snippets.

@Tiryoh
Created October 9, 2019 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tiryoh/47f99ef2b8fbba4368a252ebea22441f to your computer and use it in GitHub Desktop.
Save Tiryoh/47f99ef2b8fbba4368a252ebea22441f to your computer and use it in GitHub Desktop.
pyotpでTOTPを使うサンプル
#!/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