@opendatalabs/vana-sdk 3.0.1 → 3.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/dist/__tests__/interop-personal-server.test.d.ts +1 -0
- package/dist/auth/oauth-client.cjs +250 -0
- package/dist/auth/oauth-client.cjs.map +1 -0
- package/dist/auth/oauth-client.d.ts +90 -0
- package/dist/auth/oauth-client.js +228 -0
- package/dist/auth/oauth-client.js.map +1 -0
- package/dist/auth/oauth-client.test.d.ts +1 -0
- package/dist/index.browser.d.ts +1 -0
- package/dist/index.browser.js +224 -1
- package/dist/index.browser.js.map +3 -3
- package/dist/index.node.cjs +225 -1
- package/dist/index.node.cjs.map +3 -3
- package/dist/index.node.d.ts +1 -0
- package/dist/index.node.js +224 -1
- package/dist/index.node.js.map +3 -3
- package/dist/storage/index.cjs.map +1 -1
- package/dist/storage/index.d.ts +1 -1
- package/dist/storage/index.js.map +1 -1
- package/dist/storage/providers/vana-storage.cjs +1 -1
- package/dist/storage/providers/vana-storage.cjs.map +1 -1
- package/dist/storage/providers/vana-storage.d.ts +2 -2
- package/dist/storage/providers/vana-storage.js +1 -1
- package/dist/storage/providers/vana-storage.js.map +1 -1
- package/package.json +2 -1
package/dist/index.browser.js
CHANGED
|
@@ -29006,7 +29006,7 @@ async function buildWeb3SignedHeader(params) {
|
|
|
29006
29006
|
}
|
|
29007
29007
|
|
|
29008
29008
|
// src/storage/providers/vana-storage.ts
|
|
29009
|
-
var DEFAULT_ENDPOINT = "https://storage.vana.
|
|
29009
|
+
var DEFAULT_ENDPOINT = "https://storage.vana.org";
|
|
29010
29010
|
var BLOB_PATH_PREFIX = "/v1/blobs";
|
|
29011
29011
|
var DEFAULT_TOKEN_TTL_SECONDS = 300;
|
|
29012
29012
|
var VanaStorage = class {
|
|
@@ -31432,6 +31432,228 @@ var InMemoryTokenStore = class {
|
|
|
31432
31432
|
}
|
|
31433
31433
|
};
|
|
31434
31434
|
|
|
31435
|
+
// src/auth/oauth-client.ts
|
|
31436
|
+
var VERIFIER_TTL_SECONDS = 600;
|
|
31437
|
+
var RESERVED_AUTHORIZE_PARAMS = /* @__PURE__ */ new Set([
|
|
31438
|
+
"response_type",
|
|
31439
|
+
"client_id",
|
|
31440
|
+
"redirect_uri",
|
|
31441
|
+
"scope",
|
|
31442
|
+
"state",
|
|
31443
|
+
"code_challenge",
|
|
31444
|
+
"code_challenge_method"
|
|
31445
|
+
]);
|
|
31446
|
+
var OAuthClient = class {
|
|
31447
|
+
#config;
|
|
31448
|
+
constructor(config) {
|
|
31449
|
+
const fetchImpl = config.fetchImpl ?? globalThis.fetch;
|
|
31450
|
+
if (typeof fetchImpl !== "function") {
|
|
31451
|
+
throw new TypeError(
|
|
31452
|
+
"OAuthClient requires a global `fetch` or an explicit `fetchImpl`"
|
|
31453
|
+
);
|
|
31454
|
+
}
|
|
31455
|
+
this.#config = {
|
|
31456
|
+
authorizationEndpoint: config.authorizationEndpoint,
|
|
31457
|
+
tokenEndpoint: config.tokenEndpoint,
|
|
31458
|
+
clientId: config.clientId,
|
|
31459
|
+
redirectUri: config.redirectUri,
|
|
31460
|
+
scope: config.scope,
|
|
31461
|
+
tokenStore: config.tokenStore ?? new InMemoryTokenStore(),
|
|
31462
|
+
fetchImpl,
|
|
31463
|
+
generateState: config.generateState ?? defaultGenerateState
|
|
31464
|
+
};
|
|
31465
|
+
}
|
|
31466
|
+
/** Build the authorize URL and persist the PKCE verifier keyed by `state`. */
|
|
31467
|
+
async buildAuthorizationUrl(opts = {}) {
|
|
31468
|
+
const state = opts.state ?? this.#config.generateState();
|
|
31469
|
+
const scope = opts.scope ?? this.#config.scope;
|
|
31470
|
+
const verifier = generatePkceVerifier();
|
|
31471
|
+
const challenge = await computePkceChallenge(verifier);
|
|
31472
|
+
await this.#config.tokenStore.set(this.#verifierKey(state), {
|
|
31473
|
+
token: verifier,
|
|
31474
|
+
expiresAt: Math.floor(Date.now() / 1e3) + VERIFIER_TTL_SECONDS
|
|
31475
|
+
});
|
|
31476
|
+
const params = new URLSearchParams();
|
|
31477
|
+
params.set("response_type", "code");
|
|
31478
|
+
params.set("client_id", this.#config.clientId);
|
|
31479
|
+
params.set("redirect_uri", this.#config.redirectUri);
|
|
31480
|
+
if (scope !== void 0 && scope.length > 0) {
|
|
31481
|
+
params.set("scope", scope);
|
|
31482
|
+
}
|
|
31483
|
+
params.set("state", state);
|
|
31484
|
+
params.set("code_challenge", challenge);
|
|
31485
|
+
params.set("code_challenge_method", "S256");
|
|
31486
|
+
if (opts.extraParams !== void 0) {
|
|
31487
|
+
for (const k of Object.keys(opts.extraParams)) {
|
|
31488
|
+
if (RESERVED_AUTHORIZE_PARAMS.has(k)) {
|
|
31489
|
+
throw new Error(
|
|
31490
|
+
`extraParams may not override the reserved OAuth/PKCE parameter "${k}"`
|
|
31491
|
+
);
|
|
31492
|
+
}
|
|
31493
|
+
}
|
|
31494
|
+
for (const [k, v] of Object.entries(opts.extraParams)) {
|
|
31495
|
+
params.set(k, v);
|
|
31496
|
+
}
|
|
31497
|
+
}
|
|
31498
|
+
const sep = this.#config.authorizationEndpoint.includes("?") ? "&" : "?";
|
|
31499
|
+
const url = `${this.#config.authorizationEndpoint}${sep}${params.toString()}`;
|
|
31500
|
+
return { url, state };
|
|
31501
|
+
}
|
|
31502
|
+
/**
|
|
31503
|
+
* Handle the redirect-callback URL. Validates `state`, retrieves the saved
|
|
31504
|
+
* verifier, exchanges the authorization code + verifier for tokens, and
|
|
31505
|
+
* persists them. Returns the access {@link TokenRecord}.
|
|
31506
|
+
*/
|
|
31507
|
+
async handleCallback(callbackUrl) {
|
|
31508
|
+
const parsed = new URL(callbackUrl);
|
|
31509
|
+
const params = parsed.searchParams;
|
|
31510
|
+
const errorCode = params.get("error");
|
|
31511
|
+
if (errorCode !== null) {
|
|
31512
|
+
throw new Error(
|
|
31513
|
+
formatOAuthError({
|
|
31514
|
+
error: errorCode,
|
|
31515
|
+
error_description: params.get("error_description") ?? void 0
|
|
31516
|
+
})
|
|
31517
|
+
);
|
|
31518
|
+
}
|
|
31519
|
+
const code = params.get("code");
|
|
31520
|
+
const state = params.get("state");
|
|
31521
|
+
if (code === null || state === null) {
|
|
31522
|
+
throw new Error("OAuth callback is missing `code` or `state`");
|
|
31523
|
+
}
|
|
31524
|
+
const verifierRecord = await this.#config.tokenStore.get(
|
|
31525
|
+
this.#verifierKey(state)
|
|
31526
|
+
);
|
|
31527
|
+
if (verifierRecord === null) {
|
|
31528
|
+
throw new Error(
|
|
31529
|
+
"OAuth callback state does not match any in-flight verifier (possible CSRF or expired flow)"
|
|
31530
|
+
);
|
|
31531
|
+
}
|
|
31532
|
+
const body = new URLSearchParams();
|
|
31533
|
+
body.set("grant_type", "authorization_code");
|
|
31534
|
+
body.set("code", code);
|
|
31535
|
+
body.set("redirect_uri", this.#config.redirectUri);
|
|
31536
|
+
body.set("client_id", this.#config.clientId);
|
|
31537
|
+
body.set("code_verifier", verifierRecord.token);
|
|
31538
|
+
let tokens;
|
|
31539
|
+
try {
|
|
31540
|
+
tokens = await this.#tokenRequest(body);
|
|
31541
|
+
} finally {
|
|
31542
|
+
await this.#config.tokenStore.delete(this.#verifierKey(state));
|
|
31543
|
+
}
|
|
31544
|
+
return this.#persistTokens(tokens);
|
|
31545
|
+
}
|
|
31546
|
+
/**
|
|
31547
|
+
* Exchange a stored refresh token for a fresh access token. Throws if no
|
|
31548
|
+
* refresh token is available.
|
|
31549
|
+
*/
|
|
31550
|
+
async refresh() {
|
|
31551
|
+
const refreshRecord = await this.#config.tokenStore.get(this.#refreshKey());
|
|
31552
|
+
if (refreshRecord === null) {
|
|
31553
|
+
throw new Error("OAuth refresh failed: no refresh token stored");
|
|
31554
|
+
}
|
|
31555
|
+
const body = new URLSearchParams();
|
|
31556
|
+
body.set("grant_type", "refresh_token");
|
|
31557
|
+
body.set("refresh_token", refreshRecord.token);
|
|
31558
|
+
body.set("client_id", this.#config.clientId);
|
|
31559
|
+
const tokens = await this.#tokenRequest(body);
|
|
31560
|
+
return this.#persistTokens(tokens, refreshRecord.token);
|
|
31561
|
+
}
|
|
31562
|
+
/**
|
|
31563
|
+
* Get the current access token if valid (refreshing first if expired and a
|
|
31564
|
+
* refresh token is available). Returns `null` when no usable token exists.
|
|
31565
|
+
*/
|
|
31566
|
+
async getAccessToken() {
|
|
31567
|
+
const stored = await this.#config.tokenStore.get(this.#accessKey());
|
|
31568
|
+
if (stored !== null) return stored.token;
|
|
31569
|
+
const refresh = await this.#config.tokenStore.get(this.#refreshKey());
|
|
31570
|
+
if (refresh === null) return null;
|
|
31571
|
+
try {
|
|
31572
|
+
const refreshed = await this.refresh();
|
|
31573
|
+
return refreshed.token;
|
|
31574
|
+
} catch {
|
|
31575
|
+
return null;
|
|
31576
|
+
}
|
|
31577
|
+
}
|
|
31578
|
+
/** Forget tokens (logout). Does NOT call any remote revocation endpoint. */
|
|
31579
|
+
async signOut() {
|
|
31580
|
+
await this.#config.tokenStore.delete(this.#accessKey());
|
|
31581
|
+
await this.#config.tokenStore.delete(this.#refreshKey());
|
|
31582
|
+
}
|
|
31583
|
+
#accessKey() {
|
|
31584
|
+
return `oauth:tokens:${this.#config.clientId}`;
|
|
31585
|
+
}
|
|
31586
|
+
#refreshKey() {
|
|
31587
|
+
return `oauth:refresh:${this.#config.clientId}`;
|
|
31588
|
+
}
|
|
31589
|
+
#verifierKey(state) {
|
|
31590
|
+
return `oauth:verifier:${state}`;
|
|
31591
|
+
}
|
|
31592
|
+
async #tokenRequest(body) {
|
|
31593
|
+
const response = await this.#config.fetchImpl(this.#config.tokenEndpoint, {
|
|
31594
|
+
method: "POST",
|
|
31595
|
+
headers: {
|
|
31596
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
31597
|
+
Accept: "application/json"
|
|
31598
|
+
},
|
|
31599
|
+
body: body.toString()
|
|
31600
|
+
});
|
|
31601
|
+
const text = await response.text();
|
|
31602
|
+
const parsed = parseJsonBody(text);
|
|
31603
|
+
if (!response.ok) {
|
|
31604
|
+
throw new Error(formatOAuthError(parsed ?? {}, response.status));
|
|
31605
|
+
}
|
|
31606
|
+
if (parsed === null || typeof parsed !== "object" || typeof parsed.access_token !== "string") {
|
|
31607
|
+
throw new Error(
|
|
31608
|
+
"OAuth token endpoint returned a response without an `access_token` string"
|
|
31609
|
+
);
|
|
31610
|
+
}
|
|
31611
|
+
return parsed;
|
|
31612
|
+
}
|
|
31613
|
+
async #persistTokens(tokens, previousRefreshToken) {
|
|
31614
|
+
const record = { token: tokens.access_token };
|
|
31615
|
+
if (typeof tokens.expires_in === "number" && tokens.expires_in > 0) {
|
|
31616
|
+
record.expiresAt = Math.floor(Date.now() / 1e3) + tokens.expires_in;
|
|
31617
|
+
}
|
|
31618
|
+
await this.#config.tokenStore.set(this.#accessKey(), record);
|
|
31619
|
+
const newRefresh = tokens.refresh_token ?? previousRefreshToken;
|
|
31620
|
+
if (newRefresh !== void 0) {
|
|
31621
|
+
await this.#config.tokenStore.set(this.#refreshKey(), {
|
|
31622
|
+
token: newRefresh
|
|
31623
|
+
});
|
|
31624
|
+
}
|
|
31625
|
+
return record;
|
|
31626
|
+
}
|
|
31627
|
+
};
|
|
31628
|
+
function defaultGenerateState() {
|
|
31629
|
+
const bytes = new Uint8Array(24);
|
|
31630
|
+
crypto.getRandomValues(bytes);
|
|
31631
|
+
let binary = "";
|
|
31632
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
31633
|
+
binary += String.fromCharCode(bytes[i]);
|
|
31634
|
+
}
|
|
31635
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
31636
|
+
}
|
|
31637
|
+
function parseJsonBody(text) {
|
|
31638
|
+
if (text.length === 0) return null;
|
|
31639
|
+
try {
|
|
31640
|
+
return JSON.parse(text);
|
|
31641
|
+
} catch {
|
|
31642
|
+
return null;
|
|
31643
|
+
}
|
|
31644
|
+
}
|
|
31645
|
+
function formatOAuthError(body, status) {
|
|
31646
|
+
const parts = ["OAuth token request failed"];
|
|
31647
|
+
if (status !== void 0) parts.push(`(HTTP ${String(status)})`);
|
|
31648
|
+
if (body.error !== void 0 && body.error.length > 0) {
|
|
31649
|
+
parts.push(`: ${body.error}`);
|
|
31650
|
+
if (body.error_description !== void 0 && body.error_description.length > 0) {
|
|
31651
|
+
parts.push(`- ${body.error_description}`);
|
|
31652
|
+
}
|
|
31653
|
+
}
|
|
31654
|
+
return parts.join(" ").replace(" : ", ": ").replace(" - ", " - ");
|
|
31655
|
+
}
|
|
31656
|
+
|
|
31435
31657
|
// src/platform/utils.ts
|
|
31436
31658
|
function detectPlatform() {
|
|
31437
31659
|
if (typeof process !== "undefined" && process.versions && process.versions.node) {
|
|
@@ -32209,6 +32431,7 @@ export {
|
|
|
32209
32431
|
MissingAuthError,
|
|
32210
32432
|
NetworkError,
|
|
32211
32433
|
NonceError,
|
|
32434
|
+
OAuthClient,
|
|
32212
32435
|
PKCE_CHALLENGE_PATTERN,
|
|
32213
32436
|
PKCE_VERIFIER_PATTERN,
|
|
32214
32437
|
PSError,
|