Class: Treant

protector.Treant

Treant

Constructor

new Treant(config)

  • Authenticate service help authenticate user and authorize them permission
  • It also support generate credential as json web token
Parameters:
Name Type Description
config module:protector.TreantConfig

Configuration of treant protector

Source:
Example
var treant = new Treant({
  db: dbclient,
  gClientId: '327834646153-27e424bo1lpofea0lkfs8bvrf7sv5j2e.apps.googleusercontent.com',
  gClientSecret: 'y7RCBAr4AT26oACJBJUSJIxv',
  gRedirectUrl: 'http://localhost:9001/v1/oauth/login',
  gScopes: [
    'https://www.googleapis.com/auth/plus.me',
    'profile',
    'email'
  ],
  privateKeyPath: '/usr/lib/node_module/gwisp/gwisp.key',
  tokenTimeout: 1800
})

Members

_config :module:protector.TreanConfig

Treant configuration

Type:
  • module:protector.TreanConfig
Source:

Methods

_gOauth() → {module:googleapis.OAuth2}

Create new google oauth 2 object

Source:
Returns:
Type
module:googleapis.OAuth2
Example
var oauth = this._gOauth()

check(optionsopt) → {module:express.Middleware}

This function return an express middleware allow authenticate or authorize depend on options

  • If authorize successful, continue next route
  • If authorize fails, response message to endpoint and terminate
Parameters:
Name Type Attributes Default Description
options module:protector.AuthOptions <optional>
null

Options for authorization

If parameter is not specify, use default options

Source:
Returns:
Type
module:express.Middleware
Example
const express = require('express')

// see constructor for detail
const treant = new Treant(config)

const app = express()

app.use('/root', treant.inspect(group: 'root'), function(req, res) {
  // if this code block is reached, account in root group is logged in
  // if this code block is not reached, no account in root group is logged in
  var rootAccount = req.account

  // do some thing here
})

app.use('/account', treant.inspect(), function(req, res) {
  // if this code block is reach, an account is logged in
  // if this code block is not reached, no account is logged in
  var account = req.account

  // do some thing here
})

gAccount(accessToken, callback)

Get account information from google

Parameters:
Name Type Description
accessToken string

Access token of google oauth 2

callback StdCallback
  • Function will be call after done
  • Result is object contains account information from google
Source:

gLoginCode(code, callback)

  • Login with code from google
  • Function will perform exhange code to get token
  • If account is exist, create new local token and give back
  • If account is not exist, create new account in database, set default setting. Then create local token and give back
Parameters:
Name Type Description
code string

Code to exchange token from google

callback StdCallback

Function will be call after done

  • Result is local token
Source:
Example
// see constructor for detail
var treant = new Treant(config)

treant.gLoginCode('1234567890abcdef', function(err, token) {
  if (err) return

  // do some thing with token here
})

gLoginUrl() → {string}

  • Get login url of oauth 2 service from google
  • Then caller can perform GET http with url to require login with google
$ curl <gLoginUrl>
Source:
Returns:
Type
string
Example
// see constructor for detail
var treant = new Treant(config)

var url = treant.gLoginUrl()

// url: https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.
// googleapis.com%2Fauth%2Fplus.me%20profile%20email&approval_prompt=
// auto&access_type=offline&response_type=code&client_id=736160870024-
// 0cu341if9scqjfpvdsaqhutjebobjjq0.apps.googleusercontent.com&redirect_uri=
// http%3A%2F%2Flocalhost%3A9001%2Foauth%2Flogin

identify(token, getInfo, callback)

Identify account from token

Parameters:
Name Type Description
token string

Token from client

getInfo boolean

Decide query database to retrieve account or not

callback StdCallback
  • Function will be call after done, result is object
  • Result contains field token. This is token is decoded
  • If getInfo is true, result contains field account information
Source:
Example
// see constructor for detail
var treant = new Treant(config)

treant.identity(token, true, function(err, res) {
  if (err) return

  // do some thing with account here
  var account = res.account

  // do some thing with token decoded here
  var token = res.token
})