Skip to main content

Encryption and your users

Markdown

What your app sends through WordPlus Cloud is sealed on the way: the realtime servers carry it, keep the history of a room and count its members without being able to read or change any of it. You write no encryption code. Your server holds the keys, gives each user the ones they may have, and the page opens everything before your listener sees it.

What is sealed​

WhatSealed withWho can open it
Events your server sends to a room, and events pages send to itthe room keywhoever the room rule lets in
The history of a roomthe room key, as each event was sentthe same
The info of a presence memberthe room keythe same
What toUser() sendsthe key of the userthat user
What the people in a call or a group call see of each otherthe day keyeveryone your app knows
Your users in the directory (name, avatar …)the day keyeveryone your app knows

A room with public: true is not sealed, unless it says seal: true: anyone with your app key may join it. A sealed room may say seal: false too, for data nobody needs to hide. The realtime servers see room names, event names, user ids, which key a value needs, and sealed values. They never hold a key.

On the left, what WordPlus Cloud carried of the update of an order, as the page of Ann shows it: the room and the event name in plain text, its data a sealed string, then what her page opened. On the right, the browser of Ben, which opened it too, showing his order as delivered

Calls and live streams​

WhatEncrypted end to end
A call between two peopleyes, on every path: direct between the two, through our relays, or through our media servers
A group call that comes from a call, when someone is addedyes
A group call your server names (groupCalls())when its rule says encrypt: true
A live streamno: its picture and its chat are for everyone watching, so they are encrypted on the way, and the media servers see them

An encrypted call is encrypted in the browser, with a key your server makes from its local key and gives only to the people in the call: our relays and media servers carry what they cannot read (calls, live streams).

Your local key​

WORDPLUS_LOCAL_KEY, 64 hex characters you make (openssl rand -hex 32), is the master every key is derived from: HMAC-SHA256(local key, "wordplus-realtime key v1|" + label). Keep it:

  • the same on every server of the app, and in every language: the Node, PHP and Python packages derive the same keys from it.
  • as long as the app: a new one means pages holding old keys cannot open what comes, and the history of a room cannot be read again.
  • out of every page and app, as the secret.

It also makes the key of each call between two people, so WordPlus Cloud can read neither how its two pages connect nor its media, and the media key of a group call that is encrypted end to end (calls and live streams).

Who gets which key​

KeyLabelGiven to
The day keyd:{day}every user your server knows, for yesterday, today and tomorrow
The key of a useru:{id}:{generation}that user
The key of a roomch:{room}:{generation}whoever the room rule lets in, with the room grant
Your own kind{kind}:{…}whoever your kind() callback says

The page takes keys from your server only (its settings, the grant of a room, or your keys route), never from the realtime servers. hasKeys: ( user ) => false (PHP 'hasKeys', Python has_keys=) cuts a user off.

Signing out​

A single-page app calls rt.close() when its user signs out, before another user signs in on the same page: it closes the connection and forgets the keys of the page.

When someone loses access​

Kicking a user out of a room moves its key on by itself. To do it without a kick, or for the key of a user:

await realtime.rotateRoom( 'order:42' );   // what the room carries from now on is sealed from them
await realtime.rotateUser( userId ); // their own key moves on

Rotating needs somewhere to keep the key generations, shared by your servers, and kept for good:

createRealtime( { …, generations: {
get: async ( name ) => Number( await redis.get( 'rt-gen:' + name ) ) || null,
set: async ( name, n ) => void ( await redis.set( 'rt-gen:' + name, n ) ),
} } );

Those still let in get the new key from your server with the next grant of the room, or when its first event comes. The history kept before stays sealed with the key it was sent with, which they hold too.

Your own sealed values​

A kind of key of your own is named after what it seals: note:12 for note 12. A page that lacks the key asks your keys route, and a rule answers: a function your server declares once for the kind note, given the rest of the name, 12. This rule says:

  1. Visitors who are not signed in get no key.
  2. Those who may read the note get its key, and nobody else.
realtime.kind( 'note', async ( rest, user ) => {
// 1. Visitors get no key.
if ( user === null ) return false;

// 2. Those who may read note 12, and nobody else.
return mayRead( user, rest );
} );

const sealed = await realtime.keys.seal( JSON.stringify( note ), 'note:' + note.id ); // send it any way you like

In the browser:

import { keyring, labelOf } from '@wordplus/realtime';
await keyring().ensure( [ labelOf( sealed ) ] ); // asks your keys route for `note:…` if the page lacks it
const note = keyring().openJson( sealed ); // undefined when this user may not have it

Your users​

A directory of your users on WordPlus Cloud, each profile sealed with the day key, shared by every page of your app:

const users = await rt.users( [ 34, 42 ] );   // { 34: { user_id: 34, name: 'Ann', avatar: '…' }, 42: { … } }
  • A page vouches for its user when it connects, with their identity and profile signed by your server (your profile callback). It may then read those of others.
  • A user is in it once they connect, for a day after they last did. A page that sees someone come online (who is online) may ask again.
  • Every name and picture the ready-made parts show comes from it: the chat senders, the list of who is online, the call screen caller.
  • Any user id your app gives: numbers, and ids of text such as UUIDs or Auth0 ids.