@onehilltech/blueprint-gatekeeper
Authentication framework for Web application programming interfaces (APIs)
Last updated 23 days ago by onehilltech .
Apache-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @onehilltech/blueprint-gatekeeper 
SYNC missed versions from official npm registry.

Gatekeeper

OAuth 2.0 token server and module for Blueprint.js

npm version Build Status Dependencies Coverage Status

Installation

yarn add @onehilltech/blueprint-gatekeeper

or

npm install @onehilltech/blueprint-gatekeeper --save

Getting Started

Defining the configuration

Gatekeeper uses JSON Web Tokens to manage authorization of authenticated users. To get started, you must first configure the options for token generation. The options supported are the same as those in node-jsonwebtoken.

// app/configs/gatekeeper.js

module.exports = {
 tokens: {
     // This is the base options for all token generators.
     $: {
       issuer: '[your-issuer-name-here]',
       expiresIn: '1h',
       algorithm: 'HS256',
       secret: 'ssshhh'
     }
 },
};

The jwtid option is not supported since Gatekeeper uses it to generate a unique id for each token.

Mount Gatekeeper router endpoint

Next, we need to import (or mount) the Gatekeeper router into our application. This will expose routes for managing and authenticating accounts and clients.

// app/routers/endpoint.js

const blueprint = require ('@onehilltech/blueprint');
const { Router } = blueprint;

module.exports = Router.extend ({
  specification: {
    '/gatekeeper': blueprint.mount ('@onehilltech/blueprint-gatekeeper:v1')    
  }
});

In the example above, we are mounting the v1 router to the /gatekeeper endpoint. This means that all routers in v1 will be accessible at http://[hostname]/gatekeeper/.

Protecting routes

The last step is to define what routes require authorization (i.e., are protected) using the gatekeeper.auth.bearer Blueprint policy.

// app/routers/endpoint.js

const blueprint = require ('@onehilltech/blueprint');
const { Router } = blueprint;

module.exports = Router.extend ({
  specification: {
    '/gatekeeper': blueprint.mount ('@onehilltech/blueprint-gatekeeper:v1'),
    
    // Let's protect the /v1 routes.
    '/v1': {
      policy: 'gatekeeper.auth.bearer'
    }  
  }
});

In the example above, the router will protect all routes under the /v1 path, which also includes all routers located in app/routers/v1 directory. The client will need to define the Authorization header and include a generated token.

Accessing the Authorized User

The req.user property contains the account models for an authorized user making the request to access a protected route. For example, here is an example of setting the user making the request as the owner of a created resource.

const { models } = require ('@onehilltech/blueprint');
const { ResourceController } = require ('@onehilltech/blueprint-mongodb');

module.exports = ResourceController.extend ({
  Model: models ('tweet'),
  
  create () {
    return this._super (this, ...arguments).extend ({
      prepareDocument (req, doc) {
        // Make the authorized user the owner of the created resource.
        doc.user = req.user._id;
        return doc;
      }
    });
  }
});

Gatekeeper has a UserResourceController that automatically adds the authorized user making the request as the owner of the resource being created.

Cross-Origin Resource Sharing (CORS)

Cross-Origin Resource Sharing (CORS) is a mechanism that allows a client from one domain to access resources from another domain. This typically occurs when you have a web browser accessing an API protected by Gatekeeper. If you need to enable CORS support for your Blueprint application, then use the gatekeeper.cors() middleware:

// app/routes/v1.js

const { env } = require ('@onehilltech/blueprint');
const { cors } = require ('@onehilltech/blueprint-gatekeeper');

module.exports = {
  '/v1': {
    use: [cors ({
      origin: env !== 'production' ? true : null
    })]
  }
};

Now, any request for a route that begins with /v1 will support CORS. The gatekeeper.cors() middleware is a wrapper around Express CORS. It will check if the origin in the request matches any registered client only when running in production. All other times, CORS is enabled by default regardless of the origin in the request.

Third-party Authentication

Third-party authentication is when you use another service to authenticate the user, and then issues an access token for authenticated user on the host system/service. For example, you use Facebook to authenticate a user and then issue an access token from the host service for the authenticated user. The rationale for such capabilities is you want to use the host service authorization mechanism to control access, but do not want to require users to manage authentication (i.e., username and passwords) on either system.

To account for this scenario, Gatekeeper provides a session service that can be used to issue access tokens for accounts/users against a client. It is the host system's responsibility to ensure the generated access tokens are for authenticated users. To use the service, just inject the session service into your controller. Then, define an authenticate() method to authenticate a user using the third-party service. This means the user will submit their username and password to the host service, and the host service will forward the information along to the third-party service.

module.exports = Controller.extend ({
  /// Inject the session service into the controller.
  session: service (),
  
  authenticate () {
    return Action.extend ({
      execute (req, res) {
        return this.authenticateUsingThirdParty ();
          .then (result => {
            //
          });
      }
    });
  }
})

Once the user is authenticated, you then use information about the authenticated user to issue an access token. As shown in the example below, you will first use findAccountByEmail or findAccountByUsername to lookup the corresponding account for the user.

findAccountByEmail and findAccountByUsername does not create an account if it does not exist. If the account does not exist, you will have to create one.

Then use issueToken to issue a token for the account on the corresonding client. Lastly, use serializeToken to convert the token from an object to a JSON object. The JSON object can then be sent as a response to the client.

module.exports = Controller.extend ({
  /// Inject the session service into the controller.
  session: service (),
  
  authenticate () {
    return Action.extend ({
      execute (req, res) {
        const { client_id } = req.body;
        
        return this.authenticateUsingThirdParty (req)
          .then (result => this.session.findAccountByEmail (result.email))
          .then (account => this.session.issueToken (client_id, account))
          .then (token => this.session.serializeToken (token))
          .then (token => res.json (token));
      }
    });
  },
  
  authenticateUsingThirdParty () {
    // Perform the remote authentication.
  }
})

Gatekeeper Client Libraries

Next Steps

See the Wiki for more information.

Current Tags

  • 4.22.0                                ...           latest (23 days ago)

146 Versions

  • 4.22.0                                ...           23 days ago
  • 4.21.3                                ...           a month ago
  • 4.21.2                                ...           a month ago
  • 4.21.0                                ...           a month ago
  • 4.19.6                                ...           a month ago
  • 4.19.5                                ...           a month ago
  • 4.19.4                                ...           a month ago
  • 4.19.2                                ...           2 months ago
  • 4.19.1                                ...           2 months ago
  • 4.19.0                                ...           3 months ago
  • 4.18.3                                ...           3 months ago
  • 4.18.2                                ...           4 months ago
  • 4.18.1                                ...           4 months ago
  • 4.18.0                                ...           4 months ago
  • 4.17.0                                ...           4 months ago
  • 4.10.0                                ...           6 months ago
  • 4.8.0                                ...           6 months ago
  • 4.15.1                                ...           6 months ago
  • 4.14.1                                ...           6 months ago
  • 4.14.0                                ...           6 months ago
  • 4.12.0                                ...           6 months ago
  • 4.9.0                                ...           6 months ago
  • 4.7.0                                ...           6 months ago
  • 0.98.2                                ...           6 months ago
  • 0.98.1                                ...           7 months ago
  • 0.98.0                                ...           7 months ago
  • 0.97.1                                ...           8 months ago
  • 0.97.0                                ...           8 months ago
  • 0.96.6                                ...           8 months ago
  • 0.96.5                                ...           9 months ago
  • 0.96.4                                ...           9 months ago
  • 0.96.3                                ...           9 months ago
  • 0.96.2                                ...           9 months ago
  • 0.96.1                                ...           9 months ago
  • 0.96.0                                ...           9 months ago
  • 0.95.3                                ...           9 months ago
  • 0.95.2                                ...           10 months ago
  • 0.95.1                                ...           10 months ago
  • 0.95.0                                ...           10 months ago
  • 0.94.0                                ...           a year ago
  • 0.93.2                                ...           a year ago
  • 0.93.1                                ...           a year ago
  • 0.93.0                                ...           a year ago
  • 0.92.0                                ...           a year ago
  • 0.91.1                                ...           a year ago
  • 0.91.0                                ...           a year ago
  • 0.90.0                                ...           a year ago
  • 0.89.2                                ...           a year ago
  • 0.89.1                                ...           a year ago
  • 0.89.0                                ...           a year ago
  • 0.88.0                                ...           a year ago
  • 0.87.1                                ...           a year ago
  • 0.87.0                                ...           a year ago
  • 0.86.2                                ...           2 years ago
  • 0.86.1                                ...           2 years ago
  • 0.86.0                                ...           2 years ago
  • 0.85.2                                ...           2 years ago
  • 0.85.1                                ...           2 years ago
  • 0.85.0                                ...           2 years ago
  • 0.84.6                                ...           2 years ago
  • 0.84.5                                ...           2 years ago
  • 0.84.4                                ...           2 years ago
  • 0.84.3                                ...           2 years ago
  • 0.84.2                                ...           2 years ago
  • 0.84.1                                ...           2 years ago
  • 0.84.0                                ...           2 years ago
  • 0.83.2                                ...           2 years ago
  • 0.83.1                                ...           2 years ago
  • 0.83.0                                ...           2 years ago
  • 0.82.2                                ...           2 years ago
  • 0.82.1                                ...           2 years ago
  • 0.82.0                                ...           2 years ago
  • 0.81.1                                ...           2 years ago
  • 0.81.0                                ...           2 years ago
  • 0.80.7                                ...           2 years ago
  • 0.80.6                                ...           2 years ago
  • 0.80.5                                ...           2 years ago
  • 0.80.4                                ...           2 years ago
  • 0.80.3                                ...           2 years ago
  • 0.80.2                                ...           2 years ago
  • 0.80.1                                ...           2 years ago
  • 0.80.0                                ...           2 years ago
  • 0.79.2                                ...           2 years ago
  • 0.79.1                                ...           2 years ago
  • 0.79.0                                ...           2 years ago
  • 0.78.2                                ...           2 years ago
  • 0.78.1                                ...           2 years ago
  • 0.78.0                                ...           2 years ago
  • 0.77.5                                ...           2 years ago
  • 0.77.4                                ...           2 years ago
  • 0.77.3                                ...           2 years ago
  • 0.77.2                                ...           2 years ago
  • 0.77.1                                ...           2 years ago
  • 0.77.0                                ...           2 years ago
  • 0.76.0                                ...           2 years ago
  • 0.75.2                                ...           2 years ago
  • 0.75.1                                ...           2 years ago
  • 0.75.0                                ...           2 years ago
  • 0.74.0                                ...           2 years ago
  • 0.74.0-14                                ...           2 years ago
  • 0.74.0-13                                ...           2 years ago
  • 0.74.0-12                                ...           2 years ago
  • 0.74.0-11                                ...           2 years ago
  • 0.74.0-10                                ...           2 years ago
  • 0.74.0-9                                ...           2 years ago
  • 0.74.0-8                                ...           2 years ago
  • 0.74.0-7                                ...           2 years ago
  • 0.74.0-6                                ...           2 years ago
  • 0.74.0-5                                ...           2 years ago
  • 0.74.0-4                                ...           2 years ago
  • 0.74.0-3                                ...           2 years ago
  • 0.74.0-2                                ...           2 years ago
  • 0.74.0-1                                ...           2 years ago
  • 0.74.0-0                                ...           2 years ago
  • 0.73.4                                ...           2 years ago
  • 0.73.3                                ...           2 years ago
  • 0.73.2                                ...           2 years ago
  • 0.73.1                                ...           2 years ago
  • 0.73.0                                ...           2 years ago
  • 0.72.1                                ...           2 years ago
  • 0.72.0                                ...           2 years ago
  • 0.71.5                                ...           2 years ago
  • 0.71.4                                ...           2 years ago
  • 0.71.3                                ...           2 years ago
  • 0.71.2                                ...           3 years ago
  • 0.71.1                                ...           3 years ago
  • 0.71.0                                ...           3 years ago
  • 0.70.2                                ...           3 years ago
  • 0.70.1                                ...           3 years ago
  • 0.70.0                                ...           3 years ago
  • 0.69.2                                ...           3 years ago
  • 0.69.1                                ...           3 years ago
  • 0.69.0                                ...           3 years ago
  • 0.68.2                                ...           3 years ago
  • 0.68.1                                ...           3 years ago
  • 0.68.0                                ...           3 years ago
  • 0.66.21                                ...           3 years ago
  • 0.66.20                                ...           3 years ago
  • 0.66.19                                ...           3 years ago
  • 0.66.18                                ...           3 years ago
  • 0.66.17                                ...           3 years ago
  • 0.66.16                                ...           3 years ago
  • 0.66.15                                ...           3 years ago
  • 0.66.14                                ...           3 years ago
  • 0.66.13                                ...           3 years ago
  • 0.66.12                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 33
Dependencies (19)
Dev Dependencies (6)

Copyright 2014 - 2016 © taobao.org |