From 64c8fed9f40032f58b0a752865f9fe5ff5a77bfd Mon Sep 17 00:00:00 2001 From: alma Date: Fri, 18 Apr 2025 15:10:03 +0200 Subject: [PATCH] session correction sidebar items 8 --- app/api/auth/[...nextauth]/route.ts | 29 ++++ node_modules/.package-lock.json | 9 ++ node_modules/jwt-decode/LICENSE | 21 +++ node_modules/jwt-decode/README.md | 134 ++++++++++++++++++ node_modules/jwt-decode/build/cjs/index.d.ts | 23 +++ node_modules/jwt-decode/build/cjs/index.js | 62 ++++++++ .../jwt-decode/build/cjs/package.json | 1 + node_modules/jwt-decode/build/esm/index.d.ts | 23 +++ node_modules/jwt-decode/build/esm/index.js | 57 ++++++++ node_modules/jwt-decode/package.json | 81 +++++++++++ package-lock.json | 10 ++ package.json | 1 + yarn.lock | 5 + 13 files changed, 456 insertions(+) create mode 100644 node_modules/jwt-decode/LICENSE create mode 100644 node_modules/jwt-decode/README.md create mode 100644 node_modules/jwt-decode/build/cjs/index.d.ts create mode 100644 node_modules/jwt-decode/build/cjs/index.js create mode 100644 node_modules/jwt-decode/build/cjs/package.json create mode 100644 node_modules/jwt-decode/build/esm/index.d.ts create mode 100644 node_modules/jwt-decode/build/esm/index.js create mode 100644 node_modules/jwt-decode/package.json diff --git a/app/api/auth/[...nextauth]/route.ts b/app/api/auth/[...nextauth]/route.ts index 15b13778..641eb7ad 100644 --- a/app/api/auth/[...nextauth]/route.ts +++ b/app/api/auth/[...nextauth]/route.ts @@ -1,5 +1,6 @@ import NextAuth, { NextAuthOptions } from "next-auth"; import KeycloakProvider from "next-auth/providers/keycloak"; +import { jwtDecode } from "jwt-decode"; interface KeycloakProfile { sub: string; @@ -14,6 +15,13 @@ interface KeycloakProfile { }; } +interface DecodedToken { + realm_access?: { + roles: string[]; + }; + [key: string]: any; +} + declare module "next-auth" { interface Session { user: { @@ -170,6 +178,27 @@ export const authOptions: NextAuthOptions = { tokenRoles: token.role, token }); + } else if (token.accessToken) { + // Decode the token to get roles + try { + const decoded = jwtDecode(token.accessToken); + console.log('Decoded token:', decoded); + + if (decoded.realm_access?.roles) { + const roles = decoded.realm_access.roles; + console.log('Decoded token roles:', roles); + + // Clean up roles by removing ROLE_ prefix and converting to lowercase + const cleanRoles = roles.map((role: string) => + role.replace(/^ROLE_/, '').toLowerCase() + ); + + console.log('Decoded token cleaned roles:', cleanRoles); + token.role = cleanRoles; + } + } catch (error) { + console.error('Error decoding token:', error); + } } if (Date.now() < (token.accessTokenExpires as number) * 1000) { diff --git a/node_modules/.package-lock.json b/node_modules/.package-lock.json index 790f6f07..f7ed9ace 100644 --- a/node_modules/.package-lock.json +++ b/node_modules/.package-lock.json @@ -3495,6 +3495,15 @@ "integrity": "sha512-4bYVV3aAMtDTTu4+xsDYa6sy9GyJ69/amsu9sYF2zqjiEoZA5xJi3BrfX3uY+/IekIu7MwdObdbDWpoZdBv3/A==", "license": "MIT" }, + "node_modules/jwt-decode": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/jwt-decode/-/jwt-decode-4.0.0.tgz", + "integrity": "sha512-+KJGIyHgkGuIq3IEBNftfhW/LfWhXUIY6OmyVWjliu5KH1y0fw7VQ8YndE2O4qZdMSd9SqbnC8GOcZEy0Om7sA==", + "license": "MIT", + "engines": { + "node": ">=18" + } + }, "node_modules/leac": { "version": "0.6.0", "resolved": "https://registry.npmjs.org/leac/-/leac-0.6.0.tgz", diff --git a/node_modules/jwt-decode/LICENSE b/node_modules/jwt-decode/LICENSE new file mode 100644 index 00000000..bcd1854c --- /dev/null +++ b/node_modules/jwt-decode/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Auth0, Inc. (http://auth0.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/node_modules/jwt-decode/README.md b/node_modules/jwt-decode/README.md new file mode 100644 index 00000000..dc4ce062 --- /dev/null +++ b/node_modules/jwt-decode/README.md @@ -0,0 +1,134 @@ +![Browser library that helps decoding JWT tokens which are Base64Url encoded](https://cdn.auth0.com/website/sdks/banners/jwt-decode-banner.png) + +**IMPORTANT:** This library doesn't validate the token, any well-formed JWT can be decoded. You should validate the token in your server-side logic by using something like [express-jwt](https://github.com/auth0/express-jwt), [koa-jwt](https://github.com/stiang/koa-jwt), [Microsoft.AspNetCore.Authentication.JwtBearer](https://www.nuget.org/packages/Microsoft.AspNetCore.Authentication.JwtBearer), etc. + +![Release](https://img.shields.io/npm/v/jwt-decode) +![Downloads](https://img.shields.io/npm/dw/jwt-decode) +[![License](https://img.shields.io/:license-MIT-blue.svg?style=flat)](https://opensource.org/licenses/MIT) +[![CircleCI](https://img.shields.io/circleci/build/github/auth0/jwt-decode)](https://circleci.com/gh/auth0/jwt-decode) + +:books: [Documentation](#documentation) - :rocket: [Getting Started](#getting-started) - :speech_balloon: [Feedback](#feedback) + +## Documentation + +- [Docs site](https://www.auth0.com/docs) - explore our docs site and learn more about Auth0. + +## Getting started + +### Installation + +Install with NPM or Yarn. + +Run `npm install jwt-decode` or `yarn add jwt-decode` to install the library. + +### Usage + +```js +import { jwtDecode } from "jwt-decode"; + +const token = "eyJ0eXAiO.../// jwt token"; +const decoded = jwtDecode(token); + +console.log(decoded); + +/* prints: + * { + * foo: "bar", + * exp: 1393286893, + * iat: 1393268893 + * } + */ + +// decode header by passing in options (useful for when you need `kid` to verify a JWT): +const decodedHeader = jwtDecode(token, { header: true }); +console.log(decodedHeader); + +/* prints: + * { + * typ: "JWT", + * alg: "HS256" + * } + */ +``` + +**Note:** A falsy or malformed token will throw an `InvalidTokenError` error; see below for more information on specific errors. + +## Errors + +This library works with valid JSON web tokens. The basic format of these token is +``` +[part1].[part2].[part3] +``` +All parts are supposed to be valid base64 (url) encoded json. +Depending on the `{ header: