@miguelmorales13/nestkit 0.28.0 → 0.30.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/README.md +117 -0
- package/dist/chunk-33KFVAUZ.cjs +151 -0
- package/dist/chunk-LU3HO7B3.js +151 -0
- package/dist/payments/connect/credentials-cipher.d.ts +14 -0
- package/dist/payments/connect/mercadopago-refund.d.ts +118 -0
- package/dist/payments/connect/mercadopago-signature.d.ts +6 -0
- package/dist/payments/connect/online-checkout.d.ts +69 -0
- package/dist/payments/connect/rejection.d.ts +7 -0
- package/dist/payments/connect/status.d.ts +20 -0
- package/dist/payments/connect/terminal-connections.d.ts +291 -0
- package/dist/payments/index.cjs +1394 -3
- package/dist/payments/index.d.ts +14 -0
- package/dist/payments/index.js +1392 -1
- package/dist/payments/openpay/openpay-api.d.ts +127 -0
- package/dist/payments/openpay/openpay-connections.d.ts +201 -0
- package/dist/security/index.cjs +27 -152
- package/dist/security/index.js +13 -138
- package/package.json +2 -2
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import type { I18nMessage } from '../../i18n/message.js';
|
|
2
|
+
/** A merchant's own Openpay keys, as `/oauth/merchant` returns them. The money goes to THEIR account. */
|
|
3
|
+
export interface OpenpayCredentials {
|
|
4
|
+
merchantId: string;
|
|
5
|
+
secretKey: string;
|
|
6
|
+
publicKey: string;
|
|
7
|
+
}
|
|
8
|
+
export type OpenpayMethod = 'card' | 'transfer';
|
|
9
|
+
/** What a charge is, seen from the seller. `refunded` covers a chargeback too: the money went back. */
|
|
10
|
+
export type OpenpayPaymentState = 'paid' | 'pending' | 'failed' | 'refunded';
|
|
11
|
+
export interface OpenpayCustomer {
|
|
12
|
+
name: string;
|
|
13
|
+
lastName?: string;
|
|
14
|
+
email?: string;
|
|
15
|
+
phone?: string;
|
|
16
|
+
}
|
|
17
|
+
export interface OpenpayApiOptions {
|
|
18
|
+
sandbox?: boolean;
|
|
19
|
+
/** Pass the fetch you use for Openpay (timeouts, retries, alerts). Defaults to the global `fetch`. */
|
|
20
|
+
fetchImpl?: typeof fetch;
|
|
21
|
+
/** Test doubles. */
|
|
22
|
+
apiBaseUrl?: string;
|
|
23
|
+
dashboardBaseUrl?: string;
|
|
24
|
+
messages?: Partial<OpenpayApiMessages>;
|
|
25
|
+
}
|
|
26
|
+
/** Error messages. Override to translate; defaults are Spanish. */
|
|
27
|
+
export interface OpenpayApiMessages {
|
|
28
|
+
notOpened: string | I18nMessage;
|
|
29
|
+
noAnswer: string | I18nMessage;
|
|
30
|
+
}
|
|
31
|
+
/** Fixed code so the front decides by code, not by text. */
|
|
32
|
+
export declare const OPENPAY_PROVIDER_ERROR = "PAGO_PROVEEDOR_ERROR";
|
|
33
|
+
export interface CreateCardLink {
|
|
34
|
+
/** Your id for the sale. Openpay refuses a second charge with the same one (error 1006), so a retry cannot charge twice. */
|
|
35
|
+
orderId: string;
|
|
36
|
+
description: string;
|
|
37
|
+
amountCents: number;
|
|
38
|
+
customer: OpenpayCustomer;
|
|
39
|
+
/** Where the buyer lands after paying. */
|
|
40
|
+
redirectUrl: string;
|
|
41
|
+
/** Openpay emails the link to `customer.email`. */
|
|
42
|
+
sendEmail?: boolean;
|
|
43
|
+
/** Openpay asks for the buyer's IP on card charges (anti-fraud). Send it when you have it. */
|
|
44
|
+
clientIp?: string;
|
|
45
|
+
}
|
|
46
|
+
export interface CardLink {
|
|
47
|
+
chargeId: string;
|
|
48
|
+
/** Openpay's hosted card form. */
|
|
49
|
+
url: string;
|
|
50
|
+
}
|
|
51
|
+
export interface CreateBankTransfer {
|
|
52
|
+
orderId: string;
|
|
53
|
+
description: string;
|
|
54
|
+
amountCents: number;
|
|
55
|
+
customer?: OpenpayCustomer;
|
|
56
|
+
/** Last day to pay. Openpay expires on business days, so a Friday ends on Monday. Max 30 days. */
|
|
57
|
+
dueDate?: Date;
|
|
58
|
+
}
|
|
59
|
+
export interface BankTransfer {
|
|
60
|
+
chargeId: string;
|
|
61
|
+
/** Destination account for the SPEI transfer (18 digits). */
|
|
62
|
+
clabe: string;
|
|
63
|
+
/** What the buyer types as the concept (20 digits). Openpay matches the payment with it. */
|
|
64
|
+
reference: string;
|
|
65
|
+
bank: string;
|
|
66
|
+
/** CIE agreement number, for paying from BBVA online banking. */
|
|
67
|
+
agreement: string;
|
|
68
|
+
/** Openpay's printable receipt. Only available while the charge is pending. */
|
|
69
|
+
receiptUrl: string;
|
|
70
|
+
dueDate: Date | null;
|
|
71
|
+
}
|
|
72
|
+
export interface OpenpayCharge {
|
|
73
|
+
id: string;
|
|
74
|
+
state: OpenpayPaymentState;
|
|
75
|
+
/** Raw Openpay status (`completed`, `in_progress`, `charge_pending`, `cancelled`, `failed`, `refunded`…). */
|
|
76
|
+
status: string;
|
|
77
|
+
method: OpenpayMethod | 'store' | 'other';
|
|
78
|
+
orderId: string | null;
|
|
79
|
+
amountCents: number;
|
|
80
|
+
/** What was collected, in cents; `null` until it is paid. */
|
|
81
|
+
paidCents: number | null;
|
|
82
|
+
dueDate: Date | null;
|
|
83
|
+
}
|
|
84
|
+
export interface OpenpayWebhook {
|
|
85
|
+
id: string;
|
|
86
|
+
status: 'verified' | 'unverified' | string;
|
|
87
|
+
}
|
|
88
|
+
/** Openpay's charge status → what the seller needs to know. Anything unknown stays `pending`: never invent a payment. */
|
|
89
|
+
export declare function openpayState(status: string | undefined): OpenpayPaymentState;
|
|
90
|
+
/**
|
|
91
|
+
* Openpay's REST API for ONE merchant, with that merchant's own keys. Creates the card link and the bank (SPEI)
|
|
92
|
+
* transfer, reads a charge, refunds a card charge and manages the webhook. The state of a payment is always read
|
|
93
|
+
* from Openpay, never from what a buyer or a notification body says.
|
|
94
|
+
*/
|
|
95
|
+
export declare class OpenpayApi {
|
|
96
|
+
private readonly cred;
|
|
97
|
+
private readonly msg;
|
|
98
|
+
private readonly http;
|
|
99
|
+
private readonly base;
|
|
100
|
+
private readonly dashboard;
|
|
101
|
+
constructor(cred: OpenpayCredentials, o?: OpenpayApiOptions);
|
|
102
|
+
private url;
|
|
103
|
+
private headers;
|
|
104
|
+
private customerBody;
|
|
105
|
+
/** `requestJson` with the error shaped for the app: provider failures keep their body; duplicates keep their code. */
|
|
106
|
+
private call;
|
|
107
|
+
/** Card: Openpay's hosted form (a "virtual terminal" charge). Pays with any card; the buyer never touches your server. */
|
|
108
|
+
createCardLink(c: CreateCardLink): Promise<CardLink>;
|
|
109
|
+
/** Bank: a CLABE plus a 20-digit reference. The buyer pays from any bank by SPEI; Openpay confirms it by webhook. */
|
|
110
|
+
createBankTransfer(c: CreateBankTransfer): Promise<BankTransfer>;
|
|
111
|
+
private shape;
|
|
112
|
+
getCharge(chargeId: string): Promise<OpenpayCharge>;
|
|
113
|
+
/** Your own id is the way back when you only have the sale (the notice carries both). */
|
|
114
|
+
findByOrderId(orderId: string): Promise<OpenpayCharge | null>;
|
|
115
|
+
/** Card charges only: Openpay does not refund SPEI. `amountCents` omitted = the whole charge. */
|
|
116
|
+
refundCard(chargeId: string, amountCents?: number, description?: string): Promise<OpenpayCharge>;
|
|
117
|
+
/** Notices go to `url` with HTTP Basic (`user`/`password`), the only authentication Openpay supports. */
|
|
118
|
+
createWebhook(w: {
|
|
119
|
+
url: string;
|
|
120
|
+
user: string;
|
|
121
|
+
password: string;
|
|
122
|
+
eventTypes: string[];
|
|
123
|
+
}): Promise<OpenpayWebhook>;
|
|
124
|
+
/** Activates a webhook with the code Openpay sent to its URL in the `verification` notice. */
|
|
125
|
+
verifyWebhook(webhookId: string, code: string): Promise<void>;
|
|
126
|
+
deleteWebhook(webhookId: string): Promise<void>;
|
|
127
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import type { I18nMessage } from '../../i18n/message.js';
|
|
2
|
+
import type { CredentialsCipher } from '../connect/credentials-cipher.js';
|
|
3
|
+
import { OPENPAY_PROVIDER_ERROR, type BankTransfer, type OpenpayCredentials, type OpenpayCustomer, type OpenpayMethod, type OpenpayPaymentState } from './openpay-api.js';
|
|
4
|
+
/** What is persisted (encrypted JSON) per connected merchant. Keep the names if you already have rows. */
|
|
5
|
+
export interface OpenpayStoredCredentials extends OpenpayCredentials {
|
|
6
|
+
/** Partner token, kept to fetch the merchant again; Openpay's own secret key does not expire. */
|
|
7
|
+
refreshToken?: string;
|
|
8
|
+
/** Basic-auth password that Openpay sends back on every notice for this merchant. */
|
|
9
|
+
webhookPassword: string;
|
|
10
|
+
/** Openpay's own view of the account (`active`…): a freshly registered merchant may still be confirming. */
|
|
11
|
+
merchantStatus?: string;
|
|
12
|
+
}
|
|
13
|
+
/** One connected merchant of a tenant (a store, a branch…). What your database row looks like. */
|
|
14
|
+
export interface OpenpayConnectionRecord {
|
|
15
|
+
/** The tenant that owns it. One connection per tenant. */
|
|
16
|
+
scopeId: string;
|
|
17
|
+
encryptedCredentials: string;
|
|
18
|
+
/** Goes in the webhook URL and is the Basic-auth user. Unique. */
|
|
19
|
+
webhookToken: string;
|
|
20
|
+
webhookId: string | null;
|
|
21
|
+
/** The code Openpay sent to activate the webhook, until it is used. */
|
|
22
|
+
verificationCode: string | null;
|
|
23
|
+
/** Keys of one environment only: a sandbox merchant never charges for real. */
|
|
24
|
+
sandbox: boolean;
|
|
25
|
+
active: boolean;
|
|
26
|
+
createdAt: Date;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Where connections live. Implement it over any table. `findByWebhookToken` runs without a tenant in context (the
|
|
30
|
+
* webhook), so it needs an implementation that is allowed to cross tenants.
|
|
31
|
+
*/
|
|
32
|
+
export interface OpenpayConnectionStore {
|
|
33
|
+
find(scopeId: string): Promise<OpenpayConnectionRecord | null>;
|
|
34
|
+
/** Insert or replace the tenant's only connection. */
|
|
35
|
+
upsert(data: Omit<OpenpayConnectionRecord, 'createdAt'>): Promise<OpenpayConnectionRecord>;
|
|
36
|
+
update(scopeId: string, patch: Partial<Omit<OpenpayConnectionRecord, 'scopeId' | 'createdAt'>>): Promise<void>;
|
|
37
|
+
/** `false` when the tenant had none. */
|
|
38
|
+
delete(scopeId: string): Promise<boolean>;
|
|
39
|
+
findByWebhookToken(token: string): Promise<OpenpayConnectionRecord | null>;
|
|
40
|
+
}
|
|
41
|
+
/** An Openpay partner application: what Openpay emailed when the partner registered (sandbox and production differ). */
|
|
42
|
+
export interface OpenpayPartner {
|
|
43
|
+
clientId: string;
|
|
44
|
+
clientSecret: string;
|
|
45
|
+
/** Character for character the URL registered on the partner form. */
|
|
46
|
+
redirectUri: string;
|
|
47
|
+
}
|
|
48
|
+
export type OpenpayMessageKey = 'notConfigured' | 'notConnected' | 'badState' | 'notAuthorized' | 'refundUnsupported' | 'minimumAmount';
|
|
49
|
+
type Message = string | I18nMessage;
|
|
50
|
+
export interface OpenpayConnectionsOptions {
|
|
51
|
+
store: OpenpayConnectionStore;
|
|
52
|
+
/** Without it the whole feature reports `available === false`. Build it with `CredentialsCipher.fromHex`. */
|
|
53
|
+
cipher: CredentialsCipher | null | (() => CredentialsCipher | null);
|
|
54
|
+
/** Your partner application for the environment in use; `null` when it is not configured. */
|
|
55
|
+
partner: OpenpayPartner | null | (() => OpenpayPartner | null);
|
|
56
|
+
/** `true` while testing against Openpay's sandbox. Default `false`. */
|
|
57
|
+
sandbox?: boolean | (() => boolean);
|
|
58
|
+
/** Secret that signs the OAuth `state`. */
|
|
59
|
+
stateSecret: string | (() => string | undefined);
|
|
60
|
+
/** Public URL of your API that receives the notices, given the connection's token. */
|
|
61
|
+
publicWebhookUrl: (webhookToken: string) => string;
|
|
62
|
+
/** The `fetch` for Openpay (timeouts, retries, alerts). Charges are idempotent by `orderId`, so retrying is safe. */
|
|
63
|
+
fetchImpl?: (scopeId?: string) => typeof fetch;
|
|
64
|
+
/** Test doubles. */
|
|
65
|
+
apiBaseUrl?: string;
|
|
66
|
+
dashboardBaseUrl?: string;
|
|
67
|
+
/** Smallest amount Openpay accepts, in cents. Default 1000 (MX$10). */
|
|
68
|
+
minimumCents?: number;
|
|
69
|
+
messages?: Partial<Record<OpenpayMessageKey, Message>>;
|
|
70
|
+
}
|
|
71
|
+
/** The connection without its secrets, ready to show. */
|
|
72
|
+
export interface OpenpayConnectionView {
|
|
73
|
+
connected: boolean;
|
|
74
|
+
/** Connected and Openpay confirms the account: it can charge. */
|
|
75
|
+
ready: boolean;
|
|
76
|
+
sandbox: boolean;
|
|
77
|
+
merchantId: string | null;
|
|
78
|
+
merchantStatus: string | null;
|
|
79
|
+
/** The webhook is active: payments confirm on their own. If not, `lookup` still tells the truth. */
|
|
80
|
+
notices: boolean;
|
|
81
|
+
}
|
|
82
|
+
export interface OpenpayPaymentInput {
|
|
83
|
+
method: OpenpayMethod;
|
|
84
|
+
/** Your id for the sale: comes back in the notice and prevents charging the same sale twice. */
|
|
85
|
+
reference: string;
|
|
86
|
+
description: string;
|
|
87
|
+
amountCents: number;
|
|
88
|
+
customer?: OpenpayCustomer;
|
|
89
|
+
/** Card only: where the buyer lands after paying. */
|
|
90
|
+
returnUrl?: string;
|
|
91
|
+
/** Transfer only: last day to pay. Default 3 days. */
|
|
92
|
+
dueDate?: Date;
|
|
93
|
+
sendEmail?: boolean;
|
|
94
|
+
clientIp?: string;
|
|
95
|
+
}
|
|
96
|
+
export interface OpenpayPayment {
|
|
97
|
+
method: OpenpayMethod;
|
|
98
|
+
chargeId: string;
|
|
99
|
+
reference: string;
|
|
100
|
+
state: OpenpayPaymentState;
|
|
101
|
+
/** Card: Openpay's form. Transfer: the printable receipt with the CLABE (only while pending). */
|
|
102
|
+
url: string;
|
|
103
|
+
/** Transfer only: what to show the buyer. */
|
|
104
|
+
transfer: BankTransfer | null;
|
|
105
|
+
}
|
|
106
|
+
export interface OpenpayLookup {
|
|
107
|
+
chargeId: string;
|
|
108
|
+
method: string;
|
|
109
|
+
reference: string | null;
|
|
110
|
+
state: OpenpayPaymentState;
|
|
111
|
+
paidCents: number | null;
|
|
112
|
+
}
|
|
113
|
+
/** What a notice means once Openpay itself confirmed it. */
|
|
114
|
+
export type OpenpayNotice = {
|
|
115
|
+
kind: 'payment';
|
|
116
|
+
scopeId: string;
|
|
117
|
+
chargeId: string;
|
|
118
|
+
reference: string | null;
|
|
119
|
+
state: OpenpayPaymentState;
|
|
120
|
+
paidCents: number | null;
|
|
121
|
+
method: string;
|
|
122
|
+
} | {
|
|
123
|
+
kind: 'verification';
|
|
124
|
+
scopeId: string;
|
|
125
|
+
} | {
|
|
126
|
+
kind: 'ignored';
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* Everything around Openpay for a multi-tenant app: the merchant signs in (or registers) at Openpay, authorizes you
|
|
130
|
+
* through OAuth and you charge on THEIR account — cards through Openpay's hosted form and bank transfers (SPEI) with a
|
|
131
|
+
* CLABE and a reference. No keys are typed in; the money never passes through you. Payments confirm through a webhook
|
|
132
|
+
* that this class registers for them, and the state is always re-read from Openpay.
|
|
133
|
+
*
|
|
134
|
+
* Framework-free: persistence is the {@link OpenpayConnectionStore} port, secrets go through a
|
|
135
|
+
* {@link CredentialsCipher}, and what changes between sandbox and production is an option.
|
|
136
|
+
*/
|
|
137
|
+
export declare class OpenpayConnections {
|
|
138
|
+
private readonly o;
|
|
139
|
+
private readonly log;
|
|
140
|
+
private readonly msg;
|
|
141
|
+
constructor(o: OpenpayConnectionsOptions);
|
|
142
|
+
private get cipher();
|
|
143
|
+
private get partner();
|
|
144
|
+
private get sandbox();
|
|
145
|
+
private get stateSecret();
|
|
146
|
+
private dashboard;
|
|
147
|
+
private http;
|
|
148
|
+
private apiOptions;
|
|
149
|
+
/** The feature is on: a cipher, a partner application and a signing secret are configured. */
|
|
150
|
+
get available(): boolean;
|
|
151
|
+
private requireCipher;
|
|
152
|
+
private credentialsOf;
|
|
153
|
+
private recordOf;
|
|
154
|
+
private apiOf;
|
|
155
|
+
/**
|
|
156
|
+
* Where to send the merchant: Openpay lets them pick "use my account" (they sign in and authorize) or "new account"
|
|
157
|
+
* (they register right there). `extra` rides in the signed `state` and comes back from {@link complete}.
|
|
158
|
+
*/
|
|
159
|
+
authorizationUrl(scopeId: string, extra?: Record<string, string>): string;
|
|
160
|
+
/** Reads the callback's `state` without trading the code: who started the flow (and what they sent along). */
|
|
161
|
+
readState<T extends Record<string, string> = Record<string, string>>(state: string | undefined): (T & {
|
|
162
|
+
scopeId: string;
|
|
163
|
+
}) | null;
|
|
164
|
+
/**
|
|
165
|
+
* Trades the callback's `code` for the merchant's keys, saves them encrypted and registers the webhook that
|
|
166
|
+
* confirms payments. A failure of the webhook does not undo the connection: charges still work and `lookup` reads
|
|
167
|
+
* the truth; call {@link ensureNotices} to retry.
|
|
168
|
+
*/
|
|
169
|
+
complete(input: {
|
|
170
|
+
code?: string;
|
|
171
|
+
state?: string;
|
|
172
|
+
error?: string;
|
|
173
|
+
}): Promise<{
|
|
174
|
+
scopeId: string;
|
|
175
|
+
extra: Record<string, string>;
|
|
176
|
+
view: OpenpayConnectionView;
|
|
177
|
+
}>;
|
|
178
|
+
private oauth;
|
|
179
|
+
/** Registers (or repairs) the webhook so paid sales confirm by themselves. Safe to call again. */
|
|
180
|
+
ensureNotices(scopeId: string): Promise<boolean>;
|
|
181
|
+
private dropWebhook;
|
|
182
|
+
view(scopeId: string, known?: OpenpayConnectionRecord | null): Promise<OpenpayConnectionView>;
|
|
183
|
+
/** Disconnects: removes the webhook (best effort) and forgets the keys. */
|
|
184
|
+
disconnect(scopeId: string): Promise<boolean>;
|
|
185
|
+
/** Opens a charge on the merchant's account: a card link or a bank transfer with CLABE. */
|
|
186
|
+
createPayment(scopeId: string, p: OpenpayPaymentInput): Promise<OpenpayPayment>;
|
|
187
|
+
/** The state of a charge according to Openpay. */
|
|
188
|
+
lookup(scopeId: string, chargeId: string): Promise<OpenpayLookup>;
|
|
189
|
+
/** Gives money back on a card charge (Openpay does not refund SPEI). `amountCents` omitted = all of it. */
|
|
190
|
+
refund(scopeId: string, chargeId: string, amountCents?: number, description?: string): Promise<OpenpayLookup>;
|
|
191
|
+
private text;
|
|
192
|
+
/**
|
|
193
|
+
* Handles a webhook call. `token` is the last segment of the URL, `authorization` the `Authorization` header (HTTP
|
|
194
|
+
* Basic, user = token, password = the one stored for the merchant). A payment notice is never believed: the charge
|
|
195
|
+
* is read again from Openpay and only that answer counts. Always answer 200 to what this returns; throw only for a
|
|
196
|
+
* wrong password so a forged call learns nothing.
|
|
197
|
+
*/
|
|
198
|
+
handleNotice(token: string, authorization: string | undefined, body: unknown): Promise<OpenpayNotice>;
|
|
199
|
+
private validBasic;
|
|
200
|
+
}
|
|
201
|
+
export { OPENPAY_PROVIDER_ERROR };
|
package/dist/security/index.cjs
CHANGED
|
@@ -1,152 +1,27 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (typeof ts !== "number" || !Number.isFinite(ts) || Date.now() - ts >= ttl) return null;
|
|
29
|
-
const { ts: _ts, nonce: _nonce, ...rest } = parsed;
|
|
30
|
-
return rest;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// src/security/mask.ts
|
|
34
|
-
function maskEmail(email) {
|
|
35
|
-
const clean = email.trim();
|
|
36
|
-
const at = clean.lastIndexOf("@");
|
|
37
|
-
if (at < 1) return "***";
|
|
38
|
-
return `${clean[0]}***${clean.slice(at)}`;
|
|
39
|
-
}
|
|
40
|
-
function maskPhone(phone, visible = 4) {
|
|
41
|
-
const digits = phone.replace(/\D/g, "");
|
|
42
|
-
if (digits.length <= visible) return "***";
|
|
43
|
-
return `***${digits.slice(-visible)}`;
|
|
44
|
-
}
|
|
45
|
-
function maskToken(token, keep = 2) {
|
|
46
|
-
if (token.length <= keep * 2) return "***";
|
|
47
|
-
return `${token.slice(0, keep)}***${token.slice(-keep)}`;
|
|
48
|
-
}
|
|
49
|
-
function shapeOf(value, depth = 4) {
|
|
50
|
-
if (value === null) return "null";
|
|
51
|
-
if (Array.isArray(value)) return depth <= 0 ? "array" : value.slice(0, 3).map((v) => shapeOf(v, depth - 1));
|
|
52
|
-
if (typeof value === "object") {
|
|
53
|
-
if (depth <= 0) return "object";
|
|
54
|
-
return Object.fromEntries(
|
|
55
|
-
Object.entries(value).map(([k, v]) => [k, shapeOf(v, depth - 1)])
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
return typeof value;
|
|
59
|
-
}
|
|
60
|
-
var apply = (rule, value) => {
|
|
61
|
-
if (typeof rule === "function") return rule(value);
|
|
62
|
-
if (rule === "drop") return void 0;
|
|
63
|
-
if (rule === "redact" || typeof value !== "string") return "***";
|
|
64
|
-
if (rule === "email") return maskEmail(value);
|
|
65
|
-
if (rule === "phone") return maskPhone(value);
|
|
66
|
-
return maskToken(value);
|
|
67
|
-
};
|
|
68
|
-
function maskFields(value, rules, maxDepth = 8) {
|
|
69
|
-
const byKey = new Map(Object.entries(rules).map(([k, r]) => [k.toLowerCase(), r]));
|
|
70
|
-
const seen = /* @__PURE__ */ new WeakSet();
|
|
71
|
-
const walk = (v, depth) => {
|
|
72
|
-
if (v === null || typeof v !== "object") return v;
|
|
73
|
-
if (depth > maxDepth || seen.has(v)) return "[\u2026]";
|
|
74
|
-
seen.add(v);
|
|
75
|
-
if (Array.isArray(v)) return v.map((x) => walk(x, depth + 1));
|
|
76
|
-
const out = {};
|
|
77
|
-
for (const [k, x] of Object.entries(v)) {
|
|
78
|
-
const rule = byKey.get(k.toLowerCase());
|
|
79
|
-
if (rule === void 0) out[k] = walk(x, depth + 1);
|
|
80
|
-
else {
|
|
81
|
-
try {
|
|
82
|
-
const masked = apply(rule, x);
|
|
83
|
-
if (masked !== void 0) out[k] = masked;
|
|
84
|
-
} catch (e2) {
|
|
85
|
-
out[k] = "***";
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return out;
|
|
90
|
-
};
|
|
91
|
-
return walk(value, 0);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// src/security/hash.ts
|
|
95
|
-
|
|
96
|
-
var hashSha256 = (value) => _crypto.createHash.call(void 0, "sha256").update(value).digest("hex");
|
|
97
|
-
function constantTimeEqual(a, b) {
|
|
98
|
-
const ba = Buffer.from(a);
|
|
99
|
-
const bb = Buffer.from(b);
|
|
100
|
-
if (ba.length !== bb.length) {
|
|
101
|
-
_crypto.timingSafeEqual.call(void 0, ba, ba);
|
|
102
|
-
return false;
|
|
103
|
-
}
|
|
104
|
-
return _crypto.timingSafeEqual.call(void 0, ba, bb);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// src/security/signed-grant.ts
|
|
108
|
-
|
|
109
|
-
var sign = (b64, secret) => _crypto.createHmac.call(void 0, "sha256", secret).update(b64).digest("base64url");
|
|
110
|
-
function signGrant(claims, secret, ttlMs = 6e4) {
|
|
111
|
-
const body = JSON.stringify({
|
|
112
|
-
a: claims.actor,
|
|
113
|
-
p: claims.permission,
|
|
114
|
-
r: claims.resource,
|
|
115
|
-
t: claims.tenant,
|
|
116
|
-
exp: Date.now() + ttlMs,
|
|
117
|
-
n: _crypto.randomBytes.call(void 0, 8).toString("hex")
|
|
118
|
-
});
|
|
119
|
-
const b64 = Buffer.from(body).toString("base64url");
|
|
120
|
-
return `${b64}.${sign(b64, secret)}`;
|
|
121
|
-
}
|
|
122
|
-
function verifyGrant(token, secret, expected) {
|
|
123
|
-
if (!token) return null;
|
|
124
|
-
const parts = token.split(".");
|
|
125
|
-
if (parts.length !== 2) return null;
|
|
126
|
-
const [b64, sig] = parts;
|
|
127
|
-
if (!constantTimeEqual(sig, sign(b64, secret))) return null;
|
|
128
|
-
let p;
|
|
129
|
-
try {
|
|
130
|
-
p = JSON.parse(Buffer.from(b64, "base64url").toString());
|
|
131
|
-
} catch (e3) {
|
|
132
|
-
return null;
|
|
133
|
-
}
|
|
134
|
-
if (typeof p.exp !== "number" || !Number.isFinite(p.exp) || Date.now() >= p.exp) return null;
|
|
135
|
-
if (typeof p.a !== "string" || typeof p.p !== "string" || typeof p.r !== "string" || typeof p.t !== "string") return null;
|
|
136
|
-
if (p.p !== expected.permission || p.r !== expected.resource || p.t !== expected.tenant) return null;
|
|
137
|
-
if (expected.actor !== void 0 && p.a !== expected.actor) return null;
|
|
138
|
-
return { actor: p.a, permission: p.p, resource: p.r, tenant: p.t };
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
exports.constantTimeEqual = constantTimeEqual; exports.hashSha256 = hashSha256; exports.maskEmail = maskEmail; exports.maskFields = maskFields; exports.maskPhone = maskPhone; exports.maskToken = maskToken; exports.readState = readState; exports.shapeOf = shapeOf; exports.signGrant = signGrant; exports.signState = signState; exports.verifyGrant = verifyGrant;
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
var _chunk33KFVAUZcjs = require('../chunk-33KFVAUZ.cjs');
|
|
14
|
+
require('../chunk-LRX7AMSS.cjs');
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
exports.constantTimeEqual = _chunk33KFVAUZcjs.constantTimeEqual; exports.hashSha256 = _chunk33KFVAUZcjs.hashSha256; exports.maskEmail = _chunk33KFVAUZcjs.maskEmail; exports.maskFields = _chunk33KFVAUZcjs.maskFields; exports.maskPhone = _chunk33KFVAUZcjs.maskPhone; exports.maskToken = _chunk33KFVAUZcjs.maskToken; exports.readState = _chunk33KFVAUZcjs.readState; exports.shapeOf = _chunk33KFVAUZcjs.shapeOf; exports.signGrant = _chunk33KFVAUZcjs.signGrant; exports.signState = _chunk33KFVAUZcjs.signState; exports.verifyGrant = _chunk33KFVAUZcjs.verifyGrant;
|
package/dist/security/index.js
CHANGED
|
@@ -1,142 +1,17 @@
|
|
|
1
|
+
import {
|
|
2
|
+
constantTimeEqual,
|
|
3
|
+
hashSha256,
|
|
4
|
+
maskEmail,
|
|
5
|
+
maskFields,
|
|
6
|
+
maskPhone,
|
|
7
|
+
maskToken,
|
|
8
|
+
readState,
|
|
9
|
+
shapeOf,
|
|
10
|
+
signGrant,
|
|
11
|
+
signState,
|
|
12
|
+
verifyGrant
|
|
13
|
+
} from "../chunk-LU3HO7B3.js";
|
|
1
14
|
import "../chunk-D3DDJWUD.js";
|
|
2
|
-
|
|
3
|
-
// src/security/signed-state.ts
|
|
4
|
-
import { createHmac, randomBytes, timingSafeEqual } from "crypto";
|
|
5
|
-
function signState(payload, options) {
|
|
6
|
-
const body = JSON.stringify({ ts: Date.now(), nonce: randomBytes(8).toString("hex"), ...payload });
|
|
7
|
-
const b64 = Buffer.from(body).toString("base64url");
|
|
8
|
-
const sig = createHmac("sha256", options.secret).update(b64).digest("base64url");
|
|
9
|
-
return `${b64}.${sig}`;
|
|
10
|
-
}
|
|
11
|
-
function readState(state, options) {
|
|
12
|
-
if (!state) return null;
|
|
13
|
-
const parts = state.split(".");
|
|
14
|
-
if (parts.length !== 2) return null;
|
|
15
|
-
const [b64, sig] = parts;
|
|
16
|
-
const expected = createHmac("sha256", options.secret).update(b64).digest("base64url");
|
|
17
|
-
const provided = Buffer.from(sig);
|
|
18
|
-
const wanted = Buffer.from(expected);
|
|
19
|
-
if (provided.length !== wanted.length || !timingSafeEqual(provided, wanted)) return null;
|
|
20
|
-
let parsed;
|
|
21
|
-
try {
|
|
22
|
-
parsed = JSON.parse(Buffer.from(b64, "base64url").toString());
|
|
23
|
-
} catch {
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
const ts = parsed.ts;
|
|
27
|
-
const ttl = options.ttlMs ?? 10 * 6e4;
|
|
28
|
-
if (typeof ts !== "number" || !Number.isFinite(ts) || Date.now() - ts >= ttl) return null;
|
|
29
|
-
const { ts: _ts, nonce: _nonce, ...rest } = parsed;
|
|
30
|
-
return rest;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// src/security/mask.ts
|
|
34
|
-
function maskEmail(email) {
|
|
35
|
-
const clean = email.trim();
|
|
36
|
-
const at = clean.lastIndexOf("@");
|
|
37
|
-
if (at < 1) return "***";
|
|
38
|
-
return `${clean[0]}***${clean.slice(at)}`;
|
|
39
|
-
}
|
|
40
|
-
function maskPhone(phone, visible = 4) {
|
|
41
|
-
const digits = phone.replace(/\D/g, "");
|
|
42
|
-
if (digits.length <= visible) return "***";
|
|
43
|
-
return `***${digits.slice(-visible)}`;
|
|
44
|
-
}
|
|
45
|
-
function maskToken(token, keep = 2) {
|
|
46
|
-
if (token.length <= keep * 2) return "***";
|
|
47
|
-
return `${token.slice(0, keep)}***${token.slice(-keep)}`;
|
|
48
|
-
}
|
|
49
|
-
function shapeOf(value, depth = 4) {
|
|
50
|
-
if (value === null) return "null";
|
|
51
|
-
if (Array.isArray(value)) return depth <= 0 ? "array" : value.slice(0, 3).map((v) => shapeOf(v, depth - 1));
|
|
52
|
-
if (typeof value === "object") {
|
|
53
|
-
if (depth <= 0) return "object";
|
|
54
|
-
return Object.fromEntries(
|
|
55
|
-
Object.entries(value).map(([k, v]) => [k, shapeOf(v, depth - 1)])
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
return typeof value;
|
|
59
|
-
}
|
|
60
|
-
var apply = (rule, value) => {
|
|
61
|
-
if (typeof rule === "function") return rule(value);
|
|
62
|
-
if (rule === "drop") return void 0;
|
|
63
|
-
if (rule === "redact" || typeof value !== "string") return "***";
|
|
64
|
-
if (rule === "email") return maskEmail(value);
|
|
65
|
-
if (rule === "phone") return maskPhone(value);
|
|
66
|
-
return maskToken(value);
|
|
67
|
-
};
|
|
68
|
-
function maskFields(value, rules, maxDepth = 8) {
|
|
69
|
-
const byKey = new Map(Object.entries(rules).map(([k, r]) => [k.toLowerCase(), r]));
|
|
70
|
-
const seen = /* @__PURE__ */ new WeakSet();
|
|
71
|
-
const walk = (v, depth) => {
|
|
72
|
-
if (v === null || typeof v !== "object") return v;
|
|
73
|
-
if (depth > maxDepth || seen.has(v)) return "[\u2026]";
|
|
74
|
-
seen.add(v);
|
|
75
|
-
if (Array.isArray(v)) return v.map((x) => walk(x, depth + 1));
|
|
76
|
-
const out = {};
|
|
77
|
-
for (const [k, x] of Object.entries(v)) {
|
|
78
|
-
const rule = byKey.get(k.toLowerCase());
|
|
79
|
-
if (rule === void 0) out[k] = walk(x, depth + 1);
|
|
80
|
-
else {
|
|
81
|
-
try {
|
|
82
|
-
const masked = apply(rule, x);
|
|
83
|
-
if (masked !== void 0) out[k] = masked;
|
|
84
|
-
} catch {
|
|
85
|
-
out[k] = "***";
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return out;
|
|
90
|
-
};
|
|
91
|
-
return walk(value, 0);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// src/security/hash.ts
|
|
95
|
-
import { createHash, timingSafeEqual as timingSafeEqual2 } from "crypto";
|
|
96
|
-
var hashSha256 = (value) => createHash("sha256").update(value).digest("hex");
|
|
97
|
-
function constantTimeEqual(a, b) {
|
|
98
|
-
const ba = Buffer.from(a);
|
|
99
|
-
const bb = Buffer.from(b);
|
|
100
|
-
if (ba.length !== bb.length) {
|
|
101
|
-
timingSafeEqual2(ba, ba);
|
|
102
|
-
return false;
|
|
103
|
-
}
|
|
104
|
-
return timingSafeEqual2(ba, bb);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// src/security/signed-grant.ts
|
|
108
|
-
import { createHmac as createHmac2, randomBytes as randomBytes2 } from "crypto";
|
|
109
|
-
var sign = (b64, secret) => createHmac2("sha256", secret).update(b64).digest("base64url");
|
|
110
|
-
function signGrant(claims, secret, ttlMs = 6e4) {
|
|
111
|
-
const body = JSON.stringify({
|
|
112
|
-
a: claims.actor,
|
|
113
|
-
p: claims.permission,
|
|
114
|
-
r: claims.resource,
|
|
115
|
-
t: claims.tenant,
|
|
116
|
-
exp: Date.now() + ttlMs,
|
|
117
|
-
n: randomBytes2(8).toString("hex")
|
|
118
|
-
});
|
|
119
|
-
const b64 = Buffer.from(body).toString("base64url");
|
|
120
|
-
return `${b64}.${sign(b64, secret)}`;
|
|
121
|
-
}
|
|
122
|
-
function verifyGrant(token, secret, expected) {
|
|
123
|
-
if (!token) return null;
|
|
124
|
-
const parts = token.split(".");
|
|
125
|
-
if (parts.length !== 2) return null;
|
|
126
|
-
const [b64, sig] = parts;
|
|
127
|
-
if (!constantTimeEqual(sig, sign(b64, secret))) return null;
|
|
128
|
-
let p;
|
|
129
|
-
try {
|
|
130
|
-
p = JSON.parse(Buffer.from(b64, "base64url").toString());
|
|
131
|
-
} catch {
|
|
132
|
-
return null;
|
|
133
|
-
}
|
|
134
|
-
if (typeof p.exp !== "number" || !Number.isFinite(p.exp) || Date.now() >= p.exp) return null;
|
|
135
|
-
if (typeof p.a !== "string" || typeof p.p !== "string" || typeof p.r !== "string" || typeof p.t !== "string") return null;
|
|
136
|
-
if (p.p !== expected.permission || p.r !== expected.resource || p.t !== expected.tenant) return null;
|
|
137
|
-
if (expected.actor !== void 0 && p.a !== expected.actor) return null;
|
|
138
|
-
return { actor: p.a, permission: p.p, resource: p.r, tenant: p.t };
|
|
139
|
-
}
|
|
140
15
|
export {
|
|
141
16
|
constantTimeEqual,
|
|
142
17
|
hashSha256,
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@miguelmorales13/nestkit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.30.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"description": "Reusable NestJS building blocks: i18n, error handling, standardized responses, request tracking, base entities, generic CRUD, Postgres/Supabase connection helpers, outbound Telegram/WhatsApp/Stripe/Meta clients, short-lived media hosting, batteries-included auth, Stripe webhooks, Resend/Nodemailer email, card-present terminal payments (Mercado Pago Point, Clip) with marketplace OAuth
|
|
7
|
+
"description": "Reusable NestJS building blocks: i18n, error handling, standardized responses, request tracking, base entities, generic CRUD, Postgres/Supabase connection helpers, outbound Telegram/WhatsApp/Stripe/Meta clients, short-lived media hosting, batteries-included auth, Stripe webhooks, Resend/Nodemailer email, card-present terminal payments (Mercado Pago Point, Clip) with marketplace OAuth, Store/POS provisioning and a multi-tenant connections service (encrypted credentials, token refresh, webhooks, refunds, online checkout), signed stateless OAuth connect state, and CFDI 4.0 invoicing (Facturapi, Facturama).",
|
|
8
8
|
"publishConfig": {
|
|
9
9
|
"access": "public"
|
|
10
10
|
},
|