3.2.10 • Published 2 years ago

@xomicloud/xomi v3.2.10

Weekly downloads
-
License
MIT, Anti-996
Repository
github
Last release
2 years ago

Xomi

Secure functionality for Xomi. This is divided up as follows:

This readme file contains installation instructions and usage examples. The tutorials and knowledge base articles referenced throughout may provide a friendlier introduction, however, and they are collected in the related links section near the end.

Installation

You can install Xomi with npm:

npm install @xomicloud/xomi

You can also clone the repository with Git...

git clone https://github.com/xomicloud/xomi.git

...and then install the dependencies with npm from within the project's root directory:

npm install

You only need to do this if you are interested in Xomi's development, however.

Usage

All of the functions take a plain old JavaScript object as their first configuration argument, the properties of which must be the client configuration. If you are using the standard set of environment variables then the following will return the required object:

"use strict";

const { CLIENT_HOST, CLIENT_ID, REDIRECT_URI, CLIENT_SECRET } = process.env,
      clientId = CLIENT_ID, ///
      clientHost = CLIENT_HOST, ///
      redirectURI = REDIRECT_URI, ///
      clientSecret = CLIENT_SECRET, ///
      configuration = {
        clientId,
        clientHost,
        redirectURI,
        clientSecret
      }; 

module.exports = configuration;

You invoke the functions as follows:

const { oAuth, cookie, account } = require("@xomicloud/xomi");

const configuration = require("./configuration");

account(configuration, ...) // Retrieve account ifformation

oAuth.callback(configuration, ...); //  Make an OAuth callback

cookie.setAuthenticationCookie(configuration, ...)  //  Set an authentication cookie

Some package exports, such as aacount, are single functions. Others, such as oAuth, are collections of functions. Note the use of the aforementioned configuration argument which is assumed to be defined as above.

OAuth

  • redirect()
  • callback()

These functions will redirect the browser to the Xomi authentication site and handle the subsequent callback, respectively. Usage examples can be found in the JavaScript secure application repository and further information can be found in The Anatomy of a Secure Application knowledge base article.

  • The redirect() function takes configuration and response arguments. The response is expected to be an instance of Node's ServerResponse class. You can also pass a third, optional createAccount argument that, if set to true, instructs the authentication site to show the form to create an account rather than the sign up form.
const { oAuth } = require("@xomicloud/xomi");

const configuration = require("./configuration");

function signInHandler(request, response, next) {
  const createAccount = false;

  oAuth.redirect(configuration, response, createAccount);
}
  • The callback() function takes configuration, code and callback() function arguments. The code argument is the code returned by the authentication site when the user successfully authenticates and can be recovered from the request object, assuming it is an instance of Node's IncomingMessage class. The callback function should accept an error argument together witih accessToken, refreshToken and identityToken arguments.
const { oAuth, cookie } = require("@xomicloud/xomi");

const configuration = require("./configuration");

function callbackHandler(request, response, next) {
  const { query } = request,
        { code } = query;

  oAuth.callback(configuration, code, (error, accessToken, refreshToken, identityToken) => {
    ///
  });
}

Cookies

  • setAuthenticationCookie()
  • removeAuthenticationCookie()
  • isAuthenticationCookiePresent()
  • getAccessTokenFromAuthenticationCookie()
  • getIdentityTokenFromAuthenticationCookie()

These functions supply basic authentication cookie functionality. Usage examples can again be found in the JavaScript secure application repository and further information in The Anatomy of a Secure Application knowledge base article.

  • The setAuthenticationCookie() function takes configuration, response, accessToken and identityToken arguments. It also takes an optional rememberMe argument which, if set to true, sets the expiry of the cookie well into the future. The response is expected to be an instance of Node's ServerResponse class.
const { oAuth, cookie } = require("@xomicloud/xomi");

const configuration = require("./configuration");

function callbackHandler(request, response, next) {
  const { query } = request,
        { code } = query;

  oAuth.callback(configuration, code, (error, accessToken, refreshToken, identityToken) => {
    ///

    const { remember_me } = query,
          rememberMe = !!remember_me;

    cookie.setAuthenticationCookie(configuration, response, accessToken, identityToken, rememberMe);

    ///
  });
}

Note that the refresh token returned from the oAuth.callback() function, which will likely be null, is not saved in the authentication cookie.

  • The removeAuthenticationCookie() function takes configuration and response arguments.
const { cookie, oAuth } = require("@xomicloud/xomi");

const configuration = require("./configuration");

function signOutHandler(request, response, next) {
  cookie.removeAuthenticationCookie(configuration, response);

  oAuth.redirect(configuration, response);
}
  • The isAuthenticationCookiePresent() function takes configuration and response arguments.
const { oAuth, cookie } = require("@xomicloud/xomi");

const configuration = require("./configuration");

function homePageHandler(request, response, next) {
  const authenticationCookiePresent = cookie.isAuthenticationCookiePresent(configuration, request);

  if (!authenticationCookiePresent) {
    oAuth.redirect(configuration, response);

    return;
  }

  ///
}
  • The getAccessTokenFromAuthenticationCookie() function takes configuration and response arguments. It returns an access token if the authentication cookie exists and can be parsed, null otherwise.

  • The getIdentityTokenFromAuthenticationCookie() function similarly takes configuration and response arguments. It returns an identity token if the authentication cookie exists and can be parsed, null otherwise.

Integrations

  • api()

There is only one function that connects to Xomi's API server. Only an outline of its usage is given here. More detailed information can be found in the Dropbox integration tutorial.

  • The api() function takes configuration, request and response arguments:

const { api } = require("@xomicloud/xomi");

api(configuration, request, response);

In this instance the request and response objects do not have to be instances of Node's IncomingMessage and ServerResponse classes and can be hand rolled. Again, see the tutorial for more details.

Authentication

  • signIn()
  • createAccount()
  • resetPassword()

There are three functions relating to managing accounts. Together they provide an alternative to the usual browser based OAuth flow for authentication. For more information see the Ajax Account Management tutorial.

  • The signIn() function takes configuration, emailAddressOrUsername, password and callback arguments:
const { authenticate } = require("@xomicloud/xomi");

const configuration = require("./configuration");

authenticate.signIn(configuration, emailAddressOrUsername, password, (error, accessToken, identityToken) => {
  if (error) {
    ///
    
    return;
  }
  
  ///
}));

The function will invoke the callback function you provide with an error code and, if successful, an access token and an identity token.

  • The createAccount() function takes configuration, emailAddress, username, password and callback arguments. The username argument can be null if usernames are not needed:
const { authenticate } = require("@xomicloud/xomi");

const configuration = require("./configuration");

authenticate.createAccount(configuration, emailAddress, username, password, (error, accessToken, identityToken) => {
  if (error) {
    ///
    
    return;
  }
  
  ///
}));

The function will invoke the callback function you provide with an error code and, if successful, an access token and an identity token.

  • The restePassword() function takes configuration, emailAddress and callback arguments:
const { authenticate } = require("@xomicloud/xomi");

const configuration = require("./configuration");

authenticate.resetPassword(configuration, emailAddress, (error) => {
  if (error) {
    ///
    
    return;
  }
  
  ///
}));

If the email address corresponds to an user'a account then an email will be sent. The function additionally will invoke the callback function you provide with an error code but does not indicate whether or not an email was sent for security reasons.

Account information

  • account()

Again there is only one function. It provides user information in exchange for their identity token.

  • The account() function takes configuration, identityToken and callback arguments:
const { account } = require("@xomicloud/xomi");

const configuration = require("./configuration");

account(configuration, identityToken, (error, account) => {
  if (error) {
    ///
    
    return;
  }
  
  const { username, email_address } = account;
  
  ///
}));

The function will invoke the callback function you provide with an error code and, provided you are authorized, a plain old JavaScript object with the user's email address and username.

Related links

Contact

  • james.smith@djalbat.com
3.2.2

2 years ago

3.2.0

2 years ago

3.2.6

2 years ago

3.2.5

2 years ago

3.2.4

2 years ago

3.2.3

2 years ago

3.0.67

2 years ago

3.0.69

2 years ago

3.2.9

2 years ago

3.2.8

2 years ago

3.2.7

2 years ago

3.1.3

2 years ago

3.1.2

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.1.7

2 years ago

3.1.5

2 years ago

3.1.4

2 years ago

3.0.70

2 years ago

3.0.71

2 years ago

3.2.10

2 years ago

3.1.9

2 years ago

3.0.12

3 years ago

3.0.13

3 years ago

3.0.10

3 years ago

3.0.11

3 years ago

3.0.16

3 years ago

3.0.17

3 years ago

3.0.14

3 years ago

3.0.15

3 years ago

3.0.65

2 years ago

3.0.66

2 years ago

3.0.60

2 years ago

3.0.63

2 years ago

3.0.61

2 years ago

3.0.45

2 years ago

3.0.46

2 years ago

3.0.43

2 years ago

3.0.44

2 years ago

3.0.49

2 years ago

3.0.48

2 years ago

3.0.41

2 years ago

3.0.42

2 years ago

3.0.40

2 years ago

3.0.56

2 years ago

3.0.57

2 years ago

3.0.54

2 years ago

3.0.55

2 years ago

3.0.8

3 years ago

3.0.7

3 years ago

3.0.58

2 years ago

3.0.59

2 years ago

3.0.52

2 years ago

3.0.53

2 years ago

3.0.50

2 years ago

3.0.51

2 years ago

3.0.23

3 years ago

3.0.24

3 years ago

3.0.21

3 years ago

3.0.22

3 years ago

3.0.27

3 years ago

3.0.28

2 years ago

3.0.25

3 years ago

3.0.26

3 years ago

3.0.20

3 years ago

3.0.18

3 years ago

3.0.19

3 years ago

3.0.9

3 years ago

3.0.34

2 years ago

3.0.35

2 years ago

3.0.32

2 years ago

3.0.33

2 years ago

3.0.38

2 years ago

3.0.39

2 years ago

3.0.36

2 years ago

3.0.37

2 years ago

3.0.30

2 years ago

3.0.31

2 years ago

3.0.29

2 years ago

2.0.3

3 years ago

2.0.5

3 years ago

2.0.7

3 years ago

2.0.9

3 years ago

2.0.8

3 years ago

1.3.17

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

3.0.4

3 years ago

1.3.19

3 years ago

3.0.6

3 years ago

3.0.5

3 years ago

3.0.0

3 years ago

1.3.20

3 years ago

1.3.16

3 years ago

1.3.14

3 years ago

1.3.15

3 years ago

1.2.13

3 years ago

1.3.10

3 years ago

1.3.13

3 years ago

1.3.11

3 years ago

1.3.12

3 years ago

1.3.9

3 years ago

1.3.8

3 years ago

1.3.7

3 years ago

1.2.8

3 years ago

1.3.6

3 years ago

1.2.7

3 years ago

1.3.5

3 years ago

1.3.4

3 years ago

1.3.3

3 years ago

1.3.2

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.9

3 years ago

1.2.12

3 years ago

1.2.10

3 years ago

1.2.11

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago