@open-banking-io/client 1.0.0 → 1.1.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 +94 -0
- package/dist/index.cjs +369 -70
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +175 -1
- package/dist/index.d.ts +175 -1
- package/dist/index.js +354 -68
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -47,6 +47,100 @@ const client = new OpenBankingClient({ apiBaseUrl, apiKey, privateKeyPkcs8 });
|
|
|
47
47
|
Every request carries a `User-Agent: open-banking-io/node/<version>` header and a default 30s timeout
|
|
48
48
|
(override via the `timeoutMs` option) so a hung connection can't block forever.
|
|
49
49
|
|
|
50
|
+
## Partner Connect (OAuth 2.0 + PKCE)
|
|
51
|
+
|
|
52
|
+
Partners let their users connect banks through open-banking.io and receive a delegated key plus the
|
|
53
|
+
user's private key — a standard authorization-code flow with PKCE and `form_post`, documented at
|
|
54
|
+
[open-banking.io/en/docs/partners](https://open-banking.io/en/docs/partners). The `connect` helpers
|
|
55
|
+
cover every step; keep the client secret and the verifier on your server.
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import {
|
|
59
|
+
buildAuthorizeUrl,
|
|
60
|
+
createPkce,
|
|
61
|
+
createState,
|
|
62
|
+
discover,
|
|
63
|
+
exchangeCode,
|
|
64
|
+
parseRelay,
|
|
65
|
+
OpenBankingClient,
|
|
66
|
+
RelayError,
|
|
67
|
+
} from "@open-banking-io/client";
|
|
68
|
+
|
|
69
|
+
const ISSUER = "https://open-banking.io";
|
|
70
|
+
|
|
71
|
+
// 1. Start: keep the verifier server-side, keyed by state, and send the browser to the URL.
|
|
72
|
+
app.get("/connect", async (req, res) => {
|
|
73
|
+
const pkce = createPkce();
|
|
74
|
+
const state = createState();
|
|
75
|
+
const mode = req.query.mode === "redirect" ? "redirect" : "popup";
|
|
76
|
+
await flows.put(state, {
|
|
77
|
+
state,
|
|
78
|
+
verifier: pkce.verifier,
|
|
79
|
+
sessionId: req.session.id,
|
|
80
|
+
mode,
|
|
81
|
+
expiresAt: Date.now() + 600_000,
|
|
82
|
+
});
|
|
83
|
+
res.redirect(
|
|
84
|
+
buildAuthorizeUrl({
|
|
85
|
+
issuer: ISSUER,
|
|
86
|
+
clientId: CLIENT_ID,
|
|
87
|
+
redirectUri: `${SELF_URL}/callback`,
|
|
88
|
+
state,
|
|
89
|
+
codeChallenge: pkce.challenge,
|
|
90
|
+
...(mode === "popup" && { challenge: "pin_code" }),
|
|
91
|
+
}),
|
|
92
|
+
);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// 2. Callback: the consent page form-posts code, state, iss, privateKey and publicKey — or
|
|
96
|
+
// error=access_denied when the user went back to you. Consume the flow by state first, so a
|
|
97
|
+
// cancel consumes it too; the lookup is what binds the POST to the session that started it.
|
|
98
|
+
app.post("/callback", express.urlencoded({ extended: false }), async (req, res) => {
|
|
99
|
+
const { issuer } = await discover(ISSUER);
|
|
100
|
+
const flow = await flows.take(req.body.state);
|
|
101
|
+
if (!flow || flow.expiresAt < Date.now()) return res.status(400).send("unknown or expired state");
|
|
102
|
+
let relay;
|
|
103
|
+
try {
|
|
104
|
+
relay = parseRelay(req.body, { expectedState: flow.state, issuer });
|
|
105
|
+
} catch (e) {
|
|
106
|
+
if (e instanceof RelayError && e.code === "access_denied")
|
|
107
|
+
return finish(res, flow, "cancelled");
|
|
108
|
+
throw e;
|
|
109
|
+
}
|
|
110
|
+
const token = await exchangeCode({
|
|
111
|
+
issuer,
|
|
112
|
+
clientId: CLIENT_ID,
|
|
113
|
+
clientSecret: CLIENT_SECRET,
|
|
114
|
+
code: relay.code,
|
|
115
|
+
codeVerifier: flow.verifier,
|
|
116
|
+
redirectUri: `${SELF_URL}/callback`,
|
|
117
|
+
});
|
|
118
|
+
await bundles.put(flow.sessionId, { token, privateKey: relay.privateKey });
|
|
119
|
+
finish(res, flow, "connected");
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// A redirect-mode flow lands back on your page; a popup signals its opener and closes.
|
|
123
|
+
const finish = (res, flow, outcome) =>
|
|
124
|
+
flow.mode === "redirect"
|
|
125
|
+
? res.redirect(302, `/?connect=${outcome}`)
|
|
126
|
+
: res.send(closePage(outcome));
|
|
127
|
+
const closePage = (
|
|
128
|
+
outcome,
|
|
129
|
+
) => `<!doctype html><p>${outcome === "connected" ? "Connected — you can close this window." : "Cancelled."}</p>
|
|
130
|
+
<script>try{new BroadcastChannel("bank-connect").postMessage(${JSON.stringify(outcome)})}catch{}setTimeout(()=>window.close(),300)</script>`;
|
|
131
|
+
|
|
132
|
+
// 3. Read (inside any handler that has the stored pair): the token plus the relayed private key
|
|
133
|
+
// is a complete credentials bundle.
|
|
134
|
+
const { token, privateKey } = await bundles.get(req.session.id);
|
|
135
|
+
const client = OpenBankingClient.fromTokenResponse(token, privateKey);
|
|
136
|
+
const accounts = await client.getAccounts();
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
`parseRelay` throws a `RelayError` (`access_denied` — the user cancelled, a normal outcome —
|
|
140
|
+
`oauth_error`, `state_mismatch`, `issuer_mismatch`, `missing_code`, `missing_private_key`) and compares `state` and `iss` in constant time;
|
|
141
|
+
`exchangeCode`, `revokeToken` and `userinfo` throw an `OAuthError` carrying the RFC 6749 `error`
|
|
142
|
+
and `error_description`. An `invalid_grant` is terminal for that code — restart the flow.
|
|
143
|
+
|
|
50
144
|
## Money
|
|
51
145
|
|
|
52
146
|
Amounts (`balance.amount`, `transaction.amount`, `transaction.balanceAfterTransaction`) are exposed
|
package/dist/index.cjs
CHANGED
|
@@ -20,84 +20,33 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
CONNECT_RELAY_FIELDS: () => CONNECT_RELAY_FIELDS,
|
|
24
|
+
OAuthError: () => OAuthError,
|
|
23
25
|
OpenBankingClient: () => OpenBankingClient,
|
|
26
|
+
RelayError: () => RelayError,
|
|
27
|
+
buildAuthorizeUrl: () => buildAuthorizeUrl,
|
|
28
|
+
bundleFromToken: () => bundleFromToken,
|
|
29
|
+
createPkce: () => createPkce,
|
|
30
|
+
createState: () => createState,
|
|
24
31
|
decryptEnvelope: () => decryptEnvelope,
|
|
25
32
|
decryptTo: () => decryptTo,
|
|
26
|
-
|
|
33
|
+
discover: () => discover,
|
|
34
|
+
exchangeCode: () => exchangeCode,
|
|
35
|
+
importPrivateKey: () => importPrivateKey,
|
|
36
|
+
parseRelay: () => parseRelay,
|
|
37
|
+
pkceChallenge: () => pkceChallenge,
|
|
38
|
+
revokeToken: () => revokeToken,
|
|
39
|
+
userinfo: () => userinfo
|
|
27
40
|
});
|
|
28
41
|
module.exports = __toCommonJS(index_exports);
|
|
29
42
|
|
|
30
|
-
// src/
|
|
31
|
-
var import_node_fs = require("fs");
|
|
32
|
-
|
|
33
|
-
// src/envelope.ts
|
|
43
|
+
// src/connect.ts
|
|
34
44
|
var import_node_crypto = require("crypto");
|
|
35
|
-
var { subtle } = import_node_crypto.webcrypto;
|
|
36
|
-
var VERSION = 1;
|
|
37
|
-
var POINT_LEN = 65;
|
|
38
|
-
var NONCE_LEN = 12;
|
|
39
|
-
var TAG_LEN = 16;
|
|
40
|
-
var HKDF_INFO = new TextEncoder().encode("bank.core.ci/zk/v1");
|
|
41
|
-
var HKDF_SALT = new Uint8Array(32);
|
|
42
|
-
async function decryptEnvelope(privateKey, envelopeBase64) {
|
|
43
|
-
const bytes = base64ToBytes(envelopeBase64);
|
|
44
|
-
if (bytes.length < 1 + POINT_LEN + NONCE_LEN + TAG_LEN || bytes[0] !== VERSION) {
|
|
45
|
-
throw new Error("Invalid or unsupported envelope");
|
|
46
|
-
}
|
|
47
|
-
const ephPub = bytes.subarray(1, 1 + POINT_LEN);
|
|
48
|
-
const nonce = bytes.subarray(1 + POINT_LEN, 1 + POINT_LEN + NONCE_LEN);
|
|
49
|
-
const tag = bytes.subarray(1 + POINT_LEN + NONCE_LEN, 1 + POINT_LEN + NONCE_LEN + TAG_LEN);
|
|
50
|
-
const ciphertext = bytes.subarray(1 + POINT_LEN + NONCE_LEN + TAG_LEN);
|
|
51
|
-
const ephKey = await subtle.importKey(
|
|
52
|
-
"raw",
|
|
53
|
-
ephPub,
|
|
54
|
-
{ name: "ECDH", namedCurve: "P-256" },
|
|
55
|
-
false,
|
|
56
|
-
[]
|
|
57
|
-
);
|
|
58
|
-
const shared = new Uint8Array(
|
|
59
|
-
await subtle.deriveBits({ name: "ECDH", public: ephKey }, privateKey, 256)
|
|
60
|
-
);
|
|
61
|
-
const hkdf = await subtle.importKey("raw", shared, "HKDF", false, ["deriveKey"]);
|
|
62
|
-
const aesKey = await subtle.deriveKey(
|
|
63
|
-
{ name: "HKDF", hash: "SHA-256", salt: HKDF_SALT, info: HKDF_INFO },
|
|
64
|
-
hkdf,
|
|
65
|
-
{ name: "AES-GCM", length: 256 },
|
|
66
|
-
false,
|
|
67
|
-
["decrypt"]
|
|
68
|
-
);
|
|
69
|
-
const ctWithTag = new Uint8Array(ciphertext.length + tag.length);
|
|
70
|
-
ctWithTag.set(ciphertext, 0);
|
|
71
|
-
ctWithTag.set(tag, ciphertext.length);
|
|
72
|
-
const plaintext = await subtle.decrypt(
|
|
73
|
-
{ name: "AES-GCM", iv: nonce, tagLength: 128 },
|
|
74
|
-
aesKey,
|
|
75
|
-
ctWithTag
|
|
76
|
-
);
|
|
77
|
-
return new Uint8Array(plaintext);
|
|
78
|
-
}
|
|
79
|
-
async function decryptTo(privateKey, envelopeBase64) {
|
|
80
|
-
if (envelopeBase64 == null) return null;
|
|
81
|
-
const plaintext = await decryptEnvelope(privateKey, envelopeBase64);
|
|
82
|
-
return JSON.parse(new TextDecoder().decode(plaintext));
|
|
83
|
-
}
|
|
84
|
-
async function importPrivateKey(pkcs8Base64) {
|
|
85
|
-
return subtle.importKey(
|
|
86
|
-
"pkcs8",
|
|
87
|
-
base64ToBytes(pkcs8Base64),
|
|
88
|
-
{ name: "ECDH", namedCurve: "P-256" },
|
|
89
|
-
false,
|
|
90
|
-
["deriveBits"]
|
|
91
|
-
);
|
|
92
|
-
}
|
|
93
|
-
function base64ToBytes(b64) {
|
|
94
|
-
return new Uint8Array(Buffer.from(b64, "base64"));
|
|
95
|
-
}
|
|
96
45
|
|
|
97
46
|
// package.json
|
|
98
47
|
var package_default = {
|
|
99
48
|
name: "@open-banking-io/client",
|
|
100
|
-
version: "1.
|
|
49
|
+
version: "1.1.0",
|
|
101
50
|
description: "Server-to-server client for open-banking.io: API-key auth + client-side decryption of the zero-knowledge data envelopes with your exported private key.",
|
|
102
51
|
type: "module",
|
|
103
52
|
license: "MIT",
|
|
@@ -156,8 +105,338 @@ var package_default = {
|
|
|
156
105
|
};
|
|
157
106
|
|
|
158
107
|
// src/version.ts
|
|
159
|
-
var
|
|
160
|
-
var USER_AGENT = `open-banking-io/node/${
|
|
108
|
+
var VERSION = package_default.version;
|
|
109
|
+
var USER_AGENT = `open-banking-io/node/${VERSION}`;
|
|
110
|
+
|
|
111
|
+
// src/connect.ts
|
|
112
|
+
var DEFAULT_TIMEOUT_MS = 3e4;
|
|
113
|
+
var CONNECT_RELAY_FIELDS = ["code", "state", "iss", "privateKey", "publicKey"];
|
|
114
|
+
function createPkce() {
|
|
115
|
+
const verifier = (0, import_node_crypto.randomBytes)(32).toString("base64url");
|
|
116
|
+
return { verifier, challenge: pkceChallenge(verifier), method: "S256" };
|
|
117
|
+
}
|
|
118
|
+
function pkceChallenge(verifier) {
|
|
119
|
+
return (0, import_node_crypto.createHash)("sha256").update(verifier).digest("base64url");
|
|
120
|
+
}
|
|
121
|
+
function createState() {
|
|
122
|
+
return (0, import_node_crypto.randomBytes)(16).toString("base64url");
|
|
123
|
+
}
|
|
124
|
+
function buildAuthorizeUrl(options) {
|
|
125
|
+
const params = new URLSearchParams({
|
|
126
|
+
response_type: "code",
|
|
127
|
+
client_id: options.clientId,
|
|
128
|
+
redirect_uri: options.redirectUri,
|
|
129
|
+
scope: options.scope ?? "accounts.read",
|
|
130
|
+
state: options.state,
|
|
131
|
+
code_challenge: options.codeChallenge,
|
|
132
|
+
code_challenge_method: "S256",
|
|
133
|
+
response_mode: "form_post"
|
|
134
|
+
});
|
|
135
|
+
if (options.loginHint) params.set("login_hint", options.loginHint);
|
|
136
|
+
if (options.challenge) params.set("challenge", options.challenge);
|
|
137
|
+
if (options.uiLocales) params.set("ui_locales", options.uiLocales);
|
|
138
|
+
return `${endpoint(options.issuer, "/oauth/authorize")}?${params.toString()}`;
|
|
139
|
+
}
|
|
140
|
+
var RelayError = class extends Error {
|
|
141
|
+
code;
|
|
142
|
+
error;
|
|
143
|
+
errorDescription;
|
|
144
|
+
constructor(code, message, oauth) {
|
|
145
|
+
super(message);
|
|
146
|
+
this.name = "RelayError";
|
|
147
|
+
this.code = code;
|
|
148
|
+
this.error = oauth?.error;
|
|
149
|
+
this.errorDescription = oauth?.errorDescription;
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
function parseRelay(input, options) {
|
|
153
|
+
const fields = toRecord(input);
|
|
154
|
+
const read = (name) => {
|
|
155
|
+
const value = fields[name];
|
|
156
|
+
return typeof value === "string" ? value : "";
|
|
157
|
+
};
|
|
158
|
+
if (!options.expectedState || !constantTimeEqual(read("state"), options.expectedState)) {
|
|
159
|
+
throw new RelayError("state_mismatch", "The relayed state does not match this flow");
|
|
160
|
+
}
|
|
161
|
+
const iss = read("iss");
|
|
162
|
+
const wantIss = options.issuer !== void 0 || options.requireIss === true;
|
|
163
|
+
if (wantIss && !iss) throw new RelayError("issuer_mismatch", "The relay carries no iss");
|
|
164
|
+
if (options.issuer !== void 0 && !constantTimeEqual(trimSlash(iss), trimSlash(options.issuer))) {
|
|
165
|
+
throw new RelayError("issuer_mismatch", "The relayed iss is not the expected issuer");
|
|
166
|
+
}
|
|
167
|
+
const error = read("error");
|
|
168
|
+
if (error) {
|
|
169
|
+
const description = read("error_description");
|
|
170
|
+
throw new RelayError(
|
|
171
|
+
error === "access_denied" ? "access_denied" : "oauth_error",
|
|
172
|
+
description ? `${error}: ${description}` : error,
|
|
173
|
+
{
|
|
174
|
+
error,
|
|
175
|
+
errorDescription: description || void 0
|
|
176
|
+
}
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
const code = read("code");
|
|
180
|
+
if (!code) throw new RelayError("missing_code", "The relay carries no authorization code");
|
|
181
|
+
const privateKey = read("privateKey");
|
|
182
|
+
if (!privateKey) throw new RelayError("missing_private_key", "The relay carries no private key");
|
|
183
|
+
return { code, state: read("state"), iss, privateKey, publicKey: read("publicKey") };
|
|
184
|
+
}
|
|
185
|
+
var OAuthError = class extends Error {
|
|
186
|
+
status;
|
|
187
|
+
error;
|
|
188
|
+
errorDescription;
|
|
189
|
+
constructor(status, error, errorDescription) {
|
|
190
|
+
super(errorDescription ? `${error}: ${errorDescription}` : error);
|
|
191
|
+
this.name = "OAuthError";
|
|
192
|
+
this.status = status;
|
|
193
|
+
this.error = error;
|
|
194
|
+
this.errorDescription = errorDescription;
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
async function exchangeCode(options) {
|
|
198
|
+
const body = new URLSearchParams({
|
|
199
|
+
grant_type: "authorization_code",
|
|
200
|
+
code: options.code,
|
|
201
|
+
code_verifier: options.codeVerifier
|
|
202
|
+
});
|
|
203
|
+
if (options.redirectUri) body.set("redirect_uri", options.redirectUri);
|
|
204
|
+
const response = await post(
|
|
205
|
+
endpoint(options.issuer, "/oauth/token"),
|
|
206
|
+
body,
|
|
207
|
+
{
|
|
208
|
+
Authorization: basicAuth(options.clientId, options.clientSecret),
|
|
209
|
+
Accept: "application/json"
|
|
210
|
+
},
|
|
211
|
+
options
|
|
212
|
+
);
|
|
213
|
+
await throwIfOAuthError(response);
|
|
214
|
+
const wire = await response.json();
|
|
215
|
+
const accessToken = wire.access_token ?? wire.apiKey;
|
|
216
|
+
if (!accessToken)
|
|
217
|
+
throw new OAuthError(
|
|
218
|
+
response.status,
|
|
219
|
+
"invalid_response",
|
|
220
|
+
"The token response carries no access token"
|
|
221
|
+
);
|
|
222
|
+
return {
|
|
223
|
+
accessToken,
|
|
224
|
+
tokenType: wire.token_type ?? "Bearer",
|
|
225
|
+
expiresIn: wire.expires_in ?? 0,
|
|
226
|
+
scope: wire.scope ?? "",
|
|
227
|
+
apiKey: wire.apiKey ?? accessToken,
|
|
228
|
+
apiBaseUrl: wire.apiBaseUrl ?? trimSlash(options.issuer),
|
|
229
|
+
user: wire.user ?? ""
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
async function revokeToken(options) {
|
|
233
|
+
const body = new URLSearchParams({ token: options.token, token_type_hint: "access_token" });
|
|
234
|
+
const response = await post(
|
|
235
|
+
endpoint(options.issuer, "/oauth/revoke"),
|
|
236
|
+
body,
|
|
237
|
+
{
|
|
238
|
+
Authorization: basicAuth(options.clientId, options.clientSecret)
|
|
239
|
+
},
|
|
240
|
+
options
|
|
241
|
+
);
|
|
242
|
+
await throwIfOAuthError(response);
|
|
243
|
+
}
|
|
244
|
+
async function userinfo(options) {
|
|
245
|
+
const response = await request(
|
|
246
|
+
endpoint(options.issuer, "/oauth/userinfo"),
|
|
247
|
+
{
|
|
248
|
+
method: "GET",
|
|
249
|
+
headers: {
|
|
250
|
+
Authorization: `Bearer ${options.accessToken}`,
|
|
251
|
+
Accept: "application/json",
|
|
252
|
+
"User-Agent": USER_AGENT
|
|
253
|
+
}
|
|
254
|
+
},
|
|
255
|
+
options
|
|
256
|
+
);
|
|
257
|
+
await throwIfOAuthError(response);
|
|
258
|
+
const wire = await response.json();
|
|
259
|
+
return {
|
|
260
|
+
sub: wire.sub,
|
|
261
|
+
email: wire.email,
|
|
262
|
+
partnerId: wire.partner_id ?? null,
|
|
263
|
+
clientId: wire.client_id ?? null,
|
|
264
|
+
scope: wire.scope,
|
|
265
|
+
expiresAt: wire.expires_at ?? null
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
var discoveryCache = /* @__PURE__ */ new Map();
|
|
269
|
+
var DISCOVERY_TTL_MS = 60 * 60 * 1e3;
|
|
270
|
+
async function discover(issuer, options = {}) {
|
|
271
|
+
const key = trimSlash(issuer);
|
|
272
|
+
const cached = discoveryCache.get(key);
|
|
273
|
+
if (cached && cached.expiresAt > Date.now()) return cached.metadata;
|
|
274
|
+
const response = await request(
|
|
275
|
+
`${key}/.well-known/oauth-authorization-server`,
|
|
276
|
+
{
|
|
277
|
+
method: "GET",
|
|
278
|
+
headers: { Accept: "application/json", "User-Agent": USER_AGENT }
|
|
279
|
+
},
|
|
280
|
+
options
|
|
281
|
+
);
|
|
282
|
+
if (!response.ok)
|
|
283
|
+
throw new OAuthError(response.status, "discovery_failed", `HTTP ${response.status}`);
|
|
284
|
+
const metadata = await response.json();
|
|
285
|
+
if (typeof metadata.issuer !== "string" || trimSlash(metadata.issuer) !== key) {
|
|
286
|
+
throw new OAuthError(
|
|
287
|
+
response.status,
|
|
288
|
+
"discovery_failed",
|
|
289
|
+
`Issuer mismatch: ${String(metadata.issuer)}`
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
discoveryCache.set(key, { metadata, expiresAt: Date.now() + DISCOVERY_TTL_MS });
|
|
293
|
+
return metadata;
|
|
294
|
+
}
|
|
295
|
+
function bundleFromToken(token, privateKey) {
|
|
296
|
+
return {
|
|
297
|
+
service: "open-banking.io",
|
|
298
|
+
apiBaseUrl: token.apiBaseUrl,
|
|
299
|
+
user: token.user,
|
|
300
|
+
apiKey: token.accessToken,
|
|
301
|
+
encryptionKey: { scheme: "ecdh-p256-hkdf-aes-256-gcm", curve: "P-256", privateKey }
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
function endpoint(issuer, path) {
|
|
305
|
+
return trimSlash(issuer) + path;
|
|
306
|
+
}
|
|
307
|
+
function trimSlash(value) {
|
|
308
|
+
let end = value.length;
|
|
309
|
+
while (end > 0 && value[end - 1] === "/") end--;
|
|
310
|
+
return value.slice(0, end);
|
|
311
|
+
}
|
|
312
|
+
function basicAuth(clientId, clientSecret) {
|
|
313
|
+
const pair = `${encodeURIComponent(clientId)}:${encodeURIComponent(clientSecret)}`;
|
|
314
|
+
return `Basic ${Buffer.from(pair, "utf8").toString("base64")}`;
|
|
315
|
+
}
|
|
316
|
+
function constantTimeEqual(a, b) {
|
|
317
|
+
const x = (0, import_node_crypto.createHash)("sha256").update(a, "utf8").digest();
|
|
318
|
+
const y = (0, import_node_crypto.createHash)("sha256").update(b, "utf8").digest();
|
|
319
|
+
return (0, import_node_crypto.timingSafeEqual)(x, y) && a.length === b.length;
|
|
320
|
+
}
|
|
321
|
+
function toRecord(input) {
|
|
322
|
+
if (typeof input === "string") {
|
|
323
|
+
const trimmed = input.trim();
|
|
324
|
+
if (trimmed.startsWith("{")) return JSON.parse(trimmed);
|
|
325
|
+
return Object.fromEntries(new URLSearchParams(trimmed));
|
|
326
|
+
}
|
|
327
|
+
if (input instanceof URLSearchParams) return Object.fromEntries(input);
|
|
328
|
+
if (input instanceof FormData) {
|
|
329
|
+
const record = {};
|
|
330
|
+
input.forEach((value, name) => {
|
|
331
|
+
if (typeof value === "string") record[name] = value;
|
|
332
|
+
});
|
|
333
|
+
return record;
|
|
334
|
+
}
|
|
335
|
+
return input;
|
|
336
|
+
}
|
|
337
|
+
async function post(url, body, headers, options) {
|
|
338
|
+
return request(
|
|
339
|
+
url,
|
|
340
|
+
{
|
|
341
|
+
method: "POST",
|
|
342
|
+
headers: {
|
|
343
|
+
...headers,
|
|
344
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
345
|
+
"User-Agent": USER_AGENT
|
|
346
|
+
},
|
|
347
|
+
body: body.toString()
|
|
348
|
+
},
|
|
349
|
+
options
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
async function request(url, init, options) {
|
|
353
|
+
const fetchImpl = options.fetch ?? fetch;
|
|
354
|
+
return fetchImpl(url, {
|
|
355
|
+
...init,
|
|
356
|
+
signal: AbortSignal.timeout(options.timeoutMs ?? DEFAULT_TIMEOUT_MS)
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
async function throwIfOAuthError(response) {
|
|
360
|
+
if (response.ok) return;
|
|
361
|
+
let error = "http_error";
|
|
362
|
+
let description = `HTTP ${response.status}`;
|
|
363
|
+
try {
|
|
364
|
+
const body = await response.json();
|
|
365
|
+
if (body.error) {
|
|
366
|
+
error = body.error;
|
|
367
|
+
description = body.error_description;
|
|
368
|
+
}
|
|
369
|
+
} catch {
|
|
370
|
+
}
|
|
371
|
+
throw new OAuthError(response.status, error, description);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// src/client.ts
|
|
375
|
+
var import_node_fs = require("fs");
|
|
376
|
+
|
|
377
|
+
// src/envelope.ts
|
|
378
|
+
var import_node_crypto2 = require("crypto");
|
|
379
|
+
var { subtle } = import_node_crypto2.webcrypto;
|
|
380
|
+
var VERSION2 = 1;
|
|
381
|
+
var POINT_LEN = 65;
|
|
382
|
+
var NONCE_LEN = 12;
|
|
383
|
+
var TAG_LEN = 16;
|
|
384
|
+
var HKDF_INFO = new TextEncoder().encode("bank.core.ci/zk/v1");
|
|
385
|
+
var HKDF_SALT = new Uint8Array(32);
|
|
386
|
+
async function decryptEnvelope(privateKey, envelopeBase64) {
|
|
387
|
+
const bytes = base64ToBytes(envelopeBase64);
|
|
388
|
+
if (bytes.length < 1 + POINT_LEN + NONCE_LEN + TAG_LEN || bytes[0] !== VERSION2) {
|
|
389
|
+
throw new Error("Invalid or unsupported envelope");
|
|
390
|
+
}
|
|
391
|
+
const ephPub = bytes.subarray(1, 1 + POINT_LEN);
|
|
392
|
+
const nonce = bytes.subarray(1 + POINT_LEN, 1 + POINT_LEN + NONCE_LEN);
|
|
393
|
+
const tag = bytes.subarray(1 + POINT_LEN + NONCE_LEN, 1 + POINT_LEN + NONCE_LEN + TAG_LEN);
|
|
394
|
+
const ciphertext = bytes.subarray(1 + POINT_LEN + NONCE_LEN + TAG_LEN);
|
|
395
|
+
const ephKey = await subtle.importKey(
|
|
396
|
+
"raw",
|
|
397
|
+
ephPub,
|
|
398
|
+
{ name: "ECDH", namedCurve: "P-256" },
|
|
399
|
+
false,
|
|
400
|
+
[]
|
|
401
|
+
);
|
|
402
|
+
const shared = new Uint8Array(
|
|
403
|
+
await subtle.deriveBits({ name: "ECDH", public: ephKey }, privateKey, 256)
|
|
404
|
+
);
|
|
405
|
+
const hkdf = await subtle.importKey("raw", shared, "HKDF", false, ["deriveKey"]);
|
|
406
|
+
const aesKey = await subtle.deriveKey(
|
|
407
|
+
{ name: "HKDF", hash: "SHA-256", salt: HKDF_SALT, info: HKDF_INFO },
|
|
408
|
+
hkdf,
|
|
409
|
+
{ name: "AES-GCM", length: 256 },
|
|
410
|
+
false,
|
|
411
|
+
["decrypt"]
|
|
412
|
+
);
|
|
413
|
+
const ctWithTag = new Uint8Array(ciphertext.length + tag.length);
|
|
414
|
+
ctWithTag.set(ciphertext, 0);
|
|
415
|
+
ctWithTag.set(tag, ciphertext.length);
|
|
416
|
+
const plaintext = await subtle.decrypt(
|
|
417
|
+
{ name: "AES-GCM", iv: nonce, tagLength: 128 },
|
|
418
|
+
aesKey,
|
|
419
|
+
ctWithTag
|
|
420
|
+
);
|
|
421
|
+
return new Uint8Array(plaintext);
|
|
422
|
+
}
|
|
423
|
+
async function decryptTo(privateKey, envelopeBase64) {
|
|
424
|
+
if (envelopeBase64 == null) return null;
|
|
425
|
+
const plaintext = await decryptEnvelope(privateKey, envelopeBase64);
|
|
426
|
+
return JSON.parse(new TextDecoder().decode(plaintext));
|
|
427
|
+
}
|
|
428
|
+
async function importPrivateKey(pkcs8Base64) {
|
|
429
|
+
return subtle.importKey(
|
|
430
|
+
"pkcs8",
|
|
431
|
+
base64ToBytes(pkcs8Base64),
|
|
432
|
+
{ name: "ECDH", namedCurve: "P-256" },
|
|
433
|
+
false,
|
|
434
|
+
["deriveBits"]
|
|
435
|
+
);
|
|
436
|
+
}
|
|
437
|
+
function base64ToBytes(b64) {
|
|
438
|
+
return new Uint8Array(Buffer.from(b64, "base64"));
|
|
439
|
+
}
|
|
161
440
|
|
|
162
441
|
// src/client.ts
|
|
163
442
|
var OpenBankingClient = class _OpenBankingClient {
|
|
@@ -206,6 +485,13 @@ var OpenBankingClient = class _OpenBankingClient {
|
|
|
206
485
|
fetch: overrides.fetch
|
|
207
486
|
});
|
|
208
487
|
}
|
|
488
|
+
/**
|
|
489
|
+
* Builds a client from a Partner Connect token response and the private key the consent page
|
|
490
|
+
* relayed — see {@link exchangeCode} and {@link parseRelay}.
|
|
491
|
+
*/
|
|
492
|
+
static fromTokenResponse(token, privateKey, overrides = {}) {
|
|
493
|
+
return _OpenBankingClient.fromBundle(bundleFromToken(token, privateKey), overrides);
|
|
494
|
+
}
|
|
209
495
|
/** Lists the user's accounts with all sensitive fields decrypted. */
|
|
210
496
|
async getAccounts() {
|
|
211
497
|
const wires = await this.getAccountWires();
|
|
@@ -366,9 +652,22 @@ var OpenBankingClient = class _OpenBankingClient {
|
|
|
366
652
|
};
|
|
367
653
|
// Annotate the CommonJS export names for ESM import in node:
|
|
368
654
|
0 && (module.exports = {
|
|
655
|
+
CONNECT_RELAY_FIELDS,
|
|
656
|
+
OAuthError,
|
|
369
657
|
OpenBankingClient,
|
|
658
|
+
RelayError,
|
|
659
|
+
buildAuthorizeUrl,
|
|
660
|
+
bundleFromToken,
|
|
661
|
+
createPkce,
|
|
662
|
+
createState,
|
|
370
663
|
decryptEnvelope,
|
|
371
664
|
decryptTo,
|
|
372
|
-
|
|
665
|
+
discover,
|
|
666
|
+
exchangeCode,
|
|
667
|
+
importPrivateKey,
|
|
668
|
+
parseRelay,
|
|
669
|
+
pkceChallenge,
|
|
670
|
+
revokeToken,
|
|
671
|
+
userinfo
|
|
373
672
|
});
|
|
374
673
|
//# sourceMappingURL=index.cjs.map
|