Second factors
TOTP enrolment: the QR code
On this page
Introduction
An authenticator app on a phone can be a person's second factor. By default, it shows a six-digit code that changes every 30 seconds, called a time-based one-time password (TOTP). To make those codes, the authenticator app needs a secret that it shares with Signari.
Signari's enrolment page shows that secret as a Quick Response (QR) code to scan.
The key is still available for manual entry, behind a Can't scan it?
disclosure.
People set up their own authenticator app from their account, at
/account/mfa/totp.
Why a QR code
Most people expect to scan a code with their phone's camera. A base32 secret of 32 characters is hard to type on a phone keyboard.
Enrolment never disturbs the factor you already have
The secret shown on the setup page is stored as an enrolment in progress, apart from any authenticator the account already has. That one keeps working until the new code is confirmed.
So an abandoned setup page changes nothing. A setup page somebody else sent you to cannot turn your second factor off.
Opening the page is a GET and stores nothing at all. A GET is what a link from any other site can make a signed-in browser send.
Confirming a replacement tells the account holder by email, and by text message where a verified number exists.
Starting and confirming both need a sign-in from the last ten minutes. An older session is sent to sign in again, and brought back to the page. Both are refused outright in an impersonation session. Confirming replaces the authenticator and deletes the recovery codes, so a stolen cookie must not be enough to do it.
Removing it
/account/security lists the authenticator app with the person's other sign-in
methods. It removes the authenticator app under the same recent-sign-in rule.
The holder is told on every channel they have, and the removal is audited as
mfa.totp_removed. When it was their last second factor, the recovery codes go
with it.
Failure behaviour
If the QR code cannot be built, the page still renders with the key for manual entry, and logs a warning, so the person can still turn on a second factor.
Example: enrolling from the QR code
A person can enrol using only what the QR code carries:
TextGET /account/mfa/totp -> introduction and a button; nothing is storedPOST /account/mfa/totp/start -> page with an inline <svg> (CSRF token required)decode the served SVG -> otpauth://totp/Default:...?secret=YDFBKF3G...derive a TOTP code from it -> 6 digitsPOST /account/mfa/totp -> "Two-factor authentication is on", 10 recovery codes
The secret read out of the QR code matches the one shown for manual entry, and the code derived from it is accepted.
Why the encoder is in this repo
The usual Go library, github.com/skip2/go-qrcode, is archived and has not been
updated since 2020. It would be an unmaintained dependency rendering the
enrolment secret for the second factor. About 450 lines of code that match a
reference encoder are the better choice.
Scope is deliberately narrow: byte mode, error correction level M, versions 1
to 12. That covers every otpauth:// address Signari produces, and nothing
else. Those addresses run 90–160 bytes, and version 12 holds 288. Anything wider
would be paths nothing here exercises.
Correctness
The output matches Python's qrcode module for module, across six inputs ×
all eight masks. Python's qrcode is the reference here. The match holds with two
things fixed:
- the reference in single-segment byte mode. Left alone, it switches to alphanumeric for uppercase input. That is a denser, equally valid encoding of different bits.
- the mask, the same on both sides, because selection is a separate question (below).
Penalty scores match the reference's scorer exactly, for every mask.
Penalty rule 3 looks for the finder-like pattern as the standard states it: one
eleven-module window, 10111010000 or 00001011101, inside the symbol. A
position off the symbol is never counted as light, so a window that would run off
the edge does not score. A wrong score lets a worse mask win, which gives a valid
symbol that is harder to scan.
OpenCV's detector decodes the rendered image back to the original address. That is the property that matters: a camera pointed at it recovers the secret.
The 15-bit format field is written least-significant bit first along each arm. It is how a decoder learns which mask to undo. With its bits reversed, a symbol can have flawless finder patterns, correct timing and a perfect data region, and still no scanner can read it.
Where we differ from the reference, on purpose
Python's qrcode sometimes scores mask 2 lowest, and then returns mask 5
anyway. That is a quirk in its selection, not its scoring. The standard says the
lowest penalty wins, which is what this package does.
Rendering
The code is drawn as Scalable Vector Graphics (SVG), inline. There is no image encoder, no rasterising, and no blurring when a browser scales it. It can also be inlined, which matters because the enrolment page carries a Content-Security-Policy that forbids external sources.
The background is drawn explicitly white. On a dark-themed page, a transparent background puts dark modules on dark, and the symbol does not scan.
It is inserted with template.HTML, which turns off escaping. That is only safe
while the SVG is built entirely from integers and literals, and it is. Fed
</svg><script>alert(1)</script>..., the output still holds
exactly four opening and four closing brackets: the <svg>,
<rect/>, <path/> and </svg> this package emits, and nothing else.
Changed Signari v0.2