@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.node.cjs
CHANGED
|
@@ -1199,6 +1199,7 @@ __export(index_node_exports, {
|
|
|
1199
1199
|
NodeECIESProvider: () => NodeECIESUint8Provider,
|
|
1200
1200
|
NodePlatformAdapter: () => NodePlatformAdapter,
|
|
1201
1201
|
NonceError: () => NonceError,
|
|
1202
|
+
OAuthClient: () => OAuthClient,
|
|
1202
1203
|
PKCE_CHALLENGE_PATTERN: () => PKCE_CHALLENGE_PATTERN,
|
|
1203
1204
|
PKCE_VERIFIER_PATTERN: () => PKCE_VERIFIER_PATTERN,
|
|
1204
1205
|
PSError: () => PSError,
|
|
@@ -29139,7 +29140,7 @@ async function buildWeb3SignedHeader(params) {
|
|
|
29139
29140
|
}
|
|
29140
29141
|
|
|
29141
29142
|
// src/storage/providers/vana-storage.ts
|
|
29142
|
-
var DEFAULT_ENDPOINT = "https://storage.vana.
|
|
29143
|
+
var DEFAULT_ENDPOINT = "https://storage.vana.org";
|
|
29143
29144
|
var BLOB_PATH_PREFIX = "/v1/blobs";
|
|
29144
29145
|
var DEFAULT_TOKEN_TTL_SECONDS = 300;
|
|
29145
29146
|
var VanaStorage = class {
|
|
@@ -32402,6 +32403,228 @@ var InMemoryTokenStore = class {
|
|
|
32402
32403
|
}
|
|
32403
32404
|
};
|
|
32404
32405
|
|
|
32406
|
+
// src/auth/oauth-client.ts
|
|
32407
|
+
var VERIFIER_TTL_SECONDS = 600;
|
|
32408
|
+
var RESERVED_AUTHORIZE_PARAMS = /* @__PURE__ */ new Set([
|
|
32409
|
+
"response_type",
|
|
32410
|
+
"client_id",
|
|
32411
|
+
"redirect_uri",
|
|
32412
|
+
"scope",
|
|
32413
|
+
"state",
|
|
32414
|
+
"code_challenge",
|
|
32415
|
+
"code_challenge_method"
|
|
32416
|
+
]);
|
|
32417
|
+
var OAuthClient = class {
|
|
32418
|
+
#config;
|
|
32419
|
+
constructor(config) {
|
|
32420
|
+
const fetchImpl = config.fetchImpl ?? globalThis.fetch;
|
|
32421
|
+
if (typeof fetchImpl !== "function") {
|
|
32422
|
+
throw new TypeError(
|
|
32423
|
+
"OAuthClient requires a global `fetch` or an explicit `fetchImpl`"
|
|
32424
|
+
);
|
|
32425
|
+
}
|
|
32426
|
+
this.#config = {
|
|
32427
|
+
authorizationEndpoint: config.authorizationEndpoint,
|
|
32428
|
+
tokenEndpoint: config.tokenEndpoint,
|
|
32429
|
+
clientId: config.clientId,
|
|
32430
|
+
redirectUri: config.redirectUri,
|
|
32431
|
+
scope: config.scope,
|
|
32432
|
+
tokenStore: config.tokenStore ?? new InMemoryTokenStore(),
|
|
32433
|
+
fetchImpl,
|
|
32434
|
+
generateState: config.generateState ?? defaultGenerateState
|
|
32435
|
+
};
|
|
32436
|
+
}
|
|
32437
|
+
/** Build the authorize URL and persist the PKCE verifier keyed by `state`. */
|
|
32438
|
+
async buildAuthorizationUrl(opts = {}) {
|
|
32439
|
+
const state = opts.state ?? this.#config.generateState();
|
|
32440
|
+
const scope = opts.scope ?? this.#config.scope;
|
|
32441
|
+
const verifier = generatePkceVerifier();
|
|
32442
|
+
const challenge = await computePkceChallenge(verifier);
|
|
32443
|
+
await this.#config.tokenStore.set(this.#verifierKey(state), {
|
|
32444
|
+
token: verifier,
|
|
32445
|
+
expiresAt: Math.floor(Date.now() / 1e3) + VERIFIER_TTL_SECONDS
|
|
32446
|
+
});
|
|
32447
|
+
const params = new URLSearchParams();
|
|
32448
|
+
params.set("response_type", "code");
|
|
32449
|
+
params.set("client_id", this.#config.clientId);
|
|
32450
|
+
params.set("redirect_uri", this.#config.redirectUri);
|
|
32451
|
+
if (scope !== void 0 && scope.length > 0) {
|
|
32452
|
+
params.set("scope", scope);
|
|
32453
|
+
}
|
|
32454
|
+
params.set("state", state);
|
|
32455
|
+
params.set("code_challenge", challenge);
|
|
32456
|
+
params.set("code_challenge_method", "S256");
|
|
32457
|
+
if (opts.extraParams !== void 0) {
|
|
32458
|
+
for (const k of Object.keys(opts.extraParams)) {
|
|
32459
|
+
if (RESERVED_AUTHORIZE_PARAMS.has(k)) {
|
|
32460
|
+
throw new Error(
|
|
32461
|
+
`extraParams may not override the reserved OAuth/PKCE parameter "${k}"`
|
|
32462
|
+
);
|
|
32463
|
+
}
|
|
32464
|
+
}
|
|
32465
|
+
for (const [k, v] of Object.entries(opts.extraParams)) {
|
|
32466
|
+
params.set(k, v);
|
|
32467
|
+
}
|
|
32468
|
+
}
|
|
32469
|
+
const sep = this.#config.authorizationEndpoint.includes("?") ? "&" : "?";
|
|
32470
|
+
const url = `${this.#config.authorizationEndpoint}${sep}${params.toString()}`;
|
|
32471
|
+
return { url, state };
|
|
32472
|
+
}
|
|
32473
|
+
/**
|
|
32474
|
+
* Handle the redirect-callback URL. Validates `state`, retrieves the saved
|
|
32475
|
+
* verifier, exchanges the authorization code + verifier for tokens, and
|
|
32476
|
+
* persists them. Returns the access {@link TokenRecord}.
|
|
32477
|
+
*/
|
|
32478
|
+
async handleCallback(callbackUrl) {
|
|
32479
|
+
const parsed = new URL(callbackUrl);
|
|
32480
|
+
const params = parsed.searchParams;
|
|
32481
|
+
const errorCode = params.get("error");
|
|
32482
|
+
if (errorCode !== null) {
|
|
32483
|
+
throw new Error(
|
|
32484
|
+
formatOAuthError({
|
|
32485
|
+
error: errorCode,
|
|
32486
|
+
error_description: params.get("error_description") ?? void 0
|
|
32487
|
+
})
|
|
32488
|
+
);
|
|
32489
|
+
}
|
|
32490
|
+
const code = params.get("code");
|
|
32491
|
+
const state = params.get("state");
|
|
32492
|
+
if (code === null || state === null) {
|
|
32493
|
+
throw new Error("OAuth callback is missing `code` or `state`");
|
|
32494
|
+
}
|
|
32495
|
+
const verifierRecord = await this.#config.tokenStore.get(
|
|
32496
|
+
this.#verifierKey(state)
|
|
32497
|
+
);
|
|
32498
|
+
if (verifierRecord === null) {
|
|
32499
|
+
throw new Error(
|
|
32500
|
+
"OAuth callback state does not match any in-flight verifier (possible CSRF or expired flow)"
|
|
32501
|
+
);
|
|
32502
|
+
}
|
|
32503
|
+
const body = new URLSearchParams();
|
|
32504
|
+
body.set("grant_type", "authorization_code");
|
|
32505
|
+
body.set("code", code);
|
|
32506
|
+
body.set("redirect_uri", this.#config.redirectUri);
|
|
32507
|
+
body.set("client_id", this.#config.clientId);
|
|
32508
|
+
body.set("code_verifier", verifierRecord.token);
|
|
32509
|
+
let tokens;
|
|
32510
|
+
try {
|
|
32511
|
+
tokens = await this.#tokenRequest(body);
|
|
32512
|
+
} finally {
|
|
32513
|
+
await this.#config.tokenStore.delete(this.#verifierKey(state));
|
|
32514
|
+
}
|
|
32515
|
+
return this.#persistTokens(tokens);
|
|
32516
|
+
}
|
|
32517
|
+
/**
|
|
32518
|
+
* Exchange a stored refresh token for a fresh access token. Throws if no
|
|
32519
|
+
* refresh token is available.
|
|
32520
|
+
*/
|
|
32521
|
+
async refresh() {
|
|
32522
|
+
const refreshRecord = await this.#config.tokenStore.get(this.#refreshKey());
|
|
32523
|
+
if (refreshRecord === null) {
|
|
32524
|
+
throw new Error("OAuth refresh failed: no refresh token stored");
|
|
32525
|
+
}
|
|
32526
|
+
const body = new URLSearchParams();
|
|
32527
|
+
body.set("grant_type", "refresh_token");
|
|
32528
|
+
body.set("refresh_token", refreshRecord.token);
|
|
32529
|
+
body.set("client_id", this.#config.clientId);
|
|
32530
|
+
const tokens = await this.#tokenRequest(body);
|
|
32531
|
+
return this.#persistTokens(tokens, refreshRecord.token);
|
|
32532
|
+
}
|
|
32533
|
+
/**
|
|
32534
|
+
* Get the current access token if valid (refreshing first if expired and a
|
|
32535
|
+
* refresh token is available). Returns `null` when no usable token exists.
|
|
32536
|
+
*/
|
|
32537
|
+
async getAccessToken() {
|
|
32538
|
+
const stored = await this.#config.tokenStore.get(this.#accessKey());
|
|
32539
|
+
if (stored !== null) return stored.token;
|
|
32540
|
+
const refresh = await this.#config.tokenStore.get(this.#refreshKey());
|
|
32541
|
+
if (refresh === null) return null;
|
|
32542
|
+
try {
|
|
32543
|
+
const refreshed = await this.refresh();
|
|
32544
|
+
return refreshed.token;
|
|
32545
|
+
} catch {
|
|
32546
|
+
return null;
|
|
32547
|
+
}
|
|
32548
|
+
}
|
|
32549
|
+
/** Forget tokens (logout). Does NOT call any remote revocation endpoint. */
|
|
32550
|
+
async signOut() {
|
|
32551
|
+
await this.#config.tokenStore.delete(this.#accessKey());
|
|
32552
|
+
await this.#config.tokenStore.delete(this.#refreshKey());
|
|
32553
|
+
}
|
|
32554
|
+
#accessKey() {
|
|
32555
|
+
return `oauth:tokens:${this.#config.clientId}`;
|
|
32556
|
+
}
|
|
32557
|
+
#refreshKey() {
|
|
32558
|
+
return `oauth:refresh:${this.#config.clientId}`;
|
|
32559
|
+
}
|
|
32560
|
+
#verifierKey(state) {
|
|
32561
|
+
return `oauth:verifier:${state}`;
|
|
32562
|
+
}
|
|
32563
|
+
async #tokenRequest(body) {
|
|
32564
|
+
const response = await this.#config.fetchImpl(this.#config.tokenEndpoint, {
|
|
32565
|
+
method: "POST",
|
|
32566
|
+
headers: {
|
|
32567
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
32568
|
+
Accept: "application/json"
|
|
32569
|
+
},
|
|
32570
|
+
body: body.toString()
|
|
32571
|
+
});
|
|
32572
|
+
const text = await response.text();
|
|
32573
|
+
const parsed = parseJsonBody(text);
|
|
32574
|
+
if (!response.ok) {
|
|
32575
|
+
throw new Error(formatOAuthError(parsed ?? {}, response.status));
|
|
32576
|
+
}
|
|
32577
|
+
if (parsed === null || typeof parsed !== "object" || typeof parsed.access_token !== "string") {
|
|
32578
|
+
throw new Error(
|
|
32579
|
+
"OAuth token endpoint returned a response without an `access_token` string"
|
|
32580
|
+
);
|
|
32581
|
+
}
|
|
32582
|
+
return parsed;
|
|
32583
|
+
}
|
|
32584
|
+
async #persistTokens(tokens, previousRefreshToken) {
|
|
32585
|
+
const record = { token: tokens.access_token };
|
|
32586
|
+
if (typeof tokens.expires_in === "number" && tokens.expires_in > 0) {
|
|
32587
|
+
record.expiresAt = Math.floor(Date.now() / 1e3) + tokens.expires_in;
|
|
32588
|
+
}
|
|
32589
|
+
await this.#config.tokenStore.set(this.#accessKey(), record);
|
|
32590
|
+
const newRefresh = tokens.refresh_token ?? previousRefreshToken;
|
|
32591
|
+
if (newRefresh !== void 0) {
|
|
32592
|
+
await this.#config.tokenStore.set(this.#refreshKey(), {
|
|
32593
|
+
token: newRefresh
|
|
32594
|
+
});
|
|
32595
|
+
}
|
|
32596
|
+
return record;
|
|
32597
|
+
}
|
|
32598
|
+
};
|
|
32599
|
+
function defaultGenerateState() {
|
|
32600
|
+
const bytes = new Uint8Array(24);
|
|
32601
|
+
crypto.getRandomValues(bytes);
|
|
32602
|
+
let binary = "";
|
|
32603
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
32604
|
+
binary += String.fromCharCode(bytes[i]);
|
|
32605
|
+
}
|
|
32606
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
32607
|
+
}
|
|
32608
|
+
function parseJsonBody(text) {
|
|
32609
|
+
if (text.length === 0) return null;
|
|
32610
|
+
try {
|
|
32611
|
+
return JSON.parse(text);
|
|
32612
|
+
} catch {
|
|
32613
|
+
return null;
|
|
32614
|
+
}
|
|
32615
|
+
}
|
|
32616
|
+
function formatOAuthError(body, status) {
|
|
32617
|
+
const parts = ["OAuth token request failed"];
|
|
32618
|
+
if (status !== void 0) parts.push(`(HTTP ${String(status)})`);
|
|
32619
|
+
if (body.error !== void 0 && body.error.length > 0) {
|
|
32620
|
+
parts.push(`: ${body.error}`);
|
|
32621
|
+
if (body.error_description !== void 0 && body.error_description.length > 0) {
|
|
32622
|
+
parts.push(`- ${body.error_description}`);
|
|
32623
|
+
}
|
|
32624
|
+
}
|
|
32625
|
+
return parts.join(" ").replace(" : ", ": ").replace(" - ", " - ");
|
|
32626
|
+
}
|
|
32627
|
+
|
|
32405
32628
|
// src/protocol/eip712.ts
|
|
32406
32629
|
var DOMAIN_NAME = "Vana Data Portability";
|
|
32407
32630
|
var DOMAIN_VERSION = "1";
|
|
@@ -32956,6 +33179,7 @@ async function parsePSError(response) {
|
|
|
32956
33179
|
NodeECIESProvider,
|
|
32957
33180
|
NodePlatformAdapter,
|
|
32958
33181
|
NonceError,
|
|
33182
|
+
OAuthClient,
|
|
32959
33183
|
PKCE_CHALLENGE_PATTERN,
|
|
32960
33184
|
PKCE_VERIFIER_PATTERN,
|
|
32961
33185
|
PSError,
|