Skip to content

Commit 07716f7

Browse files
committed
feat: add config option rolesJwtClaim in AuthKeycloakRedirect provider
1 parent 4c629b7 commit 07716f7

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

src/AuthKeycloakRedirect/AuthKeycloakRedirect.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,33 @@ const _updateTokensInStorage = (tokens) => {
150150
};
151151

152152
const _extractRolesFromAccessToken = (accessToken) => {
153-
let result = [];
154-
if (accessToken) {
155-
const decodedToken = JSON.parse(atob(accessToken.split('.')[1]));
156-
// The exact key to use may depend from keycloak client & API configuration
157-
if (decodedToken?.roles) result = decodedToken.roles;
158-
else if (decodedToken?.userRoles) result = decodedToken.userRoles;
153+
if (!accessToken) return [];
154+
155+
const decodedToken = JSON.parse(atob(accessToken.split('.')[1]));
156+
// The exact key to use may depend from keycloak client & Cosmo Tech API configuration (c.f. the value of
157+
// csm.platform.authorization.roles-jwt-claim in your k8s tenant secrets)
158+
const rolesTokenAttribute = config?.rolesJwtClaim;
159+
if (rolesTokenAttribute) {
160+
if (decodedToken?.[rolesTokenAttribute]) return decodedToken?.[rolesTokenAttribute];
161+
console.warn(
162+
`Authentication provider configuration defined rolesJwtClaim="${rolesTokenAttribute}" ` +
163+
'but this key was not found in the access token. Please check your webapp and API configuration.'
164+
);
165+
}
166+
167+
if (decodedToken?.roles) return decodedToken.roles; // Legacy default key in token
168+
169+
if (decodedToken?.userRoles) {
170+
console.warn(
171+
"DEPRECATED: the token claim for API roles was automatically found in 'userRoles', but the lookup " +
172+
'for this specific key will be removed in a future version. Please update your webapp configuration to ' +
173+
"explicitly set AUTH_KEYCLOAK_ROLES_JWT_CLAIM to 'userRoles'."
174+
);
175+
return decodedToken.userRoles;
159176
}
160-
return result;
177+
178+
console.warn("Couldn't extract roles from access token. Please check your webapp and API configuration.");
179+
return [];
161180
};
162181

163182
export const isUserSignedIn = async () => {

0 commit comments

Comments
 (0)