-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkrAuthenticator.py
More file actions
98 lines (82 loc) · 2.93 KB
/
krAuthenticator.py
File metadata and controls
98 lines (82 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import json
import requests
from tkinter import messagebox
import os
import sys
from datetime import datetime
import json
import socket
import tkinter as tk
# 主ウィンドウが表示されないようにする
root = tk.Tk()
root.withdraw()
def is_connected():
try:
# Googleの公共DNSサーバーに接続を試みる
socket.create_connection(("www.google.com", 80))
return True
except OSError:
pass
return False
def can_access_web():
try:
response = requests.get('https://www.google.com')
if response.status_code == 200:
return True
except requests.RequestException:
pass
return False
# 実行ファイルの位置を取得(PyInstaller対応)
if getattr(sys, 'frozen', False):
current_dir = os.path.dirname(sys.executable)
else:
current_dir = os.path.dirname(os.path.abspath(__file__))
# data.jsonの絶対パスを作成
json_path = os.path.join(current_dir, 'login_info.json')
# 認証用のエンドポイントURLを指定してください
auth_url = "https://webauth.omu.ac.jp/portal/logon.cgi"
def authenticate(username, password):
payload = {"PtUser": username, "PtPwd": password, "PtButton": "Logon"}
response = requests.post(auth_url, data=payload, verify=False)
if "ログインに失敗しました" in response.text:
# messagebox.showerror("krAuthenticator", "認証失敗")
pass
else:
messagebox.showinfo("krAuthenticator", "認証成功")
# jsonファイルからデータを読み込む
try:
file = json.load(open(json_path, "r"))
# ユーザー名とパスワードを取得します
username = file["username"]
password = file["password"]
# 日付を取得してPythonのdatetimeオブジェクトに変換
stored_date_str = file["date"]
stored_date = datetime.strptime(stored_date_str, '%Y-%m-%d').date()
# 現在の日付を取得
current_date = datetime.now().date()
# 現在の日付と保存された日付を比較
is_past_date = stored_date < current_date
# 時間を超えていた時,jsonファイルをさ削除する
if is_past_date:
os.remove(json_path)
if not is_connected() or not can_access_web():
# 認証を試みます
authenticate(
username,
password
)
else:
pass
# messagebox.showinfo("krAuthenticator", "ネットワークに接続されています.")
except FileNotFoundError:
messagebox.showerror(
"krAuthenticator",
"パスワード情報が見つかりませんでした."
+"exeファイルと同じディレクトリにlogin_info.jsonを配置してください."
+str(sys.exc_info()[0])
)
except :
messagebox.showerror(
"krAuthenticator",
"エラーが発生しました."
+str(sys.exc_info()[0]))