LoginSignup
0
1

More than 3 years have passed since last update.

【第4回】pythonで某Authenticator的なツールを作ってみた

Posted at

今回は、DBを片付けてみた。

  • 各DBの動作をdefで書き出した。
  • root = tk.Tk()以下のあたりから表示系
  • うまい具合にまとめられた感じがする。
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tkinter as tk
import pyotp
import sqlite3
import os

def create_table():
    # データベースに接続する
    conn = sqlite3.connect('gauth.db')
    c = conn.cursor()
    # テーブルの作成
    sql='''CREATE TABLE gauth
    (id integer primary key AUTOINCREMENT,
     name text,
     private_key text)'''

    c.execute(sql)
    # 挿入した結果を保存(コミット)する
    conn.commit()
    # データベースへのアクセスが終わったら close する
    conn.close()

def view():
    # データベースに接続する
    conn = sqlite3.connect('gauth.db')
    c = conn.cursor()
    for a in c.execute("select * from gauth"):
        totp = pyotp.TOTP(a[2])  # 鍵的な値
        totp.now()
        # 表示用ラベル
        Static1 = tk.Label(text=a[1])
        Static1.pack(side='left')

        Static2 = tk.Label(text=totp.now())
        Static2.pack(side='left')
    # 挿入した結果を保存(コミット)する
    conn.commit()
    # データベースへのアクセスが終わったら close する
    conn.close()

def insert(id,username,private_key):
    # データベースに接続する
    conn = sqlite3.connect('gauth.db')
    c = conn.cursor()
    # データの挿入
    c.execute(
        "INSERT INTO gauth VALUES (?,?,?)",(id,username,private_key))
    # 挿入した結果を保存(コミット)する
    conn.commit()
    # データベースへのアクセスが終わったら close する
    conn.close()

# tkinterでwindowの作成とタイトルを作る
# windowサイズの指定
root = tk.Tk()
root.title(u"g_authentication_tool")
root.geometry("300x200")
# メニューバー
menubar = tk.Menu(root)
filemenu = tk.Menu(menubar)
filemenu.add_command(label="新規登録")
filemenu.add_command(label="登録削除")
filemenu.add_command(label="閉じる")
menubar.add_cascade(label="ファイル", menu=filemenu)
root.config(menu=menubar)
if(os.path.exists('gauth.db')):
    view()
else:
    create_table()
    #insert(1,"user1",'base32secret3232')
    view()
root.mainloop()

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1