Skip to content

MFA & Phone OTP

Kirak provides two distinct phone and device authentication mechanisms:

  1. Phone OTP (SMS): Passwordless login and registration via one-time SMS verification codes.
  2. MFA (TOTP): Two-factor authentication using standard authenticator apps (Google Authenticator, Microsoft Authenticator, 1Password, Authy).

MFA requires the pyotp extra:

Terminal window
pip install "kirak[mfa]"

Phone OTP allows users to authenticate using their mobile phone number without requiring a password.

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_URL is set, or the auth_otp table).
  • Rate Limit: 3 requests per phone number per hour.

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" and email: null. It is a sign-up: with auth.registration "closed" it answers 403 REGISTRATION_CLOSED, and the before_register / after_register hooks run with method "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:

  1. Request Phone Code:
    Terminal window
    POST /auth/phone/request
    Authorization: Bearer <user_token>
    {"phone_number": "+14155552671"}
  2. Verify and Link Phone:
    Terminal window
    POST /auth/phone/verify
    Authorization: Bearer <user_token>
    {"phone_number": "+14155552671", "otp": "482913"}

Security Note: Users cannot directly update phone_number via standard CRUD operations (PUT /users/update). Phone numbers can only be stored through the SMS verification handshake, ensuring users cannot claim unverified numbers.


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.

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 authenticated
Terminal window
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..."
}
}

The mfa_token is valid for 5 minutes. Submit it with the code:

Terminal window
POST /auth/mfa/challenge
Content-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_code directly in the initial /auth/login request body to complete authentication in a single HTTP round-trip.


Users can register up to 5 authenticators (e.g., primary phone, backup tablet).

POST /auth/setup-mfa (Requires Bearer token)

POST /auth/setup-mfa
Authorization: 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.

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"
}

Terminal window
GET /auth/mfa/methods
Authorization: Bearer <user_token>

Returns all registered devices and the count of remaining backup codes.

Terminal window
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.

Terminal window
POST /auth/mfa/backup-codes
Authorization: Bearer <user_token>
Content-Type: application/json
{"code": "482913"}

Generates 10 new backup codes and invalidates all previous ones.

Terminal window
POST /auth/disable-mfa
Authorization: Bearer <user_token>
Content-Type: application/json
{"code": "482913"}

  • Encrypted at Rest: TOTP secrets in auth_mfa are encrypted using AES-256-GCM with the keys defined in KIRAK_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_LOCKED without 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 when KIRAK_REDIS_URL is set) cannot be used, the code is refused with 503 AUTH_UNAVAILABLE.