Skip to content

Auth Strategy

thiagobustamante edited this page Jun 19, 2017 · 5 revisions

An Authentication Strategy middleware is a function than can instantiate any passportjs strategy to satisfy a custom authentication method.

This functions receives a paramenter with any custom configurations you desire to pass when registering the middlware inside an API Authentication.

Each middleware must be defined on its own .js file.

Example:

'use strict';

var JwtStrategy = require('passport-jwt').Strategy,
    ExtractJwt = require('passport-jwt').ExtractJwt;

/**
 * You can define your own passportjs strategy here.
 * @param authConfig - Constains any configuration you inform on your API Authentication configuration.
 */
module.exports = function (authConfig) {
    var opts = {}
    opts.jwtFromRequest = ExtractJwt.fromUrlQueryParameter('jwt');
    opts.secretOrKey = authConfig.secret;
    return new JwtStrategy(opts, function(jwt_payload, done) {
        done(null,jwt_payload.sub);
    });
}; 

For instance, if you configure the authentication of your API like:

{
    "authentication": {
        "strategy": {
            "name": "myJwtStrategy",
            "options": {
                "secret": "secret"
            }
        }
    }}

The middleware function will receive as paramater:

{
    "secret": "secret"
}

You can configure an auth strategy middleware through:

  • Admin Rest API: POST /midleware/authentication/strategies
  • SDK: sdk.middleware.addAuthStrategy(name, fileName);
  • CLI: treeGatewayConfig middleware authStrategy -a <name> ./filename.js
Clone this wiki locally