The Auth library (
@bitwarden/auth) represents the public API of the Auth team at Bitwarden. It handles all authentication flows, login strategies, and token management.Overview
The Auth library is responsible for managing user authentication across all Bitwarden client applications. It provides a comprehensive set of services and strategies for handling different authentication methods, from traditional master password login to modern WebAuthn and SSO.Authentication Methods
Bitwarden supports five authentication methods, each implemented as a dedicated login strategy:Master Password
Traditional login with master password and KDF validation
Auth Request
Login with Device - authenticate using a one-time access code
Single Sign-On
SSO login via SAML or OpenID Connect (OIDC)
Passkey (WebAuthn)
Passwordless authentication using WebAuthn credentials
User API Key
API key-based authentication for programmatic access
Core Architecture
Login Strategy Pattern
The library uses the Strategy Design Pattern to handle different authentication methods. Each authentication method has its own login strategy:Login Flow
The authentication flow follows these steps:Build Credentials Object
The initiating component creates a credentials object (e.g.,
PasswordLoginCredentials) containing the necessary authentication data.Call LoginStrategyService
The credentials are passed to
LoginStrategyService.logIn(credentials), which determines the appropriate strategy based on the AuthenticationType.Execute Strategy
The selected strategy builds a
LoginStrategyData object with a TokenRequest and caches it for potential 2FA or device verification.Token Request
The strategy sends a POST request to
/connect/token on the Identity Server with the appropriate credentials.Process Response
The server responds with one of three types:
IdentityTokenResponse- Authentication successfulIdentityTwoFactorResponse- 2FA requiredIdentityDeviceVerificationResponse- Device verification required
Key Services
LoginStrategyService
The orchestrator service that manages login strategy initialization and execution.libs/auth/src/common/services/login-strategies/login-strategy.service.ts
Auth Request Service
Manages authentication request flows for login-with-device scenarios.libs/auth/src/common/abstractions/auth-request.service.abstraction.ts
Logout Service
Handles cleanup and state management during user logout.libs/auth/src/common/abstractions/logout.service.ts
Directory Structure
Domain Boundaries
Authentication vs. Authorization: The Auth library focuses on authentication (verifying user identity) and initial session establishment. Authorization and access control for specific resources are handled by other domain libraries like
@bitwarden/admin-console for organization policies.Token Management: While the Auth library handles token acquisition during login, long-term token storage and refresh logic may be coordinated with platform-level services in
@bitwarden/platform.Login Credentials
Each authentication method uses a specific credentials object:| Credentials Type | Authentication Method | Required Fields |
|---|---|---|
PasswordLoginCredentials | Master Password | email, masterPassword |
AuthRequestLoginCredentials | Login with Device | email, accessCode, authRequestId |
SsoLoginCredentials | Single Sign-On | code, codeVerifier, redirectUrl |
WebAuthnLoginCredentials | Passkey (WebAuthn) | token, deviceResponse |
UserApiLoginCredentials | API Key | clientId, clientSecret |
Two-Factor Authentication
When 2FA is required, the authentication flow is modified:- Initial login attempt returns
IdentityTwoFactorResponse LoginStrategyDatais cached in theLoginStrategyService- User is routed to
/2fato enter their 2FA token logInTwoFactor()appends the 2FA token to the cached request- Re-submit to
/connect/tokenwith 2FA token included
New Device Verification
For users without 2FA on a new device:- Initial login returns
IdentityDeviceVerificationResponse - User receives verification code via email
- User is routed to
/device-verification logInNewDeviceVerification()submits the OTP code- Authentication completes upon successful verification
Usage Example
Related Libraries
- @bitwarden/platform - Platform abstractions and state management
- @bitwarden/vault - Post-authentication vault access
- @bitwarden/admin-console - Organization policies and access control