2013-12-20 01:49:47 +00:00
|
|
|
/*!
|
2013-07-02 23:51:38 +00:00
|
|
|
* Module Dependencies.
|
|
|
|
*/
|
|
|
|
|
2014-10-09 15:32:03 +00:00
|
|
|
var loopback = require('../../lib/loopback')
|
2013-11-15 02:34:51 +00:00
|
|
|
, assert = require('assert')
|
2013-11-14 23:27:36 +00:00
|
|
|
, uid = require('uid2')
|
2014-10-13 08:55:08 +00:00
|
|
|
, 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
|
|
|
*
|
|
|
|
* @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.
|
2014-08-11 17:55:23 +00:00
|
|
|
* @property {Date} created When the token was created
|
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) {
|
2013-07-12 22:47:58 +00:00
|
|
|
|
2014-10-13 08:23:35 +00:00
|
|
|
// Workaround for https://github.com/strongloop/loopback/issues/292
|
|
|
|
AccessToken.definition.rawProperties.created.default =
|
|
|
|
AccessToken.definition.properties.created.default = function() {
|
|
|
|
return new Date();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Anonymous Token
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* assert(AccessToken.ANONYMOUS.id === '$anonymous');
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
|
|
|
|
AccessToken.ANONYMOUS = new AccessToken({id: '$anonymous'});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-11-15 00:47:24 +00:00
|
|
|
|
2014-10-13 08:23:35 +00:00
|
|
|
/*!
|
|
|
|
* Hook to create accessToken id.
|
|
|
|
*/
|
2013-11-14 21:01:47 +00:00
|
|
|
|
2014-10-13 08:23:35 +00:00
|
|
|
AccessToken.beforeCreate = function(next, data) {
|
|
|
|
data = data || {};
|
2013-11-14 21:01:47 +00:00
|
|
|
|
2014-10-13 08:23:35 +00:00
|
|
|
AccessToken.createAccessTokenId(function(err, id) {
|
|
|
|
if (err) {
|
|
|
|
next(err);
|
2013-12-11 05:49:18 +00:00
|
|
|
} else {
|
2014-10-13 08:23:35 +00:00
|
|
|
data.id = id;
|
|
|
|
|
|
|
|
next();
|
2013-11-15 02:34:51 +00:00
|
|
|
}
|
|
|
|
});
|
2013-11-14 21:01:47 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
var id = tokenIdForRequest(req, options);
|
|
|
|
|
|
|
|
if (id) {
|
|
|
|
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('Invalid Access Token');
|
|
|
|
e.status = e.statusCode = 401;
|
|
|
|
cb(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
});
|
2013-11-15 02:34:51 +00:00
|
|
|
} else {
|
2014-10-13 08:23:35 +00:00
|
|
|
process.nextTick(function() {
|
|
|
|
cb();
|
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(
|
|
|
|
this.created && typeof this.created.getTime === 'function',
|
|
|
|
'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');
|
|
|
|
|
|
|
|
var now = Date.now();
|
|
|
|
var created = this.created.getTime();
|
|
|
|
var elapsedSeconds = (now - created) / 1000;
|
|
|
|
var secondsToLive = this.ttl;
|
|
|
|
var isValid = elapsedSeconds < secondsToLive;
|
|
|
|
|
|
|
|
if (isValid) {
|
|
|
|
cb(null, isValid);
|
|
|
|
} else {
|
|
|
|
this.destroy(function(err) {
|
|
|
|
cb(err, isValid);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
cb(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function tokenIdForRequest(req, options) {
|
|
|
|
var params = options.params || [];
|
|
|
|
var headers = options.headers || [];
|
|
|
|
var cookies = options.cookies || [];
|
|
|
|
var i = 0;
|
|
|
|
var length;
|
|
|
|
var id;
|
2013-11-14 21:01:47 +00:00
|
|
|
|
2014-10-13 08:23:35 +00:00
|
|
|
params = params.concat(['access_token']);
|
|
|
|
headers = headers.concat(['X-Access-Token', 'authorization']);
|
|
|
|
cookies = cookies.concat(['access_token', 'authorization']);
|
2013-11-14 21:01:47 +00:00
|
|
|
|
2014-10-13 08:23:35 +00:00
|
|
|
for (length = params.length; i < length; i++) {
|
|
|
|
id = req.param(params[i]);
|
2013-11-14 21:01:47 +00:00
|
|
|
|
2014-10-13 08:23:35 +00:00
|
|
|
if (typeof id === 'string') {
|
|
|
|
return id;
|
|
|
|
}
|
2013-11-14 21:01:47 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 08:23:35 +00:00
|
|
|
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
|
|
|
|
if (id.indexOf('Bearer ') === 0) {
|
|
|
|
id = id.substring(7);
|
|
|
|
// Decode from base64
|
|
|
|
var buf = new Buffer(id, 'base64');
|
|
|
|
id = buf.toString('utf8');
|
|
|
|
}
|
|
|
|
return id;
|
2014-07-02 16:02:13 +00:00
|
|
|
}
|
2013-11-14 21:01:47 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 08:23:35 +00:00
|
|
|
if (req.signedCookies) {
|
|
|
|
for (i = 0, length = cookies.length; i < length; i++) {
|
|
|
|
id = req.signedCookies[cookies[i]];
|
2013-11-14 21:01:47 +00:00
|
|
|
|
2014-10-13 08:23:35 +00:00
|
|
|
if (typeof id === 'string') {
|
|
|
|
return id;
|
|
|
|
}
|
2013-12-12 00:42:47 +00:00
|
|
|
}
|
2013-11-14 21:01:47 +00:00
|
|
|
}
|
2014-10-13 08:23:35 +00:00
|
|
|
return null;
|
2013-11-14 21:01:47 +00:00
|
|
|
}
|
2014-10-13 08:23:35 +00:00
|
|
|
};
|