@nitra/cf-security 3.3.2 → 3.4.0

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,7 +1,8 @@
1
1
  {
2
2
  "name": "@nitra/cf-security",
3
- "version": "3.3.2",
3
+ "version": "3.4.0",
4
4
  "description": "check header in cloud functions",
5
+ "main": "src/index.js",
5
6
  "type": "module",
6
7
  "exports": {
7
8
  ".": "./src/index.js",
@@ -21,15 +22,7 @@
21
22
  "url": "https://github.com/nitra/cf-security/issues"
22
23
  },
23
24
  "homepage": "https://github.com/nitra/cf-security#readme",
24
- "prettier": "@nitra/prettier-config",
25
- "eslintConfig": {
26
- "extends": [
27
- "@nitra/eslint-config/node"
28
- ]
29
- },
30
25
  "devDependencies": {
31
- "@nitra/eslint-config": "^1.0.9",
32
- "@nitra/prettier-config": "^1.0.1",
33
26
  "test-director": "^7.0.0"
34
27
  },
35
28
  "dependencies": {
package/src/index.js CHANGED
@@ -1,4 +1,3 @@
1
- import getLogger from '@nitra/bunyan/trace'
2
1
  import checkEnv from '@nitra/check-env'
3
2
  checkEnv(['X_NITRA_CF_KEY'])
4
3
 
@@ -9,25 +8,23 @@ checkEnv(['X_NITRA_CF_KEY'])
9
8
  * @return {boolean} if check passed
10
9
  */
11
10
  export const cfSecurity = req => {
12
- const log = getLogger(req)
13
-
14
11
  if (typeof req.headers === 'undefined') {
15
- log.info('Request without headers')
12
+ req.log.info('Request without headers')
16
13
  return false
17
14
  }
18
15
 
19
16
  if (typeof req.headers['x-nitra-cf-key'] === 'undefined') {
20
- log.info('Nitra key not exist in request')
17
+ req.log.info('Nitra key not exist in request')
21
18
  return false
22
19
  }
23
20
 
24
21
  if (req.headers['x-nitra-cf-key'] === 0) {
25
- log.info('Empty Nitra key in headers request')
22
+ req.log.info('Empty Nitra key in headers request')
26
23
  return false
27
24
  }
28
25
 
29
26
  if (req.headers['x-nitra-cf-key'] !== process.env.X_NITRA_CF_KEY) {
30
- log.info('Not equal Nitra key')
27
+ req.log.info('Not equal Nitra key')
31
28
  return false
32
29
  }
33
30
 
package/src/jwt-c.js CHANGED
@@ -8,15 +8,19 @@ import { isDev } from '@nitra/isenv'
8
8
  * @return {string} token if check passed
9
9
  */
10
10
  export default async (req, allowedRoles) => {
11
+ if (!req.raw.headers?.cookie) {
12
+ throw new Error('[verification] missing cookie')
13
+ }
14
+
11
15
  // Читаємо кукі
12
16
  const c = Object.fromEntries(req.raw.headers.cookie.split('; ').map(v => v.split(/=(.*)/s).map(decodeURIComponent)))
13
17
 
14
18
  // Для дева можна й не передавати токен
15
19
  if (isDev) {
16
20
  // Але якщо передали - то беремо контент з нього
17
- if (c['jwt-auth']) {
21
+ if (c.__session) {
18
22
  // ігноруючи expired
19
- const token = await verify(c['jwt-auth'], { ignoreExpiration: true })
23
+ const token = await verify(c.__session, { ignoreExpiration: true })
20
24
  return token.body
21
25
  } else {
22
26
  return { name: 'dev', 'https://hasura.io/jwt/claims': { 'x-hasura-allowed-roles': allowedRoles } }
@@ -24,11 +28,11 @@ export default async (req, allowedRoles) => {
24
28
  }
25
29
 
26
30
  // Перевіряємо токен тільки
27
- if (!c['jwt-auth']) {
31
+ if (!c.__session) {
28
32
  throw new Error('[verification] no authorization header')
29
33
  }
30
34
 
31
- const token = await verify(c['jwt-auth'])
35
+ const token = await verify(c.__session)
32
36
 
33
37
  if (!token) {
34
38
  throw new Error('[verification] invalid token')