@@ -50,15 +50,14 @@ const ACTION_MAP = {
5050
5151
5252/**
53- * Helper para cacheo y respuesta
53+ * Helper for caching and response
5454 */
5555function cacheAndReturn ( cacheKey , decision , callback ) {
5656 const finalDecision = decision || 'Invalid' ;
5757 cacheUtils . get ( ) . data . validation . set ( cacheKey , finalDecision ) ;
5858 callback ( null , finalDecision ) ;
5959}
6060
61-
6261/**
6362 * validation request using local PDP implementation
6463 */
@@ -77,47 +76,51 @@ function validationRequest(logger, roles, frn, action, headers, callback) {
7776 return callback ( new Error ( 'Invalid FRN format' ) ) ;
7877 }
7978
80- const component = frnParts [ 1 ] . toUpperCase ( ) ; // ORION, PERSEO, IOTA , STH
81- const subserviceRaw = frnParts [ 3 ] ; // "/tourism", "/// ", etc
79+ const component = frnParts [ 1 ] . toUpperCase ( ) ; // ORION, PERSEO, IOTAGENT , STH
80+ const subserviceRaw = frnParts [ 3 ] ; // "/tourism", "/", etc
8281 const subservice = subserviceRaw . replace ( / \/ / g, '' ) || null ; // "tourism" o null
8382
8483 const isServiceOperation = subservice === null ;
8584 const isSubserviceOperation = subservice !== null ;
8685
8786 // 2. For each role: get roleType and component
88- let matchedRole = null ;
87+ let hasMatchingRole = false ;
88+ let isPermitted = false ;
8989
9090 for ( const role of roles ) {
9191 const name = ( role . name || '' ) . trim ( ) ;
9292
9393 // if name role with '#', then get right part; otherwise name as is
9494 const hashParts = name . split ( '#' ) ;
9595 const roleInfoRaw = ( hashParts . length === 2 ? hashParts [ 1 ] : hashParts [ 0 ] ) . trim ( ) ;
96+
9697 // Alias: admin (without #) = ServiceAdmin for all components
9798 const roleInfo = / ^ a d m i n $ / i. test ( roleInfoRaw ) ? 'ServiceAdmin' : roleInfoRaw ;
9899
99- // Try extrat type and component (i.e.: ServiceCustomerORION)
100+ // Try extract type and component (i.e.: ServiceCustomerORION)
100101 let match = roleInfo . match (
101- / ( S e r v i c e C u s t o m e r | S e r v i c e A d m i n | S u b S e r v i c e C u s t o m e r | S u b S e r v i c e A d m i n ) ( [ A - Z ] + ) $ / i
102+ / ^ ( S e r v i c e C u s t o m e r | S e r v i c e A d m i n | S u b S e r v i c e C u s t o m e r | S u b S e r v i c e A d m i n ) ( [ A - Z ] + ) $ / i
102103 ) ;
103104
104- let roleType , roleComponent ;
105+ let roleType ;
106+ let roleComponent ;
105107
106- // Case 1: rol includes component
108+ // Case 1: role includes component
107109 if ( match ) {
108110 roleType = match [ 1 ] ;
109111 roleComponent = match [ 2 ] . toUpperCase ( ) ;
110112 }
111- // Caso 2: rol does NOT includes component -> apply over all components
113+ // Case 2: role does NOT include component -> apply over all components
112114 else {
113115 match = roleInfo . match (
114116 / ^ ( S e r v i c e C u s t o m e r | S e r v i c e A d m i n | S u b S e r v i c e C u s t o m e r | S u b S e r v i c e A d m i n ) $ / i
115117 ) ;
118+
116119 if ( ! match ) {
117120 continue ;
118121 }
119122
120- roleType = match [ 1 ] ;
123+ roleType = match [ 1 ] ;
121124 roleComponent = 'ANY' ;
122125 }
123126
@@ -137,27 +140,20 @@ function validationRequest(logger, roles, frn, action, headers, callback) {
137140 continue ;
138141 }
139142
140- matchedRole = { roleType, roleComponent } ;
141- break ;
142- } // end for
143-
144- if ( ! matchedRole ) {
145- cacheAndReturn ( cacheKey , 'Deny' , callback ) ;
146- return ;
147- }
148-
149- // 3. Final decision
150- const { roleType } = matchedRole ;
151- const ROLE_CLASS = roleType . endsWith ( 'Customer' ) ? 'CUSTOMER' : 'ADMIN' ;
143+ hasMatchingRole = true ;
152144
153- let allowedActions = [ ] ;
145+ // 3) Check whether THIS role permits the action
146+ const ROLE_CLASS = roleType . endsWith ( 'Customer' ) ? 'CUSTOMER' : 'ADMIN' ;
147+ const allowedActions =
148+ ( ACTION_MAP [ component ] && ACTION_MAP [ component ] [ ROLE_CLASS ] ) || [ ] ;
154149
155- if ( ACTION_MAP [ component ] && ACTION_MAP [ component ] [ ROLE_CLASS ] ) {
156- allowedActions = ACTION_MAP [ component ] [ ROLE_CLASS ] ;
150+ if ( allowedActions . includes ( action ) ) {
151+ isPermitted = true ;
152+ break ; // one permitting role is enough
153+ }
157154 }
158155
159- const decision = allowedActions . includes ( action ) ? 'Permit' : 'Deny' ;
160-
156+ const decision = ( hasMatchingRole && isPermitted ) ? 'Permit' : 'Deny' ;
161157 cacheAndReturn ( cacheKey , decision , callback ) ;
162158
163159 } catch ( err ) {
@@ -166,5 +162,4 @@ function validationRequest(logger, roles, frn, action, headers, callback) {
166162 }
167163}
168164
169-
170165exports . validationRequest = validationRequest ;
0 commit comments