Skip to content

Commit 237a7ed

Browse files
committed
fix updates because of change to Keycloak 25
1 parent 7677e77 commit 237a7ed

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,16 @@ Then you can run the REST-examples in the `http` directory.
323323
## Keycloak Setup
324324
In order for this setup to work correctly, you'll have to have a Keycloak-client (private with secret) and standard-authorization-flow enabled (should be enabled anyway).
325325
You need the following user-attributes of type `string` with mapper to the token.
326-
327-
| ATTRIBUTE-NAME | TYPE | DESCRIPTION |
328-
| ------------------------------------------------------------------------------------------------------------------ | ------ | ----------------------------------------------------------------------------------------------------- |
329-
| Client -> Client Scopes -> ...-dedicated -> Add Mapper (User Attribute)<br>`config` | string | Holds several preference-values like dark-mode or not or the preferred font, fontsize or line-height. |
330-
| Client -> Client Scopes -> ...-dedicated -> Add Mapper (User Attribute)<br>`lastVisitedUrl` | string | Holds the last-visited page of the current user. |
331-
| Client -> Client Scopes -> ...-dedicated -> Add Mapper (User Attribute)<br>map the field `LDAP_ENTRY_DN` to `ldap` | string | Holds the users LDAP information (class, teacher, etc.). |
326+
You also need to add the user-attributes to the user-profile first (`Realm settings` -> `User profile (Attribute group = none, not user-metadata)`) in order to allow for the addition of data to your users. Be sure to set those to `allow edit and view for User and Admin` so that the application is able to change the values.
327+
The application uses the following endpoints of the Keycloak-API to do that:
328+
- `GET {{keycloakUrl}}/realms/{{realm}}/account`
329+
- `POST {{keycloakUrl}}/realms/{{realm}}/account`
330+
The user-metadata field `LDAP_ENTRY_DN` will be automatically present because of the LDAP mapper in your Keycloak instance. It will be readable from the `access-token`. So there are no additional setup-steps requried.
331+
332+
| ATTRIBUTE-NAME | TYPE | DESCRIPTION |
333+
| ------------------------------------------------------------------------------------------- | ------ | ----------------------------------------------------------------------------------------------------- |
334+
| Client -> Client Scopes -> ...-dedicated -> Add Mapper (User Attribute)<br>`config` | string | Holds several preference-values like dark-mode or not or the preferred font, fontsize or line-height. |
335+
| Client -> Client Scopes -> ...-dedicated -> Add Mapper (User Attribute)<br>`lastVisitedUrl` | string | Holds the last-visited page of the current user. |
332336
## MD-File Conversion
333337
This is done using [marked](https://www.npmjs.com/package/marked) which is installed on the web-server (via `package.json`).
334338
With the help of this you can link to any MD-file and show it in the context of your site.

middlewares/keycloak-middleware.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ export function getLdapGroups(req) {
156156
return r;
157157
}
158158
const ldap = req.user.ldap;
159+
// console.log("LDAP-String", ldap)
159160

160161
// Regular expression to match "OU=..."
161162
const regex = /OU=[^,]*/gi;
@@ -170,7 +171,7 @@ export function getLdapGroups(req) {
170171
r[value] = true;
171172
});
172173
}
173-
174+
// console.log("LDAP-Groups", r);
174175
return r;
175176
}
176177

@@ -194,13 +195,14 @@ export async function getUserAttributes(req, getAll = false) {
194195
return response.json();
195196
})
196197
.then((data) => {
197-
for(let key in data) {
198-
data[key] = data[key][0];
198+
// console.log("data of user attributes", data);
199+
for(let key in data.attributes) {
200+
data.attributes[key] = data.attributes[key][0];
199201
}
200202
if(getAll) {
201-
// Remove fields from the object that are not needed.
202-
let { userProfileMetadata, id, username, emailVerified, ...d} = data;
203-
return d;
203+
// Remove fields from the object that are not needed.
204+
let { userProfileMetadata, id, username, emailVerified, ...d} = data;
205+
return d;
204206
}
205207
return data;
206208
})
@@ -222,12 +224,12 @@ export async function setUserAttribute(req, attributeName, attributeValue) {
222224

223225
// Fetch current user attributes
224226
const currentAttributes = await getUserAttributes(req, true);
227+
// console.log("current attributes", currentAttributes);
225228

226-
// Merge current and new attributes at the root level
227-
let mergedAttributes = { ...currentAttributes, [attributeName]: attributeValue };
228-
mergedAttributes = {
229-
attributes: mergedAttributes
230-
}
229+
// Merge current and new attributes
230+
const mas = { ...currentAttributes.attributes, [attributeName]: attributeValue };
231+
const mergedAttributes = { ...currentAttributes, attributes: mas };
232+
// console.log("merged attributes before saving", mergedAttributes);
231233

232234
const result = fetch(url, {
233235
method: "POST",

utils.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,17 @@ async function hasRoles(req, clientRoles, all, override) {
9696
let clientAccess = null;
9797
const attributes = await getUserAttributes(req);
9898
const ccr = await getClientRoles(req, clientRoles);
99+
// console.log("Client roles", ccr);
100+
// console.log("Request user rolesCalculated", req.user.rolesCalculated);
99101
if (
100102
req.user.rolesCalculated !== undefined &&
101103
req.user.rolesCalculated !== null
102104
) {
103-
if (attributes && attributes.config) {
104-
const a = JSON.parse(attributes.config);
105+
// console.log("attributes", attributes);
106+
if (attributes && attributes.attributes && attributes.attributes.config) {
107+
const a = JSON.parse(attributes.attributes.config);
105108
const r = JSON.parse(req.user.rolesCalculated);
109+
// console.log("Roles Calculated", r);
106110
const cr = await getClientRoles(req, clientRoles);
107111
if (cr) {
108112
for (const role of cr) {

0 commit comments

Comments
 (0)