@nitra/cf-security 4.0.0 → 4.0.2

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/jwt.js +13 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nitra/cf-security",
3
- "version": "4.0.0",
3
+ "version": "4.0.2",
4
4
  "description": "check header in serverless",
5
5
  "type": "module",
6
6
  "exports": {
package/src/jwt.js CHANGED
@@ -5,7 +5,8 @@ import { isDev } from '@nitra/isenv'
5
5
  * Check request for Nitra security rules WI
6
6
  *
7
7
  * @param {object} req - Fastify Request for check
8
- * @return {string} token if check passed
8
+ * @param {Array} allowedRoles - Allowed roles
9
+ * @return {Promise<string>} token if check passed
9
10
  */
10
11
  export default async (req, allowedRoles) => {
11
12
  // Для дева можна й не передавати токен
@@ -14,9 +15,19 @@ export default async (req, allowedRoles) => {
14
15
  if (req.headers?.authorization) {
15
16
  // ігноруючи expired
16
17
  const token = await verify(req.headers.authorization.split(' ')[1], { ignoreExpiration: true })
18
+
19
+ const intersectRoles = intersection(
20
+ token.body['https://hasura.io/jwt/claims']['x-hasura-allowed-roles'],
21
+ allowedRoles
22
+ )
23
+
24
+ if (intersectRoles.length === 0) {
25
+ throw new Error(`[verification] unallowed roles ${allowedRoles}`)
26
+ }
27
+
17
28
  return token.body
18
29
  } else {
19
- return { name: 'dev', 'https://hasura.io/jwt/claims': { 'x-hasura-allowed-roles': allowedRoles } }
30
+ return JSON.stringify({ name: 'dev', 'https://hasura.io/jwt/claims': { 'x-hasura-allowed-roles': allowedRoles } })
20
31
  }
21
32
  }
22
33