diff --git a/packages/oauth-client/src/client.ts b/packages/oauth-client/src/client.ts index ba753bb..29f98f2 100644 --- a/packages/oauth-client/src/client.ts +++ b/packages/oauth-client/src/client.ts @@ -4,6 +4,7 @@ import { importJwk } from "./import-jwk"; import { HappyViewSession } from "./session"; import { MemoryStorage } from "./storage"; import type { + GetSessionResponse, HappyViewOAuthClientOptions, ProvisionKeyResponse, RegisterSessionParams, @@ -135,6 +136,7 @@ export class HappyViewOAuthClient { accessToken: params.accessToken, clientKey: this.clientKey, instanceUrl: this.instanceUrl, + scopes: data.scopes, }; await this.storage.set( `${STORAGE_PREFIX}${data.did}`, @@ -148,6 +150,7 @@ export class HappyViewOAuthClient { accessToken: params.accessToken, clientKey: this.clientKey, instanceUrl: this.instanceUrl, + scopes: data.scopes, fetch: this._fetch, }); } @@ -190,10 +193,34 @@ export class HappyViewOAuthClient { accessToken: data.accessToken, clientKey: data.clientKey, instanceUrl: data.instanceUrl, + scopes: data.scopes ?? [], fetch: this._fetch, }); } + async getSession(did: string): Promise { + const session = await this.restoreSession(did); + if (!session) { + throw new ApiError("No stored session for this DID", 404, {}); + } + + const resp = await session.fetchHandler( + `${this.instanceUrl}/oauth/sessions/${did}`, + { method: "GET" }, + ); + + if (!resp.ok) { + const body = await resp.json().catch(() => ({})); + throw new ApiError( + `Failed to get session: ${resp.status} ${(body as any).message ?? resp.statusText}`, + resp.status, + body, + ); + } + + return resp.json(); + } + async restore(): Promise { const did = await this.storage.get(LAST_ACTIVE_KEY); if (!did) return null; diff --git a/packages/oauth-client/src/index.ts b/packages/oauth-client/src/index.ts index 1275cba..eaa7592 100644 --- a/packages/oauth-client/src/index.ts +++ b/packages/oauth-client/src/index.ts @@ -13,6 +13,7 @@ export type { HappyViewSessionOptions } from "./session"; export { MemoryStorage } from "./storage"; export type { DpopProvision, + GetSessionResponse, HappyViewOAuthClientOptions, ProvisionKeyResponse, RegisterSessionParams, diff --git a/packages/oauth-client/src/session.ts b/packages/oauth-client/src/session.ts index 564ed72..f7b5033 100644 --- a/packages/oauth-client/src/session.ts +++ b/packages/oauth-client/src/session.ts @@ -6,6 +6,7 @@ export interface HappyViewSessionOptions { accessToken: string; clientKey: string; instanceUrl: string; + scopes: string[]; fetch?: typeof globalThis.fetch; } @@ -28,6 +29,7 @@ function base64url(buffer: ArrayBuffer): string { export class HappyViewSession { readonly did: string; + readonly scopes: string[]; private readonly dpopKey: Key; private readonly accessToken: string; @@ -38,6 +40,7 @@ export class HappyViewSession { constructor(options: HappyViewSessionOptions) { this.did = options.did; + this.scopes = options.scopes; this.dpopKey = options.dpopKey; this.accessToken = options.accessToken; this.clientKey = options.clientKey; diff --git a/packages/oauth-client/src/types.ts b/packages/oauth-client/src/types.ts index c3a0dc9..f611f44 100644 --- a/packages/oauth-client/src/types.ts +++ b/packages/oauth-client/src/types.ts @@ -40,6 +40,7 @@ export interface StoredSession { accessToken: string; clientKey: string; instanceUrl: string; + scopes: string[]; } export interface ProvisionKeyResponse { @@ -50,4 +51,10 @@ export interface ProvisionKeyResponse { export interface RegisterSessionResponse { session_id: string; did: string; + scopes: string[]; +} + +export interface GetSessionResponse { + did: string; + scopes: string[]; }