2014-01-06 23:52:08 +00:00
|
|
|
/*!
|
2013-11-13 19:49:08 +00:00
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
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');
|
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;
|
|
|
|
|
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.
|
|
|
|
* @property {Array} [model] An AccessToken object to use.
|
|
|
|
* @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 || {};
|
2013-12-03 01:16:43 +00:00
|
|
|
var TokenModel = options.model || loopback.AccessToken;
|
2013-11-14 21:01:47 +00:00
|
|
|
assert(TokenModel, 'loopback.token() middleware requires a AccessToken model');
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
return function(req, res, next) {
|
2014-02-04 15:17:32 +00:00
|
|
|
if (req.accessToken !== undefined) return next();
|
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;
|
2014-11-10 17:37:35 +00:00
|
|
|
var ctx = loopback.getCurrentContext();
|
|
|
|
if (ctx) ctx.set('accessToken', token);
|
2014-06-17 06:27:41 +00:00
|
|
|
next(err);
|
2013-11-14 21:01:47 +00:00
|
|
|
});
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2013-11-13 19:49:08 +00:00
|
|
|
}
|