36 lines
1.0 KiB
Python
36 lines
1.0 KiB
Python
"""校验 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)
|