@nitra/cf-security 4.5.4 → 5.0.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nitra/cf-security",
3
- "version": "4.5.4",
3
+ "version": "5.0.1",
4
4
  "description": "check header in serverless",
5
5
  "type": "module",
6
6
  "types": "./types/index.d.ts",
@@ -24,16 +24,15 @@
24
24
  },
25
25
  "homepage": "https://github.com/nitra/cf-security#readme",
26
26
  "dependencies": {
27
- "@nitra/bunyan": "^2.0.3",
28
27
  "@nitra/check-env": "^3.2.0",
29
28
  "@nitra/isenv": "^2.0.1",
30
- "@nitra/jwt": "^3.7.1"
29
+ "@nitra/jwt": "^4.0.3"
31
30
  },
32
31
  "files": [
33
32
  "src",
34
33
  "types"
35
34
  ],
36
35
  "engines": {
37
- "node": ">=18.0.0"
36
+ "node": ">=20.0.0"
38
37
  }
39
38
  }
package/src/index.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Check request for Nitra security rules
3
+ *
4
+ * @param {object} req - ApolloServer or Express Request for check
5
+ * @returns {boolean} if check passed
6
+ */
7
+ export default function _default(req: object): boolean
package/src/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { checkEnv, env } from '@nitra/check-env'
2
+
2
3
  checkEnv(['X_NITRA_CF_KEY'])
3
4
 
4
5
  /**
package/src/jwt-both.js CHANGED
@@ -1,16 +1,16 @@
1
- import { runSecurityHeader } from './jwt.js'
2
1
  import { runSecurityCookie } from './jwt-c.js'
2
+ import { runSecurityHeader } from './jwt.js'
3
3
 
4
4
  /**
5
5
  * Check request for Nitra security з токеном в кукі
6
6
  * @param {object} req - Fastify Request for check
7
- * @param allowedRoles
7
+ * @param {Array<string>} allowedRoles - Allowed roles
8
8
  * @returns {Promise<object>} token if check passed
9
9
  */
10
10
  export default async (req, allowedRoles) => {
11
+ // oxlint-disable-line require-await
11
12
  if (req.headers.authorization) {
12
13
  return runSecurityHeader(req, allowedRoles)
13
- } else {
14
- return runSecurityCookie(req, allowedRoles)
15
14
  }
15
+ return runSecurityCookie(req, allowedRoles)
16
16
  }
package/src/jwt-c.js CHANGED
@@ -1,11 +1,11 @@
1
- import verify from '@nitra/jwt/verify'
2
1
  import { isDev } from '@nitra/isenv'
2
+ import verify from '@nitra/jwt/verify'
3
3
  import { intersection } from './utils/intersection.js'
4
4
 
5
5
  /**
6
6
  * Check request for Nitra security з токеном в кукі
7
7
  * @param {object} req - Fastify Request for check
8
- * @param allowedRoles
8
+ * @param {Array<string>} allowedRoles - Allowed roles
9
9
  * @returns {Promise<object>} token if check passed
10
10
  */
11
11
  export default async (req, allowedRoles) => {
@@ -26,7 +26,7 @@ export const runSecurityCookie = async (req, allowedRoles) => {
26
26
 
27
27
  // Читаємо кукі
28
28
  const c = Object.fromEntries(
29
- req.raw.headers.cookie.split('; ').map(v => v.split(/=(.*)/s).map(x => decodeURIComponent(x)))
29
+ req.raw.headers.cookie.split('; ').map(v => v.split(/[=](.*)/s).map(x => decodeURIComponent(x)))
30
30
  )
31
31
 
32
32
  // Для дева можна й не передавати токен
@@ -36,11 +36,10 @@ export const runSecurityCookie = async (req, allowedRoles) => {
36
36
  // ігноруючи expired
37
37
  const token = await verify(c.__session, { ignoreExpiration: true })
38
38
  return { raw: c.__session, parsed: token.body }
39
- } else {
40
- return {
41
- raw: 0,
42
- parsed: { name: 'dev', 'https://hasura.io/jwt/claims': { 'x-hasura-allowed-roles': allowedRoles } }
43
- }
39
+ }
40
+ return {
41
+ raw: 0,
42
+ parsed: { name: 'dev', 'https://hasura.io/jwt/claims': { 'x-hasura-allowed-roles': allowedRoles } }
44
43
  }
45
44
  }
46
45
 
package/src/jwt.js CHANGED
@@ -38,11 +38,10 @@ export const runSecurityHeader = async (req, allowedRoles) => {
38
38
 
39
39
  const authHeaders = req.headers.authorization.split(' ')
40
40
  return { raw: authHeaders[1], parsed: token.body }
41
- } else {
42
- return {
43
- raw: 0,
44
- parsed: { name: 'dev', 'https://hasura.io/jwt/claims': { 'x-hasura-allowed-roles': allowedRoles } }
45
- }
41
+ }
42
+ return {
43
+ raw: 0,
44
+ parsed: { name: 'dev', 'https://hasura.io/jwt/claims': { 'x-hasura-allowed-roles': allowedRoles } }
46
45
  }
47
46
  }
48
47
 
@@ -1,7 +1,7 @@
1
1
  /**
2
- *
3
- * @param a
4
- * @param b
2
+ * @param {Array<string>} a - First array
3
+ * @param {Array<string>} b - Second array
4
+ * @returns {Array<string>} - Intersection of two arrays.
5
5
  */
6
6
  export function intersection(a, b) {
7
7
  const setA = new Set(a)
package/types/index.d.ts CHANGED
@@ -2,6 +2,6 @@
2
2
  * Check request for Nitra security rules
3
3
  *
4
4
  * @param {object} req - ApolloServer or Express Request for check
5
- * @return {boolean} if check passed
5
+ * @returns {boolean} if check passed
6
6
  */
7
- export default function _default(req: object): boolean;
7
+ export default function _default(req: object): boolean