guardian

Authentication Gateway Proxy

Guardian

Avoid dealing with OAuth logic in your code, and spend more time creating your product. Guardian reduces the OAuth footprint in your code to a single request.

Built with modularity in mind, Guardian leverages plugins to handle OAuth flows, should you encounter a flow that Guardian doesn't handle, create a small flow plugin to do so and carry on. Guardian comes with 5 pre-made plugins that cover 99% of OAuth services.

Not to mention, Guardian is perfect for both production and testing. Services like Github require you to enter a single callback url, this is fine when in production, but move to another environment and soon you'll have conflicts, require building complex services to juggle environment scenarios and more. Guardian is centralized and easily configurable to allow multiple environments giving you the flexibility you need.

Created with love by nijikokun at http://mashape.com

  1. Install Redis

  2. Globally install Guardian

    $ npm install -g guardian
$ guardian

Configuration files are loaded from the current working directory of where you call Guardian. Should no configuration argument be passed ./config/default.js is loaded.

$ guardian -c <relative configuration path>

Options

PropertyDefaultDescription
hostlocalhost:3000Public IP or Domain Name, used to generate the callback uri.
protocolhttpHost Protocol
port3000Server Port
workersrequire('os').cpus().lengthNumber of forked instances of the Guardian server to run, suggested amount is CPU count.
pid.dir./.guardian.pid file output directory, for production we suggest placing this under the /home/<user>/ directory, requires trailing slash.
redis.host127.0.0.1Host redis can be reached on
redis.port6379Port redis is current running on
redis.passRedis password
redis.expire60Guardian store expiration timeout in Seconds
cookie.secretGuardian cookie secret
session.secretGuardian session secret

Guardian HTTP API for handling authentication flows.

POST /store

Stores information given, returns a session hash to be used later on. Information stored lives for 60 seconds by default, change redis.expire to alter timeout duration.

Details specific to OAuth2

KeyDefaultDescription
client_idOAuth Client Identifier
client_secretOAuth Client Secret
grant_typeCommon values (dependant on OAuth flow used): authorization_code, client_credentials, password, refresh_token, ...
access_nameaccess_tokenAccess token name
authorize_methodBearerOptional - Authorization header method, some possible values: Bearer, OAuth, Digest
stateState identifier, depends on provider
scopeOAuth request scopes, depends on provider

Details specific to OAuth 1.0a

KeyDefaultDescription
consumer_keyOAuth Consumer Identifier
consumer_secretOAuth Consumer Secret
signature_methodHMAC-SHA1OAuth Header encryption method, possible values: PLAINTEXT, HMAC-SHA1, RSA-SHA1
oauth_tokenOptional; OAuth Token. Used in OAuth 1.0a 1-Legged (Resource request)

Parameters combined to create the plugin file name.

KeyDefaultDescription
auth_typeoauthAuthentication type, a-z characters accepted only.
auth_flowOptional; Authentication flow, would be echo, owner_resources, etc... a-z characters accepted only.
auth_versionOptional; Authentication version, for OAuth 2, we would use 2, numeric only.
auth_legOptional; Authentication leg, for OAuth 2 (3-legged), we would use 3, numeric only.

For example, plugins/oauth_2_3-legged.js (OAuth 2, 3-legged), would look like:

{
  ...
  auth_type: 'oauth',
  auth_version: 2,
  auth_leg: 3
  ...
}
KeyDescription
request_urlAuthentication Request Url, e.g. https://github.com/login/oauth/request_url
access_urlAuthentication Access Url, e.g. https://github.com/login/oauth/access_token
authorize_urlAuthentication Authorization Url, e.g. https://github.com/login/oauth/authorize
callbackAuthentication Callback URL on requesting server to obtain access_token and access_secret, e.g. http://localhost:3001/callback

Request:

> POST https://<guardian-host>/store 
 
{
  client_id: 'Client Identifier',
  client_secret: 'Client Secret',
  access_name: 'access_token',
  authorize_url: 'https://github.com/login/oauth/authorize',
  access_url: 'https://github.com/login/oauth/access_token',
  request_url: 'https://github.com/login/oauth/request_url',
  auth_type: "oauth",
  auth_version: 2,
  auth_leg: 3,
  callback: "http://localhost:3001/callback"
}

Response:

< 200 OK
< Header: Content-Type=application/json
 
{
  hash: '<guardian session hash>',
  url: 'https://<guardian-host>/start?hash=<guardian session hash>'
}
GET /hash-check

Allows you to preview / verify your stored information in-case of error or malformed response.

Once again, stored information by default lasts only 10 seconds.

KeyDescription
hashGuardian session hash obtained from Storage
GET /start?hash=<guardian store hash>

Redirecting the client to this route starts the Guardian authentication steps, Each steps are done with 302 response code and should be followed.

KeyDescription
hashGuardian session hash obtained from Storage

Used in the OAuth 1.0a Signature Process for 1-Legged requests. Example.

KeyDescription
urlRequest URL, query parameters will be parsed from here as well as parameters property.
methodRequest Method.
bodyRequest Payload / Body.
parametersRequest Parameters for Request Signatures or etc...

Each test in the test folder is based on an API or feature of guardian rather than TDD or BDD based tests, we verify successful authentication and we can retrieve information while authenticated from the API using tokens Guardian provides.

In this manner the tests also serve as very good examples of how to use Guardian.

To run one of these test you'll need to have keys ready and run the following command:

$ node tests/<provider name>.js \
  -k {Your Consumer/Client Key/Id} \
  -s {Your Consumer/Client Secret} \
  -h {host, ie: localhost or domain}

Then visit the server running on port 3001 to start the authentication process.

MIT