This page documents the authentication approaches employed in Dify, including email/password login, OAuth with GitHub and Google, passwordless email code login, and token management for session security. It covers backend implementations, data flow for each method, key classes and functions, and token lifecycle management (access, refresh, and CSRF tokens). The focus includes security measures like rate limiting and cryptographic validation employed across authentication flows.
Dify's authentication system encompasses multiple entry points and layers adapted for various client types (Console UI, WebApp, API). Central logic is implemented primarily in AccountService and coordinated through controller endpoints and utility classes.
Sources: api/services/account_service.py130-170 api/controllers/console/auth/login.py107-180 api/controllers/console/auth/oauth.py130-200 api/controllers/console/auth/forgot_password.py59-94 api/libs/passport.py1-50 api/services/webapp_auth_service.py1-100
The primary method for user authentication is via email and password.
AccountService.authenticate(email, password, session):
libs.password.hash_password) and compared with compare_password [api/libs/password.py].AccountService.login(account, session, ip_address):
TokenPair.RateLimiter to protect login endpoints from brute force api/services/account_service.py143-165LoginApi.post (console) handles user input, rate limiting, invitation validation, and triggers login api/controllers/console/auth/login.py117-181Sources: api/services/account_service.py167-452 api/controllers/console/auth/login.py117-181 api/libs/password.py1-40 api/libs/helper.py333-370
OAuth 2.0 allows users to log in via external providers:
OAuthLogin.get(provider) initiates OAuth by redirecting users to provider endpoints, passing optional invitation tokens, timezone, language, and redirect URL parameters api/controllers/console/auth/oauth.py153-170OAuthCallback.get(provider) handles OAuth provider callback:
RegisterService.get_or_create_account_by_oauth to find or create a linked user account.AccountService.login.AccountIntegrate model linking provider type and external identifiers.Sources: api/controllers/console/auth/oauth.py131-230 api/services/account_service.py370-402 api/libs/oauth.py1-150
Dify supports login via email codes (short, time-limited one-time tokens) for passwordless auth:
EmailCodeLoginSendEmailApi endpoint sends a 6-digit code by email.TokenManager.generate_token api/libs/helper.py350-400EmailCodeLoginApi validates submitted code/token pairing.AccountService.login.Sources: api/controllers/console/auth/login.py215-313 api/libs/helper.py350-400 api/services/account_service.py760-810
Users who forget passwords can reset it via email verification:
ForgotPasswordSendEmailApi POST /forgot-password):
AccountService.send_reset_password_email to generate and email a reset token with a verification code.ForgotPasswordCheckApi POST /forgot-password/validity):
ForgotPasswordResetApi POST /forgot-password/resets):
All steps enforce rate limiting and token validation for security.
Sources: api/controllers/console/auth/forgot_password.py59-192 api/services/account_service.py880-900 api/libs/password.py1-40
| Token Type | Purpose | Storage | Example Lifetime |
|---|---|---|---|
| Access Token | Authenticate API requests | HTTP-only cookie | 30 minutes (config) |
| Refresh Token | Renew Access Token | HTTP-only cookie + Redis | 30 days (config) |
| CSRF Token | Prevent cross-site request forgery | Non-HTTP-only cookie | Session duration |
"refresh_token:{token}" → account id"account_refresh_token:{account_id}" → refresh token stringAccountService.login handles token creation and storing in Redis api/services/account_service.py135-452generate_csrf_token and validated on state-changing HTTP requests api/libs/token.py131-133 api/controllers/console/auth/login.py178Sources: api/services/account_service.py135-452 api/libs/token.py1-60 api/controllers/console/auth/login.py174-180
PassportService for issuing and verifying JWT tokens representing end-user sessions.PassportResource (/passport) endpoint manages issuing tokens, requiring app code headers and optional user session parameters.PassportService.verify.EndUser model.Sources: api/controllers/web/passport.py41-130 api/libs/passport.py1-50 api/services/webapp_auth_service.py1-100
ApiToken model with type annotations.web/app/signin/invite-settings/page.tsx provide activation UI.RateLimiter instances restrict request rates on fragile endpoints:
Dify provides robust multi-method authentication with:
These flows are implemented cohesively by AccountService, WebAppAuthService, token utilities in libs, and enforced via decorated REST endpoints.
All citations:
Refresh this wiki
This wiki was recently refreshed. Please wait 6 days to refresh again.