Skip to content

Commit 4e00947

Browse files
authored
🔀 Merge pull request #1894 from Alexis-BX/Fix-Oidc-user-islogin-and-admin
Fix OIDC getUser and added admin role and group
2 parents d4fa310 + ac13e54 commit 4e00947

File tree

6 files changed

+56
-4
lines changed

6 files changed

+56
-4
lines changed

docs/authentication.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ appConfig:
286286
clientId: [registered client id]
287287
endpoint: [OIDC endpoint]
288288
scope: [The scope(s) to request from the OIDC provider]
289+
adminGroup: admin
289290
```
290291

291292
Because Dashy is a SPA, a [public client](https://datatracker.ietf.org/doc/html/rfc6749#section-2.1) registration with PKCE is needed.

docs/configuring.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ For more info, see the **[Authentication Docs](/docs/authentication.md)**
204204
--- | --- | --- | ---
205205
**`clientId`** | `string` | Required | The client id registered in the OIDC server
206206
**`endpoint`** | `string` | Required | The URL of the OIDC server that should be used.
207+
**`adminRole`** | `string` | _Optional_ | The role that will be considered as admin.
208+
**`adminGroup`** | `string` | _Optional_ | The group that will be considered as admin.
207209
**`scope`** | `string` | Required | The scope(s) to request from the OIDC provider
208210

209211
**[⬆️ Back to Top](#configuring)**

src/utils/Auth.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,19 @@ const getUsers = () => {
3131
return []; // Support for old data structure now removed
3232
}
3333
// Otherwise, return the users array, if available
34-
return auth.users || [];
34+
35+
const users = auth.users || [];
36+
if (isOidcEnabled()) {
37+
if (localStorage[localStorageKeys.USERNAME]) {
38+
const user = {
39+
user: localStorage[localStorageKeys.USERNAME],
40+
type: localStorage[localStorageKeys.ISADMIN] === 'true' ? 'admin' : 'normal',
41+
};
42+
users.push(user);
43+
}
44+
}
45+
46+
return users;
3547
};
3648

3749
/**
@@ -80,6 +92,17 @@ export const makeBasicAuthHeaders = () => {
8092
export const isLoggedIn = () => {
8193
const users = getUsers();
8294
const cookieToken = getCookieToken();
95+
96+
if (isOidcEnabled()) {
97+
const username = localStorage[localStorageKeys.USERNAME]; // Get username
98+
if (!username) return false; // No username
99+
return users.some((user) => {
100+
if (user.user === username || generateUserToken(user) === cookieToken) {
101+
return true;
102+
} else return false;
103+
});
104+
}
105+
83106
return users.some((user) => {
84107
if (generateUserToken(user) === cookieToken) {
85108
localStorage.setItem(localStorageKeys.USERNAME, user.user);

src/utils/ConfigSchema.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,18 @@
566566
"type": "string",
567567
"description": "ClientId from OIDC provider"
568568
},
569+
"adminRole" : {
570+
"title": "Admin Role",
571+
"type": "string",
572+
"default": false,
573+
"description": "The role that will be considered as admin. If not set, no roles will be considered as admin"
574+
},
575+
"adminGroup" : {
576+
"title": "Admin Group",
577+
"type": "string",
578+
"default": false,
579+
"description": "The group that will be considered as admin. If not set, no groups will be considered as admin"
580+
},
569581
"scope" : {
570582
"title": "OIDC Scope",
571583
"type": "string",

src/utils/OidcAuth.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ const getAppConfig = () => {
1313
class OidcAuth {
1414
constructor() {
1515
const { auth } = getAppConfig();
16-
const { clientId, endpoint, scope } = auth.oidc;
16+
const {
17+
clientId,
18+
endpoint,
19+
scope,
20+
adminGroup,
21+
adminRole,
22+
} = auth.oidc;
1723
const settings = {
1824
userStore: new WebStorageStateStore({ store: window.localStorage }),
1925
authority: endpoint,
@@ -25,6 +31,8 @@ class OidcAuth {
2531
filterProtocolClaims: true,
2632
};
2733

34+
this.adminGroup = adminGroup;
35+
this.adminRole = adminRole;
2836
this.userManager = new UserManager(settings);
2937
}
3038

@@ -43,22 +51,27 @@ class OidcAuth {
4351
if (user === null) {
4452
await this.userManager.signinRedirect();
4553
} else {
46-
const { roles, groups } = user.profile;
54+
const { roles = [], groups = [] } = user.profile;
4755
const info = {
4856
groups,
4957
roles,
5058
};
59+
const isAdmin = (Array.isArray(groups) && groups.includes(this.adminGroup))
60+
|| (Array.isArray(roles) && roles.includes(this.adminRole))
61+
|| false;
5162

52-
statusMsg(`user: ${user.profile.preferred_username}`, JSON.stringify(info));
63+
statusMsg(`user: ${user.profile.preferred_username} admin: ${isAdmin}`, JSON.stringify(info));
5364

5465
localStorage.setItem(localStorageKeys.KEYCLOAK_INFO, JSON.stringify(info));
5566
localStorage.setItem(localStorageKeys.USERNAME, user.profile.preferred_username);
67+
localStorage.setItem(localStorageKeys.ISADMIN, isAdmin);
5668
}
5769
}
5870

5971
async logout() {
6072
localStorage.removeItem(localStorageKeys.USERNAME);
6173
localStorage.removeItem(localStorageKeys.KEYCLOAK_INFO);
74+
localStorage.removeItem(localStorageKeys.ISADMIN);
6275

6376
try {
6477
await this.userManager.signoutRedirect();

src/utils/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ module.exports = {
137137
MOST_USED: 'mostUsed',
138138
LAST_USED: 'lastUsed',
139139
KEYCLOAK_INFO: 'keycloakInfo',
140+
ISADMIN: 'isAdmin',
140141
DISABLE_CRITICAL_WARNING: 'disableCriticalWarning',
141142
},
142143
/* Key names for cookie identifiers */

0 commit comments

Comments
 (0)