Two-factor authentication
Protect identity and user accounts with time-based one-time passwords from apps such as Google Authenticator or 1Password.
Spica supports two-factor authentication with time-based one-time passwords (TOTP). After a user or identity enables it, logging in takes their password and a 6-digit code from an authenticator app.
curl https://api.example.com/api/passport/identity/factors -H "Authorization: IDENTITY <token>"[{"type": "totp", "title": "Time based One Time Password", "description": "Verification with time based one time passwords which are usually generated by apps like Google Auth, Duo etc.", "config": {}}]Enable it
The examples use identities. For users, replace /passport/identity/<id> with /passport/user/<id>. Users can enable it on their own account with their own token.
Start the setup
Terminalcurl -X POST https://api.example.com/api/passport/identity/<identityId>/start-factor-verification \ -H "Authorization: IDENTITY <token>" -H "Content-Type: application/json" \ -d '{"type": "totp", "config": {}}'JSON{"challenge": "data:image/png;base64,iVBORw0KGgo...", "answerUrl": "passport/identity/<identityId>/complete-factor-verification"}challengeis a QR code image. Show it to the person and let them scan it with their authenticator app.Confirm with a code
Terminalcurl -X POST https://api.example.com/api/passport/identity/<identityId>/complete-factor-verification \ -H "Authorization: IDENTITY <token>" -H "Content-Type: application/json" \ -d '{"answer": "123456"}'The setup must be completed within 5 minutes of starting it.
Log in with a second factor
When two-factor authentication is on, the login response is a challenge instead of a token:
curl -X POST https://api.example.com/api/passport/identify \
-H "Content-Type: application/json" \
-d '{"identifier": "spica", "password": "spica"}'{"challenge": "Please enter the 6 digit code", "answerUrl": "passport/identify/<identityId>/factor-authentication"}Send the code to answerUrl to receive the token:
curl -X POST https://api.example.com/api/passport/identify/<identityId>/factor-authentication \
-H "Content-Type: application/json" \
-d '{"answer": "123456"}'For users the endpoints are POST /passport/login and POST /passport/login/<userId>/factor-authentication.
With the identity devkit, login returns either a token or a challenge, and isChallenge tells them apart.
Disable it
curl -X DELETE https://api.example.com/api/passport/identity/<identityId>/factors \
-H "Authorization: IDENTITY <token>"