Sign in

The JS client does this for you. This page is the HTTP underneath. Part of the client API.

Sign-in is by a one-time code sent to an email address. There are no passwords.

1. Send a code#

Lang
Text
POST /runtime/auth/send_magic_code
{"email": "ada@example.com"}
Lang
JSON
{"sent": true}

The email is trimmed and lower-cased. Errors:

Status Body
400 {"type":"validation-failed","errors":{"email":["should be a valid email"]},"etype":"$users"}
400 {"type":"validation-failed","errors":{"email":["missing required key"]},"etype":"$users"}
429 {"type":"rate-limited","limit":"resend","retryAfterMs":21000,"message":"A code was sent recently; wait before requesting another"} (a code was sent to this address moments ago)
429 {"type":"rate-limited","limit":"email","retryAfterMs":...,"message":"Too many sign-in codes were sent to this address. Try again in 42 minutes."} (too many codes for this address recently: see Send limits)
429 {"type":"rate-limited","limit":"tenant-day","cap":...,"retryAfterMs":...,"message":"This app has sent all the sign-in emails it may send today. Try again in 7 hours."} (a daily cap)
502 {"type":"email-failed","message":"The sign-in email couldn't be sent: the mail provider refused it. ..."}
503 {"type":"email-failed","message":"The sign-in email couldn't be sent: the mail provider didn't answer in time. ..."}

Every 429 has a Retry-After header, in seconds. Show the message to the user.

A code is valid for 10 minutes, and only once.

Demo mode. Demo mode is a setting of each tenant, off unless its admin turns it on (october demo SLUG on, see cli-commands.md). When your tenant is in demo mode, the response also contains the code, and your app can sign the user in at once:

Lang
JSON
{"sent": true, "demo_code": "978255"}

GET /runtime/config tells you which mode the tenant is in: {"demo": true}. Show that in your UI.

Sign-in email#

A new tenant sends real sign-in email from the start, on a server with a mailer, within the daily limits. On the hosted service, sign-in email is on for new apps. It is not a setting a tenant admin changes: the server may turn a tenant's email off (for example after too many bounces or spam complaints), and a tenant created before email was on by default may have it off. On a server with no mailer, email is never on. Where it is off, send_magic_code still makes a code but sends it nowhere, and the answer says so ("delivered": false, with a message), so your UI can tell the user instead of saying "check your email":

Lang
JSON
{"sent": true, "delivered": false, "message": "This tenant doesn't email sign-in codes, so this one wasn't emailed. ..."}

Where the code goes then depends on the server, not on the tenant:

  • A server with a real mailer (SES configured, as on a hosted server) never writes a code to its log: anyone who reads the log could sign in with it. It logs only that a code was made for that address, on that tenant, and not sent. Users of a tenant with email off can't sign in until its email is on again; use demo mode while you build.
  • A server with no mailer (a server you host yourself without email) writes the code to its log (Magic code for ...), because that is the only way anyone can sign in there. Treat that log as a secret.

When email is on, the answer is {"sent": true} and the user gets a short plain email from the server's sender address:

  • subject "Your sign-in code for SLUG" (the tenant's slug);
  • the six-digit code, large;
  • when it expires (10 minutes);
  • a line saying to ignore the email if they didn't ask to sign in.

There are no images and no links in it. Demo mode takes precedence: a tenant in demo mode never sends email, even with email on, because the code is in the answer anyway.

Send limits. Anyone can ask for a code for any address, so sending codes is rate-limited. A refused request answers 429 with one of these limit values, sends nothing and counts nothing:

limit in the 429 Meaning
resend a code went to this address moments ago
email too many codes for this address recently
client too many codes from this caller recently
tenant this app has sent too many codes recently
server the server has sent too many codes recently

Room comes back over time, and Retry-After says when. Every address counts the same whether or not it belongs to a user, so the limits tell nobody who has signed up. The server sets the limits, for the whole server or for one tenant (a sign-up page used from one office network may need more room); on the hosted service, the service sets them.

Daily caps#

On top of those, real email has caps per day (UTC), which hold across all of the server's instances:

limit in the 429 Meaning
tenant-day this app has sent all the email it may send today
new-tenant-day the same, for a new app, which starts with a lower cap
account-day every app one tenant admin created, together, has
server-day the server has sent all the email it sends today

Over a cap, send_magic_code answers 429 with that limit, the cap, and Retry-After until the next UTC midnight; no code is made or sent, and nothing is counted. Show the message to the user: it says the app can't send more sign-in emails today, never anything about the address. Sign-in for users who already have a token is not affected. The caps count every real email of the tenant: sign-in codes, and the notifications the server sends for it. The caps are the server's, not your token's (on the hosted service, paid plans will raise them later); a tenant given a cap of its own is out of its creator's account-day cap. The first time a day a cap refuses your tenant's email, its events say so (email-cap-reached).

Suppressed addresses#

An address whose email bounced for good, or whose owner reported an email from this server as spam, is never mailed again, by any tenant. send_magic_code answers as if it sent the code ({"sent": true}), so no app learns what happened to an address on another app, and sends nothing. Your tenant's admins see each one in the tenant's events (email-suppressed, with the address your app asked for), along with your own emails' bounces and spam reports (email-bounced, email-complaint). If a user says no code arrives, ask for another address.

A tenant whose emails bounce or are reported as spam too often (more than 5 % of its emails bounce, or 0.1 % are reported as spam, over a week) has its real email turned off by the server, and its events say email-auto-disabled. Its send_magic_code then answers delivered: false, as for a tenant whose email was never on, until the server turns it on again. When the server stops all of its email at once, the answer is the same delivered: false.

If the email can't be sent, the answer is 502 (the mail provider refused it) or 503 (it didn't answer in time). The code is withdrawn, so the user can ask again at once, without the 30-second wait.

2. Verify the code#

Lang
Text
POST /runtime/auth/verify_magic_code
{"email": "ada@example.com", "code": "978255"}
Lang
JSON
{"user": {"id": "67c7970d-d9b8-4c0f-a7f7-6543a754336e", "email": "ada@example.com"},
 "access_token": "5b0e1c9f7a3d4e8b9c2f6a1d0e7b3c4a8f9d2e1b6c5a4f3e2d1c0b9a8f7e6d5c",
 "refresh_token": "c061522ba3eb61806373acfd5481a9ccc8fbcc5f9c75f66c547ca9239d076337",
 "token_type": "Bearer",
 "expires_in": 900,
 "refresh_expires_in": 2592000}

The first sign-in of an email creates its user. A wrong, expired or used code gives 400 {"type":"invalid-code","message":"Invalid or expired code"}. After a few wrong codes, the email's outstanding codes are revoked and the user must request a new one.

Guesses per address are capped. One address may try only a limited number of codes a day (UTC) on one tenant, across all the codes it was sent; a successful sign-in doesn't count. A try counts once for each code the address holds at that moment, since it is checked against each. Past the cap, every code for that address is refused with the same invalid-code answer as a wrong one, the right code too, until 00:00 UTC. Nothing in the answer says the cap was reached: that would tell someone guessing when to stop. A user who sees "Invalid or expired code" for a code they just received can sign in again after midnight UTC. Tokens they already hold keep working. With 6-digit codes this keeps the chance of guessing into one account below 1 in 100,000 a day.

3. Use the access token#

A sign-in is a pair of tokens:

  • the access token goes as a bearer token on POST /graphql and POST /live. It lasts expires_in seconds (15 minutes).
  • the refresh token is used only to get a new pair (step 4). The sign-in it belongs to lasts refresh_expires_in seconds (30 days) at most, however often it is refreshed; then the user signs in again.
Lang
Text
Authorization: Bearer 5b0e1c9f7a3d...
  • No Authorization header: the request runs as anonymous. me is null, reads show what anonymous callers may see, and writes that need a user fail with UNAUTHENTICATED.

  • A bearer token that doesn't name a live sign-in is refused, never read as anonymous: HTTP 401, in both shapes, so a REST caller and a GraphQL client each find what they look for:

    Lang
    JSON
    {"type": "token-expired", "reason": "token-expired",
     "message": "The access token has expired: refresh it",
     "errors": [{"message": "The access token has expired: refresh it",
                 "extensions": {"code": "UNAUTHENTICATED", "reason": "token-expired"}}]}

    token-expired: the access token is past its 15 minutes. Refresh (step 4) and send the request again. invalid-token: the sign-in has ended (signed out, revoked, past its 30 days, or the token is unknown, or it is a refresh token): sign in again. The response also carries WWW-Authenticate: Bearer error="invalid_token".

  • Users belong to one tenant. The same email in another tenant is a different user with a different id.

  • Long-lived tokens are not for apps' users. There are no service tokens yet: a server that talks to october on its own behalf, with no user's request to act for, signs in as an app user of its own and works under that user's permissions.

4. Refresh#

Lang
Text
POST /runtime/auth/refresh
{"refresh_token": "c061522ba3eb..."}

The answer is a new pair, in the same shape as step 2's. The refresh token you sent is now spent: keep the new one. Refresh when the access token is about to expire, or on a token-expired 401.

  • A refresh token works once. Sent again within 30 seconds of its use, while the pair it got hasn't been used itself, it answers with that same pair: the same client asking twice (an answer lost on the network, two tabs at once). Any other second use is taken as a stolen token: the answer is 401 {"type":"invalid-token","reason":"reused"}, and the whole sign-in ends, every token of it, its live streams included.
  • Other refusals are 401 invalid-token with reason revoked (signed out), expired (past its 30 days) or unknown. Sign in again.
  • An access token from before a refresh keeps working until it expires.
  • A token issued before october had refresh (a single refresh_token in user) no longer works: it gets 401 invalid-token, and the user signs in again.

5. Sign out#

Lang
Text
POST /runtime/signout
Authorization: Bearer 5b0e1c9f7a3d...

or with the refresh token in the body: {"refresh_token": "c061522ba3eb..."}. The answer is always {"ok": true}. Every token of that sign-in stops working at once (an expired access token is enough to sign out), and live streams opened with it are closed.

In the browser#

The client does all of this, refreshes included. By hand:

Lang
JavaScript
const API = 'https://october.sombra.so/t/shop';   // an allowed origin (api.md, CORS); '/t/shop' behind a same-origin proxy

async function post(path, body, token) {
  const headers = {'content-type': 'application/json'};
  if (token) headers.authorization = 'Bearer ' + token;
  const res = await fetch(API + path, {method: 'POST', headers, body: JSON.stringify(body)});
  return res.json();
}

const sent = await post('/runtime/auth/send_magic_code', {email});
// In demo mode use sent.demo_code; otherwise ask the user for the code.
const pair = await post('/runtime/auth/verify_magic_code', {email, code});
localStorage.setItem('session', JSON.stringify(pair));

// the access token, refreshed when it says it expired
async function call(path, body) {
  let pair = JSON.parse(localStorage.getItem('session') || 'null');
  let res = await post(path, body, pair?.access_token);
  if (res.type === 'token-expired') {
    pair = await post('/runtime/auth/refresh', {refresh_token: pair.refresh_token});
    if (!pair.access_token) { localStorage.removeItem('session'); throw new Error('signed out'); }
    localStorage.setItem('session', JSON.stringify(pair));
    res = await post(path, body, pair.access_token);
  }
  return res;
}

Two tabs refreshing at once is safe (the second gets the same pair), but a real app refreshes once for all its requests at a time.