| 1 | export class AuthClient |
| 2 | { |
| 3 | constructor(config) |
| 4 | { |
| 5 | this.config = config; |
| 6 | this.cache = new Map(); |
| 7 | } |
| 8 | |
| 9 | async exchangeCode ({ code, redirectUri }) |
| 10 | { |
| 11 | const body = new URLSearchParams({ |
| 12 | grant_type: "authorization_code", |
| 13 | code, |
| 14 | redirect_uri: redirectUri, |
| 15 | client_id: this.config.clientId, |
| 16 | client_secret: this.config.clientSecret |
| 17 | }); |
| 18 | const response = await fetch(`${this.config.baseUrl}/oauth/token`, { |
| 19 | method: "POST", |
| 20 | headers: { |
| 21 | "Content-Type": "application/x-www-form-urlencoded", |
| 22 | "Accept": "application/json", |
| 23 | "User-Agent": this.config.userAgent |
| 24 | }, |
| 25 | body |
| 26 | }); |
| 27 | const payload = await response.json().catch(() => ({})); |
| 28 | if (!response.ok) |
| 29 | { |
| 30 | const error = new Error(payload.error ?? "Token exchange failed."); |
| 31 | error.status = response.status; |
| 32 | throw error; |
| 33 | } |
| 34 | return payload; |
| 35 | } |
| 36 | |
| 37 | async fetchMe (accessToken) |
| 38 | { |
| 39 | const cached = this.cache.get(accessToken); |
| 40 | if (cached && cached.expiresAt > Date.now()) |
| 41 | { return cached.user; } |
| 42 | |
| 43 | const response = await fetch(`${this.config.baseUrl}/api/me`, { |
| 44 | headers: { |
| 45 | "Authorization": `Bearer ${accessToken}`, |
| 46 | "Accept": "application/json", |
| 47 | "User-Agent": this.config.userAgent |
| 48 | }, |
| 49 | signal: AbortSignal.timeout(15_000) |
| 50 | }); |
| 51 | const payload = await response.json().catch(() => ({})); |
| 52 | if (!response.ok) |
| 53 | { |
| 54 | this.cache.delete(accessToken); |
| 55 | return null; |
| 56 | } |
| 57 | const user = payload.user ?? null; |
| 58 | if (user) |
| 59 | { |
| 60 | this.cache.set(accessToken, { |
| 61 | user, |
| 62 | expiresAt: Date.now() + 60_000 |
| 63 | }); |
| 64 | } |
| 65 | return user; |
| 66 | } |
| 67 | |
| 68 | async extendSession (accessToken) |
| 69 | { |
| 70 | const response = await fetch(`${this.config.baseUrl}/api/session/extend`, { |
| 71 | method: "POST", |
| 72 | headers: { |
| 73 | "Authorization": `Bearer ${accessToken}`, |
| 74 | "Accept": "application/json", |
| 75 | "User-Agent": this.config.userAgent |
| 76 | } |
| 77 | }); |
| 78 | if (!response.ok) |
| 79 | { return false; } |
| 80 | this.cache.delete(accessToken); |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | async revokeToken (accessToken) |
| 85 | { |
| 86 | this.cache.delete(accessToken); |
| 87 | if (!accessToken) |
| 88 | { return; } |
| 89 | await fetch(`${this.config.baseUrl}/api/logout`, { |
| 90 | method: "POST", |
| 91 | headers: { |
| 92 | "Authorization": `Bearer ${accessToken}`, |
| 93 | "Accept": "application/json", |
| 94 | "User-Agent": this.config.userAgent |
| 95 | } |
| 96 | }).catch(() => { }); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | export function authSubjectForUser (user) |
| 101 | { |
| 102 | return `auth:${user.id}`; |
| 103 | } |
| 104 | |
| 105 | export function identityFromUser (user, accessToken = null) |
| 106 | { |
| 107 | const rawEmail = user?.email; |
| 108 | const email = typeof rawEmail === "string" && rawEmail.trim() !== "" ? rawEmail.trim() : null; |
| 109 | const rawUsername = user?.username; |
| 110 | const username = typeof rawUsername === "string" && rawUsername.trim() !== "" ? rawUsername.trim() : null; |
| 111 | return { |
| 112 | authSubject: authSubjectForUser(user), |
| 113 | // Auth no longer shares email; keep null-tolerant for older tokens / stored sessions. |
| 114 | email, |
| 115 | username, |
| 116 | accessToken |
| 117 | }; |
| 118 | } |