@portal-hq/utils 4.8.0 → 5.0.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/lib/commonjs/credentials/index.js +260 -0
- package/lib/commonjs/errors/http-error.js +1 -0
- package/lib/commonjs/errors/index.js +7 -1
- package/lib/commonjs/errors/portal-auth-error.js +63 -0
- package/lib/commonjs/errors/portal-credential-error.js +58 -0
- package/lib/commonjs/errors/portal-error.js +13 -1
- package/lib/commonjs/events/index.js +2 -2
- package/lib/commonjs/index.js +17 -2
- package/lib/commonjs/requesters/http.js +33 -16
- package/lib/commonjs/version/version.json +1 -1
- package/lib/esm/credentials/index.js +249 -0
- package/lib/esm/errors/http-error.js +1 -0
- package/lib/esm/errors/index.js +2 -0
- package/lib/esm/errors/portal-auth-error.js +60 -0
- package/lib/esm/errors/portal-credential-error.js +55 -0
- package/lib/esm/errors/portal-error.js +11 -0
- package/lib/esm/events/index.js +2 -2
- package/lib/esm/index.js +2 -1
- package/lib/esm/requesters/http.js +33 -16
- package/lib/esm/version/version.json +1 -1
- package/package.json +2 -2
- package/src/credentials/index.ts +307 -0
- package/src/definitions/index.ts +40 -8
- package/src/errors/http-error.ts +5 -0
- package/src/errors/index.ts +10 -0
- package/src/errors/portal-auth-error.ts +86 -0
- package/src/errors/portal-credential-error.ts +80 -0
- package/src/errors/portal-error.ts +14 -0
- package/src/events/index.ts +3 -2
- package/src/index.ts +21 -0
- package/src/requesters/http.ts +35 -21
- package/src/version/version.json +1 -1
- package/types.d.ts +34 -25
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var _a;
|
|
11
|
+
import { InvalidApiKeyError, PortalCredentialError, PortalCredentialErrorReason, PortalMpcError, isMpcAuthFailure, } from '../errors';
|
|
12
|
+
import { sdkLogger } from '../logger';
|
|
13
|
+
/**
|
|
14
|
+
* Cross-copy brand. The key and the branded shape (a string `value`) are a
|
|
15
|
+
* compatibility contract across versions — see `PortalCredentialError`.
|
|
16
|
+
*/
|
|
17
|
+
const BRAND = Symbol.for('@portal-hq/utils/StaticCredentials');
|
|
18
|
+
export class StaticCredentials {
|
|
19
|
+
constructor(apiKey) {
|
|
20
|
+
/** @internal Cross-copy brand. Read it through {@link StaticCredentials.is}. */
|
|
21
|
+
this[_a] = true;
|
|
22
|
+
this.value = apiKey;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Brand check, not `instanceof` — an instance from another copy of this
|
|
26
|
+
* package must still match. Reports the brand only: callers reading a member
|
|
27
|
+
* must re-check it, as {@link staticApiKeyOf} does.
|
|
28
|
+
*/
|
|
29
|
+
static is(credentials) {
|
|
30
|
+
return (typeof credentials === 'object' &&
|
|
31
|
+
credentials !== null &&
|
|
32
|
+
credentials[BRAND] === true);
|
|
33
|
+
}
|
|
34
|
+
getToken() {
|
|
35
|
+
return Promise.resolve(this.value);
|
|
36
|
+
}
|
|
37
|
+
/** No-op: a Client API Key is custodian-owned and holds no local state to drop. */
|
|
38
|
+
invalidate() {
|
|
39
|
+
return Promise.resolve();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
_a = BRAND;
|
|
43
|
+
/**
|
|
44
|
+
* Resolves the single credential source a component was constructed with.
|
|
45
|
+
*
|
|
46
|
+
* Throws `InvalidApiKeyError` when neither source is given — callers depend on
|
|
47
|
+
* that type.
|
|
48
|
+
*/
|
|
49
|
+
export function resolveCredentials({ apiKey, credentials, }) {
|
|
50
|
+
if (credentials && apiKey) {
|
|
51
|
+
throw new Error('[Portal] Provide either `apiKey` or `credentials`, not both.');
|
|
52
|
+
}
|
|
53
|
+
if (credentials) {
|
|
54
|
+
return credentials;
|
|
55
|
+
}
|
|
56
|
+
if (!apiKey) {
|
|
57
|
+
throw new InvalidApiKeyError();
|
|
58
|
+
}
|
|
59
|
+
return new StaticCredentials(apiKey);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The SDK's credential boundary: normalizes every failure to
|
|
63
|
+
* `PortalCredentialError`, since a host-supplied `PortalCredentials` can throw
|
|
64
|
+
* anything. A `PortalCredentialError` from the provider passes through
|
|
65
|
+
* untouched so its `reason` is never downgraded.
|
|
66
|
+
*/
|
|
67
|
+
export function resolveCredentialToken(credentials) {
|
|
68
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
69
|
+
let token;
|
|
70
|
+
try {
|
|
71
|
+
token = yield credentials.getToken();
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
if (PortalCredentialError.is(error)) {
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
throw new PortalCredentialError({
|
|
78
|
+
reason: PortalCredentialErrorReason.ProviderFailure,
|
|
79
|
+
cause: error,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
if (!token) {
|
|
83
|
+
throw new PortalCredentialError({
|
|
84
|
+
reason: PortalCredentialErrorReason.Unavailable,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return token;
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Keyed by the credential, not by subsystem, so several requesters 401-ing at
|
|
92
|
+
* once — all of which arrive here through `reportUnauthorized` — share one
|
|
93
|
+
* invalidation instead of each issuing a keychain delete.
|
|
94
|
+
*/
|
|
95
|
+
const invalidations = new WeakMap();
|
|
96
|
+
export function invalidateCredentials(credentials) {
|
|
97
|
+
const inFlight = invalidations.get(credentials);
|
|
98
|
+
if (inFlight) {
|
|
99
|
+
return inFlight;
|
|
100
|
+
}
|
|
101
|
+
// A host-supplied `invalidate()` may throw synchronously or return something
|
|
102
|
+
// that is not a native Promise. Starting from `Promise.resolve()` turns both
|
|
103
|
+
// into a rejection here rather than a throw that escapes before the guard
|
|
104
|
+
// below is registered, which would silently disable deduplication.
|
|
105
|
+
const invalidation = Promise.resolve()
|
|
106
|
+
.then(() => credentials.invalidate())
|
|
107
|
+
.finally(() => {
|
|
108
|
+
invalidations.delete(credentials);
|
|
109
|
+
});
|
|
110
|
+
invalidations.set(credentials, invalidation);
|
|
111
|
+
return invalidation;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Host listeners, keyed by the credential rather than by the `Portal` holding
|
|
115
|
+
* it, so they are reachable from the 401 call sites — which see a credential
|
|
116
|
+
* and nothing else — and so a credential that is dropped takes its listeners
|
|
117
|
+
* with it.
|
|
118
|
+
*
|
|
119
|
+
* One entry is one subscription, never one callback: two subscribers may hand
|
|
120
|
+
* in the same function, and an entry keyed on the function itself would
|
|
121
|
+
* collapse them into one that either unsubscribe removes — silently
|
|
122
|
+
* deregistering a subscriber that never asked to be.
|
|
123
|
+
*/
|
|
124
|
+
const invalidationListeners = new WeakMap();
|
|
125
|
+
/** Credentials already announced to the host, so each is reported once. */
|
|
126
|
+
const reported = new WeakSet();
|
|
127
|
+
/**
|
|
128
|
+
* Subscribes to the backend invalidating `credentials`. Returns an
|
|
129
|
+
* unsubscribe; see `Portal.onSessionInvalidated` for the host-facing contract.
|
|
130
|
+
*
|
|
131
|
+
* A credential can already have been reported before the host gets to
|
|
132
|
+
* subscribe — validity is discovered by making a request, so a host that
|
|
133
|
+
* awaits one before mounting its subscriber would otherwise never hear that
|
|
134
|
+
* the session is over, and nothing else exposes that state.
|
|
135
|
+
*/
|
|
136
|
+
export function onCredentialsInvalidated(credentials, listener) {
|
|
137
|
+
var _b;
|
|
138
|
+
const listeners = (_b = invalidationListeners.get(credentials)) !== null && _b !== void 0 ? _b : new Set();
|
|
139
|
+
// This subscription's identity in `listeners`, distinct from every other
|
|
140
|
+
// subscription of the same `listener` — see the note on that map.
|
|
141
|
+
const subscription = () => listener();
|
|
142
|
+
listeners.add(subscription);
|
|
143
|
+
invalidationListeners.set(credentials, listeners);
|
|
144
|
+
// `notifyInvalidated` will not run again for this credential, so catch this
|
|
145
|
+
// subscription up itself. Deferred so the caller holds its unsubscribe
|
|
146
|
+
// before the callback can run, and re-checked then so a subscription
|
|
147
|
+
// dropped straight away stays silent — which is also what keeps a React
|
|
148
|
+
// effect that tears down and remounts within one commit to a single call.
|
|
149
|
+
// A Client API Key never reaches here: it is never recorded as reported.
|
|
150
|
+
if (reported.has(credentials)) {
|
|
151
|
+
void Promise.resolve().then(() => {
|
|
152
|
+
if (!listeners.has(subscription)) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
try {
|
|
156
|
+
subscription();
|
|
157
|
+
}
|
|
158
|
+
catch (_b) {
|
|
159
|
+
// As in `notifyInvalidated`: a host callback failing is not this
|
|
160
|
+
// module's problem to propagate.
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
return () => {
|
|
165
|
+
listeners.delete(subscription);
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* At most once per credential, and never for a Client API Key: that credential
|
|
170
|
+
* is custodian-owned, its `invalidate()` is a no-op, and there is no local
|
|
171
|
+
* session for a 401 to have ended.
|
|
172
|
+
*/
|
|
173
|
+
function notifyInvalidated(credentials) {
|
|
174
|
+
var _b;
|
|
175
|
+
if (StaticCredentials.is(credentials) || reported.has(credentials)) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
reported.add(credentials);
|
|
179
|
+
// Snapshot: a listener is free to unsubscribe itself, or subscribe another,
|
|
180
|
+
// from inside the callback.
|
|
181
|
+
for (const listener of Array.from((_b = invalidationListeners.get(credentials)) !== null && _b !== void 0 ? _b : [])) {
|
|
182
|
+
try {
|
|
183
|
+
listener();
|
|
184
|
+
}
|
|
185
|
+
catch (_c) {
|
|
186
|
+
// A host callback failing must not take the other listeners, the
|
|
187
|
+
// invalidation, or this bookkeeping down with it.
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* The 401 path every Portal-authenticated requester routes through: invalidate
|
|
193
|
+
* the credential, then tell the host its session ended.
|
|
194
|
+
*
|
|
195
|
+
* Kept separate from {@link invalidateCredentials}, which stays the plain
|
|
196
|
+
* operation a host-initiated sign-out uses — only a backend rejection is news
|
|
197
|
+
* to the host.
|
|
198
|
+
*
|
|
199
|
+
* Notifies even when the invalidation rejects: the token is dropped in memory
|
|
200
|
+
* first, so the session is over either way. The rejection still propagates,
|
|
201
|
+
* and every call site already treats it as bookkeeping that must not replace
|
|
202
|
+
* the original failure.
|
|
203
|
+
*/
|
|
204
|
+
export function reportUnauthorized(credentials) {
|
|
205
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
206
|
+
try {
|
|
207
|
+
yield invalidateCredentials(credentials);
|
|
208
|
+
}
|
|
209
|
+
finally {
|
|
210
|
+
notifyInvalidated(credentials);
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* The MPC equivalent of the 401 path above: the binary issues its own
|
|
216
|
+
* authenticated requests, so a rejected credential arrives as an `AUTH_FAILED`
|
|
217
|
+
* envelope rather than a status code any requester here can see.
|
|
218
|
+
*
|
|
219
|
+
* The binary's error is rethrown unchanged either way — the invalidation is
|
|
220
|
+
* bookkeeping, and callers keep branching on `PortalMpcError` and its `id`.
|
|
221
|
+
*/
|
|
222
|
+
export function failFromMpcError(credentials, error) {
|
|
223
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
224
|
+
if (isMpcAuthFailure(error)) {
|
|
225
|
+
try {
|
|
226
|
+
yield reportUnauthorized(credentials);
|
|
227
|
+
}
|
|
228
|
+
catch (bookkeeping) {
|
|
229
|
+
sdkLogger.error('[Portal] Failed to invalidate a credential the MPC binary rejected', bookkeeping);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
throw new PortalMpcError(error);
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* The raw Client API Key behind a credential, or `''` when it is not a static
|
|
237
|
+
* key — never a wrong or stale token. Temporary bridge for subsystems that
|
|
238
|
+
* need a synchronous `apiKey`.
|
|
239
|
+
*/
|
|
240
|
+
export function staticApiKeyOf(credentials) {
|
|
241
|
+
if (!StaticCredentials.is(credentials)) {
|
|
242
|
+
return '';
|
|
243
|
+
}
|
|
244
|
+
// The brand identifies the shape, it does not prove it: an instance from
|
|
245
|
+
// another copy of this package is only structurally compatible, so re-check
|
|
246
|
+
// the type.
|
|
247
|
+
const { value } = credentials;
|
|
248
|
+
return typeof value === 'string' ? value : '';
|
|
249
|
+
}
|
package/lib/esm/errors/index.js
CHANGED
|
@@ -6,5 +6,7 @@ export { default as InvalidGatewayConfigError } from './invalid-gateway-config-e
|
|
|
6
6
|
export { default as MissingOptionError } from './missing-option-error';
|
|
7
7
|
export { default as MpcSigningError } from './mpc-signing-error';
|
|
8
8
|
export { default as NoAddressError } from './no-address-error';
|
|
9
|
+
export { default as PortalAuthError, PortalAuthErrorReason, } from './portal-auth-error';
|
|
10
|
+
export { default as PortalCredentialError, PortalCredentialErrorReason, } from './portal-credential-error';
|
|
9
11
|
export * from './portal-error';
|
|
10
12
|
export { default as ProviderRpcError, RpcErrorCodes, } from './provider-rpc-error';
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
var _a;
|
|
2
|
+
/**
|
|
3
|
+
* Why a Client Auth login could not be completed.
|
|
4
|
+
*
|
|
5
|
+
* The backend answers every unusable grant with the same undifferentiated 401
|
|
6
|
+
* — replayed, expired, forged, wrong auth environment, and disabled or invalid
|
|
7
|
+
* auth method are deliberately indistinguishable — so these reasons describe
|
|
8
|
+
* what the SDK actually knows and the action the host should take, not what
|
|
9
|
+
* went wrong server-side.
|
|
10
|
+
*/
|
|
11
|
+
export var PortalAuthErrorReason;
|
|
12
|
+
(function (PortalAuthErrorReason) {
|
|
13
|
+
/**
|
|
14
|
+
* The backend refused to exchange the grant. Whatever the cause, the grant
|
|
15
|
+
* is spent and cannot be retried: start a new sign-in.
|
|
16
|
+
*/
|
|
17
|
+
PortalAuthErrorReason["GrantRejected"] = "GRANT_REJECTED";
|
|
18
|
+
/**
|
|
19
|
+
* The authentication provider sent the user back with an error instead of a
|
|
20
|
+
* grant. The user never completed the provider's flow.
|
|
21
|
+
*/
|
|
22
|
+
PortalAuthErrorReason["ProviderRejected"] = "PROVIDER_REJECTED";
|
|
23
|
+
})(PortalAuthErrorReason || (PortalAuthErrorReason = {}));
|
|
24
|
+
const authErrors = {
|
|
25
|
+
[PortalAuthErrorReason.GrantRejected]: 'The authentication grant was rejected. It may already have been used, expired, or been issued for a different auth environment. Start a new sign-in.',
|
|
26
|
+
[PortalAuthErrorReason.ProviderRejected]: 'The authentication provider rejected the sign-in.',
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Cross-copy brand. `instanceof` compares prototypes, so it returns `false`
|
|
30
|
+
* when a consumer's tree resolves two `@portal-hq/utils`.
|
|
31
|
+
*
|
|
32
|
+
* This key and the members {@link PortalAuthError.is} implies are a
|
|
33
|
+
* compatibility contract: changing either breaks recognition between copies.
|
|
34
|
+
*/
|
|
35
|
+
const BRAND = Symbol.for('@portal-hq/utils/PortalAuthError');
|
|
36
|
+
/**
|
|
37
|
+
* The single error every Client Auth completion failure is normalized to;
|
|
38
|
+
* branch on `reason`.
|
|
39
|
+
*/
|
|
40
|
+
class PortalAuthError extends Error {
|
|
41
|
+
constructor({ reason, detail, cause }) {
|
|
42
|
+
super(`[PortalAuth] ${authErrors[reason]}${detail ? ` (${detail})` : ''}`);
|
|
43
|
+
/** @internal Cross-copy brand. Read it through {@link PortalAuthError.is}. */
|
|
44
|
+
this[_a] = true;
|
|
45
|
+
Object.setPrototypeOf(this, PortalAuthError.prototype);
|
|
46
|
+
this.reason = reason;
|
|
47
|
+
this.cause = cause;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Brand check — see BRAND. Narrowing a branded error from another copy to
|
|
51
|
+
* this class is intentional.
|
|
52
|
+
*/
|
|
53
|
+
static is(error) {
|
|
54
|
+
return (typeof error === 'object' &&
|
|
55
|
+
error !== null &&
|
|
56
|
+
error[BRAND] === true);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
_a = BRAND;
|
|
60
|
+
export default PortalAuthError;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
var _a;
|
|
2
|
+
/** Why a credential could not be turned into a usable token. */
|
|
3
|
+
export var PortalCredentialErrorReason;
|
|
4
|
+
(function (PortalCredentialErrorReason) {
|
|
5
|
+
/** Resolution succeeded but produced nothing usable. */
|
|
6
|
+
PortalCredentialErrorReason["Unavailable"] = "CREDENTIAL_UNAVAILABLE";
|
|
7
|
+
/** The credential provider itself failed — a storage read, a custom
|
|
8
|
+
* `PortalCredentials` implementation throwing, and so on. */
|
|
9
|
+
PortalCredentialErrorReason["ProviderFailure"] = "CREDENTIAL_PROVIDER_FAILURE";
|
|
10
|
+
/** The credential is known to be dead: the session was invalidated. */
|
|
11
|
+
PortalCredentialErrorReason["SessionInvalidated"] = "SESSION_INVALIDATED";
|
|
12
|
+
})(PortalCredentialErrorReason || (PortalCredentialErrorReason = {}));
|
|
13
|
+
const credentialErrors = {
|
|
14
|
+
[PortalCredentialErrorReason.Unavailable]: 'No credential was available. Provide an apiKey or credentials when constructing Portal.',
|
|
15
|
+
[PortalCredentialErrorReason.ProviderFailure]: 'The credential provider failed to supply a credential.',
|
|
16
|
+
[PortalCredentialErrorReason.SessionInvalidated]: 'The session is no longer valid. Authenticate again to obtain a new one.',
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Cross-copy brand. `instanceof` compares prototypes, so it returns `false`
|
|
20
|
+
* when a consumer's tree resolves two `@portal-hq/utils` — a real
|
|
21
|
+
* `SessionInvalidated` then silently degrades to a provider failure.
|
|
22
|
+
*
|
|
23
|
+
* This key and the members {@link PortalCredentialError.is} implies are a
|
|
24
|
+
* compatibility contract: changing either breaks recognition between copies.
|
|
25
|
+
*/
|
|
26
|
+
const BRAND = Symbol.for('@portal-hq/utils/PortalCredentialError');
|
|
27
|
+
/**
|
|
28
|
+
* The single error every credential resolution failure is normalized to; branch
|
|
29
|
+
* on `reason`.
|
|
30
|
+
*/
|
|
31
|
+
class PortalCredentialError extends Error {
|
|
32
|
+
constructor({ reason, cause }) {
|
|
33
|
+
super(`[Portal] ${credentialErrors[reason]}`);
|
|
34
|
+
/** @internal Cross-copy brand. Read it through {@link PortalCredentialError.is}. */
|
|
35
|
+
this[_a] = true;
|
|
36
|
+
Object.setPrototypeOf(this, PortalCredentialError.prototype);
|
|
37
|
+
this.reason = reason;
|
|
38
|
+
this.cause = cause;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Brand check — see BRAND. Narrowing a branded error from another copy to
|
|
42
|
+
* this class is intentional.
|
|
43
|
+
*/
|
|
44
|
+
static is(error) {
|
|
45
|
+
return (typeof error === 'object' &&
|
|
46
|
+
error !== null &&
|
|
47
|
+
error[BRAND] === true);
|
|
48
|
+
}
|
|
49
|
+
/** True when re-authentication is the only way forward. */
|
|
50
|
+
get requiresReauthentication() {
|
|
51
|
+
return this.reason === PortalCredentialErrorReason.SessionInvalidated;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
_a = BRAND;
|
|
55
|
+
export default PortalCredentialError;
|
|
@@ -73,3 +73,14 @@ export class PortalMpcError extends Error {
|
|
|
73
73
|
this.id = error.id;
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* The MPC binary issues its own authenticated requests, so the SDK never sees
|
|
78
|
+
* the HTTP 401 behind a rejected credential — this ID is the only form it
|
|
79
|
+
* reaches us in. Matched on the ID, never the companion `code`, which exists
|
|
80
|
+
* only for clients predating these IDs.
|
|
81
|
+
*/
|
|
82
|
+
export const MPC_AUTH_FAILED_ERROR_ID = 'AUTH_FAILED';
|
|
83
|
+
/** Accepts a raw error envelope or a thrown {@link PortalMpcError}. */
|
|
84
|
+
export const isMpcAuthFailure = (error) => typeof error === 'object' &&
|
|
85
|
+
error !== null &&
|
|
86
|
+
error.id === MPC_AUTH_FAILED_ERROR_ID;
|
package/lib/esm/events/index.js
CHANGED
|
@@ -13,9 +13,7 @@ var Events;
|
|
|
13
13
|
Events["GetEnabledDapps"] = "Get Enabled Dapps";
|
|
14
14
|
Events["GetNFTs"] = "Get NFTs";
|
|
15
15
|
Events["GetNetworks"] = "Get Networks";
|
|
16
|
-
Events["GetQuote"] = "Get Quote";
|
|
17
16
|
Events["GetSigningShareMetadata"] = "Get Signing Share Metadata";
|
|
18
|
-
Events["GetSources"] = "Get Sources";
|
|
19
17
|
Events["GetTransactions"] = "Get Transactions";
|
|
20
18
|
Events["GetTransactionHistory"] = "Get Transaction History";
|
|
21
19
|
Events["PrepareClientEject"] = "Prepare Client Eject";
|
|
@@ -62,6 +60,8 @@ var Events;
|
|
|
62
60
|
Events["EvmAccountType7702BuildAuthorizationList"] = "Evm Account Type 7702 BuildAuthorizationList";
|
|
63
61
|
Events["EvmAccountType7702BuildUpgradeTx"] = "Evm Account Type 7702 BuildUpgradeTx";
|
|
64
62
|
Events["EvmAccountType7702Upgrade"] = "Evm Account Type 7702 Upgrade";
|
|
63
|
+
Events["UserOperationsBuildBatched"] = "UserOperations BuildBatched";
|
|
64
|
+
Events["UserOperationsBroadcastBatched"] = "UserOperations BroadcastBatched";
|
|
65
65
|
Events["NoahInitiateKyc"] = "Noah Initiate KYC";
|
|
66
66
|
Events["NoahInitiatePayin"] = "Noah Initiate Payin";
|
|
67
67
|
Events["NoahSimulatePayin"] = "Noah Simulate Payin";
|
package/lib/esm/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
export { chainIdToName, chainNameToId, getChainIdForName, getChainNameForId, CHAIN_NAMESPACES, SUPPORTED_BITCOIN_P2WPKH_CHAINS, FRIENDLY_CHAIN_NAME_TO_CAIP2, CAIP2_CHAIN_ID_REGEX, DEFAULT_CHAIN_ID_NUMERIC, DEFAULT_CHAIN_ID_CAIP2, EVM_CHAIN_IDS, SOLANA_CHAIN_IDS, TRON_CHAIN_IDS, BITCOIN_CHAIN_IDS, ALL_CHAIN_IDS, NAMESPACE_TO_CURVE, } from './chains';
|
|
2
2
|
export { buildDefaultGatewayConfig, DEFAULT_HOSTS } from './config';
|
|
3
|
+
export { StaticCredentials, failFromMpcError, invalidateCredentials, onCredentialsInvalidated, reportUnauthorized, resolveCredentials, resolveCredentialToken, staticApiKeyOf, } from './credentials';
|
|
3
4
|
export { convertLocalToCrypto, convertCryptoToLocal, convert } from './currency';
|
|
4
5
|
export { KeychainAdapter, MpcStatuses, PasswordStorage, PortalRequestMethod, Storage, } from './definitions';
|
|
5
|
-
export { CloudStorageReadError, HttpError, InvalidApiKeyError, InvalidChainIdError, InvalidGatewayConfigError, MissingOptionError, MpcSigningError, NoAddressError, PortalErrorCodes, PortalErrorCodeTypes, PortalMpcError, ProviderRpcError, RpcErrorCodes, } from './errors';
|
|
6
|
+
export { CloudStorageReadError, HttpError, InvalidApiKeyError, InvalidChainIdError, InvalidGatewayConfigError, MissingOptionError, MpcSigningError, NoAddressError, PortalAuthError, PortalAuthErrorReason, PortalCredentialError, PortalCredentialErrorReason, MPC_AUTH_FAILED_ERROR_ID, PortalErrorCodes, PortalErrorCodeTypes, PortalMpcError, isMpcAuthFailure, ProviderRpcError, RpcErrorCodes, } from './errors';
|
|
6
7
|
export { default as Events } from './events';
|
|
7
8
|
export { getClientPlatformVersion } from './version';
|
|
8
9
|
export { numberToHexString, parseRequest } from './eth-requests';
|
|
@@ -7,11 +7,38 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
+
import { HttpError } from '../errors';
|
|
10
11
|
import { HttpRequest } from '../index';
|
|
11
12
|
import { TRACE_ID_HEADER } from '../tracing';
|
|
12
13
|
class HttpRequester {
|
|
13
|
-
constructor({ baseUrl }) {
|
|
14
|
+
constructor({ baseUrl, onUnauthorized }) {
|
|
14
15
|
this.baseUrl = baseUrl;
|
|
16
|
+
this.onUnauthorized = onUnauthorized;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Sends a request, notifying `onUnauthorized` on 401. Observational only:
|
|
20
|
+
* the hook's own failure is swallowed and the original `HttpError` is always
|
|
21
|
+
* what the caller sees.
|
|
22
|
+
*/
|
|
23
|
+
send(requestOptions) {
|
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
try {
|
|
26
|
+
return (yield new HttpRequest(requestOptions).send());
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
if (this.onUnauthorized &&
|
|
30
|
+
error instanceof HttpError &&
|
|
31
|
+
error.status === 401) {
|
|
32
|
+
try {
|
|
33
|
+
yield this.onUnauthorized();
|
|
34
|
+
}
|
|
35
|
+
catch (_a) {
|
|
36
|
+
// Never let credential bookkeeping replace the original failure.
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
15
42
|
}
|
|
16
43
|
buildHeaders(headers = {}, traceId) {
|
|
17
44
|
return Object.assign(Object.assign({ 'Content-Type': 'application/json' }, headers), (traceId ? { [TRACE_ID_HEADER]: traceId } : {}));
|
|
@@ -24,9 +51,7 @@ class HttpRequester {
|
|
|
24
51
|
url: `${this.baseUrl}${path}`,
|
|
25
52
|
};
|
|
26
53
|
requestOptions.headers = this.buildHeaders(((_a = options === null || options === void 0 ? void 0 : options.headers) !== null && _a !== void 0 ? _a : {}), options === null || options === void 0 ? void 0 : options.traceId);
|
|
27
|
-
|
|
28
|
-
const response = (yield request.send());
|
|
29
|
-
return response;
|
|
54
|
+
return this.send(requestOptions);
|
|
30
55
|
});
|
|
31
56
|
}
|
|
32
57
|
get(path, options) {
|
|
@@ -37,9 +62,7 @@ class HttpRequester {
|
|
|
37
62
|
url: `${this.baseUrl}${path}`,
|
|
38
63
|
};
|
|
39
64
|
requestOptions.headers = this.buildHeaders(((_a = options === null || options === void 0 ? void 0 : options.headers) !== null && _a !== void 0 ? _a : {}), options === null || options === void 0 ? void 0 : options.traceId);
|
|
40
|
-
|
|
41
|
-
const response = (yield request.send());
|
|
42
|
-
return response;
|
|
65
|
+
return this.send(requestOptions);
|
|
43
66
|
});
|
|
44
67
|
}
|
|
45
68
|
post(path, options) {
|
|
@@ -53,9 +76,7 @@ class HttpRequester {
|
|
|
53
76
|
if (options === null || options === void 0 ? void 0 : options.body) {
|
|
54
77
|
requestOptions.body = options.body;
|
|
55
78
|
}
|
|
56
|
-
|
|
57
|
-
const response = (yield request.send());
|
|
58
|
-
return response;
|
|
79
|
+
return this.send(requestOptions);
|
|
59
80
|
});
|
|
60
81
|
}
|
|
61
82
|
put(path, options) {
|
|
@@ -69,9 +90,7 @@ class HttpRequester {
|
|
|
69
90
|
if (options === null || options === void 0 ? void 0 : options.body) {
|
|
70
91
|
requestOptions.body = options.body;
|
|
71
92
|
}
|
|
72
|
-
|
|
73
|
-
const response = (yield request.send());
|
|
74
|
-
return response;
|
|
93
|
+
return this.send(requestOptions);
|
|
75
94
|
});
|
|
76
95
|
}
|
|
77
96
|
patch(path, options) {
|
|
@@ -85,9 +104,7 @@ class HttpRequester {
|
|
|
85
104
|
if (options === null || options === void 0 ? void 0 : options.body) {
|
|
86
105
|
requestOptions.body = options.body;
|
|
87
106
|
}
|
|
88
|
-
|
|
89
|
-
const response = (yield request.send());
|
|
90
|
-
return response;
|
|
107
|
+
return this.send(requestOptions);
|
|
91
108
|
});
|
|
92
109
|
}
|
|
93
110
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{ "version": "
|
|
1
|
+
{ "version": "5.0.0" }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@portal-hq/utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"main": "lib/commonjs/index",
|
|
5
5
|
"module": "lib/esm/index",
|
|
6
6
|
"source": "src/index",
|
|
@@ -26,5 +26,5 @@
|
|
|
26
26
|
"ts-jest": "^29.0.3",
|
|
27
27
|
"typescript": "^4.8.4"
|
|
28
28
|
},
|
|
29
|
-
"gitHead": "
|
|
29
|
+
"gitHead": "1250896d62af2930cca389e2725336bedb247f9a"
|
|
30
30
|
}
|