Download app.py from muyuehltd/2fa: direct link, hf CLI and curl.
- Browser
- Download file 3.46 kB
-
https://huggingface.co/spaces/muyuehltd/2fa/resolve/main/app.py
- Command line
-
hf download hf://spaces/muyuehltd/2fa/app.py
-
curl -L -o app.py https://huggingface.co/spaces/muyuehltd/2fa/resolve/main/app.py
3.46 kB
| import base64 | |
| import hmac | |
| import hashlib | |
| import os | |
| import time | |
| import requests | |
| import json | |
| import binascii | |
| import gradio as gr | |
| # Base32 解碼 (類似於 PHP 中 Base32::decode) | |
| def base32_decode(encoded_secret): | |
| missing_padding = len(encoded_secret) % 8 | |
| if missing_padding != 0: | |
| encoded_secret += '=' * (8 - missing_padding) | |
| try: | |
| return base64.b32decode(encoded_secret) | |
| except binascii.Error as e: | |
| raise ValueError(f"Base32 解碼失敗: {e}") | |
| # HOTP 驗證碼生成 | |
| def generate_hotp(secret, counter): | |
| decoded_secret = base32_decode(secret) | |
| counter_bytes = counter.to_bytes(8, byteorder="big") | |
| hmac_hash = hmac.new(decoded_secret, counter_bytes, hashlib.sha1).digest() | |
| return dynamic_truncation(hmac_hash) % (10 ** 6) | |
| # 動態截斷算法 | |
| def dynamic_truncation(hmac_value): | |
| offset = hmac_value[-1] & 0x0F | |
| code = ( | |
| ((hmac_value[offset] & 0x7f) << 24) | |
| | ((hmac_value[offset + 1] & 0xff) << 16) | |
| | ((hmac_value[offset + 2] & 0xff) << 8) | |
| | (hmac_value[offset + 3] & 0xff) | |
| ) | |
| return code | |
| # 驗證碼生成 (基於時間的計數器,每 30 秒更新一次) | |
| def generate_otp(secret): | |
| counter = int(time.time() // 30) | |
| otp_code = generate_hotp(secret, counter) | |
| return f"{otp_code:06d}" | |
| # 發送訊息到 Slack | |
| def send_message_to_slack(message): | |
| slack_webhook_url = os.getenv("SLACK_WEBHOOK_URL") | |
| if not slack_webhook_url: | |
| raise ValueError("SLACK_WEBHOOK_URL is not set in environment variables.") | |
| payload = {"text": message} | |
| response = requests.post(slack_webhook_url, json=payload) | |
| if response.status_code != 200: | |
| raise ValueError( | |
| f"Request to Slack returned an error {response.status_code}, the response is: {response.text}" | |
| ) | |
| print(f"Message sent to Slack: {response.status_code}") | |
| # 發送 2FA 驗證碼到 Slack | |
| def send_2fa_code(name, service_type): | |
| secret = os.getenv("gmail_2fa") | |
| if not secret: | |
| return f"{service_type} 的密鑰沒有正確設置為環境變數。" | |
| code = generate_otp(secret) | |
| message = f"{name} 要登入 {service_type} 帳號,驗證碼為 {code}" | |
| send_message_to_slack(message) | |
| return "驗證碼已發送至指定的 Slack 頻道。" | |
| # 初始化邏輯 | |
| def initialize(): | |
| if not os.getenv("gmail_2fa"): | |
| return "初始化失敗:未設置 Gmail 2FA 的密鑰環境變數 'gmail_2fa'" | |
| if not os.getenv("SLACK_WEBHOOK_URL"): | |
| return "初始化失敗:未設置 Slack Webhook URL 環境變數 'SLACK_WEBHOOK_URL'" | |
| return "應用初始化成功,系統已準備就緒!" | |
| # Gradio 前端邏輯 | |
| def two_fa_interface(name, service_type): | |
| return send_2fa_code(name, service_type) | |
| # Gradio 介面 | |
| service_options = ["hi@gaiconf.com"] | |
| with gr.Blocks() as demo: | |
| # 初始化狀態 | |
| status_message = gr.Textbox( | |
| label="應用狀態", interactive=False, value="初始化中..." | |
| ) | |
| demo.load(initialize, outputs=status_message) | |
| # 驗證碼生成介面 | |
| gr.Markdown("### GAI 總籌兩步驗證") | |
| name = gr.Textbox(label="請輸入您的暱稱", placeholder="您的暱稱") | |
| service = gr.Dropdown(label="選擇服務", choices=service_options) | |
| result = gr.Textbox(label="結果", interactive=False) | |
| submit_button = gr.Button("送出") | |
| submit_button.click(two_fa_interface, inputs=[name, service], outputs=result) | |
| # 啟動應用 | |
| demo.launch(share=True) | |