Permalink
Fetching contributors…
Cannot retrieve contributors at this time
138 lines (94 sloc) 5.67 KB

@cogitojs/telepath-js

Provides a secure channel for communication between a web app running in a browser and an app running on a mobile device.

Setting up a secure channel between the mobile device and a browser is done using a QR code that is presented by the web app whenever a secure channel is required. The user can then open the secure channel by scanning the QR code using the app on the phone.

Description

Telepath consists of a javascript library that can be used in web apps, and an iOS library for mobile apps.

Because both the browser and phone are likely to be behind distinct NAT, we use a service with a public ip address to facilitate peer-to-peer communication between them. This is a fairly simple service that only holds end-to-end encrypted messages in queues.

The channel supports bi-directional communication; both from the web app to the mobile app, and vice-versa. A channel therefore consists of two queues, which we’ve named ‘blue’ and ‘red’. The red queue is used to send messages from the web app to the mobile app, and the blue queue is used for the reverse route.

-------------                             ------------
|    web    |  ---------- red ----------> |  mobile  |
|    app    |  <--------- blue ---------- |    app   |
-------------                             ------------

Setting up a secure channel is done using these steps:

  1. The web app requests a secure connection to the identity app by invoking the createChannel function on the javascript library.
  2. The createChannel function generates a random channel id I and a symmetric encryption key E.
  3. The web app displays a QR code containing the channel id I and key E.
  4. The owner of the phone opens the app, points the camera to the QR code.
  5. The phone app extracts the channel id and the key from the QR code.
  6. Both phone and web app can now communicate on channel I. They encrypt/decrypt their messages using key E.

QR Code

The channel id I and key E are first encoded in a URL, and then the URL is encoded in a QR code. This allows a user to scan the QR code using the standard camera app in iOS and be directed to the telepath-enabled mobile app.

An example of such a URL and its QR Code:

https://example.com/telepath/connect#I=2oxSJ6_eyP7JXsn6VK7ooB_u&E=m8JzVbVlEwlzzR0-o8-AU0F6oONYcqvLW5YVLvLLP6s

Example QR Code

The URL is made up of the following components:

<base url>telepath/connect#I=<channel id>&E=<encryption key>

where:

  • <base url> is the url that is registered to open the mobile app in iOS or Android
  • <channel id> is the channel id string, percent encoded for use in a URL fragment
  • <encryption key> is the encryption key, base64url encoded

Usage

Add @cogitojs/telepath-js as a dependency:

$ yarn add @cogitojs/telepath-js

Then import Telepath in your own module:

import { Telepath } from '@cogitojs/telepath-js'

Creating an instance of Telepath

You create an instance of Telepath by providing the URL of the queuing service to the Telepath class:

const telepath = new Telepath('https://queuing.example.com')

Having an instance of Telepath, you can create a new channel with a random id and a random encryption key as follows:

const channel = await telepath.createChannel({ appName: 'My Web App' })

If you do not want the channel with a random id and a key, but rather recreate the channel with the previously obtained id and the key, you can provide them as additional attributes in the object passed to the createChannel method:

const channel = await this.telepath.createChannel({
  id: channelId,
  key: channelKey,
  appName: 'My Web App'
})

Obtaining a connection URL

At some point, you will need to create a QR code so that you can scan with a mobile app and obtain the channel identifier and the symmetric key as described above. Telepath provides a convenience method that given the base url (see Section QR Code above) it return a properly formatted connection URL. You can use this connection URL as an input to your QR code generation library. The resulting QR code can be scanned by the mobile app and used to connect to this channel. The telepath library does not include functionality for displaying QR codes, you can use a QR code component such as qrcode.react for this purpose.

const connectUrl = channel.createConnectUrl('https://example.com')
// returns: https://example.com/telepath/connect#I=<channelID>&E=<symmetricKey>

Sending messages through the channel

Messages are exchanged using JSON-RPC:

const request = { jsonrpc: '2.0', method:'test', id: 1 }
const response = await channel.send(request)

The send method returns a promise. The queuing service will be polled for a response for at least 10 minutes. If no response is available after this time it will return null.

Known Limitations

Currently uses independent encryption of messages. A recipient can therefore not detect if some messages have been duplicated, deleted or reordered.

See also

Telepath often comes hand in hand with Cogito Web3.