- Docs
- Specs
- Auth
- Phone + OTP
Phone + OTP¶
1. Purpose¶
Define the contract for phone-based authentication. TyteRide uses phone + OTP everywhere; there is no email/password authentication.
Sources: Client-Service Contract §2, §3, §7, §10, Sessions and Roles, Glossary.
2. Scope¶
This spec covers OTP send/verify, Sanctum bearer token issuance, and the session bootstrap/logout endpoints delivered by Identity Slice 1. Enrollment into a Company (POST /auth/enroll-company) is related but specified in Enrollment Approval.
All paths below include the API base path. There is no URL version segment.
3. Flow¶
sequenceDiagram
autonumber
participant C as Client (Front Views / Dashboard / App)
participant A as API
participant S as SMS Provider
participant DB as API Store
C->>A: POST /api/auth/otp/send {phone, locale?}
A->>A: Validate + apply send/resend limits
A->>A: Generate 6-digit OTP and store only its hash
A->>S: Send OTP
S-->>A: Delivery accepted
A->>DB: Store OTP verification state and pending locale
A-->>C: 200 {message}
C->>A: POST /api/auth/otp/verify {phone, otp, client?}
A->>A: Validate + verify OTP hash, expiry, and attempts
alt OTP valid
A->>DB: Resolve/create User and consume OTP
A->>A: Issue Sanctum bearer token
A-->>C: 200 {token, user, locale}
else OTP invalid or expired
A-->>C: 422 {message, errors}
else Attempt limit reached
A-->>C: 429 {message}
end
4. Endpoints¶
| Method | Path | Auth | Purpose |
|---|---|---|---|
POST |
/api/auth/otp/send |
Public, rate-limited | Send an OTP to a phone number. |
POST |
/api/auth/otp/verify |
Public | Verify OTP, resolve/create the User, and issue a Sanctum bearer token. |
GET |
/api/auth/me |
auth:sanctum |
Return the current User and locale. |
POST |
/api/auth/logout |
auth:sanctum |
Revoke the current bearer token. |
4.1 Send OTP¶
Request:
{ "phone": "+967700000000", "locale": "en" }
phoneis a required string. Surrounding whitespace is trimmed; this slice does not apply country-aware normalization.localeis optional and accepts onlyenorar. It selects the send response/SMS language and is retained with the pending challenge for successful verification.
Success (200):
{ "message": "OTP sent." }
Arabic locale returns the localized equivalent, تم إرسال رمز التحقق..
4.2 Verify OTP¶
Request:
{ "phone": "+967700000000", "otp": "123456", "client": "dashboard" }
phoneis a required string and is trimmed in the same way as send.otpis a required six-digit string.clientis an optional string used as the Sanctum token name. Without it, the token name isauth.
Success (200):
{
"token": "<sanctum-bearer-token>",
"user": {
"id": 1,
"name": "+967700000000",
"phone": "+967700000000",
"email": null,
"role": "passenger",
"locale": "en",
"company_id": null
},
"locale": "en"
}
The first successful verification creates the User. Its initial name is the normalized-by-trimming phone value, email is null, role is passenger, locale comes from the pending send challenge (falling back to the verify request locale), and company_id is null. Existing Users retain their profile and role while their locale is updated from the challenge.
The OTP is never included in the response.
4.3 Current User¶
GET /api/auth/me returns (200):
{
"user": {
"id": 1,
"name": "+967700000000",
"phone": "+967700000000",
"email": null,
"role": "passenger",
"locale": "en",
"company_id": null
},
"company": null,
"locale": "en"
}
The company key is always present. From Slice 2 it is null for Users without a membership and { id, name_en, name_ar, status, tier: { code, name, max_users } } for Users with one. A pending Manager can authenticate; OTP login does not require Company approval.
4.4 Logout¶
POST /api/auth/logout revokes only the bearer token used for the request and returns (200):
{ "message": "Logged out." }
Other tokens issued to the same User are not revoked.
5. OTP Policy and Storage¶
| Rule | Contract |
|---|---|
| Code | Six decimal digits. |
| Lifetime | Five minutes (300 seconds). |
| Resend cooldown | One send per phone per 60 seconds. A successful new send invalidates earlier unconsumed codes. |
| Verification attempts | At most five incorrect attempts per code. The fifth incorrect attempt locks the code. |
| Send rate limit | Five sends per phone + IP pair per ten minutes. |
| Verify rate limit | Attempt limit is enforced per OTP code. |
| Storage | otp_codes: phone, code_hash, attempts, expires_at, consumed_at, created_at; phone is indexed. |
| Secret handling | Only a hash is persisted. Plaintext exists only while generating/sending the code. |
The SMS boundary is SmsSender. Slice 1 binds the log driver (sms.driver=log, sms.from). Local/testing delivery writes through that SMS log driver. Production responses never expose the OTP, and production log delivery is silent by default. For manual testing only, an operator can explicitly opt in by setting SMS_LOG_OTP_IN_PRODUCTION=true (sms.log_otp_in_production), which makes the log driver write the full message — including the plaintext OTP — to the logs. Security warning: enabling this flag writes plaintext OTPs to logs; it must be temporary and turned off immediately after testing.
6. Validation and Errors¶
All errors use the standard API envelope:
{ "message": "Human-readable message", "errors": { "field": ["Reason"] } }
errors is present only for 422 validation failures.
| Case | Status | Notes |
|---|---|---|
| Missing/invalid send or verify fields | 422 | message and field-level errors. |
| Wrong, consumed, or expired OTP | 422 | Field-level error under otp. |
| Resend cooldown or send rate limit | 429 | message only; Retry-After is supplied when available. |
| Fifth incorrect verification attempt / locked code | 429 | message only. |
| Missing/invalid bearer token | 401 | message only. |
| Server/SMS boundary failure | 500 | Generic localized message; no internal details. |
7. Localization¶
Accept-Language: en|arselects response language; default isen.- Send body
locale, when present, takes precedence overAccept-Languagefor that send and its pending challenge. - The
messagefield, validation messages, and SMS text uselang/enandlang/ar. users.localeis returned in verify and current-user responses so clients can persist the choice.
8. Security and Boundaries¶
- OTP send/verify endpoints are public and must be rate-limited where specified.
- SMS/external delivery is faked at test boundaries; tests never use live services.
- OTP hashes use a one-way hash; OTP plaintext is never stored in the database or returned by the API.
- OTP verification, consumption, User resolution, and token issuance run in one transaction.
- Tokens use Sanctum bearer authentication and the token naming policy above.
9. Tests¶
Minimum verification for this slice:
- Send success, validation, cooldown, per-phone + IP rate limit, and localized response.
- Verify success/token naming, first User creation, wrong OTP, expiry, consumption, and maximum attempts.
- Current User and current-token logout, including bearer authentication and error envelopes.
- English/Arabic locale behavior and pending locale persistence.
- Domain unit tests for phone trimming, six-digit generation, one-way hashing, expiry, attempt limits, and consumption.
- SMS boundary faked; persisted OTP contains a hash rather than plaintext.
10. Future Questions¶
- Canonical country-aware/E.164 normalization and supported country formats.
- Production SMS provider selection and delivery-provider-specific behavior.
- Token expiry/refresh policy beyond the current-token revocation behavior defined here.