Skip to content

Commit 6f9ae2a

Browse files
committed
Support scopes defined via model settings
1 parent cafe704 commit 6f9ae2a

4 files changed

Lines changed: 85 additions & 17 deletions

File tree

‎common/models/acl.js‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ module.exports = function(ACL) {
256256
model: req.model,
257257
property: req.property,
258258
accessType: req.accessType,
259-
accessScope: req.accessScope,
260259
permission: permission || ACL.DEFAULT,
261260
registry: this.registry});
262261

@@ -444,7 +443,6 @@ module.exports = function(ACL) {
444443
var modelDefaultPermission = model && model.settings.defaultPermission;
445444
var property = context.property;
446445
var accessType = context.accessType;
447-
var accessScope = context.accessScope;
448446
var modelName = context.modelName;
449447

450448
var methodNames = context.methodNames;
@@ -460,18 +458,18 @@ module.exports = function(ACL) {
460458
model: modelName,
461459
property,
462460
accessType,
463-
accessScope,
464461
permission: ACL.DEFAULT,
465462
methodNames,
466463
registry: this.registry});
467464

468465
if (!context.isScopeAllowed()) {
469466
req.permission = ACL.DENY;
470467
debug('--Denied by scope config--');
471-
debug('Scopes allowed:', context.accessToken.scopes || '<default>');
472-
debug('Scope required:', accessScope || '<default>');
468+
debug('Scopes allowed: %j', context.accessToken.scopes || ['DEFAULT']);
469+
debug('Scopes required: %j', context.getScopes());
473470
context.debug();
474-
return callback(null, req);
471+
callback(null, req);
472+
return callback.promise;
475473
}
476474

477475
var effectiveACLs = [];

‎lib/access-context.js‎

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ var assert = require('assert');
88
var loopback = require('./loopback');
99
var debug = require('debug')('loopback:security:access-context');
1010

11+
const DEFAULT_SCOPES = ['DEFAULT'];
12+
1113
/**
1214
* Access context represents the context for a request to access protected
1315
* resources
@@ -32,7 +34,6 @@ var debug = require('debug')('loopback:security:access-context');
3234
* @property {String} property The model property/method/relation name
3335
* @property {String} method The model method to be invoked
3436
* @property {String} accessType The access type: READ, REPLICATE, WRITE, or EXECUTE.
35-
* @property {String[]} accessScopes The access scope required by the invoked method.
3637
* @property {AccessToken} accessToken The access token resolved for the request
3738
* @property {RemotingContext} remotingContext The request's remoting context
3839
* @property {Registry} registry The application or global registry
@@ -71,7 +72,6 @@ function AccessContext(context) {
7172
}
7273

7374
this.accessType = context.accessType || AccessContext.ALL;
74-
this.accessScopes = context.accessScopes;
7575

7676
assert(loopback.AccessToken,
7777
'AccessToken model must be defined before AccessContext model');
@@ -196,6 +196,41 @@ AccessContext.prototype.isAuthenticated = function() {
196196
return !!(this.getUserId() || this.getAppId());
197197
};
198198

199+
/**
200+
* Get the list of scopes required by the current access context.
201+
*/
202+
AccessContext.prototype.getScopes = function() {
203+
if (!this.sharedMethod)
204+
return DEFAULT_SCOPES;
205+
206+
// For backwards compatibility, methods with no scopes defined
207+
// are assigned a single "DEFAULT" scope
208+
const methodLevel = this.sharedMethod.accessScopes || DEFAULT_SCOPES;
209+
210+
const modelSettings = this.sharedMethod.sharedClass.ctor.settings || {};
211+
const modelScopes = modelSettings.accessScopes || {};
212+
213+
const allMethodNames = [this.sharedMethod.name]
214+
.concat(this.sharedMethod.aliases)
215+
.map(n => (this.sharedMethod.isStatic ? '' : 'prototype.') + n);
216+
217+
debug('--Context scopes of %s()--', this.sharedMethod.stringName);
218+
debug(' method-level: %j', methodLevel);
219+
220+
debug(' model-level:');
221+
let modelLevel = [];
222+
allMethodNames.forEach(alias => {
223+
const scopes = modelScopes[alias];
224+
if (!scopes) return;
225+
modelLevel = modelLevel.concat(scopes);
226+
debug(' - %s: %j', alias, scopes);
227+
});
228+
if (!modelLevel.length)
229+
debug(' (empty)');
230+
231+
return methodLevel.concat(modelLevel);
232+
};
233+
199234
/**
200235
* Check if the scope required by the remote method is allowed
201236
* by the scopes granted to the requesting access token.
@@ -206,11 +241,9 @@ AccessContext.prototype.isScopeAllowed = function() {
206241

207242
// For backwards compatibility, tokens with no scopes are treated
208243
// as if they have "DEFAULT" scope granted
209-
const scopes = this.accessToken.scopes || ['DEFAULT'];
244+
const scopes = this.accessToken.scopes || DEFAULT_SCOPES;
210245

211-
// For backwards compatibility, methods with no scopes defined
212-
// are assigned a single "DEFAULT" scope
213-
const requiredScopes = this.accessScopes || ['DEFAULT'];
246+
const requiredScopes = this.getScopes();
214247

215248
// Scope is allowed when at least one of token's scopes
216249
// is found in method's scopes.
@@ -238,12 +271,12 @@ AccessContext.prototype.debug = function() {
238271
debug('property %s', this.property);
239272
debug('method %s', this.method);
240273
debug('accessType %s', this.accessType);
241-
debug('accessScopes %s', this.accessScopes || ['DEFAULT']);
274+
debug('accessScopes %j', this.getScopes());
242275
if (this.accessToken) {
243276
debug('accessToken:');
244277
debug(' id %j', this.accessToken.id);
245278
debug(' ttl %j', this.accessToken.ttl);
246-
debug(' scopes', this.accessToken.scopes || ['DEFAULT']);
279+
debug(' scopes %j', this.accessToken.scopes || DEFAULT_SCOPES);
247280
}
248281
debug('getUserId() %s', this.getUserId());
249282
debug('isAuthenticated() %s', this.isAuthenticated());
@@ -399,7 +432,6 @@ AccessRequest.prototype.debug = function() {
399432
debug(' model %s', this.model);
400433
debug(' property %s', this.property);
401434
debug(' accessType %s', this.accessType);
402-
debug(' accessScopes %s', this.accessScopes);
403435
debug(' permission %s', this.permission);
404436
debug(' isWildcard() %s', this.isWildcard());
405437
debug(' isAllowed() %s', this.isAllowed());

‎lib/model.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,6 @@ module.exports = function(registry) {
347347
sharedMethod: sharedMethod,
348348
modelId: modelId,
349349
accessType: this._getAccessTypeForMethod(sharedMethod),
350-
accessScopes: sharedMethod.accessScopes,
351350
remotingContext: ctx,
352351
}, function(err, accessRequest) {
353352
if (err) return callback(err);

‎test/authorization-scopes.test.js‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,52 @@ describe('Authorization scopes', () => {
6262

6363
it('allows invocation when at least one method scope is matched', () => {
6464
givenRemoteMethodWithCustomScope(['read', 'write']);
65-
givenScopedToken(['read', 'execute']).then(() => {
65+
return givenScopedToken(['read', 'execute']).then(() => {
6666
return request.get('/users/scoped')
6767
.set('Authorization', scopedToken.id)
6868
.expect(204);
6969
});
7070
});
7171

72+
describe('scope config defined at model-level', () => {
73+
beforeEach(logAllServerErrors);
74+
75+
it('hounours scope defined for method name', () => {
76+
User.settings.accessScopes = {
77+
findById: ['read'],
78+
};
79+
80+
return givenScopedToken(['read']).then(() => {
81+
return request.get('/users/' + testUser.id)
82+
.set('Authorization', scopedToken.id)
83+
.expect(200);
84+
});
85+
});
86+
87+
it('honours scope defined for method alias', () => {
88+
User.settings.accessScopes = {
89+
'prototype.updateAttributes': ['write'],
90+
};
91+
92+
return givenScopedToken(['write']).then(() => {
93+
return request.patch('/users/' + testUser.id)
94+
.send({username: 'test-user'})
95+
.set('Authorization', scopedToken.id)
96+
.expect(200);
97+
});
98+
});
99+
100+
it('adds model-level scopes to method-level scopes', () => {
101+
User.settings.accessScopes = {
102+
findById: ['read'],
103+
};
104+
105+
return request.get('/users/' + testUser.id)
106+
.set('Authorization', regularToken.id)
107+
.expect(200);
108+
});
109+
});
110+
72111
function givenAppAndRequest() {
73112
app = loopback({localRegistry: true, loadBuiltinModels: true});
74113
app.set('remoting', {rest: {handleErrors: false}});

0 commit comments

Comments
 (0)