@open-banking-io/client 1.0.0 → 1.2.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/dist/index.d.ts CHANGED
@@ -132,6 +132,196 @@ interface OpenBankingClientOptions {
132
132
  fetch?: typeof globalThis.fetch;
133
133
  }
134
134
 
135
+ /** The relay fields the consent page form-posts to a redirect URI, in wire order. */
136
+ declare const CONNECT_RELAY_FIELDS: readonly ["code", "state", "iss", "privateKey", "publicKey"];
137
+ interface Pkce {
138
+ /** The `code_verifier` to keep server-side and send on the token request. */
139
+ verifier: string;
140
+ /** The `code_challenge` to send on the authorize request. */
141
+ challenge: string;
142
+ method: "S256";
143
+ }
144
+ /** A fresh PKCE pair (RFC 7636): 32 random bytes, base64url, S256 challenge. */
145
+ declare function createPkce(): Pkce;
146
+ /** The S256 challenge for a verifier — exposed so a stored verifier can be re-checked. */
147
+ declare function pkceChallenge(verifier: string): string;
148
+ /** A fresh opaque `state` (16 random bytes, base64url). */
149
+ declare function createState(): string;
150
+ interface AuthorizeUrlOptions {
151
+ /** The issuer, e.g. `https://open-banking.io`. */
152
+ issuer: string;
153
+ clientId: string;
154
+ /** One of the client's registered redirect URIs, character for character. */
155
+ redirectUri: string;
156
+ state: string;
157
+ /** From {@link createPkce}. */
158
+ codeChallenge: string;
159
+ /** Defaults to `accounts.read`, the only scope. */
160
+ scope?: string;
161
+ /** Prefills the email on the login page; never authenticates. */
162
+ loginHint?: string;
163
+ /** `pin_code` keeps a popup journey inside the popup (typed emailed code instead of a magic link). */
164
+ challenge?: "pin_code";
165
+ /** Space-separated locales; the first one the server publishes forces the language of every screen. */
166
+ uiLocales?: string;
167
+ }
168
+ /** The `/oauth/authorize` URL for a top-level browser navigation. */
169
+ declare function buildAuthorizeUrl(options: AuthorizeUrlOptions): string;
170
+ /** What the consent page relayed to the redirect URI. */
171
+ interface ConnectRelay {
172
+ code: string;
173
+ state: string;
174
+ /** Present on current servers (RFC 9207); empty string from older ones. */
175
+ iss: string;
176
+ /** The user's base64 PKCS#8 private key. A credential: never log it. */
177
+ privateKey: string;
178
+ publicKey: string;
179
+ }
180
+ type RelayErrorCode = "access_denied" | "oauth_error" | "state_mismatch" | "issuer_mismatch" | "missing_code" | "missing_private_key";
181
+ /** The `error_description` the server relays when the partner has not installed its decryption key. */
182
+ declare const PARTNER_KEY_MISSING_DESCRIPTION = "this partner has not installed its decryption key; no authorizations can be started until it does";
183
+ /** The `error_description` the server relays while the partner is suspended. */
184
+ declare const PARTNER_SUSPENDED_DESCRIPTION = "this partner is suspended; no authorizations can be started";
185
+ /**
186
+ * Why a `temporarily_unavailable` was relayed, when the server said so in the pinned words. Both
187
+ * are on your side, not the bank's: install the key on your partner page, or write to us.
188
+ */
189
+ type RelayRefusalReason = "partner_key_missing" | "partner_suspended";
190
+ /**
191
+ * Thrown by {@link parseRelay}. `code` says why: `access_denied` is the user pressing "Back to the
192
+ * partner" or declining (a normal outcome), `oauth_error` any other error the server relayed (the
193
+ * fields are attached), the rest are validation failures on your side.
194
+ */
195
+ declare class RelayError extends Error {
196
+ readonly code: RelayErrorCode;
197
+ readonly error?: string;
198
+ readonly errorDescription?: string;
199
+ /** Set on an `oauth_error` whose description names a partner-side refusal. */
200
+ readonly reason?: RelayRefusalReason;
201
+ constructor(code: RelayErrorCode, message: string, oauth?: {
202
+ error: string;
203
+ errorDescription?: string;
204
+ });
205
+ }
206
+ interface ParseRelayOptions {
207
+ /** The `state` this flow was started with. Compared in constant time. */
208
+ expectedState: string;
209
+ /** When set, `iss` must equal it (trailing slashes ignored). */
210
+ issuer?: string;
211
+ /** Require `iss` to be present even when `issuer` is not checked. Default: only when `issuer` is set. */
212
+ requireIss?: boolean;
213
+ /**
214
+ * Whether the relay must carry the user's private key. A partner that installed its own
215
+ * decryption key receives `privateKey=""` for every user who connected under it and decrypts
216
+ * with that key instead — pass `"optional"`. Default `"required"`, the 1.1.0 behaviour.
217
+ */
218
+ expectPrivateKey?: "required" | "optional";
219
+ }
220
+ /** A form body, `URLSearchParams`, parsed body object, or JSON string. */
221
+ type RelayInput = string | URLSearchParams | FormData | Record<string, unknown>;
222
+ /**
223
+ * Validates the form_post relay: `state` (constant time) and `iss` first, so a relayed error is only
224
+ * honoured for this flow from this issuer; then an OAuth error becomes a {@link RelayError}, and
225
+ * `code` and `privateKey` are checked.
226
+ */
227
+ declare function parseRelay(input: RelayInput, options: ParseRelayOptions): ConnectRelay;
228
+ /** An RFC 6749 §5.2 error body from the token or revocation endpoint. */
229
+ declare class OAuthError extends Error {
230
+ readonly status: number;
231
+ readonly error: string;
232
+ readonly errorDescription?: string;
233
+ constructor(status: number, error: string, errorDescription?: string);
234
+ }
235
+ interface HttpOptions {
236
+ /** Custom `fetch`, e.g. for tests. Defaults to the global. */
237
+ fetch?: typeof globalThis.fetch;
238
+ /** Per-request timeout; defaults to 30s. */
239
+ timeoutMs?: number;
240
+ }
241
+ interface ExchangeCodeOptions extends HttpOptions {
242
+ issuer: string;
243
+ clientId: string;
244
+ clientSecret: string;
245
+ /** From the relay. */
246
+ code: string;
247
+ /** From {@link createPkce}, kept server-side. */
248
+ codeVerifier: string;
249
+ /** The redirect URI used on the authorize request; sent when given and must match. */
250
+ redirectUri?: string;
251
+ }
252
+ /** The token endpoint's response. `accessToken` and `apiKey` are the same key. */
253
+ interface TokenResponse {
254
+ accessToken: string;
255
+ tokenType: string;
256
+ expiresIn: number;
257
+ scope: string;
258
+ apiKey: string;
259
+ apiBaseUrl: string;
260
+ /** The tenant subject the key reads for. */
261
+ user: string;
262
+ }
263
+ /** Exchanges the relayed code for the key, server to server, with HTTP Basic client authentication. */
264
+ declare function exchangeCode(options: ExchangeCodeOptions): Promise<TokenResponse>;
265
+ interface RevokeTokenOptions extends HttpOptions {
266
+ issuer: string;
267
+ clientId: string;
268
+ clientSecret: string;
269
+ /** The key to revoke. Only keys issued to this client are touched; an unknown key still succeeds. */
270
+ token: string;
271
+ }
272
+ /** RFC 7009 revocation. Resolves on 200; throws {@link OAuthError} on a client-authentication failure. */
273
+ declare function revokeToken(options: RevokeTokenOptions): Promise<void>;
274
+ interface UserinfoOptions extends HttpOptions {
275
+ issuer: string;
276
+ accessToken: string;
277
+ }
278
+ interface Userinfo {
279
+ sub: string;
280
+ email: string;
281
+ partnerId: string | null;
282
+ clientId: string | null;
283
+ scope: string;
284
+ expiresAt: string | null;
285
+ }
286
+ /** What a key reads for. Throws {@link OAuthError} with status 401 when the key is revoked or expired. */
287
+ declare function userinfo(options: UserinfoOptions): Promise<Userinfo>;
288
+ /** RFC 8414 metadata with the `open_banking_io` extension. */
289
+ interface ServerMetadata {
290
+ issuer: string;
291
+ authorization_endpoint: string;
292
+ token_endpoint: string;
293
+ revocation_endpoint: string;
294
+ scopes_supported: string[];
295
+ response_types_supported: string[];
296
+ response_modes_supported: string[];
297
+ grant_types_supported: string[];
298
+ token_endpoint_auth_methods_supported: string[];
299
+ revocation_endpoint_auth_methods_supported?: string[];
300
+ code_challenge_methods_supported: string[];
301
+ authorization_response_iss_parameter_supported?: boolean;
302
+ service_documentation?: string;
303
+ open_banking_io: {
304
+ userinfo_endpoint: string;
305
+ api_base_url: string;
306
+ api_key_header: string;
307
+ bearer_supported: boolean;
308
+ login_challenges_supported?: string[];
309
+ key_relay: {
310
+ response_mode: string;
311
+ fields: string[];
312
+ };
313
+ /** The server accepts a partner-held decryption key (always true on current servers). */
314
+ recipient_key_supported?: boolean;
315
+ /** Every partner must hold one before it can register a client or start an authorization. */
316
+ recipient_key_required?: boolean;
317
+ documentation: string;
318
+ };
319
+ }
320
+ /** Fetches `/.well-known/oauth-authorization-server`, cached per issuer for an hour. */
321
+ declare function discover(issuer: string, options?: HttpOptions): Promise<ServerMetadata>;
322
+ /** The credentials bundle the data client reads with: the key plus the relayed private key. */
323
+ declare function bundleFromToken(token: TokenResponse, privateKey: string): CredentialsBundle;
324
+
135
325
  /**
136
326
  * Server-to-server client for open-banking.io. Authenticates with an API key (`X-Api-Key`) and
137
327
  * decrypts the zero-knowledge data envelopes locally with the exported private key — the service
@@ -155,6 +345,11 @@ declare class OpenBankingClient {
155
345
  */
156
346
  static fromCredentials(jsonOrPath: string, overrides?: Partial<Pick<OpenBankingClientOptions, "fetch" | "timeoutMs">>): OpenBankingClient;
157
347
  static fromBundle(bundle: CredentialsBundle, overrides?: Partial<Pick<OpenBankingClientOptions, "fetch" | "timeoutMs">>): OpenBankingClient;
348
+ /**
349
+ * Builds a client from a Partner Connect token response and the private key the consent page
350
+ * relayed — see {@link exchangeCode} and {@link parseRelay}.
351
+ */
352
+ static fromTokenResponse(token: TokenResponse, privateKey: string, overrides?: Partial<Pick<OpenBankingClientOptions, "fetch" | "timeoutMs">>): OpenBankingClient;
158
353
  /** Lists the user's accounts with all sensitive fields decrypted. */
159
354
  getAccounts(): Promise<Account[]>;
160
355
  /** Returns a page of an account's statement, newest first, with decrypted fields. */
@@ -190,4 +385,30 @@ declare function decryptTo<T>(privateKey: CryptoKey, envelopeBase64: string | nu
190
385
  /** Imports a base64 PKCS#8 P-256 ECDH private key for use with {@link decryptEnvelope}. */
191
386
  declare function importPrivateKey(pkcs8Base64: string): Promise<CryptoKey>;
192
387
 
193
- export { type Account, type Balance, type Connection, type CredentialsBundle, type EncryptionKey, OpenBankingClient, type OpenBankingClientOptions, type SyncAllResult, type SyncResult, type Transaction, type TransactionPage, type TransactionQuery, decryptEnvelope, decryptTo, importPrivateKey };
388
+ /** A partner's recipient (decryption) key pair. The private half is a credential: never log it. */
389
+ interface RecipientKeyPair {
390
+ /** Base64 PKCS#8 — what your deployment loads. */
391
+ privateKeyPkcs8Base64: string;
392
+ /** Base64 raw 65-byte uncompressed P-256 point — what you install on your partner page. */
393
+ publicKeyRawBase64: string;
394
+ }
395
+ /**
396
+ * Generates the P-256 pair a partner installs on open-banking.io. The public half goes into
397
+ * `PUT /api/partner/recipient-key` (or the partner page's paste box); keep the private half
398
+ * where your deployment can read it — the service never sees it and cannot recover it.
399
+ */
400
+ declare function generateRecipientKeyPair(): Promise<RecipientKeyPair>;
401
+ /**
402
+ * The fingerprint the partner page and the admin show for a key: SHA-256 of the raw 65-byte
403
+ * point, lowercase hex, first 16 characters. Print it at boot and compare after every deploy.
404
+ */
405
+ declare function recipientKeyFingerprint(publicKeyRawBase64: string): string;
406
+ /** The raw public point of a PKCS#8 private key — to fingerprint or re-install the key you hold. */
407
+ declare function recipientPublicKey(privateKeyPkcs8Base64: string): Promise<string>;
408
+ /**
409
+ * Answers the possession challenge from `POST /api/partner/recipient-key/challenge`: opens
410
+ * the envelope with the private half and returns the nonce — the `challengeAnswer` for the install.
411
+ */
412
+ declare function answerRecipientKeyChallenge(privateKeyPkcs8Base64: string, envelopeBase64: string): Promise<string>;
413
+
414
+ export { type Account, type AuthorizeUrlOptions, type Balance, CONNECT_RELAY_FIELDS, type ConnectRelay, type Connection, type CredentialsBundle, type EncryptionKey, type ExchangeCodeOptions, type HttpOptions, OAuthError, OpenBankingClient, type OpenBankingClientOptions, PARTNER_KEY_MISSING_DESCRIPTION, PARTNER_SUSPENDED_DESCRIPTION, type ParseRelayOptions, type Pkce, type RecipientKeyPair, RelayError, type RelayErrorCode, type RelayInput, type RelayRefusalReason, type RevokeTokenOptions, type ServerMetadata, type SyncAllResult, type SyncResult, type TokenResponse, type Transaction, type TransactionPage, type TransactionQuery, type Userinfo, type UserinfoOptions, answerRecipientKeyChallenge, buildAuthorizeUrl, bundleFromToken, createPkce, createState, decryptEnvelope, decryptTo, discover, exchangeCode, generateRecipientKeyPair, importPrivateKey, parseRelay, pkceChallenge, recipientKeyFingerprint, recipientPublicKey, revokeToken, userinfo };