@greatapps/common 1.1.591 → 1.1.593

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.
@@ -1,23 +1,31 @@
1
1
  import "server-only";
2
2
  import { cookies } from "next/headers";
3
+ import { decode } from "@greatapps/jwt";
3
4
  import { ApiError } from "../../../infra/api/types";
4
5
  const AUTH_COOKIE_NAME = "greatapps";
5
6
  async function requireTokenNotExpired() {
6
7
  if (process.env.DUMMY_AUTH_TOKEN) return;
8
+ const secretJwt = process.env.SECRET_JWT;
9
+ if (!secretJwt) return;
7
10
  const cookieStore = await cookies();
8
11
  const token = cookieStore.get(AUTH_COOKIE_NAME)?.value;
9
12
  if (!token) {
10
- throw new ApiError("Sess\xE3o inv\xE1lida ou expirada", "SESSION_INVALID", 401);
13
+ throw new ApiError("Sess\xE3o inv\xE1lida ou expirada 1", "SESSION_INVALID", 401);
11
14
  }
12
15
  try {
13
16
  const payload = JSON.parse(atob(token.split(".")[1]));
14
- const now = Math.floor(Date.now() / 1e3);
15
- if (!payload.exp || payload.exp <= now) {
16
- throw new ApiError("Sess\xE3o inv\xE1lida ou expirada", "SESSION_INVALID", 401);
17
+ const idWl = payload.id_wl;
18
+ if (!idWl) {
19
+ throw new ApiError("Sess\xE3o inv\xE1lida ou expirada 2", "SESSION_INVALID", 401);
20
+ }
21
+ const secret = `${secretJwt}---${idWl}`;
22
+ const decoded = await decode(token, secret);
23
+ if (!decoded) {
24
+ throw new ApiError("Sess\xE3o inv\xE1lida ou expirada 3", "SESSION_INVALID", 401);
17
25
  }
18
26
  } catch (err) {
19
27
  if (err instanceof ApiError) throw err;
20
- throw new ApiError("Sess\xE3o inv\xE1lida ou expirada", "SESSION_INVALID", 401);
28
+ throw new ApiError("Sess\xE3o inv\xE1lida ou expirada 4", "SESSION_INVALID", 401);
21
29
  }
22
30
  }
23
31
  export {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/modules/auth/utils/require-token-not-expired.ts"],"sourcesContent":["import 'server-only';\n\nimport { cookies } from 'next/headers';\nimport { ApiError } from '../../../infra/api/types';\n\nconst AUTH_COOKIE_NAME = 'greatapps';\n\n/**\n * Verifica se o JWT do cookie `greatapps` não expirou checando o campo `exp`\n * no payload, sem bater no backend.\n *\n * - Se o token não existe → SESSION_INVALID 401\n * - Se o token é malformado → SESSION_INVALID 401\n * - Se `exp` não existe ou já passou → SESSION_INVALID 401\n *\n * Isso substitui o `requireValidSession` (que fazia POST /auth/keep) no\n * `safeMutationAction`, eliminando 1 round-trip ao backend por mutation.\n */\nexport async function requireTokenNotExpired(): Promise<void> {\n if (process.env.DUMMY_AUTH_TOKEN) return;\n\n const cookieStore = await cookies();\n const token = cookieStore.get(AUTH_COOKIE_NAME)?.value;\n\n if (!token) {\n throw new ApiError('Sessão inválida ou expirada', 'SESSION_INVALID', 401);\n }\n\n try {\n const payload = JSON.parse(atob(token.split('.')[1]));\n const now = Math.floor(Date.now() / 1000);\n\n if (!payload.exp || payload.exp <= now) {\n throw new ApiError('Sessão inválida ou expirada', 'SESSION_INVALID', 401);\n }\n } catch (err) {\n if (err instanceof ApiError) throw err;\n throw new ApiError('Sessão inválida ou expirada', 'SESSION_INVALID', 401);\n }\n}\n"],"mappings":"AAAA,OAAO;AAEP,SAAS,eAAe;AACxB,SAAS,gBAAgB;AAEzB,MAAM,mBAAmB;AAazB,eAAsB,yBAAwC;AAC5D,MAAI,QAAQ,IAAI,iBAAkB;AAElC,QAAM,cAAc,MAAM,QAAQ;AAClC,QAAM,QAAQ,YAAY,IAAI,gBAAgB,GAAG;AAEjD,MAAI,CAAC,OAAO;AACV,UAAM,IAAI,SAAS,qCAA+B,mBAAmB,GAAG;AAAA,EAC1E;AAEA,MAAI;AACF,UAAM,UAAU,KAAK,MAAM,KAAK,MAAM,MAAM,GAAG,EAAE,CAAC,CAAC,CAAC;AACpD,UAAM,MAAM,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI;AAExC,QAAI,CAAC,QAAQ,OAAO,QAAQ,OAAO,KAAK;AACtC,YAAM,IAAI,SAAS,qCAA+B,mBAAmB,GAAG;AAAA,IAC1E;AAAA,EACF,SAAS,KAAK;AACZ,QAAI,eAAe,SAAU,OAAM;AACnC,UAAM,IAAI,SAAS,qCAA+B,mBAAmB,GAAG;AAAA,EAC1E;AACF;","names":[]}
1
+ {"version":3,"sources":["../../../../src/modules/auth/utils/require-token-not-expired.ts"],"sourcesContent":["import 'server-only';\n\nimport { cookies } from 'next/headers';\nimport { decode } from '@greatapps/jwt';\nimport { ApiError } from '../../../infra/api/types';\n\nconst AUTH_COOKIE_NAME = 'greatapps';\n\n/**\n * Verifica se o JWT do cookie `greatapps` é válido (assinatura + expiração)\n * sem bater no backend.\n *\n * Usa `@greatapps/jwt` decode que:\n * 1. Valida a assinatura HMAC com `SECRET_JWT---{id_wl}`\n * 2. Checa `exp` automaticamente (rejeita se expirou)\n *\n * - Se o token não existe → SESSION_INVALID 401\n * - Se a assinatura é inválida → SESSION_INVALID 401\n * - Se `exp` já passou → SESSION_INVALID 401\n * - Se `SECRET_JWT` não está configurado skip (não bloqueia)\n */\nexport async function requireTokenNotExpired(): Promise<void> {\n if (process.env.DUMMY_AUTH_TOKEN) return;\n\n const secretJwt = process.env.SECRET_JWT;\n if (!secretJwt) return;\n\n const cookieStore = await cookies();\n const token = cookieStore.get(AUTH_COOKIE_NAME)?.value;\n\n if (!token) {\n throw new ApiError('Sessão inválida ou expirada 1', 'SESSION_INVALID', 401);\n }\n\n try {\n const payload = JSON.parse(atob(token.split('.')[1]));\n const idWl = payload.id_wl;\n\n if (!idWl) {\n throw new ApiError('Sessão inválida ou expirada 2', 'SESSION_INVALID', 401);\n }\n\n const secret = `${secretJwt}---${idWl}`;\n const decoded = await decode(token, secret);\n\n if (!decoded) {\n throw new ApiError('Sessão inválida ou expirada 3', 'SESSION_INVALID', 401);\n }\n } catch (err) {\n if (err instanceof ApiError) throw err;\n throw new ApiError('Sessão inválida ou expirada 4', 'SESSION_INVALID', 401);\n }\n}\n"],"mappings":"AAAA,OAAO;AAEP,SAAS,eAAe;AACxB,SAAS,cAAc;AACvB,SAAS,gBAAgB;AAEzB,MAAM,mBAAmB;AAezB,eAAsB,yBAAwC;AAC5D,MAAI,QAAQ,IAAI,iBAAkB;AAElC,QAAM,YAAY,QAAQ,IAAI;AAC9B,MAAI,CAAC,UAAW;AAEhB,QAAM,cAAc,MAAM,QAAQ;AAClC,QAAM,QAAQ,YAAY,IAAI,gBAAgB,GAAG;AAEjD,MAAI,CAAC,OAAO;AACV,UAAM,IAAI,SAAS,uCAAiC,mBAAmB,GAAG;AAAA,EAC5E;AAEA,MAAI;AACF,UAAM,UAAU,KAAK,MAAM,KAAK,MAAM,MAAM,GAAG,EAAE,CAAC,CAAC,CAAC;AACpD,UAAM,OAAO,QAAQ;AAErB,QAAI,CAAC,MAAM;AACT,YAAM,IAAI,SAAS,uCAAiC,mBAAmB,GAAG;AAAA,IAC5E;AAEA,UAAM,SAAS,GAAG,SAAS,MAAM,IAAI;AACrC,UAAM,UAAU,MAAM,OAAO,OAAO,MAAM;AAE1C,QAAI,CAAC,SAAS;AACZ,YAAM,IAAI,SAAS,uCAAiC,mBAAmB,GAAG;AAAA,IAC5E;AAAA,EACF,SAAS,KAAK;AACZ,QAAI,eAAe,SAAU,OAAM;AACnC,UAAM,IAAI,SAAS,uCAAiC,mBAAmB,GAAG;AAAA,EAC5E;AACF;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@greatapps/common",
3
- "version": "1.1.591",
3
+ "version": "1.1.593",
4
4
  "description": "Shared library for GreatApps frontend applications",
5
5
  "main": "./dist/index.mjs",
6
6
  "types": "./src/index.ts",
@@ -33,6 +33,7 @@
33
33
  "dependencies": {
34
34
  "@cf-wasm/photon": "^0.3.4",
35
35
  "@greatapps/cache": "^2.0.1",
36
+ "@greatapps/jwt": "^1.0.12",
36
37
  "@radix-ui/react-accordion": "^1.2.12",
37
38
  "@radix-ui/react-checkbox": "^1.3.3",
38
39
  "@radix-ui/react-popover": "^1.1.15",
@@ -1,40 +1,53 @@
1
1
  import 'server-only';
2
2
 
3
3
  import { cookies } from 'next/headers';
4
+ import { decode } from '@greatapps/jwt';
4
5
  import { ApiError } from '../../../infra/api/types';
5
6
 
6
7
  const AUTH_COOKIE_NAME = 'greatapps';
7
8
 
8
9
  /**
9
- * Verifica se o JWT do cookie `greatapps` não expirou checando o campo `exp`
10
- * no payload, sem bater no backend.
10
+ * Verifica se o JWT do cookie `greatapps` é válido (assinatura + expiração)
11
+ * sem bater no backend.
11
12
  *
12
- * - Se o token não existe → SESSION_INVALID 401
13
- * - Se o token é malformado → SESSION_INVALID 401
14
- * - Se `exp` não existe ou já passou → SESSION_INVALID 401
13
+ * Usa `@greatapps/jwt` decode que:
14
+ * 1. Valida a assinatura HMAC com `SECRET_JWT---{id_wl}`
15
+ * 2. Checa `exp` automaticamente (rejeita se expirou)
15
16
  *
16
- * Isso substitui o `requireValidSession` (que fazia POST /auth/keep) no
17
- * `safeMutationAction`, eliminando 1 round-trip ao backend por mutation.
17
+ * - Se o token não existe SESSION_INVALID 401
18
+ * - Se a assinatura é inválida SESSION_INVALID 401
19
+ * - Se `exp` já passou → SESSION_INVALID 401
20
+ * - Se `SECRET_JWT` não está configurado → skip (não bloqueia)
18
21
  */
19
22
  export async function requireTokenNotExpired(): Promise<void> {
20
23
  if (process.env.DUMMY_AUTH_TOKEN) return;
21
24
 
25
+ const secretJwt = process.env.SECRET_JWT;
26
+ if (!secretJwt) return;
27
+
22
28
  const cookieStore = await cookies();
23
29
  const token = cookieStore.get(AUTH_COOKIE_NAME)?.value;
24
30
 
25
31
  if (!token) {
26
- throw new ApiError('Sessão inválida ou expirada', 'SESSION_INVALID', 401);
32
+ throw new ApiError('Sessão inválida ou expirada 1', 'SESSION_INVALID', 401);
27
33
  }
28
34
 
29
35
  try {
30
36
  const payload = JSON.parse(atob(token.split('.')[1]));
31
- const now = Math.floor(Date.now() / 1000);
37
+ const idWl = payload.id_wl;
38
+
39
+ if (!idWl) {
40
+ throw new ApiError('Sessão inválida ou expirada 2', 'SESSION_INVALID', 401);
41
+ }
42
+
43
+ const secret = `${secretJwt}---${idWl}`;
44
+ const decoded = await decode(token, secret);
32
45
 
33
- if (!payload.exp || payload.exp <= now) {
34
- throw new ApiError('Sessão inválida ou expirada', 'SESSION_INVALID', 401);
46
+ if (!decoded) {
47
+ throw new ApiError('Sessão inválida ou expirada 3', 'SESSION_INVALID', 401);
35
48
  }
36
49
  } catch (err) {
37
50
  if (err instanceof ApiError) throw err;
38
- throw new ApiError('Sessão inválida ou expirada', 'SESSION_INVALID', 401);
51
+ throw new ApiError('Sessão inválida ou expirada 4', 'SESSION_INVALID', 401);
39
52
  }
40
53
  }