How sign-in works
This page explains what happens when connect() signs a user in to your app, and how every request your app makes afterwards proves who is sending it. You do not need any of it to build an app: the SDK does all of it for you. Read it to understand why users are safe, what is kept in their browser and what your app can never obtain. Each part is explained in plain words first; the name of the standard it follows comes after, for readers who know them. Sign users in shows the same flow from your app's side, and Users, sign-in and consent from the user's.
The passkey
A user signs in to the server with a passkey or with a password; an account can have both. A passkey is a pair of keys made by their device for this server. The private key stays on the device and is unlocked with the device's screen lock, fingerprint or face; the server keeps only the public key, which can check a signature but never make one. The passkey is tied to the server's own address, so it works only on the server's pages (https://phibac.com for this server). Your app never sees it, and nothing it does can use it.
Signing in also signs the user in to the server itself in that browser, with a cookie on the server's own address that stays valid while it is used (it lapses after 30 days without use). That is why the sign-in popup often closes by itself: the user is already signed in to the server and has already approved your app.
The standard: passkeys are WebAuthn credentials (the Web Authentication standard). The server asks for a passkey stored on the device (a discoverable credential), so the user picks it without typing an email address.
The password
A password is typed, with the account's email address, on the server's sign-in page and nowhere else. The server never stores it: it keeps a value worked out from the password and a random addition, made slow on purpose so that guessing is costly, and checks what is typed against that.
A password alone is not enough in a new place. When the browser has not signed in to that account before, the server sends a short code to the account's email address. The code works for 10 minutes and only in the browser that asked, and the user types it to finish. On the sign-in page the user can tick "Remember this browser": that browser is then not asked for a code again for 90 days, and stays signed in to the server as it does after a passkey sign-in. Without the tick, as on a borrowed device, the browser is not remembered, and the sign-in to the server ends when the browser closes, or after 12 hours at the latest.
Wrong guesses are limited: after 10 wrong passwords for one email address within an hour, password sign-in for that address waits out the hour, except from a browser it remembers. A forgotten password is reset through a link sent by email, valid for 1 hour, but only when the account has no passkey. An account with a passkey is never opened through email alone: its owner signs in with the passkey to change the password.
When a sign-in was not made with a passkey, the server asks for the password again before it changes how the account is signed in to, exports the account's data or schedules its deletion. A session left open on a borrowed device can therefore not take the account over.
The standard: the stored value is an Argon2id hash with a random salt.
The sign-in popup
When no sign-in is stored for your app in the browser, connect() signs the user in through a popup on the server:
- The SDK opens the popup. It makes a random secret, keeps it in memory, and opens the server's sign-in page with your app's ID, your page's origin, a random value of its own to recognise the answer by, and a fingerprint of the secret (its SHA-256 hash), never the secret itself.
- The server checks the request. The app must exist and not be suspended, and your page's origin must be registered for it. A page on
http://localhostthat is not registered is allowed, but the user is asked to approve your app at every sign-in. If a check fails, the popup shows why, such as "This app is not registered.", andconnect()fails withsign_in_cancelledwhen the user closes it. - The user signs in and approves. If the user is not signed in to the server in this browser, the popup asks them to sign in, with their passkey or their password (and, for a password in a browser that is new to the account, the emailed code). The first time, it asks them to connect your app, with Allow and Cancel. Cancel makes
connect()fail withaccess_denied. - The popup hands back a one-time code. The server's last page sends a code to your page and closes. It addresses the message to your page's origin, the one checked in step 2, so a page on any other origin cannot receive it. The SDK accepts it only from that popup, from the server's address, and with the random value it sent in step 1. The code works once and only for 1 minute.
- The SDK exchanges the code. It sends the code and the secret to the server, signed with the browser's key (see below). The server accepts the code only from the app it was made for, and only with the secret whose fingerprint it saw in step 1. A code used once, or presented with the wrong app or secret, is used up and refused. The answer is the user's ID and two tokens, both tied to the browser's key.
A code stolen on the way is useless: exchanging it needs the secret, which never left the page's memory, and the browser's key.
The standards: this is the OAuth 2.0 authorization code flow, with PKCE (Proof Key for Code Exchange, method S256) for the secret and its fingerprint, and the code returned to the page with postMessage instead of a redirect.
The key kept in the browser
Before its first request to a server, the SDK has the browser make a key pair for that server and stores it in the browser's IndexedDB storage. All your apps on the same server share it in that browser. The private key is made so that it cannot be exported: code running in your page can ask the browser to sign with it, but no code can read it, so it cannot be copied to another device.
The standard: an ECDSA key on the P-256 curve, made with the browser's Web Crypto interface with export turned off.
The access token and the refresh token
The exchange in step 5 gives the SDK two tokens, random strings that stand for "this user, this app, this browser's key". The server stores only a hash of each, never the token itself.
- The access token is sent with every request your app makes. It lasts 15 minutes and is kept only in the page's memory. The SDK gets a new one shortly before it runs out.
- The refresh token is what the SDK uses to get a new access token. It is kept in the browser's IndexedDB storage, so the sign-in survives a reload and works offline. Each time it is used, the server answers with a new access token and a new refresh token, and the old refresh token is used up: it rotates. A refresh token lasts 30 days from when it was issued, so a sign-in your app keeps using goes on, and one left unused for that long expires.
Presenting a used-up refresh token again ends the whole sign-in: someone may have copied it. One exception covers a lost answer: the same refresh token sent again with the same key within 1 minute of its first use gets a new pair. Tabs of your app in one browser take turns renewing, through a lock the browser offers (Web Locks), so they do not present the same refresh token twice.
When the server refuses a renewal, because the sign-in expired, the user disconnected your app in their account area or the account is gone, the SDK deletes the stored sign-in and your app's requests fail with unauthorized. The user then signs in again from a click.
Both tokens are tied to the browser's key: the server accepts them only together with a proof signed by that key.
The proof sent with every request
Every request the SDK sends to the server, including the code exchange and every renewal, carries a proof: a short statement signed with the browser's key. It says which request it is for (the method, such as GET, and the address without its query), when it was made, a random ID used only once, and, when an access token is sent, a fingerprint of that token. The server checks that:
- the signature is valid, and made by the key the token is tied to;
- the method and address are those of this request;
- the time is within 1 minute of the server's clock;
- the ID has not been used before, so a recorded request cannot be sent again.
So a stolen token is useless on another device: without the private key, nobody can make the proofs the server asks for.
The standard: DPoP (Demonstrating Proof of Possession, RFC 9449). The SDK sends Authorization: DPoP <access token> and a DPoP header with the proof, a JSON Web Token signed with ES256. Sign-in uses the same proofs, so the tokens are tied to the key from the moment they are issued.
When the device's clock is wrong
A proof carries the device's time. When the device's clock is off by more than 1 minute, the server refuses the request with unauthorized and sends its own time with the error. The SDK works out the difference, adds it to the time in every later proof, and sends the request again, once. Your app never sees this.
When a request fails with unauthorized for another reason, the SDK gets a new access token and tries once more. Only when that fails too does your app see unauthorized.
What is kept in the browser, and where
On your app's origin:
- In the page's memory only: the access token, the secret during a sign-in, and the clock correction.
- In IndexedDB: the browser's key pair, one per server; the refresh token and the user's ID, one per server and app; and, in a separate database, the copy of the user's data and the write queue, one per server, app and user, so another user signing in on the same browser never sees them.
- Nowhere else: nothing in
localStorage,sessionStorageor cookies, and no token in any address.
On the server's own address, the browser keeps the cookie that signs the user in to the server.
backend.signOut() deletes the refresh token, the copy of the data and the queued changes for your app. It keeps the browser's key and other apps' sign-ins, and it does not sign the user out of the server; the user does that in their account area.
Apps on a phone
A native app on a phone has no popup to open and no web address to be recognised by. The server has a second way to finish a sign-in for such an app. The mobile SDK that uses it is not released yet, so today this section describes the server only.
- Sign-in still happens on the server's own pages, in the phone's system browser, never inside the app. Passkeys, passwords and the emailed code work as they do on the web.
- The app asks for the result by redirect. It opens the same sign-in address with
response_mode=queryand aredirect_uriin place oforigin. When the user allows the app, the server sends the browser to that address with the code added, and the phone hands the address to the app. - Only the registered app can receive it. Each app with an iOS or Android entry has an address of its own on the server, and the phone's operating system gives links to that address to that app alone. The
redirect_urimust be exactly that address;npx phibac native showprints it. - While you develop, a
redirect_uriwith an app scheme, such asmynotes://signin, is accepted from the account that owns the app, and from anyone on a local development server. Any app can claim a scheme, so the server asks every time and shows where the result goes. - Everything after that is the same: the code is exchanged for tokens with a proof from the device's key, and every request carries a proof, as described above.
What your app can never obtain
- The user's password or passkey: both are used only on the server's own pages.
- The user's email address: sign-in gives your app the user's ID only.
- The browser's private key: your page can have it sign, but never read it.
- A sign-in for another app: tokens are made for one app and refused for any other.
- A sign-in from an address your app has not registered: the popup refuses it, except
http://localhost, where the user is asked every time. - Another user's data.
Code that runs in your page, including a script someone manages to inject into it, can use the stored sign-in while the page is open, because it runs on your origin. It still cannot take the sign-in to another device. Protect your page as you would any signed-in site: show user text with textContent, never as HTML, and load scripts only from sources you trust.