0.0.27 • Published 12 months ago

maxi-validate-permissions-test v0.0.27

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

MAXI VALIDATE PERMISSIONS

This library provides utilities to manage and validate user permissions using JSON Web Tokens (JWT). The library includes functionalities to decode JWT, check token validity, and fetch user permissions.

Table of Contents

Installing

To use this library, you need to have maxi-validate-permissions-test installed. You can install this dependency:

Using npm:

$ npm install maxi-validate-permissions-test

Using bower:

$ bower install maxi-validate-permissions-test

Using yarn:

$ yarn add maxi-validate-permissions-test

Using pnpm:

$ pnpm add maxi-validate-permissions-test

Explanation

This code is a TypeScript module that includes functions for handling user permissions and validating them based on a token received from an API. Here is a step-by-step explanation of the code:

  1. The code imports necessary modules such as axios for making HTTP requests, jwt-decode for decoding JWT tokens, and luxon for date and time manipulation.

  2. It defines various interfaces and enums for handling token decoding, response data, user roles, systems, modules, permissions, and permission information.

  3. It sets a URL for an API endpoint that checks user permissions.

  4. The callApi function is defined to make a GET request to the API endpoint with a provided token. It returns a response containing the status, message, and user role data.

  5. The decodeToken function decodes a JWT token and returns the decoded token along with a status and message.

  6. The isTokenActive function checks if a token is valid and active by decoding the token and checking its expiration time against the current time.

  7. The getPermissionsUnique function retrieves unique permissions for a specific system and permission name from a user role.

  8. The getPermissionsInGroup function retrieves permissions for a group of systems and permission names from a user role.

  9. The validatePermission function validates a single permission based on the provided token, permission system, and permission name by calling isTokenActive , callApi , and getPermissionsUnique functions.

  10. The validateGroupPermissions function validates a group of permissions for a system based on the provided token, system name, and array of permissions by calling isTokenActive , callApi , and getPermissionsInGroup functions.

In summary, this code module provides functions to handle user permissions, decode tokens, check token validity, and validate permissions based on the user's role and provided token.

Examples

Validate Permission

import { validatePermission } from 'maxi-validate-permissions-test';
//const { validatePermission } = require('maxi-validate-permissions-test'); // legacy way

const token = "*****";
const permissionSystem = "test";
const permissionName = "TEST_USER_MANGEMENT_PERM_1000";

validatePermission(
  token,
  permissionSystem,
  permissionName,
 ).then((data) => console.log(data));

Validate Group Permissions

import { validateGroupPermissions } from 'maxi-validate-permissions-test';
//const { validateGroupPermissions } = require('maxi-validate-permissions-test'); // legacy way

const token = "*****";
const systemName = "test";

const permissions = [
  "TEST_USER_MANAGMENT_PERM_2",
  "TEST_USER_MANAGMENT_PERM_1",
  "TEST_USER_MANAGMENT_PERM_10",
  "TEST_USER_MANAGMENT_PERM_3",
];

validateGroupPermissions(token, systemName, permissions).then((data) =>
  console.log(data)
);

Response

When request failed.

{ status: 'error', message: 'Request failed.' }

When token is invalid.

{ status: 'error', message: 'The token is invalid.' }

When token is expired.

{ status: 'error', message: 'The token has expired.' }

When permission system doesn´t exist.

{ status: 'success', message: 'Request successful.', permission: null }

Validate Permission

When permission doesn´t exist.

{
    status: 'success',
    message: 'Request successful.',
    permission: { TEST_USER_MANGEMENT_PERM_1000: null }
}

When the user has access to the permission.

{
    status: 'success',
    message: 'Request successful.',
    permission: { TEST_USER_MANAGMENT_PERM_2: { view: true, edit: false } }
}

Validate Group Permissions

List of permissions that the user has access to use.

{
    status: 'success',
    message: 'Request successful.',
    permission: [
      { TEST_USER_MANAGMENT_PERM_2: {"view":true,"edit":false} },
      { TEST_USER_MANAGMENT_PERM_1: null },
      { TEST_USER_MANAGMENT_PERM_10: null },
      { TEST_USER_MANAGMENT_PERM_3: {"view":true,"edit":false} }
    ]
}
0.0.27

12 months ago

0.0.26

12 months ago

0.0.25

12 months ago

0.0.24

12 months ago

0.0.23

12 months ago

0.0.22

12 months ago

0.0.21

12 months ago

0.0.20

12 months ago

0.0.19

12 months ago

0.0.18

12 months ago

0.0.17

12 months ago

0.0.16

1 year ago

0.0.15

1 year ago

0.0.14

1 year ago

0.0.13

1 year ago

0.0.12

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago