Skip to content

Setting up a method

Give signed-in users a Two-step sign-in section in their settings. Offer what getMethods allows.

  1. enrollTotp(userId, label) returns qrCode (a PNG data URI for an <img>), secret (to type in by hand) and the method, still PENDING.
  2. The user scans it with Tenon Authenticator or any authenticator app, and types the first code.
  3. activateFactor(userId, factorId, code): the method is active, and it replaces their previous one. A wrong code throws InvalidCode: let them try again. After 15 minutes or 5 wrong codes, start again (enrollment_expired).
TotpEnrollment totp = tenonAuth.enrollTotp(user.id(), "Customer portal");
// Page: <img src="${totp.qrCode()}">, totp.secret(), a field for the first code.
tenonAuth.activateFactor(user.id(), totp.factor().id(), code);
  1. enrollPush(userId, label) returns qrCode, enrollmentUri (a link that opens the app on the same phone) and activationNumber.
  2. The user scans the QR code with Tenon Authenticator, and types the activation number your page shows.
  3. Poll listFactors every 3 seconds until:
    • the method is ACTIVE: done, and it replaced their previous one;
    • it is gone: they typed a wrong number, so start again;
    • expiresAt has passed.

Only when getMethods().emailAllowed, and only for the listed domains when there are any.

  1. enrollEmail(userId, email, label, language) sends a code to the address. Take the address from your own records, e.g. the company directory, rather than letting users type any.
  2. The user types the code: activateFactor.

Errors:

  • EmailRefused with email_codes_not_allowed or email_domain_not_allowed;
  • TooManyRequests;
  • EmailUnavailable: no email could be sent, so try later.

Right after a method becomes active, call createRecoveryCodes(userId) and show the 10 codes once, with Download and Print. They replace any older codes. Show getRecoveryCodes(userId).remaining in the settings, and offer new codes when few are left.

deleteFactor(userId, factorId) removes the method. The user’s remembered devices and recovery codes stop working with it. Whether a user may turn the second step off at all is your policy.