Skip to content

Commit 218c64d

Browse files
mkilmanasevansims
andauthored
Normalize dashes in JWT permissions/scopes (#184)
### Changes When converting permissions/scopes to Symfony roles, apply replacement not only for colon (`:`) character, but also for dash (`-`) character, as some resources/permissions consist of multiple words and dash is a recommended separator in such cases. ### Testing - Have a permission/scope in Auth0 token that contains a dash (e.g. `read:licence-plates`) - Convert it to Symfony roles (by getting the roles of the JWT authenticated user/m2m): - before: it would return `ROLE_READ_LICENCE-PLATES` - after: it returns `ROLE_READ_LICENCE_PLATES` [ ] This change adds test coverage [ ] This change has been tested on the latest version of Symfony ### Checklist [x] I have read the [Auth0 general contribution guidelines](https://github.com/auth0/open-source-template/blob/master/GENERAL-CONTRIBUTING.md) [x] I have read the [Auth0 Code of Conduct](https://github.com/auth0/open-source-template/blob/master/CODE-OF-CONDUCT.md) [x] All existing and new tests complete without errors Co-authored-by: Evan Sims <hello@evansims.com>
1 parent d4c3aac commit 218c64d

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/Models/User.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,21 +258,21 @@ public function getRoles(): array
258258
}
259259

260260
foreach ($roles as $role) {
261-
$response[] = implode('_', explode(':', strtoupper($role)));
261+
$response[] = str_replace([':', '-'], '_', strtoupper($role));
262262
}
263263

264264
if (is_array($permissions)) {
265265
foreach ($permissions as $permission) {
266266
if (is_string($permission)) {
267-
$response[] = 'ROLE_' . implode('_', explode(':', strtoupper($permission)));
267+
$response[] = 'ROLE_' . str_replace([':', '-'], '_', strtoupper($permission));
268268
}
269269
}
270270
}
271271

272272
if (is_array($scopes)) {
273273
foreach ($scopes as $scope) {
274274
if (is_string($scope)) {
275-
$response[] = 'ROLE_' . implode('_', explode(':', strtoupper($scope)));
275+
$response[] = 'ROLE_' . str_replace([':', '-'], '_', strtoupper($scope));
276276
}
277277
}
278278
}

0 commit comments

Comments
 (0)