MFA & Phone OTP
Kirak provides two distinct phone and device authentication mechanisms:
- Phone OTP (SMS): Passwordless login and registration via one-time SMS verification codes.
- MFA (TOTP): Two-factor authentication using standard authenticator apps (Google Authenticator, Microsoft Authenticator, 1Password, Authy).
MFA requires the pyotp extra:
pip install "kirak[mfa]"1. Phone OTP (Passwordless Login via SMS)
Section titled “1. Phone OTP (Passwordless Login via SMS)”Phone OTP allows users to authenticate using their mobile phone number without requiring a password.
Step 1: Request SMS Code
Section titled “Step 1: Request SMS Code”POST /auth/request-otp
{ "phone_number": "+14155552671"}- E.164 Format Required: Phone numbers must follow international format (e.g.,
+14155552671). Punctuation and spaces are automatically sanitized. - Code Generation: Kirak generates a 6-digit code with a short TTL (stored in Redis if
KIRAK_REDIS_URLis set, or theauth_otptable). - Rate Limit: 3 requests per phone number per hour.
Step 2: Verify Code & Authenticate
Section titled “Step 2: Verify Code & Authenticate”POST /auth/verify-otp
{ "phone_number": "+14155552671", "otp": "482913"}- Automatic User Creation: If no user exists with this phone number, Kirak creates a new user account with role
"user"andemail: null. It is a sign-up: withauth.registration"closed"it answers403 REGISTRATION_CLOSED, and thebefore_register/after_registerhooks run withmethod"phone"(see Who Can Sign Up). - Immediate Invalidation: The OTP code is invalidated immediately after use.
- Rate Limit: 5 verification attempts per phone number per hour.
Attaching a Phone Number to an Existing User
Section titled “Attaching a Phone Number to an Existing User”To attach a verified phone number to an account that signed up with email/password:
- Request Phone Code:
Terminal window POST /auth/phone/requestAuthorization: Bearer <user_token>{"phone_number": "+14155552671"} - Verify and Link Phone:
Terminal window POST /auth/phone/verifyAuthorization: Bearer <user_token>{"phone_number": "+14155552671", "otp": "482913"}
Security Note: Users cannot directly update
phone_numbervia standard CRUD operations (PUT /users/update). Phone numbers can only be stored through the SMS verification handshake, ensuring users cannot claim unverified numbers.
2. MFA (TOTP Authenticator Apps)
Section titled “2. MFA (TOTP Authenticator Apps)”MFA provides second-factor security on top of email/password, social, or OTP logins. Once enabled, every sign-in requires a valid 6-digit TOTP code.
The 2-Step Login with MFA
Section titled “The 2-Step Login with MFA”When a user with MFA logs in, Kirak responds with 403 MFA_REQUIRED instead of JWT tokens. The client must prompt the user for their 6-digit authenticator code and complete the challenge:
sequenceDiagram autonumber participant Client as Client Application participant User as End User participant Auth as Kirak Auth Server participant AuthApp as Authenticator App (TOTP)
Client->>Auth: POST /auth/login {email, password} Auth->>Auth: Verify credentials (Valid!) Auth->>Auth: Check MFA status (MFA is Enabled) Auth-->>Client: 403 Forbidden {error: "MFA_REQUIRED", details: {mfa_token: "..."}} Client->>User: Prompt for 6-digit TOTP code User->>AuthApp: Open App (Google Authenticator / 1Password) AuthApp-->>User: Reads current 6-digit code User->>Client: Enters code (e.g. 482913) Client->>Auth: POST /auth/mfa/challenge {mfa_token, code} Auth->>Auth: Verify mfa_token & TOTP code (Valid & not reused) Auth-->>Client: 200 OK {accessToken, refreshToken, user} Note over Client,Auth: User successfully authenticatedStep 1: Initial Login Trigger
Section titled “Step 1: Initial Login Trigger”POST /auth/login{"email": "alice@example.com", "password": "Secret!Pass123"}Response:
{ "statusCode": 403, "status": "error", "error": "MFA_REQUIRED", "message": "MFA verification required to complete login", "data": null, "details": { "mfa_token": "eyJ1Ijo0Miwi...Ii0.3f9a..." }}Step 2: Submit Challenge Code
Section titled “Step 2: Submit Challenge Code”The mfa_token is valid for 5 minutes. Submit it with the code:
POST /auth/mfa/challengeContent-Type: application/json
{ "mfa_token": "eyJ1Ijo0Miwi...Ii0.3f9a...", "code": "482913"}Response returns full session credentials (accessToken, refreshToken, user).
Shortcut for API Clients: Clients that already know the TOTP code can pass
mfa_codedirectly in the initial/auth/loginrequest body to complete authentication in a single HTTP round-trip.
3. Setting Up MFA
Section titled “3. Setting Up MFA”Users can register up to 5 authenticators (e.g., primary phone, backup tablet).
Step 1: Initiate Setup
Section titled “Step 1: Initiate Setup”POST /auth/setup-mfa (Requires Bearer token)
POST /auth/setup-mfaAuthorization: Bearer <access token>Content-Type: application/json
{"name": "Personal iPhone"}Response:
{ "statusCode": 200, "status": "success", "message": "Scan the QR code with your authenticator app, then verify its first code to activate it.", "data": { "method_id": 3, "provisioning_uri": "otpauth://totp/My%20API:alice%40example.com?secret=JBSWY3DPEHPK3PXP&issuer=My%20API", "backup_codes": ["A1B2C3D4E5", "F6G7H8J9K0", "..."] }}- QR Code Generation: Render a QR code on the frontend using
provisioning_uri. - Backup Codes: 10 single-use emergency backup codes are provided only once. Instruct the user to save them securely.
Step 2: Activate Authenticator
Section titled “Step 2: Activate Authenticator”The newly added authenticator does not take effect until confirmed with a valid code:
POST /auth/verify-mfa (Requires Bearer token)
{ "method_id": 3, "code": "482913", "action": "activate"}4. Managing Authenticators & Backup Codes
Section titled “4. Managing Authenticators & Backup Codes”List Authenticators
Section titled “List Authenticators”GET /auth/mfa/methodsAuthorization: Bearer <user_token>Returns all registered devices and the count of remaining backup codes.
Remove an Authenticator
Section titled “Remove an Authenticator”DELETE /auth/mfa/methods/{method_id}Authorization: Bearer <user_token>Content-Type: application/json
{"code": "482913"}Removing the last authenticator automatically disables MFA for the account.
Regenerate Backup Codes
Section titled “Regenerate Backup Codes”POST /auth/mfa/backup-codesAuthorization: Bearer <user_token>Content-Type: application/json
{"code": "482913"}Generates 10 new backup codes and invalidates all previous ones.
Disable MFA Completely
Section titled “Disable MFA Completely”POST /auth/disable-mfaAuthorization: Bearer <user_token>Content-Type: application/json
{"code": "482913"}5. Security & Storage
Section titled “5. Security & Storage”- Encrypted at Rest: TOTP secrets in
auth_mfaare encrypted using AES-256-GCM with the keys defined inKIRAK_AUTH_ENCRYPTION_KEYS. - Replay Protection: Kirak stores the timestamp step of the last accepted code to prevent replay attacks within the 30-second window.
- Lockout Protection: Every code is counted per user, whatever the IP, before it is checked. After 5 codes without a correct one in 15 minutes, MFA is locked for that user: the next code answers
401 MFA_LOCKEDwithout being checked, and a correct code sent while locked is not used up. A correct code clears the count. If the counter (the database, or Redis whenKIRAK_REDIS_URLis set) cannot be used, the code is refused with503 AUTH_UNAVAILABLE.