2018-01-03 04:05:53 +00:00
|
|
|
// Copyright IBM Corp. 2014,2018. All Rights Reserved.
|
2016-05-03 22:50:21 +00:00
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2013-12-20 01:49:47 +00:00
|
|
|
/*!
|
2013-07-02 23:51:38 +00:00
|
|
|
* Module Dependencies.
|
|
|
|
*/
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
'use strict';
|
2016-09-16 19:31:48 +00:00
|
|
|
var g = require('../../lib/globalize');
|
2014-11-04 12:52:49 +00:00
|
|
|
var loopback = require('../../lib/loopback');
|
|
|
|
var assert = require('assert');
|
|
|
|
var uid = require('uid2');
|
|
|
|
var DEFAULT_TOKEN_LEN = 64;
|
2013-07-02 23:51:38 +00:00
|
|
|
|
|
|
|
/**
|
2013-12-20 01:49:47 +00:00
|
|
|
* Token based authentication and access control.
|
2014-01-06 23:52:08 +00:00
|
|
|
*
|
2013-12-20 01:49:47 +00:00
|
|
|
* **Default ACLs**
|
2014-10-13 08:23:35 +00:00
|
|
|
*
|
2013-12-20 01:49:47 +00:00
|
|
|
* - DENY EVERYONE `*`
|
|
|
|
* - ALLOW EVERYONE create
|
2014-08-11 17:55:23 +00:00
|
|
|
*
|
2015-02-23 21:13:52 +00:00
|
|
|
* @property {String} id Generated token ID.
|
2014-10-13 08:23:35 +00:00
|
|
|
* @property {Number} ttl Time to live in seconds, 2 weeks by default.
|
2015-02-23 21:13:52 +00:00
|
|
|
* @property {Date} created When the token was created.
|
|
|
|
* @property {Object} settings Extends the `Model.settings` object.
|
|
|
|
* @property {Number} settings.accessTokenIdLength Length of the base64-encoded string access token. Default value is 64.
|
|
|
|
* Increase the length for a more secure access token.
|
2014-10-13 08:23:35 +00:00
|
|
|
*
|
|
|
|
* @class AccessToken
|
2014-08-19 04:44:28 +00:00
|
|
|
* @inherits {PersistedModel}
|
2013-07-02 23:51:38 +00:00
|
|
|
*/
|
|
|
|
|
2014-10-13 08:23:35 +00:00
|
|
|
module.exports = function(AccessToken) {
|
|
|
|
/**
|
|
|
|
* Anonymous Token
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* assert(AccessToken.ANONYMOUS.id === '$anonymous');
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
AccessToken.ANONYMOUS = new AccessToken({id: '$anonymous'});
|
2014-10-13 08:23:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a cryptographically random access token id.
|
|
|
|
*
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {String} token
|
|
|
|
*/
|
|
|
|
|
|
|
|
AccessToken.createAccessTokenId = function(fn) {
|
|
|
|
uid(this.settings.accessTokenIdLength || DEFAULT_TOKEN_LEN, function(err, guid) {
|
|
|
|
if (err) {
|
|
|
|
fn(err);
|
|
|
|
} else {
|
|
|
|
fn(null, guid);
|
|
|
|
}
|
|
|
|
});
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2013-11-15 00:47:24 +00:00
|
|
|
|
2014-10-13 08:23:35 +00:00
|
|
|
/*!
|
|
|
|
* Hook to create accessToken id.
|
|
|
|
*/
|
2015-03-02 12:09:14 +00:00
|
|
|
AccessToken.observe('before save', function(ctx, next) {
|
|
|
|
if (!ctx.instance || ctx.instance.id) {
|
|
|
|
// We are running a partial update or the instance already has an id
|
|
|
|
return next();
|
|
|
|
}
|
2013-11-14 21:01:47 +00:00
|
|
|
|
2014-10-13 08:23:35 +00:00
|
|
|
AccessToken.createAccessTokenId(function(err, id) {
|
2015-03-02 12:09:14 +00:00
|
|
|
if (err) return next(err);
|
|
|
|
ctx.instance.id = id;
|
|
|
|
next();
|
2013-11-15 02:34:51 +00:00
|
|
|
});
|
2015-03-02 12:09:14 +00:00
|
|
|
});
|
2013-11-14 21:01:47 +00:00
|
|
|
|
2017-05-02 17:55:51 +00:00
|
|
|
/**
|
|
|
|
* Extract the access token id from the HTTP request
|
|
|
|
* @param {Request} req HTTP request object
|
|
|
|
* @options {Object} [options] Each option array is used to add additional keys to find an `accessToken` for a `request`.
|
|
|
|
* @property {Array} [cookies] Array of cookie names.
|
|
|
|
* @property {Array} [headers] Array of header names.
|
|
|
|
* @property {Array} [params] Array of param names.
|
|
|
|
* @property {Boolean} [searchDefaultTokenKeys] Use the default search locations for Token in request
|
|
|
|
* @property {Boolean} [bearerTokenBase64Encoded] Defaults to `true`. For `Bearer` token based `Authorization` headers,
|
|
|
|
* decode the value from `Base64`. If set to `false`, the decoding will be skipped and the token id will be the raw value
|
|
|
|
* parsed from the header.
|
|
|
|
* @return {String} The access token
|
|
|
|
*/
|
|
|
|
AccessToken.getIdForRequest = function(req, options) {
|
|
|
|
options = options || {};
|
|
|
|
var params = options.params || [];
|
|
|
|
var headers = options.headers || [];
|
|
|
|
var cookies = options.cookies || [];
|
|
|
|
var i = 0;
|
|
|
|
var length, id;
|
|
|
|
|
|
|
|
// https://github.com/strongloop/loopback/issues/1326
|
|
|
|
if (options.searchDefaultTokenKeys !== false) {
|
|
|
|
params = params.concat(['access_token']);
|
|
|
|
headers = headers.concat(['X-Access-Token', 'authorization']);
|
|
|
|
cookies = cookies.concat(['access_token', 'authorization']);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (length = params.length; i < length; i++) {
|
|
|
|
var param = params[i];
|
|
|
|
// replacement for deprecated req.param()
|
|
|
|
id = req.params && req.params[param] !== undefined ? req.params[param] :
|
|
|
|
req.body && req.body[param] !== undefined ? req.body[param] :
|
2017-12-12 08:33:15 +00:00
|
|
|
req.query && req.query[param] !== undefined ? req.query[param] :
|
|
|
|
undefined;
|
2017-05-02 17:55:51 +00:00
|
|
|
|
|
|
|
if (typeof id === 'string') {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0, length = headers.length; i < length; i++) {
|
|
|
|
id = req.header(headers[i]);
|
|
|
|
|
|
|
|
if (typeof id === 'string') {
|
|
|
|
// Add support for oAuth 2.0 bearer token
|
|
|
|
// http://tools.ietf.org/html/rfc6750
|
2018-11-23 16:11:27 +00:00
|
|
|
|
|
|
|
// To prevent Error: Model::findById requires the id argument
|
|
|
|
// with loopback-datasource-juggler 2.56.0+
|
|
|
|
if (id === '') continue;
|
|
|
|
|
2017-05-02 17:55:51 +00:00
|
|
|
if (id.indexOf('Bearer ') === 0) {
|
|
|
|
id = id.substring(7);
|
|
|
|
if (options.bearerTokenBase64Encoded) {
|
|
|
|
// Decode from base64
|
|
|
|
var buf = new Buffer(id, 'base64');
|
|
|
|
id = buf.toString('utf8');
|
|
|
|
}
|
|
|
|
} else if (/^Basic /i.test(id)) {
|
|
|
|
id = id.substring(6);
|
|
|
|
id = (new Buffer(id, 'base64')).toString('utf8');
|
|
|
|
// The spec says the string is user:pass, so if we see both parts
|
|
|
|
// we will assume the longer of the two is the token, so we will
|
|
|
|
// extract "a2b2c3" from:
|
|
|
|
// "a2b2c3"
|
|
|
|
// "a2b2c3:" (curl http://a2b2c3@localhost:3000/)
|
|
|
|
// "token:a2b2c3" (curl http://token:a2b2c3@localhost:3000/)
|
|
|
|
// ":a2b2c3"
|
|
|
|
var parts = /^([^:]*):(.*)$/.exec(id);
|
|
|
|
if (parts) {
|
|
|
|
id = parts[2].length > parts[1].length ? parts[2] : parts[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.signedCookies) {
|
|
|
|
for (i = 0, length = cookies.length; i < length; i++) {
|
|
|
|
id = req.signedCookies[cookies[i]];
|
|
|
|
|
|
|
|
if (typeof id === 'string') {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve and validate the access token by id
|
|
|
|
* @param {String} id Access token
|
|
|
|
* @callback {Function} cb Callback function
|
|
|
|
* @param {Error} err Error information
|
|
|
|
* @param {Object} Resolved access token object
|
|
|
|
*/
|
|
|
|
AccessToken.resolve = function(id, cb) {
|
|
|
|
this.findById(id, function(err, token) {
|
|
|
|
if (err) {
|
|
|
|
cb(err);
|
|
|
|
} else if (token) {
|
|
|
|
token.validate(function(err, isValid) {
|
|
|
|
if (err) {
|
|
|
|
cb(err);
|
|
|
|
} else if (isValid) {
|
|
|
|
cb(null, token);
|
|
|
|
} else {
|
|
|
|
var e = new Error(g.f('Invalid Access Token'));
|
|
|
|
e.status = e.statusCode = 401;
|
|
|
|
e.code = 'INVALID_TOKEN';
|
|
|
|
cb(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-10-13 08:23:35 +00:00
|
|
|
/**
|
|
|
|
* Find a token for the given `ServerRequest`.
|
|
|
|
*
|
|
|
|
* @param {ServerRequest} req
|
|
|
|
* @param {Object} [options] Options for finding the token
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {AccessToken} token
|
|
|
|
*/
|
|
|
|
AccessToken.findForRequest = function(req, options, cb) {
|
2014-11-14 09:37:22 +00:00
|
|
|
if (cb === undefined && typeof options === 'function') {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
2017-05-02 17:55:51 +00:00
|
|
|
var id = this.getIdForRequest(req, options);
|
2014-10-13 08:23:35 +00:00
|
|
|
|
|
|
|
if (id) {
|
2017-05-02 17:55:51 +00:00
|
|
|
this.resolve(id, cb);
|
2013-11-15 02:34:51 +00:00
|
|
|
} else {
|
2017-05-02 17:55:51 +00:00
|
|
|
process.nextTick(cb);
|
2013-11-15 02:34:51 +00:00
|
|
|
}
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2013-11-15 02:34:51 +00:00
|
|
|
|
2014-10-13 08:23:35 +00:00
|
|
|
/**
|
|
|
|
* Validate the token.
|
|
|
|
*
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {Boolean} isValid
|
|
|
|
*/
|
|
|
|
AccessToken.prototype.validate = function(cb) {
|
|
|
|
try {
|
|
|
|
assert(
|
2017-12-12 08:33:15 +00:00
|
|
|
this.created && typeof this.created.getTime === 'function',
|
2014-10-13 08:23:35 +00:00
|
|
|
'token.created must be a valid Date'
|
|
|
|
);
|
|
|
|
assert(this.ttl !== 0, 'token.ttl must be not be 0');
|
|
|
|
assert(this.ttl, 'token.ttl must exist');
|
|
|
|
assert(this.ttl >= -1, 'token.ttl must be >= -1');
|
|
|
|
|
2016-10-10 11:27:22 +00:00
|
|
|
var AccessToken = this.constructor;
|
|
|
|
var userRelation = AccessToken.relations.user; // may not be set up
|
|
|
|
var User = userRelation && userRelation.modelTo;
|
|
|
|
|
2016-11-21 20:51:43 +00:00
|
|
|
// redefine user model if accessToken's principalType is available
|
|
|
|
if (this.principalType) {
|
|
|
|
User = AccessToken.registry.findModel(this.principalType);
|
|
|
|
if (!User) {
|
|
|
|
process.nextTick(function() {
|
|
|
|
return cb(null, false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-13 08:23:35 +00:00
|
|
|
var now = Date.now();
|
|
|
|
var created = this.created.getTime();
|
|
|
|
var elapsedSeconds = (now - created) / 1000;
|
|
|
|
var secondsToLive = this.ttl;
|
2016-10-10 11:27:22 +00:00
|
|
|
var eternalTokensAllowed = !!(User && User.settings.allowEternalTokens);
|
|
|
|
var isEternalToken = secondsToLive === -1;
|
|
|
|
var isValid = isEternalToken ?
|
|
|
|
eternalTokensAllowed :
|
|
|
|
elapsedSeconds < secondsToLive;
|
2014-10-13 08:23:35 +00:00
|
|
|
|
|
|
|
if (isValid) {
|
2016-11-21 20:51:43 +00:00
|
|
|
process.nextTick(function() {
|
|
|
|
cb(null, isValid);
|
|
|
|
});
|
2014-10-13 08:23:35 +00:00
|
|
|
} else {
|
|
|
|
this.destroy(function(err) {
|
|
|
|
cb(err, isValid);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2016-11-21 20:51:43 +00:00
|
|
|
process.nextTick(function() {
|
|
|
|
cb(e);
|
|
|
|
});
|
2014-10-13 08:23:35 +00:00
|
|
|
}
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-10-13 08:23:35 +00:00
|
|
|
};
|