From dcf8efe428a6a0e77f8c2c347f2962936fad3307 Mon Sep 17 00:00:00 2001 From: anonpenguin23 Date: Tue, 28 Oct 2025 13:08:52 +0200 Subject: [PATCH] Refactor AuthClient to streamline imports and add wallet authentication methods including challenge, verify, and getApiKey; improve error logging during logout process. --- src/auth/client.ts | 88 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 80 insertions(+), 8 deletions(-) diff --git a/src/auth/client.ts b/src/auth/client.ts index 7643f8f..509204a 100644 --- a/src/auth/client.ts +++ b/src/auth/client.ts @@ -1,10 +1,5 @@ import { HttpClient } from "../core/http"; -import { - AuthConfig, - WhoAmI, - StorageAdapter, - MemoryStorage, -} from "./types"; +import { AuthConfig, WhoAmI, StorageAdapter, MemoryStorage } from "./types"; export class AuthClient { private httpClient: HttpClient; @@ -75,10 +70,13 @@ export class AuthClient { await this.httpClient.post("/v1/auth/logout", { all: true }); } catch (error) { // Log warning but don't fail - local cleanup is more important - console.warn('Server-side logout failed, continuing with local cleanup:', error); + console.warn( + "Server-side logout failed, continuing with local cleanup:", + error + ); } } - + // Always clear local state this.currentApiKey = undefined; this.currentJwt = undefined; @@ -94,4 +92,78 @@ export class AuthClient { this.httpClient.setJwt(undefined); await this.storage.clear(); } + + /** + * Request a challenge nonce for wallet authentication + */ + async challenge(params: { + wallet: string; + purpose?: string; + namespace?: string; + }): Promise<{ + nonce: string; + wallet: string; + namespace: string; + expires_at: string; + }> { + const response = await this.httpClient.post("/v1/auth/challenge", { + wallet: params.wallet, + purpose: params.purpose || "authentication", + namespace: params.namespace || "default", + }); + return response; + } + + /** + * Verify wallet signature and get JWT token + */ + async verify(params: { + wallet: string; + nonce: string; + signature: string; + namespace?: string; + }): Promise<{ + access_token: string; + refresh_token: string; + subject: string; + namespace: string; + }> { + const response = await this.httpClient.post("/v1/auth/verify", { + wallet: params.wallet, + nonce: params.nonce, + signature: params.signature, + namespace: params.namespace || "default", + }); + + // Automatically set the JWT + this.setJwt(response.access_token); + + return response; + } + + /** + * Get API key for wallet (creates namespace ownership) + */ + async getApiKey(params: { + wallet: string; + nonce: string; + signature: string; + namespace?: string; + }): Promise<{ + api_key: string; + namespace: string; + wallet: string; + }> { + const response = await this.httpClient.post("/v1/auth/api-key", { + wallet: params.wallet, + nonce: params.nonce, + signature: params.signature, + namespace: params.namespace || "default", + }); + + // Automatically set the API key + this.setApiKey(response.api_key); + + return response; + } }