refactor: 本地只保存工号和加密密码,并整理后端目录
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
"""校验 e 学登录,并只把工号和加密密码写入本地库。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from euniv.client import EunivClient
|
||||
from euniv.crypto import decrypt_secret, encrypt_secret
|
||||
from euniv.models import Account
|
||||
|
||||
|
||||
def login_and_store(db: Session, employee_id: str, password: str, client_factory=EunivClient) -> Account:
|
||||
client = client_factory(employee_id, password)
|
||||
try:
|
||||
client.login()
|
||||
finally:
|
||||
client.close()
|
||||
account = db.get(Account, employee_id)
|
||||
if account is None:
|
||||
account = Account(employee_id=employee_id, password_cipher=encrypt_secret(password))
|
||||
db.add(account)
|
||||
else:
|
||||
account.password_cipher = encrypt_secret(password)
|
||||
db.commit()
|
||||
db.refresh(account)
|
||||
return account
|
||||
|
||||
|
||||
def list_accounts(db: Session) -> list[Account]:
|
||||
return list(db.scalars(select(Account).order_by(Account.employee_id)))
|
||||
|
||||
|
||||
def stored_password(account: Account) -> str:
|
||||
return decrypt_secret(account.password_cipher)
|
||||
Reference in New Issue
Block a user