@greatapps/common 1.1.591 → 1.1.592
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
|
|
15
|
-
if (!
|
|
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`
|
|
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 // Extrair id_wl do payload pra montar o secret composto\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;AAEF,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.
|
|
3
|
+
"version": "1.1.592",
|
|
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,54 @@
|
|
|
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`
|
|
10
|
-
*
|
|
10
|
+
* Verifica se o JWT do cookie `greatapps` é válido (assinatura + expiração)
|
|
11
|
+
* sem bater no backend.
|
|
11
12
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
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
|
-
*
|
|
17
|
-
*
|
|
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 {
|
|
36
|
+
// Extrair id_wl do payload pra montar o secret composto
|
|
30
37
|
const payload = JSON.parse(atob(token.split('.')[1]));
|
|
31
|
-
const
|
|
38
|
+
const idWl = payload.id_wl;
|
|
39
|
+
|
|
40
|
+
if (!idWl) {
|
|
41
|
+
throw new ApiError('Sessão inválida ou expirada 2', 'SESSION_INVALID', 401);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const secret = `${secretJwt}---${idWl}`;
|
|
45
|
+
const decoded = await decode(token, secret);
|
|
32
46
|
|
|
33
|
-
if (!
|
|
34
|
-
throw new ApiError('Sessão inválida ou expirada', 'SESSION_INVALID', 401);
|
|
47
|
+
if (!decoded) {
|
|
48
|
+
throw new ApiError('Sessão inválida ou expirada 3', 'SESSION_INVALID', 401);
|
|
35
49
|
}
|
|
36
50
|
} catch (err) {
|
|
37
51
|
if (err instanceof ApiError) throw err;
|
|
38
|
-
throw new ApiError('Sessão inválida ou expirada', 'SESSION_INVALID', 401);
|
|
52
|
+
throw new ApiError('Sessão inválida ou expirada 4', 'SESSION_INVALID', 401);
|
|
39
53
|
}
|
|
40
54
|
}
|