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.

Terminal
curl https://api.example.com/api/passport/identity/factors -H "Authorization: IDENTITY <token>"
JSON
[{"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.

  1. Start the setup

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

    challenge is a QR code image. Show it to the person and let them scan it with their authenticator app.

  2. Confirm with a code

    Terminal
    curl -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:

Terminal
curl -X POST https://api.example.com/api/passport/identify \
  -H "Content-Type: application/json" \
  -d '{"identifier": "spica", "password": "spica"}'
JSON
{"challenge": "Please enter the 6 digit code", "answerUrl": "passport/identify/<identityId>/factor-authentication"}

Send the code to answerUrl to receive the token:

Terminal
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

Terminal
curl -X DELETE https://api.example.com/api/passport/identity/<identityId>/factors \
  -H "Authorization: IDENTITY <token>"