@@ -8,6 +8,8 @@ var assert = require('assert');
88var loopback = require ( './loopback' ) ;
99var 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 ( ) ) ;
0 commit comments