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
|
|
|
|
|
2014-01-06 23:52:08 +00:00
|
|
|
/*!
|
2013-11-13 19:49:08 +00:00
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
'use strict';
|
2017-03-18 00:08:17 +00:00
|
|
|
var g = require('../../lib/globalize');
|
2014-11-12 11:36:16 +00:00
|
|
|
var loopback = require('../../lib/loopback');
|
2013-12-03 00:37:42 +00:00
|
|
|
var assert = require('assert');
|
2015-03-12 15:28:15 +00:00
|
|
|
var debug = require('debug')('loopback:middleware:token');
|
2013-11-13 19:49:08 +00:00
|
|
|
|
2014-01-06 23:52:08 +00:00
|
|
|
/*!
|
2013-11-13 19:49:08 +00:00
|
|
|
* Export the middleware.
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = token;
|
|
|
|
|
2015-03-12 15:28:15 +00:00
|
|
|
/*
|
|
|
|
* Rewrite the url to replace current user literal with the logged in user id
|
|
|
|
*/
|
2017-03-18 00:08:17 +00:00
|
|
|
function rewriteUserLiteral(req, currentUserLiteral, next) {
|
|
|
|
if (!currentUserLiteral) return next();
|
|
|
|
const literalRegExp = new RegExp('/' + currentUserLiteral + '(/|$|\\?)', 'g');
|
|
|
|
|
|
|
|
if (req.accessToken && req.accessToken.userId) {
|
2015-03-12 15:28:15 +00:00
|
|
|
// Replace /me/ with /current-user-id/
|
|
|
|
var urlBeforeRewrite = req.url;
|
2017-03-18 00:08:17 +00:00
|
|
|
req.url = req.url.replace(literalRegExp,
|
2017-12-12 08:33:15 +00:00
|
|
|
'/' + req.accessToken.userId + '$1');
|
2017-03-18 00:08:17 +00:00
|
|
|
|
2015-03-12 15:28:15 +00:00
|
|
|
if (req.url !== urlBeforeRewrite) {
|
|
|
|
debug('req.url has been rewritten from %s to %s', urlBeforeRewrite,
|
|
|
|
req.url);
|
|
|
|
}
|
2017-03-18 00:08:17 +00:00
|
|
|
} else if (!req.accessToken && literalRegExp.test(req.url)) {
|
|
|
|
debug(
|
|
|
|
'URL %s matches current-user literal %s,' +
|
|
|
|
' but no (valid) access token was provided.',
|
|
|
|
req.url, currentUserLiteral);
|
|
|
|
|
|
|
|
var e = new Error(g.f('Authorization Required'));
|
|
|
|
e.status = e.statusCode = 401;
|
|
|
|
e.code = 'AUTHORIZATION_REQUIRED';
|
|
|
|
return next(e);
|
2015-03-12 15:28:15 +00:00
|
|
|
}
|
2017-03-18 00:08:17 +00:00
|
|
|
|
|
|
|
next();
|
2015-03-12 15:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function escapeRegExp(str) {
|
|
|
|
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
|
|
}
|
|
|
|
|
2014-10-16 22:54:40 +00:00
|
|
|
/**
|
2014-06-05 00:42:18 +00:00
|
|
|
* Check for an access token in cookies, headers, and query string parameters.
|
|
|
|
* This function always checks for the following:
|
2014-10-16 22:54:40 +00:00
|
|
|
*
|
2014-07-01 14:59:49 +00:00
|
|
|
* - `access_token` (params only)
|
|
|
|
* - `X-Access-Token` (headers only)
|
|
|
|
* - `authorization` (headers and cookies)
|
2014-06-05 00:42:18 +00:00
|
|
|
*
|
|
|
|
* It checks for these values in cookies, headers, and query string parameters _in addition_ to the items
|
|
|
|
* specified in the options parameter.
|
2014-10-16 22:54:40 +00:00
|
|
|
*
|
2014-06-05 00:42:18 +00:00
|
|
|
* **NOTE:** This function only checks for [signed cookies](http://expressjs.com/api.html#req.signedCookies).
|
2014-10-16 22:54:40 +00:00
|
|
|
*
|
2014-01-06 23:52:08 +00:00
|
|
|
* The following example illustrates how to check for an `accessToken` in a custom cookie, query string parameter
|
|
|
|
* and header called `foo-auth`.
|
2014-10-16 22:54:40 +00:00
|
|
|
*
|
2014-01-06 23:52:08 +00:00
|
|
|
* ```js
|
|
|
|
* app.use(loopback.token({
|
|
|
|
* cookies: ['foo-auth'],
|
|
|
|
* headers: ['foo-auth', 'X-Foo-Auth'],
|
2014-06-05 00:42:18 +00:00
|
|
|
* params: ['foo-auth', 'foo_auth']
|
2014-01-06 23:52:08 +00:00
|
|
|
* }));
|
|
|
|
* ```
|
|
|
|
*
|
2014-06-05 00:42:18 +00:00
|
|
|
* @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.
|
2015-04-29 11:45:22 +00:00
|
|
|
* @property {Boolean} [searchDefaultTokenKeys] Use the default search locations for Token in request
|
2016-02-29 15:49:41 +00:00
|
|
|
* @property {Boolean} [enableDoublecheck] Execute middleware although an instance mounted earlier in the chain didn't find a token
|
|
|
|
* @property {Boolean} [overwriteExistingToken] only has effect in combination with `enableDoublecheck`. If truthy, will allow to overwrite an existing accessToken.
|
2015-03-12 15:28:15 +00:00
|
|
|
* @property {Function|String} [model] AccessToken model name or class to use.
|
|
|
|
* @property {String} [currentUserLiteral] String literal for the current user.
|
2017-05-02 17:55:51 +00:00
|
|
|
* @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.
|
2014-06-05 00:42:18 +00:00
|
|
|
* @header loopback.token([options])
|
2014-01-06 23:52:08 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-14 23:27:36 +00:00
|
|
|
function token(options) {
|
2013-11-13 19:49:08 +00:00
|
|
|
options = options || {};
|
2015-04-01 21:50:36 +00:00
|
|
|
var TokenModel;
|
|
|
|
|
2015-03-12 15:28:15 +00:00
|
|
|
var currentUserLiteral = options.currentUserLiteral;
|
|
|
|
if (currentUserLiteral && (typeof currentUserLiteral !== 'string')) {
|
|
|
|
debug('Set currentUserLiteral to \'me\' as the value is not a string.');
|
|
|
|
currentUserLiteral = 'me';
|
|
|
|
}
|
|
|
|
if (typeof currentUserLiteral === 'string') {
|
|
|
|
currentUserLiteral = escapeRegExp(currentUserLiteral);
|
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2017-05-02 17:55:51 +00:00
|
|
|
if (options.bearerTokenBase64Encoded === undefined) {
|
|
|
|
options.bearerTokenBase64Encoded = true;
|
|
|
|
}
|
2016-02-29 15:49:41 +00:00
|
|
|
var enableDoublecheck = !!options.enableDoublecheck;
|
|
|
|
var overwriteExistingToken = !!options.overwriteExistingToken;
|
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
return function(req, res, next) {
|
2015-04-01 21:50:36 +00:00
|
|
|
var app = req.app;
|
|
|
|
var registry = app.registry;
|
|
|
|
if (!TokenModel) {
|
2017-03-03 11:35:41 +00:00
|
|
|
TokenModel = registry.getModel(options.model || 'AccessToken');
|
2015-04-01 21:50:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(typeof TokenModel === 'function',
|
|
|
|
'loopback.token() middleware requires a AccessToken model');
|
|
|
|
|
2015-03-12 15:28:15 +00:00
|
|
|
if (req.accessToken !== undefined) {
|
2016-02-29 15:49:41 +00:00
|
|
|
if (!enableDoublecheck) {
|
|
|
|
// req.accessToken is defined already (might also be "null" or "false") and enableDoublecheck
|
|
|
|
// has not been set --> skip searching for credentials
|
2017-03-18 00:08:17 +00:00
|
|
|
return rewriteUserLiteral(req, currentUserLiteral, next);
|
2016-02-29 15:49:41 +00:00
|
|
|
}
|
|
|
|
if (req.accessToken && req.accessToken.id && !overwriteExistingToken) {
|
|
|
|
// req.accessToken.id is defined, which means that some other middleware has identified a valid user.
|
|
|
|
// when overwriteExistingToken is not set to a truthy value, skip searching for credentials.
|
2017-03-18 00:08:17 +00:00
|
|
|
return rewriteUserLiteral(req, currentUserLiteral, next);
|
2016-02-29 15:49:41 +00:00
|
|
|
}
|
|
|
|
// continue normal operation (as if req.accessToken was undefined)
|
2015-03-12 15:28:15 +00:00
|
|
|
}
|
2017-03-18 00:08:17 +00:00
|
|
|
|
2013-11-14 21:01:47 +00:00
|
|
|
TokenModel.findForRequest(req, options, function(err, token) {
|
2014-06-17 06:27:41 +00:00
|
|
|
req.accessToken = token || null;
|
2017-03-18 00:08:17 +00:00
|
|
|
|
2016-07-28 14:36:23 +00:00
|
|
|
var ctx = req.loopbackContext;
|
2016-08-16 11:51:42 +00:00
|
|
|
if (ctx && ctx.active) ctx.set('accessToken', token);
|
2017-03-18 00:08:17 +00:00
|
|
|
|
|
|
|
if (err) return next(err);
|
|
|
|
rewriteUserLiteral(req, currentUserLiteral, next);
|
2013-11-14 21:01:47 +00:00
|
|
|
});
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2013-11-13 19:49:08 +00:00
|
|
|
}
|