Enable the context middleware from loopback.rest
This commit is contained in:
parent
246f38c05d
commit
885f4e047d
|
@ -5,13 +5,7 @@ var memory = loopback.createDataSource({
|
||||||
connector: loopback.Memory
|
connector: loopback.Memory
|
||||||
});
|
});
|
||||||
|
|
||||||
server.use(loopback.context());
|
server.use(loopback.rest({context: {enableHttpContext: true}}));
|
||||||
server.use(function(req, res, next) {
|
|
||||||
loopback.getCurrentContext().set('http', {req: req, res: res});
|
|
||||||
next();
|
|
||||||
});
|
|
||||||
|
|
||||||
server.use(loopback.rest());
|
|
||||||
server.model(CartItem);
|
server.model(CartItem);
|
||||||
|
|
||||||
CartItem.attachTo(memory);
|
CartItem.attachTo(memory);
|
||||||
|
|
|
@ -7,33 +7,42 @@ module.exports = context;
|
||||||
|
|
||||||
var name = 'loopback';
|
var name = 'loopback';
|
||||||
|
|
||||||
|
function createContext(scope) {
|
||||||
|
// Make the namespace globally visible via the process.context property
|
||||||
|
process.context = process.context || {};
|
||||||
|
var ns = process.context[scope];
|
||||||
|
if (!ns) {
|
||||||
|
ns = cls.createNamespace(scope);
|
||||||
|
process.context[scope] = ns;
|
||||||
|
// Set up loopback.getCurrentContext()
|
||||||
|
loopback.getCurrentContext = function() {
|
||||||
|
return ns;
|
||||||
|
};
|
||||||
|
|
||||||
|
chain(juggler);
|
||||||
|
chain(remoting);
|
||||||
|
}
|
||||||
|
return ns;
|
||||||
|
}
|
||||||
|
|
||||||
function context(options) {
|
function context(options) {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
var scope = options.name || name;
|
var scope = options.name || name;
|
||||||
var enableHttpContext = options.enableHttpContext || false;
|
var enableHttpContext = options.enableHttpContext || false;
|
||||||
var ns = cls.createNamespace(scope);
|
var ns = createContext(scope);
|
||||||
|
|
||||||
// Make the namespace globally visible via the process.context property
|
|
||||||
process.context = process.context || {};
|
|
||||||
process.context[scope] = ns;
|
|
||||||
|
|
||||||
// Set up loopback.getCurrentContext()
|
|
||||||
loopback.getCurrentContext = function() {
|
|
||||||
return ns;
|
|
||||||
};
|
|
||||||
|
|
||||||
chain(juggler);
|
|
||||||
chain(remoting);
|
|
||||||
|
|
||||||
// Return the middleware
|
// Return the middleware
|
||||||
return function(req, res, next) {
|
return function contextHandler(req, res, next) {
|
||||||
|
if (req.loopbackContext) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
req.loopbackContext = ns;
|
||||||
// Bind req/res event emitters to the given namespace
|
// Bind req/res event emitters to the given namespace
|
||||||
ns.bindEmitter(req);
|
ns.bindEmitter(req);
|
||||||
ns.bindEmitter(res);
|
ns.bindEmitter(res);
|
||||||
// Create namespace for the request context
|
// Create namespace for the request context
|
||||||
ns.run(function(context) {
|
ns.run(function processRequestInContext(context) {
|
||||||
// Run the code in the context of the namespace
|
// Run the code in the context of the namespace
|
||||||
if(enableHttpContext) {
|
if (enableHttpContext) {
|
||||||
ns.set('http', {req: req, res: res}); // Set up the transport context
|
ns.set('http', {req: req, res: res}); // Set up the transport context
|
||||||
}
|
}
|
||||||
next();
|
next();
|
||||||
|
@ -59,14 +68,14 @@ function ChainedContext(child, parent) {
|
||||||
* @param {String} name Name of the context property
|
* @param {String} name Name of the context property
|
||||||
* @returns {*} Value of the context property
|
* @returns {*} Value of the context property
|
||||||
*/
|
*/
|
||||||
ChainedContext.prototype.get = function (name) {
|
ChainedContext.prototype.get = function(name) {
|
||||||
var val = this.child && this.child.get(name);
|
var val = this.child && this.child.get(name);
|
||||||
if (val === undefined) {
|
if (val === undefined) {
|
||||||
return this.parent && this.parent.get(name);
|
return this.parent && this.parent.get(name);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
ChainedContext.prototype.set = function (name, val) {
|
ChainedContext.prototype.set = function(name, val) {
|
||||||
if (this.child) {
|
if (this.child) {
|
||||||
return this.child.set(name, val);
|
return this.child.set(name, val);
|
||||||
} else {
|
} else {
|
||||||
|
@ -74,7 +83,7 @@ ChainedContext.prototype.set = function (name, val) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
ChainedContext.prototype.reset = function (name, val) {
|
ChainedContext.prototype.reset = function(name, val) {
|
||||||
if (this.child) {
|
if (this.child) {
|
||||||
return this.child.reset(name, val);
|
return this.child.reset(name, val);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var loopback = require('../loopback');
|
var loopback = require('../loopback');
|
||||||
|
var async = require('async');
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Export the middleware.
|
* Export the middleware.
|
||||||
|
@ -21,17 +22,32 @@ module.exports = rest;
|
||||||
* @header loopback.rest()
|
* @header loopback.rest()
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function rest() {
|
function rest(options) {
|
||||||
|
options = options || {};
|
||||||
var tokenParser = null;
|
var tokenParser = null;
|
||||||
return function(req, res, next) {
|
var contextHandler = null;
|
||||||
|
if (options.context) {
|
||||||
|
var contextOptions = options.context;
|
||||||
|
if(typeof contextOptions !== 'object') {
|
||||||
|
contextOptions = {};
|
||||||
|
}
|
||||||
|
contextHandler = loopback.context(contextOptions);
|
||||||
|
}
|
||||||
|
return function restApiHandler(req, res, next) {
|
||||||
var app = req.app;
|
var app = req.app;
|
||||||
var handler = app.handler('rest');
|
var handler = app.handler('rest');
|
||||||
|
|
||||||
if (req.url === '/routes') {
|
if (req.url === '/routes') {
|
||||||
res.send(handler.adapter.allRoutes());
|
return res.send(handler.adapter.allRoutes());
|
||||||
} else if (req.url === '/models') {
|
} else if (req.url === '/models') {
|
||||||
return res.send(app.remotes().toJSON());
|
return res.send(app.remotes().toJSON());
|
||||||
} else if (app.isAuthEnabled) {
|
}
|
||||||
|
|
||||||
|
var handlers = [];
|
||||||
|
if (options.context) {
|
||||||
|
handlers.push(contextHandler);
|
||||||
|
}
|
||||||
|
if (app.isAuthEnabled) {
|
||||||
if (!tokenParser) {
|
if (!tokenParser) {
|
||||||
// NOTE(bajtos) It would be better to search app.models for a model
|
// NOTE(bajtos) It would be better to search app.models for a model
|
||||||
// of type AccessToken instead of searching all loopback models.
|
// of type AccessToken instead of searching all loopback models.
|
||||||
|
@ -41,17 +57,12 @@ function rest() {
|
||||||
// https://github.com/strongloop/loopback/commit/f07446a
|
// https://github.com/strongloop/loopback/commit/f07446a
|
||||||
var AccessToken = loopback.getModelByType(loopback.AccessToken);
|
var AccessToken = loopback.getModelByType(loopback.AccessToken);
|
||||||
tokenParser = loopback.token({ model: AccessToken });
|
tokenParser = loopback.token({ model: AccessToken });
|
||||||
|
handlers.push(tokenParser);
|
||||||
}
|
}
|
||||||
|
|
||||||
tokenParser(req, res, function(err) {
|
|
||||||
if (err) {
|
|
||||||
next(err);
|
|
||||||
} else {
|
|
||||||
handler(req, res, next);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
handler(req, res, next);
|
|
||||||
}
|
}
|
||||||
|
handlers.push(handler);
|
||||||
|
async.eachSeries(handlers, function(handler, done) {
|
||||||
|
handler(req, res, done);
|
||||||
|
}, next);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -130,46 +130,103 @@ describe('loopback.rest', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should pass req to remote method via context', function(done) {
|
describe('context propagation', function() {
|
||||||
var User = givenUserModelWithAuth();
|
var User;
|
||||||
User.getToken = function(cb) {
|
|
||||||
var context = loopback.getCurrentContext();
|
|
||||||
var req = context.get('http').req;
|
|
||||||
expect(req).to.have.property('accessToken');
|
|
||||||
|
|
||||||
var juggler = require('loopback-datasource-juggler');
|
beforeEach(function() {
|
||||||
expect(juggler.getCurrentContext().get('http').req)
|
User = givenUserModelWithAuth();
|
||||||
.to.have.property('accessToken');
|
User.getToken = function(cb) {
|
||||||
|
var context = loopback.getCurrentContext();
|
||||||
|
var req = context.get('http').req;
|
||||||
|
expect(req).to.have.property('accessToken');
|
||||||
|
|
||||||
var remoting = require('strong-remoting');
|
var juggler = require('loopback-datasource-juggler');
|
||||||
expect(remoting.getCurrentContext().get('http').req)
|
expect(juggler.getCurrentContext().get('http').req)
|
||||||
.to.have.property('accessToken');
|
.to.have.property('accessToken');
|
||||||
|
|
||||||
cb(null, req && req.accessToken ? req.accessToken.id : null);
|
var remoting = require('strong-remoting');
|
||||||
};
|
expect(remoting.getCurrentContext().get('http').req)
|
||||||
// Set up the ACL
|
.to.have.property('accessToken');
|
||||||
User.settings.acls.push({principalType: 'ROLE',
|
|
||||||
principalId: '$authenticated', permission: 'ALLOW', property: 'getToken'});
|
|
||||||
|
|
||||||
loopback.remoteMethod(User.getToken, {
|
cb(null, req && req.accessToken ? req.accessToken.id : null);
|
||||||
accepts: [],
|
};
|
||||||
returns: [{ type: 'object', name: 'id' }]
|
// Set up the ACL
|
||||||
|
User.settings.acls.push({principalType: 'ROLE',
|
||||||
|
principalId: '$authenticated', permission: 'ALLOW',
|
||||||
|
property: 'getToken'});
|
||||||
|
|
||||||
|
loopback.remoteMethod(User.getToken, {
|
||||||
|
accepts: [],
|
||||||
|
returns: [
|
||||||
|
{ type: 'object', name: 'id' }
|
||||||
|
]
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.use(loopback.context({enableHttpContext: true}));
|
function invokeGetToken(done) {
|
||||||
app.enableAuth();
|
givenLoggedInUser(function(err, token) {
|
||||||
app.use(loopback.rest());
|
if (err) return done(err);
|
||||||
|
request(app).get('/users/getToken')
|
||||||
|
.set('Authorization', token.id)
|
||||||
|
.expect(200)
|
||||||
|
.end(function(err, res) {
|
||||||
|
if (err) return done(err);
|
||||||
|
expect(res.body.id).to.equal(token.id);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
givenLoggedInUser(function(err, token) {
|
it('should enable context using loopback.context', function(done) {
|
||||||
if (err) return done(err);
|
app.use(loopback.context({enableHttpContext: true}));
|
||||||
request(app).get('/users/getToken')
|
app.enableAuth();
|
||||||
.set('Authorization', token.id)
|
app.use(loopback.rest());
|
||||||
.expect(200)
|
|
||||||
.end(function(err, res) {
|
invokeGetToken(done);
|
||||||
if (err) return done(err);
|
});
|
||||||
expect(res.body.id).to.equal(token.id);
|
|
||||||
done();
|
it('should enable context with loopback.rest', function(done) {
|
||||||
});
|
app.enableAuth();
|
||||||
|
app.use(loopback.rest({context: {enableHttpContext: true}}));
|
||||||
|
|
||||||
|
invokeGetToken(done);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should support explicit context', function(done) {
|
||||||
|
app.enableAuth();
|
||||||
|
app.use(loopback.context());
|
||||||
|
app.use(loopback.token(
|
||||||
|
{ model: loopback.getModelByType(loopback.AccessToken) }));
|
||||||
|
app.use(function(req, res, next) {
|
||||||
|
loopback.getCurrentContext().set('accessToken', req.accessToken);
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
app.use(loopback.rest());
|
||||||
|
|
||||||
|
User.getToken = function(cb) {
|
||||||
|
var context = loopback.getCurrentContext();
|
||||||
|
var accessToken = context.get('accessToken');
|
||||||
|
expect(context.get('accessToken')).to.have.property('id');
|
||||||
|
|
||||||
|
var juggler = require('loopback-datasource-juggler');
|
||||||
|
context = juggler.getCurrentContext();
|
||||||
|
expect(context.get('accessToken')).to.have.property('id');
|
||||||
|
|
||||||
|
var remoting = require('strong-remoting');
|
||||||
|
context = remoting.getCurrentContext();
|
||||||
|
expect(context.get('accessToken')).to.have.property('id');
|
||||||
|
|
||||||
|
cb(null, accessToken ? accessToken.id : null);
|
||||||
|
};
|
||||||
|
|
||||||
|
loopback.remoteMethod(User.getToken, {
|
||||||
|
accepts: [],
|
||||||
|
returns: [
|
||||||
|
{ type: 'object', name: 'id' }
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
invokeGetToken(done);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue