Skip to content

Commit

Permalink
Improve process and change libs
Browse files Browse the repository at this point in the history
Improve process and change libs
  • Loading branch information
siriusfreak committed Jan 25, 2024
1 parent 17926ce commit 17af179
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 26 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ OPENID_SESSION_SECRET=
OPENID_SCOPE="openid profile email"
OPENID_CALLBACK_URL=/oauth/openid/callback
OPENID_REQUIRED_ROLE=
OPENID_REQUIRED_ROLE_PARAMETER_PATH=

OPENID_BUTTON_LABEL=
OPENID_IMAGE_URL=
Expand Down
22 changes: 12 additions & 10 deletions api/strategies/openidStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const passport = require('passport');
const { Issuer, Strategy: OpenIDStrategy } = require('openid-client');
const { logger } = require('~/config');
const User = require('~/models/User');
const jwtDecode = require('jwt-decode');
const jwtDecode = require('jsonwebtoken/decode');

let crypto;
try {
Expand Down Expand Up @@ -46,6 +46,7 @@ async function setupOpenId() {
redirect_uris: [process.env.DOMAIN_SERVER + process.env.OPENID_CALLBACK_URL],
});
const requiredRole = process.env.OPENID_REQUIRED_ROLE;
const requiredRoleParameterPath = process.env.OPENID_REQUIRED_ROLE_PARAMETER_PATH;

const openidLogin = new OpenIDStrategy(
{
Expand Down Expand Up @@ -73,16 +74,17 @@ async function setupOpenId() {
fullName = userinfo.username || userinfo.email;
}

const decodedAccessToken = jwtDecode.jwtDecode(tokenset.access_token);
let roles = [];
if (decodedAccessToken.roles) {
roles = decodedAccessToken.roles;
} else if (decodedAccessToken.realm_access && decodedAccessToken.realm_access.roles) {
roles = decodedAccessToken.realm_access.roles;
}
user.roles = roles;
const decodedAccessToken = jwtDecode(tokenset.access_token);
const pathParts = requiredRoleParameterPath.split('.');
user.roles = pathParts.reduce((o, key) => {
if (o === null || o === undefined || !(key in o)) {
console.error(`Key '${decodedAccessToken}' not found in access token!`);
return [];
}
return o[key];
}, decodedAccessToken);

if (requiredRole && !roles.includes(requiredRole)) {
if (requiredRole && !user.roles.includes(requiredRole)) {
return done(null, false, {
message: `You must have the "${requiredRole}" role to log in.`,
});
Expand Down
4 changes: 4 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ services:
image: mongo
restart: always
user: "${UID}:${GID}"
ports:
- 27017:27017
volumes:
- ./data-node:/data/db
command: mongod --noauth
meilisearch:
container_name: chat-meilisearch
image: getmeili/meilisearch:v1.5
restart: always
ports:
- 7700:7700
env_file:
- .env
user: "${UID}:${GID}"
Expand Down
9 changes: 7 additions & 2 deletions docs/install/configuration/user_auth_system.md
Original file line number Diff line number Diff line change
Expand Up @@ -604,11 +604,15 @@ OPENID_CALLBACK_URL=/oauth/openid/callback # this should be the same for everyon
- Go to 'Users', select a user, and go to the 'Role Mappings' tab.
- Assign the appropriate role (that matches `OPENID_REQUIRED_ROLE`) to the user.

7. **Configure Client Scopes (Optional):**
7. **Get path of roles list inside access token:**
- Decode your jwtToken from OpenID provider and determine path for roles list inside access token. For example, if you are using Keycloak, the path is `realm_access.roles`.
- Put this path in `OPENID_REQUIRED_ROLE_PARAMETER_PATH` variable in `.env` file.

8. **Configure Client Scopes (Optional):**
- If you want to include role information in the token, add a client scope and mapper.
- Go to 'Client Scopes', create a new scope, and add a mapper to include the role information in the token.

8. **Update Your Project's Configuration:**
9. **Update Your Project's Configuration:**
- Open the `.env` file in your project folder and add the following variables:
```
OPENID_ISSUER=http://localhost:8080/auth/realms/[YourRealmName]
Expand All @@ -617,6 +621,7 @@ OPENID_CALLBACK_URL=/oauth/openid/callback # this should be the same for everyon
OPENID_CALLBACK_URL=http://localhost:3080/oauth/openid/callback
OPENID_SCOPE="openid profile email"
OPENID_REQUIRED_ROLE=[YourRequiredRole]
OPENID_REQUIRED_ROLE_PARAMETER_PATH="realm_access.roles"
```


Expand Down
11 changes: 0 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,5 @@
"admin/",
"packages/"
]
},
"dependencies": {
"jwt-decode": "^4.0.0"
}
}

0 comments on commit 17af179

Please sign in to comment.