Skip to content

Javascript API

S. M. Catala edited this page Jun 10, 2016 · 5 revisions

Javascript API

WARNING: preliminary specification draft, work-in-progress, subject to breaking changes.

The CryptoBox API can be conceptually split in

  • an access control API, provided by the Cryptoboxes interface,
  • and a reactive I/O API, provided by the CryptoBox interface.

Index

Example usage

import { Observable } from 'rxjs/Observable'
import cb from 'cryptobox'

const CRYPTOBOXES = cb({
  url: 'cryptobox.mydomain.com/admin',
  agent: 'user agent identifier'
})

// called when user submits signup form with verified passphrase
function onSignup (credentials) {
  CRYPTOBOXES.create(credentials)
  .then(cb => {
    console.log('registration pending email verification')
    console.log('identification string: ', CRYPTOBOXES.info().id)
  })
  .catch(err => console.log(err))
}

// called when user submits login form
function onLogin (credentials) {
  CRYPTOBOXES.access(credentials)
  .then(cb => {
    if (cb.info().secret) { // first login from new agent
      console.log('new agent pending authorization')
      console.log('agent identification string: ', CRYPTOBOXES.info().id)
      console.log('authorization secret: ', cb.info().secret)
    } else { // successfully logged in
      cb.read(Observable.of({}))  // empty range => fetch all docs
      .subscribe(docs => console.log(docs))
    }
  })
  .catch(err => console.log(err))
}

cryptobox module

Description

The default import of the cryptobox module is a function that takes a global configuration object argument and returns an object instance of the Cryptoboxes Interface. The supplied configuration object defines the URL of the cryptobox server API and the identification string of the calling user agent. Other properties of the configuration object are TBD.

Syntax

require('cryptobox')(config: object, deps: object): Cryptoboxes

Parameters

config: object

mandatory factory configuration object, with the following mandatory properties:

  • url: string URL of the cryptobox administration server API. Protocol defaults to https which is the only accepted possibility.

the config object may also include the following optional properties:

  • agent: string identification string of the calling user agent. defaults to a user agent signature based on available web APIs, or to a random UUID.
  • TBD. eg. alternate PouchDB instance, prefix for PouchDB database names, sync options, email service, delay for automatic logout, API version string, etc.
deps: object

optional object for injecting the following dependencies: TBD

Return value

Cryptoboxes

An object instance that implements the Cryptoboxes interface.

Exceptions

  • invalid argument when the config object is missing or not of type {url: string, agent: string}.

Example

import cb from 'cryptobox'

const CRYPTOBOXES = cb({
  url: 'cryptobox.mydomain.com/admin',
  agent: 'user agent identifier'
})

Cryptoboxes interface

Description

The Cryptoboxes interface provides control access methods to

  • create new CryptoBox instances: Cryptoboxes.create(),
  • retrieve existing CryptoBox instances: Cryptoboxes.access().

The Cryptoboxes interface also includes an info() method which returns a copy of the configuration data it was created with.

Implementations of the Cryptoboxes interface transparently manage the administration server API:

  • for creating a new cryptobox account,
  • for accessing an existing cryptobox for the first time from a new user agent.

These procedures specifically require two successive calls to this interface:

  • Cryptoboxes.create() or Cryptoboxes.access(): the first method call respectively requests a new account or authorization of a new user agent,
  • Cryptoboxes.access(): the second method call eventually retrieves the requested cryptobox and should be called after successful email verification (new account) or user agent authorization.

If the user agent is already registered and authorized for access to the user account, then only the second call is required to retrieve the corresponding cryptobox.

Access control is managed exclusively through the Cryptoboxes methods, which invariably require valid credentials to return a CryptoBox instance.

Regardless of successful authentication with valid credentials, returned CryptoBox instances can nonetheless eventually be locked, i.e. their methods systematically raise an UNAUTHORIZED error:

  • when the CryptoBox is pending email verification or user agent authorization,
  • when access authorization has automatically expired.

The only way to refresh authorization and unlock a CryptoBox instance is by eventually calling the Cryptoboxes.access() method anew, e.g. after successful user agent authorization.

Cryptoboxes instances may be considered immutable.

Methods

Cryptoboxes.create() method

Description

Signup for a new account with the administration server API for the given credentials and return a cryptobox pending email verification.

A CryptoBox pending email verification is locked, i.e. its methods systematically raise an UNAUTHORIZED error. It can be unlocked by calling Cryptoboxes.access() with the same credentials once the email has been successfully verified.

This method requires network access to the cryptobox administration server API.

Syntax

Cryptoboxes.create(credentials: object, options?: object): Promise<CryptoBox>

Parameters

credentials: object

The credentials object should include the following properties:

  • id: string the user's id, e.g. email
  • secret: string the user's secret passphrase. the passphrase should be verified before calling this method, i.e. it would typically have to be entered twice by the user.
options?: object

ignored in the current version of the API.

Return value

Promise<CryptoBox>

if the Promise resolves successfully, the returned CryptoBox is pending email verification and locked.

Exceptions

  • UNAUTHORIZED if a valid account already exists for the given email.
  • NETWORK_ERROR

example

CRYPTOBOXES.create(credentials)
.then(cb => {
  console.log('registration pending email verification')
  console.log('identification string: ', CRYPTOBOXES.info().id)
})
.catch(err => console.log(err))

Cryptoboxes.access() method

Description

return an existing CryptoBox for the given email.

  • if this is the first access from the calling user agent, i.e. the user agent has not yet been authorized, the returned CryptoBox is pending authorization from an authorized user agent.
  • otherwise, if online, a connection is automatically set up with the corresponding couchDB instance, and replication is initiated.

When the returned CryptoBox is pending authorization, it is locked, i.e. its methods raise an UNAUTHORIZED error. It can be unlocked by calling Cryptoboxes.access() again with the same credentials once the calling user agent has been successfully authorized.

This method only requires network access to the cryptobox administration server API upon first access from a new user agent not yet authorized.

Syntax

Cryptoboxes.access(credentials: object, options?: object): Promise<CryptoBox>

Parameters

credentials: object

The credentials object should include the following properties:

  • id: string the user's id, e.g. email
  • secret: string the user's secret passphrase.
options?: object

ignored in the current version of the API.

Return value

Promise<CryptoBox>

Exceptions

  • CRYPTOBOX_NOT_FOUND_ERROR if no valid cryptobox account exists for the given email, or if the given credentials are not valid.
  • NETWORK_ERROR

Example

CRYPTOBOXES.access(credentials)
.then(cb => {
  if (cb.info().secret) { // first login from new agent
    console.log('new agent pending authorization')
    console.log('agent identification string: ', CRYPTOBOXES.info().id)
    console.log('authorization secret: ', cb.info().secret)
  } else { // successfully logged in
    cb.read(Observable.of({})) // empty range => fetch all docs
    .subscribe(docs => console.log(docs))
  }
})
.catch(err => console.log(err))

Cryptoboxes.config object

Description

an immutable copy of the configuration data the Cryptoboxes instance was created with.

Syntax

Cryptoboxes.config: { url: string, agent: string }

Parameters

none

Return value

object

a copy of the configuration data the Cryptoboxes instance was created with.

CryptoBox Interface

Description

The CryptoBox interface primarily provides reactive I/O methods to read and write data from/to implementing instances.

Methods

CryptoBox.read() method

Description

Reactively fetch documents from the CryptoBox, i.e. this method is a reactive operator that takes an Observable sequence of document references as input, and outputs a corresponding Observable sequence of documents, similar to a through stream that maps an input stream to an output stream.

A document reference can be an individual reference or a set of references, which are any of the following:

  • a document _id string,
  • a reference object with _id and _rev strings,
  • a range object with first and/or last _id string,

By default, for document references that do not specify a _rev string, _rev defaults to the current revision of the referenced document.

This method raises an UNAUTHORIZED error when the CryptoBox is locked.

Syntax

CryptoBox.read(refs: Observable<Ref|Set<Ref>>, options?: object): Observable<Doc|Set<Doc>>

Parameters

refs: Observable<Ref|Set<Ref>>

an Observable sequence of individual Ref instances or of collections of Ref instances.

a Ref object is either a _id string, or a reference object with both an _id and a _rev string, or a range object with a first and last _id string.

options?: object

ignored in the current version of the API.

Return value

Observable<Doc|Set<Doc>>

an Observable sequence of individual Doc instances or of collections of Doc instances, as referenced by the input Observable sequence. the returned revision of each document defaults to the current revision if not specified by the input reference.

Exceptions

  • INVALID_REFERENCE when the input Observable supplies an element that is not a Ref instance or a collection of Ref instances
  • UNAUTHORIZED when the Cryptobox is locked

example

retrieve all documents

cb.read(Observable.of({})) // empty range => fetch all docs
.subscribe(docs => console.log(docs))

CryptoBox.write() method

Description

Reactively store documents into the CryptoBox, i.e. this method is a reactive operator that takes an Observable sequence of documents as input, and outputs a corresponding Observable sequence of document references of the successfully stored documents, similar to a through stream that maps an input stream to an output stream.

The output document references are the reference objects with _id and _rev strings of the successfully stored documents, and/or error objects for the documents that could not be stored.

_id UUID strings are automatically generated for Doc objects that do not have an _id property.

This method raises an UNAUTHORIZED error when the CryptoBox is locked.

Syntax

CryptoBox.write(docs: Observable<Doc|Set<Doc>>, options?: object): Observable<Res|Set<Res>>

Parameters

Observable<Doc|Set<Doc>>

an Observable sequence of individual Doc instances or of collections of Doc instances.

_id UUID strings are automatically generated for Doc objects that do not have an _id property.

options?: object

ignored in the current version of the API.

Return value

refs: Observable<Res|Set<Res>>

an Observable sequence of individual Res instances or of collections of Res instances.

a Res object is either a reference object with _id and _rev strings of the successfully stored document, or an error object if the document could not be stored.

Exceptions

  • UNAUTHORIZED when the Cryptobox is locked
  • UNSUCCESSFUL when some documents could not be successfully stored

example

delete all documents

cb.write(cb
  .read(Observable.of({})) // empty range => fetch all docs
  .map(docs => docs.map(doc => {
    doc._delete = true
    return doc
  }))
)
.subscribe(res => console.log(res))

CryptoBox.channel() method

Description

Syntax

CryptoBox.channel():

Parameters

Return value

Exceptions

example

CryptoBox.info() method

Description

Syntax

CryptoBox.():

Parameters

Return value

Exceptions

example

Clone this wiki locally