@equinor/roma-framework 0.0.7-BETA.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,3 +8,5 @@ export * from './lib/dev-portal/HttpErrorViewer';
8
8
  export * from './lib/api/trade-recap';
9
9
  export * from './lib/api/roma';
10
10
  export * from './lib/app-provider';
11
+ export * from './lib/hooks/use-get-api-roles';
12
+ export * from './lib/hooks/use-has-api-role';
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Parses a JWT token and returns an object with the roles contained in the token.
3
+ * @param token The JWT token to parse.
4
+ * @returns An object with the roles contained in the token.
5
+ */
6
+ export declare function parseJwt(token: string): {
7
+ roles: Array<string>;
8
+ };
9
+ /**
10
+ * Retrieves the roles associated with a specified API from fusion's service discovery module.
11
+ * @param api The name of the API to retrieve roles for.
12
+ * @returns An array of roles associated with the specified API.
13
+ */
14
+ export declare const useGetApiRoles: (api: string) => string[];
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Checks if the current user has a specified role associated with a specified API using the `useGetApiRoles` hook.
3
+ * @param api The name of the API to check for the specified role.
4
+ * @param role The name of the role to check for.
5
+ * @returns True if the current user has the specified role associated with the specified API, false otherwise.
6
+ */
7
+ export declare const useHasApiRole: (api: string, role: string) => boolean;
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@equinor/roma-framework",
3
- "version": "0.0.7-BETA.6",
3
+ "version": "0.0.7",
4
4
  "repository": "https://github.com/equinor/tops-roma",
5
5
  "types": "./index.d.ts",
6
6
  "private": false,
package/index.d.ts CHANGED
@@ -8,3 +8,5 @@ export * from './lib/dev-portal/HttpErrorViewer';
8
8
  export * from './lib/api/trade-recap';
9
9
  export * from './lib/api/roma';
10
10
  export * from './lib/app-provider';
11
+ export * from './lib/hooks/use-get-api-roles';
12
+ export * from './lib/hooks/use-has-api-role';
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Parses a JWT token and returns an object with the roles contained in the token.
3
+ * @param token The JWT token to parse.
4
+ * @returns An object with the roles contained in the token.
5
+ */
6
+ export declare function parseJwt(token: string): {
7
+ roles: Array<string>;
8
+ };
9
+ /**
10
+ * Retrieves the roles associated with a specified API from fusion's service discovery module.
11
+ * @param api The name of the API to retrieve roles for.
12
+ * @returns An array of roles associated with the specified API.
13
+ */
14
+ export declare const useGetApiRoles: (api: string) => string[];
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Checks if the current user has a specified role associated with a specified API using the `useGetApiRoles` hook.
3
+ * @param api The name of the API to check for the specified role.
4
+ * @param role The name of the role to check for.
5
+ * @returns True if the current user has the specified role associated with the specified API, false otherwise.
6
+ */
7
+ export declare const useHasApiRole: (api: string, role: string) => boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@equinor/roma-framework",
3
- "version": "0.0.7-BETA.6",
3
+ "version": "0.0.7",
4
4
  "repository": "https://github.com/equinor/tops-roma",
5
5
  "types": "./index.d.ts",
6
6
  "private": false,
@@ -6216,6 +6216,49 @@ const useGetAllRomaConfigurationByType = (type, options) => {
6216
6216
  query.queryKey = queryOptions.queryKey;
6217
6217
  return query;
6218
6218
  };
6219
+ function parseJwt(token) {
6220
+ const parts = token.split(".");
6221
+ if (parts.length !== 3) {
6222
+ throw new Error("Invalid JWT token format");
6223
+ }
6224
+ const base64Url = parts[1];
6225
+ const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
6226
+ let jsonPayload;
6227
+ try {
6228
+ jsonPayload = decodeURIComponent(
6229
+ window.atob(base64).split("").map(function(c) {
6230
+ return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
6231
+ }).join("")
6232
+ );
6233
+ } catch (error) {
6234
+ throw new Error("Error decoding JWT token");
6235
+ }
6236
+ try {
6237
+ return JSON.parse(jsonPayload);
6238
+ } catch (error) {
6239
+ throw new Error("Invalid JSON payload in JWT token");
6240
+ }
6241
+ }
6242
+ const useGetApiRoles = (api) => {
6243
+ const { modules } = useFramework();
6244
+ const [roles, setRoles] = useState([]);
6245
+ modules.serviceDiscovery.resolveService(api).then(async (s) => {
6246
+ const jwt = await modules.auth.acquireAccessToken({
6247
+ scopes: s.defaultScopes
6248
+ });
6249
+ if (!jwt) {
6250
+ throw new Error(`No jwt found for ${api}`);
6251
+ }
6252
+ const token = parseJwt(jwt);
6253
+ if (roles.length === 0)
6254
+ setRoles(token.roles);
6255
+ });
6256
+ return roles;
6257
+ };
6258
+ const useHasApiRole = (api, role) => {
6259
+ const roles = useGetApiRoles(api);
6260
+ return roles.includes(role);
6261
+ };
6219
6262
  export {
6220
6263
  AppContext,
6221
6264
  AppLoader,
@@ -6243,6 +6286,7 @@ export {
6243
6286
  getGetSettingsByUserIdQueryKey,
6244
6287
  getStreamEventsQueryKey,
6245
6288
  makeComponent,
6289
+ parseJwt,
6246
6290
  useAddRomaConfiguration,
6247
6291
  useAddRomaConfigurationHook,
6248
6292
  useAddRomaConfigurationMutationOptions,
@@ -6299,6 +6343,7 @@ export {
6299
6343
  useGetAllServices,
6300
6344
  useGetAllServicesHook,
6301
6345
  useGetAllServicesQueryOptions,
6346
+ useGetApiRoles,
6302
6347
  useGetAppByKey,
6303
6348
  useGetAppByKeyHook,
6304
6349
  useGetAppByKeyQueryOptions,
@@ -6328,6 +6373,7 @@ export {
6328
6373
  useGetSettingsByUserId,
6329
6374
  useGetSettingsByUserIdHook,
6330
6375
  useGetSettingsByUserIdQueryOptions,
6376
+ useHasApiRole,
6331
6377
  useStreamEvents,
6332
6378
  useStreamEventsHook,
6333
6379
  useStreamEventsQueryOptions,