accounts Accounts & Authentication¶
Author: Zhenyu Yang yangzhenyu@sust.edu.cn
Last updated: Apr 24, 2026
Corresponding API docs: Authentication & User Account API
accounts Code Entry¶
backend/apps/accounts/models/__init__.pybackend/apps/accounts/views/__init__.pybackend/apps/accounts/serializers/__init__.pybackend/apps/accounts/authentication.pybackend/apps/accounts/middleware.py
accounts Core Data¶
User¶
Inherits from
AbstractUser.Business fields include:
user_codefull_namewxidrolesstatusstudent_classtotp_secretis_totp_enabledpreferred_localeauth_version
rolesstores all roles assigned to the user.current_roleis not stored in the database; it is extracted at runtime from the JWT or request headers.auth_versionis used for token revocation; it increments on password, role, or status changes, automatically invalidating old tokens.
WebAuthn Tables¶
WebAuthnCredential: Stores registered security keys.WebAuthnChallenge: Stores registration/login challenge values.
Authentication Flows¶
Email / Student ID / Staff ID Login¶
Entry:
EmailLoginViewThe identifier field accepts both email and
user_code.Passwords are first attempted to be decrypted via SM2; on failure, the fallback allows plaintext for compatibility.
If the user has TOTP enabled, a
totp_codemust also be submitted during login.
WeChat Mini Program Login¶
Entry:
WeChatLoginViewCalls WeChat
code2sessionand locates the user bywxid=open_id.The mini program does not use cookies; it returns access/refresh tokens in the response body.
If the account has TOTP enabled, a
totp_codeis also required.
WebAuthn Login¶
Login options:
WebAuthnLoginOptionsViewLogin verification:
WebAuthnLoginViewAfter successful login, same as email login: returns tokens and writes HttpOnly cookies.
TOTP¶
setupgenerates a secret key andprovisioning_uri.enableverifies the code and activates TOTP.disablerequires verifying the code again when TOTP is already enabled.
accounts Permission Boundaries¶
User list, user detail, user create/update/delete:
superadminorsecretaryonly.
Invite foreign teacher:
superadminorsecretaryonly.
Update roles:
superadminonly.
Update account status:
superadminorsecretary.
/auth/me:All authenticated users can read and update their own language preference.
User Import¶
Entry:
UserViewSet.import_userssuperadmin/secretaryonly.Uses
user_codeas the unique key.Existing records are skipped without being overwritten.
Newly created users uniformly receive:
status="active"set_unusable_password()
The Excel file only accepts basic identity fields; plaintext or hashed passwords are never written from the import file.
Implementation Highlights¶
CookieJWTAuthenticationreads the Authorization header first, then falls back to the cookie.CurrentRoleMiddlewarevalidates that thecurrent_rolein the request belongs touser.roles; if invalid, it falls back to the default role.On login or first access, if the user has no valid
preferred_locale, the system auto-normalizes theAccept-Languageheader tozh-CNoren-US.The foreign teacher email registration endpoint
EmailRegisterViewis an anonymous endpoint; the user is logged in immediately after creation.SM2PublicKeyViewdistributes the public key to the frontend via/api/v1/accounts/sm2/public-key.