Skip to content

Commit 5919454

Browse files
committed
Fixes for scope checks
1 parent 17f01ff commit 5919454

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

src/shared/modules/global/jwt.service.ts

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ const TEST_M2M_TOKENS: Record<string, string[]> = {
3535
'm2m-token-groups': [Scope.AllGroups],
3636
};
3737

38+
const SCOPE_SYNONYMS: Record<string, string[]> = {
39+
'read:group': [Scope.ReadGroups],
40+
[Scope.ReadGroups]: ['read:group'],
41+
'write:group': [Scope.WriteGroups],
42+
[Scope.WriteGroups]: ['write:group'],
43+
'all:group': [Scope.AllGroups],
44+
[Scope.AllGroups]: ['all:group'],
45+
};
46+
3847
@Injectable()
3948
export class JwtService implements OnModuleInit {
4049
private jwksClientInstance: jwksClient.JwksClient;
@@ -177,16 +186,30 @@ export class JwtService implements OnModuleInit {
177186
*/
178187
private expandScopes(scopes: string[]): string[] {
179188
const expandedScopes = new Set<string>();
189+
const queue = [...scopes];
180190

181-
// Add all original scopes
182-
scopes.forEach((scope) => expandedScopes.add(scope));
183-
184-
// Expand all "all:*" scopes
185-
scopes.forEach((scope) => {
186-
if (ALL_SCOPE_MAPPINGS[scope]) {
187-
ALL_SCOPE_MAPPINGS[scope].forEach((s) => expandedScopes.add(s));
191+
while (queue.length > 0) {
192+
const scope = queue.shift();
193+
if (!scope || expandedScopes.has(scope)) {
194+
continue;
188195
}
189-
});
196+
197+
expandedScopes.add(scope);
198+
199+
const synonyms = SCOPE_SYNONYMS[scope] ?? [];
200+
synonyms.forEach((alias) => {
201+
if (!expandedScopes.has(alias)) {
202+
queue.push(alias);
203+
}
204+
});
205+
206+
const mappedScopes = ALL_SCOPE_MAPPINGS[scope] ?? [];
207+
mappedScopes.forEach((alias) => {
208+
if (!expandedScopes.has(alias)) {
209+
queue.push(alias);
210+
}
211+
});
212+
}
190213

191214
return Array.from(expandedScopes);
192215
}

0 commit comments

Comments
 (0)