@innvoid/getmarket-sdk 0.2.6 → 0.2.8
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/dist/{chunk-5S2JP7PR.js → chunk-4KD6646F.js} +12 -1
- package/dist/chunk-4KD6646F.js.map +1 -0
- package/dist/{chunk-WM2QICZQ.js → chunk-DT3AM34L.js} +171 -175
- package/dist/chunk-DT3AM34L.js.map +1 -0
- package/dist/{chunk-7KD7EQ2O.js → chunk-PBN2JCRX.js} +205 -31
- package/dist/chunk-PBN2JCRX.js.map +1 -0
- package/dist/clients/index.cjs +203 -29
- package/dist/clients/index.cjs.map +1 -1
- package/dist/clients/index.d.cts +39 -22
- package/dist/clients/index.d.ts +39 -22
- package/dist/clients/index.js +2 -2
- package/dist/core/index.cjs +14 -2
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/index.d.cts +6 -2
- package/dist/core/index.d.ts +6 -2
- package/dist/core/index.js +5 -3
- package/dist/express.d.cts +1 -1
- package/dist/express.d.ts +1 -1
- package/dist/index.cjs +393 -208
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +18 -25
- package/dist/index.d.ts +18 -25
- package/dist/index.js +15 -7
- package/dist/index.js.map +1 -1
- package/dist/middlewares/index.cjs +193 -68
- package/dist/middlewares/index.cjs.map +1 -1
- package/dist/middlewares/index.js +1 -1
- package/dist/{types-CRECQuHp.d.cts → types-Cc_McZgD.d.cts} +12 -10
- package/dist/{types-CRECQuHp.d.ts → types-Cc_McZgD.d.ts} +12 -10
- package/package.json +2 -2
- package/dist/chunk-5S2JP7PR.js.map +0 -1
- package/dist/chunk-7KD7EQ2O.js.map +0 -1
- package/dist/chunk-WM2QICZQ.js.map +0 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,43 +1,36 @@
|
|
|
1
1
|
export { CacheProvider, TwoLevelCache, TwoLevelCacheOptions, closeCache, getOrSet, getTwoLevelCache } from './cache/index.cjs';
|
|
2
|
-
export { ClientErrorCode, HttpClient, HttpClientOpts, InternalHttp, UpstreamError, createHttpClient, mapAxiosToUpstreamError } from './core/index.cjs';
|
|
2
|
+
export { ClientErrorCode, HttpClient, HttpClientOpts, InternalHttp, UpstreamError, createHttpClient, mapAxiosToUpstreamError, withRequestIdConfig } from './core/index.cjs';
|
|
3
3
|
export { HEADER_AUTHORIZATION, HEADER_BRANCH_UID, HEADER_COMPANY_UID, HEADER_EMPLOYEE_UID, HEADER_INTERNAL_API_KEY, HEADER_REQUEST_ID } from './headers/index.cjs';
|
|
4
4
|
import { R as RequestContext } from './parse-C4vk-fmH.cjs';
|
|
5
5
|
export { g as getRequestContextFromHeaders } from './parse-C4vk-fmH.cjs';
|
|
6
6
|
export { allowAuthAdminOrPerm, allowSysAdminOrAnyPermission, allowSysAdminOrPermissionsAll, allowSysAdminOrRoles, allowSysAdminOrRolesOrAnyPermission, internalAuth, parseHeaders, requestId, requireAnyPermission, requireAuthContext, requirePermissions, requireRoles, requireRolesOrAnyPermission, sendError, sendOk } from './middlewares/index.cjs';
|
|
7
|
-
import { a as AuthMiddlewareOptions } from './types-
|
|
8
|
-
export { A as AuthContext, b as AuthSession, c as AuthSubject, H as HydrateInput, d as HydrateResult, e as Hydrator, T as TokenType } from './types-
|
|
9
|
-
import { Response, NextFunction } from 'express';
|
|
7
|
+
import { a as AuthMiddlewareOptions } from './types-Cc_McZgD.cjs';
|
|
8
|
+
export { A as AuthContext, b as AuthSession, c as AuthSubject, H as HydrateInput, d as HydrateResult, e as Hydrator, T as TokenType } from './types-Cc_McZgD.cjs';
|
|
10
9
|
import { JwtPayload } from 'jsonwebtoken';
|
|
10
|
+
import * as express from 'express';
|
|
11
|
+
import { Response, NextFunction } from 'express';
|
|
11
12
|
export { InternalBulkRefsOptions, ServiceClientEnv, createBulkRefsClient, createFisClient, createMdClient, createMediaClient, createMkpClient, createPayClient, createPlatformClient, createResClient, readServiceEnv } from './clients/index.cjs';
|
|
12
13
|
export { BulkRefsResponse, BulkUidsRequest } from '@innvoid/getmarket-contracts';
|
|
13
14
|
import 'axios';
|
|
14
15
|
|
|
16
|
+
declare function getBearerToken(req: any): string | null;
|
|
17
|
+
declare function normalizeUid(v: any): string | null;
|
|
15
18
|
/**
|
|
16
|
-
* ✅
|
|
17
|
-
* -
|
|
18
|
-
* - Solo RS256
|
|
19
|
-
* - Cero legacy
|
|
20
|
-
* - Hidrata vía hook (OBLIGATORIO)
|
|
21
|
-
*/
|
|
22
|
-
declare function createAuthMiddleware$1(opts: AuthMiddlewareOptions): (req: any, res: Response, next: NextFunction) => Promise<void | Response<any, Record<string, any>>>;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* ✅ Keys viven en getmarket-stack:
|
|
26
|
-
* - JWT_PUBLIC_KEY_PATH=/run/secrets/jwtRS256.key.pub (recomendado)
|
|
19
|
+
* ✅ Keys centralizadas:
|
|
20
|
+
* - JWT_PUBLIC_KEY_PATH=/run/secrets/jwtRS256.key.pub
|
|
27
21
|
* - fallback env AUTH_JWT_PUBLIC_KEY / AUTH_RSA_PUBLIC_KEY
|
|
28
22
|
*/
|
|
29
23
|
declare function readRs256PublicKey(): string;
|
|
30
24
|
declare function verifyBackendJwtRS256(raw: string): JwtPayload;
|
|
25
|
+
declare function extractEmployeeUid(decoded: any): string | null;
|
|
26
|
+
declare function extractCustomerUid(decoded: any): string | null;
|
|
27
|
+
|
|
28
|
+
declare function createAuthMiddleware(opts: AuthMiddlewareOptions): (req: any, res: Response, next: NextFunction) => Promise<void | Response<any, Record<string, any>>>;
|
|
31
29
|
|
|
32
|
-
|
|
33
|
-
declare
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}): (req: any, res: Response, next: NextFunction) => Promise<void | Response<any, Record<string, any>>>;
|
|
37
|
-
declare const authEmployeeRequired: (req: any, res: Response, next: NextFunction) => Promise<void | Response<any, Record<string, any>>>;
|
|
38
|
-
declare const authCustomerRequired: (req: any, res: Response, next: NextFunction) => Promise<void | Response<any, Record<string, any>>>;
|
|
39
|
-
declare const authEmployeeAllowFirebase: (req: any, res: Response, next: NextFunction) => Promise<void | Response<any, Record<string, any>>>;
|
|
40
|
-
declare const authCustomerAllowFirebase: (req: any, res: Response, next: NextFunction) => Promise<void | Response<any, Record<string, any>>>;
|
|
30
|
+
declare const authEmployeeRequired: (req: any, res: express.Response, next: express.NextFunction) => Promise<void | express.Response<any, Record<string, any>>>;
|
|
31
|
+
declare const authCustomerRequired: (req: any, res: express.Response, next: express.NextFunction) => Promise<void | express.Response<any, Record<string, any>>>;
|
|
32
|
+
declare const authEmployeeAllowFirebase: (req: any, res: express.Response, next: express.NextFunction) => Promise<void | express.Response<any, Record<string, any>>>;
|
|
33
|
+
declare const authCustomerAllowFirebase: (req: any, res: express.Response, next: express.NextFunction) => Promise<void | express.Response<any, Record<string, any>>>;
|
|
41
34
|
|
|
42
35
|
type InternalHttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
43
36
|
type InternalHttpClientOptions = {
|
|
@@ -95,4 +88,4 @@ declare function newUidV4(): string;
|
|
|
95
88
|
/** Validación para inputs que vengan de DB/requests durante migración. */
|
|
96
89
|
declare function isUid(value: string): boolean;
|
|
97
90
|
|
|
98
|
-
export { AuthMiddlewareOptions, type InternalHttpClientOptions, InternalHttpError, type InternalHttpMethod, type InternalRequestOptions, RequestContext, authCustomerAllowFirebase, authCustomerRequired, authEmployeeAllowFirebase, authEmployeeRequired, buildInternalHeaders, createAuthMiddleware
|
|
91
|
+
export { AuthMiddlewareOptions, type InternalHttpClientOptions, InternalHttpError, type InternalHttpMethod, type InternalRequestOptions, RequestContext, authCustomerAllowFirebase, authCustomerRequired, authEmployeeAllowFirebase, authEmployeeRequired, buildInternalHeaders, createAuthMiddleware, createInternalHttpClient, extractCustomerUid, extractEmployeeUid, getBearerToken, isUid, newUid, newUidV4, normalizeUid, readRs256PublicKey, verifyBackendJwtRS256 };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,43 +1,36 @@
|
|
|
1
1
|
export { CacheProvider, TwoLevelCache, TwoLevelCacheOptions, closeCache, getOrSet, getTwoLevelCache } from './cache/index.js';
|
|
2
|
-
export { ClientErrorCode, HttpClient, HttpClientOpts, InternalHttp, UpstreamError, createHttpClient, mapAxiosToUpstreamError } from './core/index.js';
|
|
2
|
+
export { ClientErrorCode, HttpClient, HttpClientOpts, InternalHttp, UpstreamError, createHttpClient, mapAxiosToUpstreamError, withRequestIdConfig } from './core/index.js';
|
|
3
3
|
export { HEADER_AUTHORIZATION, HEADER_BRANCH_UID, HEADER_COMPANY_UID, HEADER_EMPLOYEE_UID, HEADER_INTERNAL_API_KEY, HEADER_REQUEST_ID } from './headers/index.js';
|
|
4
4
|
import { R as RequestContext } from './parse-C4vk-fmH.js';
|
|
5
5
|
export { g as getRequestContextFromHeaders } from './parse-C4vk-fmH.js';
|
|
6
6
|
export { allowAuthAdminOrPerm, allowSysAdminOrAnyPermission, allowSysAdminOrPermissionsAll, allowSysAdminOrRoles, allowSysAdminOrRolesOrAnyPermission, internalAuth, parseHeaders, requestId, requireAnyPermission, requireAuthContext, requirePermissions, requireRoles, requireRolesOrAnyPermission, sendError, sendOk } from './middlewares/index.js';
|
|
7
|
-
import { a as AuthMiddlewareOptions } from './types-
|
|
8
|
-
export { A as AuthContext, b as AuthSession, c as AuthSubject, H as HydrateInput, d as HydrateResult, e as Hydrator, T as TokenType } from './types-
|
|
9
|
-
import { Response, NextFunction } from 'express';
|
|
7
|
+
import { a as AuthMiddlewareOptions } from './types-Cc_McZgD.js';
|
|
8
|
+
export { A as AuthContext, b as AuthSession, c as AuthSubject, H as HydrateInput, d as HydrateResult, e as Hydrator, T as TokenType } from './types-Cc_McZgD.js';
|
|
10
9
|
import { JwtPayload } from 'jsonwebtoken';
|
|
10
|
+
import * as express from 'express';
|
|
11
|
+
import { Response, NextFunction } from 'express';
|
|
11
12
|
export { InternalBulkRefsOptions, ServiceClientEnv, createBulkRefsClient, createFisClient, createMdClient, createMediaClient, createMkpClient, createPayClient, createPlatformClient, createResClient, readServiceEnv } from './clients/index.js';
|
|
12
13
|
export { BulkRefsResponse, BulkUidsRequest } from '@innvoid/getmarket-contracts';
|
|
13
14
|
import 'axios';
|
|
14
15
|
|
|
16
|
+
declare function getBearerToken(req: any): string | null;
|
|
17
|
+
declare function normalizeUid(v: any): string | null;
|
|
15
18
|
/**
|
|
16
|
-
* ✅
|
|
17
|
-
* -
|
|
18
|
-
* - Solo RS256
|
|
19
|
-
* - Cero legacy
|
|
20
|
-
* - Hidrata vía hook (OBLIGATORIO)
|
|
21
|
-
*/
|
|
22
|
-
declare function createAuthMiddleware$1(opts: AuthMiddlewareOptions): (req: any, res: Response, next: NextFunction) => Promise<void | Response<any, Record<string, any>>>;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* ✅ Keys viven en getmarket-stack:
|
|
26
|
-
* - JWT_PUBLIC_KEY_PATH=/run/secrets/jwtRS256.key.pub (recomendado)
|
|
19
|
+
* ✅ Keys centralizadas:
|
|
20
|
+
* - JWT_PUBLIC_KEY_PATH=/run/secrets/jwtRS256.key.pub
|
|
27
21
|
* - fallback env AUTH_JWT_PUBLIC_KEY / AUTH_RSA_PUBLIC_KEY
|
|
28
22
|
*/
|
|
29
23
|
declare function readRs256PublicKey(): string;
|
|
30
24
|
declare function verifyBackendJwtRS256(raw: string): JwtPayload;
|
|
25
|
+
declare function extractEmployeeUid(decoded: any): string | null;
|
|
26
|
+
declare function extractCustomerUid(decoded: any): string | null;
|
|
27
|
+
|
|
28
|
+
declare function createAuthMiddleware(opts: AuthMiddlewareOptions): (req: any, res: Response, next: NextFunction) => Promise<void | Response<any, Record<string, any>>>;
|
|
31
29
|
|
|
32
|
-
|
|
33
|
-
declare
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}): (req: any, res: Response, next: NextFunction) => Promise<void | Response<any, Record<string, any>>>;
|
|
37
|
-
declare const authEmployeeRequired: (req: any, res: Response, next: NextFunction) => Promise<void | Response<any, Record<string, any>>>;
|
|
38
|
-
declare const authCustomerRequired: (req: any, res: Response, next: NextFunction) => Promise<void | Response<any, Record<string, any>>>;
|
|
39
|
-
declare const authEmployeeAllowFirebase: (req: any, res: Response, next: NextFunction) => Promise<void | Response<any, Record<string, any>>>;
|
|
40
|
-
declare const authCustomerAllowFirebase: (req: any, res: Response, next: NextFunction) => Promise<void | Response<any, Record<string, any>>>;
|
|
30
|
+
declare const authEmployeeRequired: (req: any, res: express.Response, next: express.NextFunction) => Promise<void | express.Response<any, Record<string, any>>>;
|
|
31
|
+
declare const authCustomerRequired: (req: any, res: express.Response, next: express.NextFunction) => Promise<void | express.Response<any, Record<string, any>>>;
|
|
32
|
+
declare const authEmployeeAllowFirebase: (req: any, res: express.Response, next: express.NextFunction) => Promise<void | express.Response<any, Record<string, any>>>;
|
|
33
|
+
declare const authCustomerAllowFirebase: (req: any, res: express.Response, next: express.NextFunction) => Promise<void | express.Response<any, Record<string, any>>>;
|
|
41
34
|
|
|
42
35
|
type InternalHttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
43
36
|
type InternalHttpClientOptions = {
|
|
@@ -95,4 +88,4 @@ declare function newUidV4(): string;
|
|
|
95
88
|
/** Validación para inputs que vengan de DB/requests durante migración. */
|
|
96
89
|
declare function isUid(value: string): boolean;
|
|
97
90
|
|
|
98
|
-
export { AuthMiddlewareOptions, type InternalHttpClientOptions, InternalHttpError, type InternalHttpMethod, type InternalRequestOptions, RequestContext, authCustomerAllowFirebase, authCustomerRequired, authEmployeeAllowFirebase, authEmployeeRequired, buildInternalHeaders, createAuthMiddleware
|
|
91
|
+
export { AuthMiddlewareOptions, type InternalHttpClientOptions, InternalHttpError, type InternalHttpMethod, type InternalRequestOptions, RequestContext, authCustomerAllowFirebase, authCustomerRequired, authEmployeeAllowFirebase, authEmployeeRequired, buildInternalHeaders, createAuthMiddleware, createInternalHttpClient, extractCustomerUid, extractEmployeeUid, getBearerToken, isUid, newUid, newUidV4, normalizeUid, readRs256PublicKey, verifyBackendJwtRS256 };
|
package/dist/index.js
CHANGED
|
@@ -9,8 +9,11 @@ import {
|
|
|
9
9
|
authEmployeeAllowFirebase,
|
|
10
10
|
authEmployeeRequired,
|
|
11
11
|
createAuthMiddleware,
|
|
12
|
-
|
|
12
|
+
extractCustomerUid,
|
|
13
|
+
extractEmployeeUid,
|
|
14
|
+
getBearerToken,
|
|
13
15
|
internalAuth,
|
|
16
|
+
normalizeUid,
|
|
14
17
|
parseHeaders,
|
|
15
18
|
readRs256PublicKey,
|
|
16
19
|
requestId,
|
|
@@ -22,7 +25,7 @@ import {
|
|
|
22
25
|
sendError,
|
|
23
26
|
sendOk,
|
|
24
27
|
verifyBackendJwtRS256
|
|
25
|
-
} from "./chunk-
|
|
28
|
+
} from "./chunk-DT3AM34L.js";
|
|
26
29
|
import {
|
|
27
30
|
InternalHttpError,
|
|
28
31
|
buildInternalHeaders,
|
|
@@ -36,7 +39,7 @@ import {
|
|
|
36
39
|
createPlatformClient,
|
|
37
40
|
createResClient,
|
|
38
41
|
readServiceEnv
|
|
39
|
-
} from "./chunk-
|
|
42
|
+
} from "./chunk-PBN2JCRX.js";
|
|
40
43
|
import {
|
|
41
44
|
TwoLevelCache,
|
|
42
45
|
closeCache,
|
|
@@ -47,8 +50,9 @@ import {
|
|
|
47
50
|
InternalHttp,
|
|
48
51
|
UpstreamError,
|
|
49
52
|
createHttpClient,
|
|
50
|
-
mapAxiosToUpstreamError
|
|
51
|
-
|
|
53
|
+
mapAxiosToUpstreamError,
|
|
54
|
+
withRequestIdConfig
|
|
55
|
+
} from "./chunk-4KD6646F.js";
|
|
52
56
|
import {
|
|
53
57
|
HEADER_AUTHORIZATION,
|
|
54
58
|
HEADER_BRANCH_UID,
|
|
@@ -93,7 +97,6 @@ export {
|
|
|
93
97
|
buildInternalHeaders,
|
|
94
98
|
closeCache,
|
|
95
99
|
createAuthMiddleware,
|
|
96
|
-
createAuthMiddleware2 as createAuthMiddlewareLegacySimple,
|
|
97
100
|
createBulkRefsClient,
|
|
98
101
|
createFisClient,
|
|
99
102
|
createHttpClient,
|
|
@@ -104,6 +107,9 @@ export {
|
|
|
104
107
|
createPayClient,
|
|
105
108
|
createPlatformClient,
|
|
106
109
|
createResClient,
|
|
110
|
+
extractCustomerUid,
|
|
111
|
+
extractEmployeeUid,
|
|
112
|
+
getBearerToken,
|
|
107
113
|
getOrSet,
|
|
108
114
|
getRequestContextFromHeaders,
|
|
109
115
|
getTwoLevelCache,
|
|
@@ -112,6 +118,7 @@ export {
|
|
|
112
118
|
mapAxiosToUpstreamError,
|
|
113
119
|
newUid,
|
|
114
120
|
newUidV4,
|
|
121
|
+
normalizeUid,
|
|
115
122
|
parseHeaders,
|
|
116
123
|
readRs256PublicKey,
|
|
117
124
|
readServiceEnv,
|
|
@@ -123,6 +130,7 @@ export {
|
|
|
123
130
|
requireRolesOrAnyPermission,
|
|
124
131
|
sendError,
|
|
125
132
|
sendOk,
|
|
126
|
-
verifyBackendJwtRS256
|
|
133
|
+
verifyBackendJwtRS256,
|
|
134
|
+
withRequestIdConfig
|
|
127
135
|
};
|
|
128
136
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/common/ids.ts"],"sourcesContent":["// packages/sdk/src/common/ids.ts\nimport {v7 as uuidv7, v4 as uuidv4, validate as uuidValidate, version as uuidVersion} from \"uuid\";\n\n/**\n * UID canónico GetMarket.\n * - Por defecto genera UUIDv7 (time-ordered).\n * - Durante transición, aceptamos v4 y v7 como válidos.\n */\nexport function newUid(): string {\n return uuidv7();\n}\n\n/** Útil si necesitas generar v4 puntualmente (idealmente no usarlo). */\nexport function newUidV4(): string {\n return uuidv4();\n}\n\n/** Validación para inputs que vengan de DB/requests durante migración. */\nexport function isUid(value: string): boolean {\n return uuidValidate(value) && (uuidVersion(value) === 7 || uuidVersion(value) === 4);\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../src/common/ids.ts"],"sourcesContent":["// packages/sdk/src/common/ids.ts\nimport {v7 as uuidv7, v4 as uuidv4, validate as uuidValidate, version as uuidVersion} from \"uuid\";\n\n/**\n * UID canónico GetMarket.\n * - Por defecto genera UUIDv7 (time-ordered).\n * - Durante transición, aceptamos v4 y v7 como válidos.\n */\nexport function newUid(): string {\n return uuidv7();\n}\n\n/** Útil si necesitas generar v4 puntualmente (idealmente no usarlo). */\nexport function newUidV4(): string {\n return uuidv4();\n}\n\n/** Validación para inputs que vengan de DB/requests durante migración. */\nexport function isUid(value: string): boolean {\n return uuidValidate(value) && (uuidVersion(value) === 7 || uuidVersion(value) === 4);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,SAAQ,MAAM,QAAQ,MAAM,QAAQ,YAAY,cAAc,WAAW,mBAAkB;AAOpF,SAAS,SAAiB;AAC/B,SAAO,OAAO;AAChB;AAGO,SAAS,WAAmB;AACjC,SAAO,OAAO;AAChB;AAGO,SAAS,MAAM,OAAwB;AAC5C,SAAO,aAAa,KAAK,MAAM,YAAY,KAAK,MAAM,KAAK,YAAY,KAAK,MAAM;AACpF;","names":[]}
|
|
@@ -297,29 +297,41 @@ function requireRolesOrAnyPermission(roles, perms, options) {
|
|
|
297
297
|
};
|
|
298
298
|
}
|
|
299
299
|
|
|
300
|
-
// src/auth/
|
|
301
|
-
var import_firebase_admin = __toESM(require("firebase-admin"), 1);
|
|
302
|
-
var import_jsonwebtoken = __toESM(require("jsonwebtoken"), 1);
|
|
300
|
+
// src/auth/jwt.ts
|
|
303
301
|
var import_fs2 = __toESM(require("fs"), 1);
|
|
302
|
+
var import_jsonwebtoken = __toESM(require("jsonwebtoken"), 1);
|
|
303
|
+
function readFileIfExists(path) {
|
|
304
|
+
if (!path) return null;
|
|
305
|
+
try {
|
|
306
|
+
const v = import_fs2.default.readFileSync(path, "utf8").trim();
|
|
307
|
+
return v.length ? v : null;
|
|
308
|
+
} catch {
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
304
312
|
function getBearerToken(req) {
|
|
305
|
-
const auth = String(req
|
|
313
|
+
const auth = String(req?.headers?.authorization || "");
|
|
306
314
|
if (!auth.startsWith("Bearer ")) return null;
|
|
307
315
|
const token = auth.slice(7).trim();
|
|
308
316
|
return token.length ? token : null;
|
|
309
317
|
}
|
|
310
|
-
function
|
|
311
|
-
const
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
const
|
|
318
|
-
|
|
319
|
-
|
|
318
|
+
function normalizeUid(v) {
|
|
319
|
+
const s = String(v ?? "").trim();
|
|
320
|
+
return s.length ? s : null;
|
|
321
|
+
}
|
|
322
|
+
function readRs256PublicKey() {
|
|
323
|
+
const fromFile = readFileIfExists(process.env.JWT_PUBLIC_KEY_PATH);
|
|
324
|
+
if (fromFile) return fromFile;
|
|
325
|
+
const fromEnv = String(
|
|
326
|
+
process.env.AUTH_JWT_PUBLIC_KEY || process.env.AUTH_RSA_PUBLIC_KEY || ""
|
|
327
|
+
).replace(/\\n/g, "\n").trim();
|
|
328
|
+
if (fromEnv) return fromEnv;
|
|
329
|
+
throw new Error(
|
|
330
|
+
"Missing RS256 public key (JWT_PUBLIC_KEY_PATH / AUTH_JWT_PUBLIC_KEY / AUTH_RSA_PUBLIC_KEY)"
|
|
331
|
+
);
|
|
320
332
|
}
|
|
321
333
|
function verifyBackendJwtRS256(raw) {
|
|
322
|
-
const publicKey =
|
|
334
|
+
const publicKey = readRs256PublicKey();
|
|
323
335
|
const audience = process.env.JWT_AUDIENCE || process.env.AUTH_JWT_AUDIENCE || "getmarket.api";
|
|
324
336
|
const issuer = process.env.JWT_ISSUER || process.env.AUTH_JWT_ISSUER || "getmarket-auth";
|
|
325
337
|
return import_jsonwebtoken.default.verify(raw, publicKey, {
|
|
@@ -328,34 +340,31 @@ function verifyBackendJwtRS256(raw) {
|
|
|
328
340
|
issuer
|
|
329
341
|
});
|
|
330
342
|
}
|
|
331
|
-
function normalizeUid(v) {
|
|
332
|
-
const s = String(v ?? "").trim();
|
|
333
|
-
return s.length ? s : null;
|
|
334
|
-
}
|
|
335
|
-
function deriveCompanyBranch(decoded, companyUid, branchUid) {
|
|
336
|
-
const companiesFromToken = Array.isArray(decoded?.companies) ? decoded.companies : [];
|
|
337
|
-
const company = decoded?.company ?? (companyUid ? companiesFromToken.find((c) => c?.uid === companyUid) : null) ?? null;
|
|
338
|
-
const branch = decoded?.branch ?? (branchUid && company?.branches ? (company.branches || []).find((b) => b?.uid === branchUid) : null) ?? null;
|
|
339
|
-
return { companiesFromToken, company, branch };
|
|
340
|
-
}
|
|
341
343
|
function extractEmployeeUid(decoded) {
|
|
342
|
-
const direct = normalizeUid(decoded?.employee_uid);
|
|
344
|
+
const direct = normalizeUid(decoded?.employee_uid) ?? normalizeUid(decoded?.employee?.uid);
|
|
343
345
|
if (direct) return direct;
|
|
344
346
|
const sub = normalizeUid(decoded?.sub);
|
|
345
347
|
if (!sub) return null;
|
|
346
|
-
const
|
|
347
|
-
return
|
|
348
|
+
const match = /^emp:(.+)$/i.exec(sub);
|
|
349
|
+
return match?.[1] ? normalizeUid(match[1]) : null;
|
|
348
350
|
}
|
|
349
351
|
function extractCustomerUid(decoded) {
|
|
350
|
-
const direct = normalizeUid(decoded?.customer_uid);
|
|
352
|
+
const direct = normalizeUid(decoded?.customer_uid) ?? normalizeUid(decoded?.customer?.uid);
|
|
351
353
|
if (direct) return direct;
|
|
352
354
|
const sub = normalizeUid(decoded?.sub);
|
|
353
355
|
if (!sub) return null;
|
|
354
|
-
const
|
|
355
|
-
return
|
|
356
|
+
const match = /^cus:(.+)$/i.exec(sub);
|
|
357
|
+
return match?.[1] ? normalizeUid(match[1]) : null;
|
|
356
358
|
}
|
|
359
|
+
|
|
360
|
+
// src/auth/middleware.ts
|
|
357
361
|
function createAuthMiddleware(opts) {
|
|
358
|
-
const {
|
|
362
|
+
const {
|
|
363
|
+
subject,
|
|
364
|
+
allowFirebaseIdToken = false,
|
|
365
|
+
requireSubject = true,
|
|
366
|
+
hydrate
|
|
367
|
+
} = opts;
|
|
359
368
|
return async (req, res, next) => {
|
|
360
369
|
const token = getBearerToken(req);
|
|
361
370
|
if (!token) {
|
|
@@ -365,20 +374,16 @@ function createAuthMiddleware(opts) {
|
|
|
365
374
|
message: "Missing Authorization Bearer token"
|
|
366
375
|
});
|
|
367
376
|
}
|
|
377
|
+
const headerCtx = req.context || {};
|
|
378
|
+
const company_uid = normalizeUid(headerCtx.company_uid);
|
|
379
|
+
const branch_uid = normalizeUid(headerCtx.branch_uid);
|
|
368
380
|
try {
|
|
369
381
|
const decoded = verifyBackendJwtRS256(token);
|
|
370
|
-
const
|
|
371
|
-
const companyUid = normalizeUid(headerCtx.company_uid);
|
|
372
|
-
const branchUid = normalizeUid(headerCtx.branch_uid);
|
|
373
|
-
const { companiesFromToken, company, branch } = deriveCompanyBranch(decoded, companyUid, branchUid);
|
|
374
|
-
const ctx = {
|
|
382
|
+
const baseCtx = {
|
|
375
383
|
tokenType: "backend",
|
|
376
384
|
subject,
|
|
377
|
-
company_uid:
|
|
378
|
-
branch_uid:
|
|
379
|
-
companies: companiesFromToken,
|
|
380
|
-
company,
|
|
381
|
-
branch,
|
|
385
|
+
company_uid: company_uid ?? void 0,
|
|
386
|
+
branch_uid: branch_uid ?? void 0,
|
|
382
387
|
roles: Array.isArray(decoded?.roles) ? decoded.roles : [],
|
|
383
388
|
permissions: Array.isArray(decoded?.permissions) ? decoded.permissions : [],
|
|
384
389
|
denied_permissions: Array.isArray(decoded?.denied_permissions) ? decoded.denied_permissions : [],
|
|
@@ -389,31 +394,49 @@ function createAuthMiddleware(opts) {
|
|
|
389
394
|
}
|
|
390
395
|
};
|
|
391
396
|
if (subject === "employee") {
|
|
392
|
-
|
|
393
|
-
|
|
397
|
+
baseCtx.employee_uid = extractEmployeeUid(decoded) ?? void 0;
|
|
398
|
+
} else {
|
|
399
|
+
baseCtx.customer_uid = extractCustomerUid(decoded) ?? void 0;
|
|
400
|
+
}
|
|
401
|
+
const hydrated = await hydrate({
|
|
402
|
+
decoded,
|
|
403
|
+
req,
|
|
404
|
+
subject,
|
|
405
|
+
company_uid,
|
|
406
|
+
branch_uid
|
|
407
|
+
});
|
|
408
|
+
Object.assign(baseCtx, hydrated);
|
|
409
|
+
if (subject === "employee" && !baseCtx.employee_uid) {
|
|
410
|
+
return res.status(401).json({
|
|
411
|
+
ok: false,
|
|
412
|
+
code: "AUTH_EMPLOYEE_UID_MISSING",
|
|
413
|
+
message: "employee_uid missing in token/context (expected employee_uid or sub=emp:<uid>)"
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
if (subject === "customer" && !baseCtx.customer_uid) {
|
|
417
|
+
return res.status(401).json({
|
|
418
|
+
ok: false,
|
|
419
|
+
code: "AUTH_CUSTOMER_UID_MISSING",
|
|
420
|
+
message: "customer_uid missing in token/context (expected customer_uid or sub=cus:<uid>)"
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
if (requireSubject) {
|
|
424
|
+
if (subject === "employee" && !baseCtx.employee) {
|
|
394
425
|
return res.status(401).json({
|
|
395
426
|
ok: false,
|
|
396
|
-
code: "
|
|
397
|
-
message: "
|
|
427
|
+
code: "AUTH_EMPLOYEE_NOT_FOUND",
|
|
428
|
+
message: "Employee not resolved by hydrator"
|
|
398
429
|
});
|
|
399
430
|
}
|
|
400
|
-
|
|
401
|
-
const embedded = decoded?.employee ?? decoded?.user ?? null;
|
|
402
|
-
ctx.employee = embedded && typeof embedded === "object" ? embedded : { uid: employee_uid, email: decoded?.email ?? null };
|
|
403
|
-
} else {
|
|
404
|
-
const customer_uid = extractCustomerUid(decoded);
|
|
405
|
-
if (!customer_uid) {
|
|
431
|
+
if (subject === "customer" && !baseCtx.customer) {
|
|
406
432
|
return res.status(401).json({
|
|
407
433
|
ok: false,
|
|
408
|
-
code: "
|
|
409
|
-
message: "
|
|
434
|
+
code: "AUTH_CUSTOMER_NOT_FOUND",
|
|
435
|
+
message: "Customer not resolved by hydrator"
|
|
410
436
|
});
|
|
411
437
|
}
|
|
412
|
-
ctx.customer_uid = customer_uid;
|
|
413
|
-
const embedded = decoded?.customer ?? null;
|
|
414
|
-
ctx.customer = embedded && typeof embedded === "object" ? embedded : { uid: customer_uid };
|
|
415
438
|
}
|
|
416
|
-
req.auth =
|
|
439
|
+
req.auth = baseCtx;
|
|
417
440
|
return next();
|
|
418
441
|
} catch {
|
|
419
442
|
if (!allowFirebaseIdToken) {
|
|
@@ -424,7 +447,8 @@ function createAuthMiddleware(opts) {
|
|
|
424
447
|
});
|
|
425
448
|
}
|
|
426
449
|
try {
|
|
427
|
-
const
|
|
450
|
+
const { default: admin } = await import("firebase-admin");
|
|
451
|
+
const firebaseDecoded = await admin.auth().verifyIdToken(token);
|
|
428
452
|
if (firebaseDecoded.email && firebaseDecoded.email_verified === false) {
|
|
429
453
|
return res.status(401).json({
|
|
430
454
|
ok: false,
|
|
@@ -432,15 +456,12 @@ function createAuthMiddleware(opts) {
|
|
|
432
456
|
message: "Email not verified"
|
|
433
457
|
});
|
|
434
458
|
}
|
|
435
|
-
const headerCtx = req.context || {};
|
|
436
|
-
const companyUid = normalizeUid(headerCtx.company_uid);
|
|
437
|
-
const branchUid = normalizeUid(headerCtx.branch_uid);
|
|
438
459
|
req.auth = {
|
|
439
460
|
tokenType: "backend",
|
|
440
461
|
subject,
|
|
441
462
|
firebase: firebaseDecoded,
|
|
442
|
-
company_uid:
|
|
443
|
-
branch_uid:
|
|
463
|
+
company_uid: company_uid ?? void 0,
|
|
464
|
+
branch_uid: branch_uid ?? void 0,
|
|
444
465
|
companies: [],
|
|
445
466
|
roles: [],
|
|
446
467
|
permissions: [],
|
|
@@ -457,10 +478,114 @@ function createAuthMiddleware(opts) {
|
|
|
457
478
|
}
|
|
458
479
|
};
|
|
459
480
|
}
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
481
|
+
|
|
482
|
+
// src/auth/authentication.ts
|
|
483
|
+
function deriveCompanyBranch(decoded, companyUid, branchUid) {
|
|
484
|
+
const companiesFromToken = Array.isArray(decoded?.companies) ? decoded.companies : [];
|
|
485
|
+
const company = decoded?.company ?? (companyUid ? companiesFromToken.find((c) => c?.uid === companyUid) : null) ?? null;
|
|
486
|
+
const branch = decoded?.branch ?? (branchUid && company?.branches ? (company.branches || []).find((b) => b?.uid === branchUid) : null) ?? null;
|
|
487
|
+
return {
|
|
488
|
+
companiesFromToken,
|
|
489
|
+
company,
|
|
490
|
+
branch
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
var authEmployeeRequired = createAuthMiddleware({
|
|
494
|
+
subject: "employee",
|
|
495
|
+
allowFirebaseIdToken: false,
|
|
496
|
+
requireSubject: false,
|
|
497
|
+
hydrate: async ({ decoded, company_uid, branch_uid }) => {
|
|
498
|
+
const employee_uid = extractEmployeeUid(decoded) ?? normalizeUid(decoded?.employee?.uid);
|
|
499
|
+
const { companiesFromToken, company, branch } = deriveCompanyBranch(
|
|
500
|
+
decoded,
|
|
501
|
+
company_uid,
|
|
502
|
+
branch_uid
|
|
503
|
+
);
|
|
504
|
+
const employee = decoded?.employee && typeof decoded.employee === "object" ? decoded.employee : employee_uid ? { uid: employee_uid, email: decoded?.email ?? null } : void 0;
|
|
505
|
+
return {
|
|
506
|
+
employee_uid: employee_uid ?? void 0,
|
|
507
|
+
employee,
|
|
508
|
+
companies: companiesFromToken,
|
|
509
|
+
company,
|
|
510
|
+
branch,
|
|
511
|
+
roles: Array.isArray(decoded?.roles) ? decoded.roles : [],
|
|
512
|
+
permissions: Array.isArray(decoded?.permissions) ? decoded.permissions : [],
|
|
513
|
+
denied_permissions: Array.isArray(decoded?.denied_permissions) ? decoded.denied_permissions : []
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
});
|
|
517
|
+
var authCustomerRequired = createAuthMiddleware({
|
|
518
|
+
subject: "customer",
|
|
519
|
+
allowFirebaseIdToken: false,
|
|
520
|
+
requireSubject: false,
|
|
521
|
+
hydrate: async ({ decoded, company_uid, branch_uid }) => {
|
|
522
|
+
const customer_uid = extractCustomerUid(decoded) ?? normalizeUid(decoded?.customer?.uid);
|
|
523
|
+
const { companiesFromToken, company, branch } = deriveCompanyBranch(
|
|
524
|
+
decoded,
|
|
525
|
+
company_uid,
|
|
526
|
+
branch_uid
|
|
527
|
+
);
|
|
528
|
+
const customer = decoded?.customer && typeof decoded.customer === "object" ? decoded.customer : customer_uid ? { uid: customer_uid } : void 0;
|
|
529
|
+
return {
|
|
530
|
+
customer_uid: customer_uid ?? void 0,
|
|
531
|
+
customer,
|
|
532
|
+
companies: companiesFromToken,
|
|
533
|
+
company,
|
|
534
|
+
branch,
|
|
535
|
+
roles: Array.isArray(decoded?.roles) ? decoded.roles : [],
|
|
536
|
+
permissions: Array.isArray(decoded?.permissions) ? decoded.permissions : [],
|
|
537
|
+
denied_permissions: Array.isArray(decoded?.denied_permissions) ? decoded.denied_permissions : []
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
});
|
|
541
|
+
var authEmployeeAllowFirebase = createAuthMiddleware({
|
|
542
|
+
subject: "employee",
|
|
543
|
+
allowFirebaseIdToken: true,
|
|
544
|
+
requireSubject: false,
|
|
545
|
+
hydrate: async ({ decoded, company_uid, branch_uid }) => {
|
|
546
|
+
const employee_uid = extractEmployeeUid(decoded) ?? normalizeUid(decoded?.employee?.uid);
|
|
547
|
+
const { companiesFromToken, company, branch } = deriveCompanyBranch(
|
|
548
|
+
decoded,
|
|
549
|
+
company_uid,
|
|
550
|
+
branch_uid
|
|
551
|
+
);
|
|
552
|
+
const employee = decoded?.employee && typeof decoded.employee === "object" ? decoded.employee : employee_uid ? { uid: employee_uid, email: decoded?.email ?? null } : void 0;
|
|
553
|
+
return {
|
|
554
|
+
employee_uid: employee_uid ?? void 0,
|
|
555
|
+
employee,
|
|
556
|
+
companies: companiesFromToken,
|
|
557
|
+
company,
|
|
558
|
+
branch,
|
|
559
|
+
roles: Array.isArray(decoded?.roles) ? decoded.roles : [],
|
|
560
|
+
permissions: Array.isArray(decoded?.permissions) ? decoded.permissions : [],
|
|
561
|
+
denied_permissions: Array.isArray(decoded?.denied_permissions) ? decoded.denied_permissions : []
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
});
|
|
565
|
+
var authCustomerAllowFirebase = createAuthMiddleware({
|
|
566
|
+
subject: "customer",
|
|
567
|
+
allowFirebaseIdToken: true,
|
|
568
|
+
requireSubject: false,
|
|
569
|
+
hydrate: async ({ decoded, company_uid, branch_uid }) => {
|
|
570
|
+
const customer_uid = extractCustomerUid(decoded) ?? normalizeUid(decoded?.customer?.uid);
|
|
571
|
+
const { companiesFromToken, company, branch } = deriveCompanyBranch(
|
|
572
|
+
decoded,
|
|
573
|
+
company_uid,
|
|
574
|
+
branch_uid
|
|
575
|
+
);
|
|
576
|
+
const customer = decoded?.customer && typeof decoded.customer === "object" ? decoded.customer : customer_uid ? { uid: customer_uid } : void 0;
|
|
577
|
+
return {
|
|
578
|
+
customer_uid: customer_uid ?? void 0,
|
|
579
|
+
customer,
|
|
580
|
+
companies: companiesFromToken,
|
|
581
|
+
company,
|
|
582
|
+
branch,
|
|
583
|
+
roles: Array.isArray(decoded?.roles) ? decoded.roles : [],
|
|
584
|
+
permissions: Array.isArray(decoded?.permissions) ? decoded.permissions : [],
|
|
585
|
+
denied_permissions: Array.isArray(decoded?.denied_permissions) ? decoded.denied_permissions : []
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
});
|
|
464
589
|
|
|
465
590
|
// src/middlewares/guards.ts
|
|
466
591
|
function normalizeRole(r) {
|