From 09f813403fde0b0d9cb67e40d1eeac7de52dbf02 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 23 Oct 2024 12:58:27 -0500 Subject: [PATCH] Actually do import the token into the auth app, fix applicationSchema and authAppSchema --- src/reducers/auth.ts | 13 +++++++++++++ src/schemas/application.ts | 11 ++++++----- src/schemas/soapbox/soapbox-auth.ts | 8 +++++++- src/schemas/token.ts | 2 ++ 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/reducers/auth.ts b/src/reducers/auth.ts index d5bc456a1..b5145a340 100644 --- a/src/reducers/auth.ts +++ b/src/reducers/auth.ts @@ -15,6 +15,7 @@ import { SWITCH_ACCOUNT, VERIFY_CREDENTIALS_SUCCESS, VERIFY_CREDENTIALS_FAIL, + AUTH_APP_AUTHORIZED, } from '../actions/auth'; import { ME_FETCH_SKIP } from '../actions/me'; @@ -143,6 +144,18 @@ function reducer(state: SoapboxAuth, action: UnknownAction): SoapboxAuth { const result = applicationSchema.safeParse(action.app); return result.success ? importApplication(state, result.data) : state; } + case AUTH_APP_AUTHORIZED: { + const result = tokenSchema.safeParse(action.token); + if (result.success) { + return produce(state, draft => { + if (draft.app) { + draft.app.access_token = result.data.access_token; + } + }); + } else { + return state; + } + } case AUTH_LOGGED_IN: { const result = tokenSchema.safeParse(action.token); return result.success ? importToken(state, result.data) : state; diff --git a/src/schemas/application.ts b/src/schemas/application.ts index a94ee1066..7366789bf 100644 --- a/src/schemas/application.ts +++ b/src/schemas/application.ts @@ -1,18 +1,19 @@ import { z } from 'zod'; const applicationSchema = z.object({ - name: z.string(), + name: z.string().catch(''), website: z.string().url().nullable().catch(null), scopes: z.string().array().catch([]), redirect_uris: z.string().url().array().optional().catch(undefined), redirect_uri: z.string().url().optional().catch(undefined), + client_id: z.string().optional().catch(undefined), + client_secret: z.string().optional().catch(undefined), + client_secret_expires_at: z.number().optional().catch(0), }).transform((app) => { - const { name, website, scopes, redirect_uris, redirect_uri } = app; + const { redirect_uris, redirect_uri, ...rest } = app; return { - name, - website, - scopes, + ...rest, redirect_uris: redirect_uris || (redirect_uri ? [redirect_uri] : []), }; }); diff --git a/src/schemas/soapbox/soapbox-auth.ts b/src/schemas/soapbox/soapbox-auth.ts index 9fa3ef798..e51a8f794 100644 --- a/src/schemas/soapbox/soapbox-auth.ts +++ b/src/schemas/soapbox/soapbox-auth.ts @@ -9,8 +9,14 @@ const authUserSchema = z.object({ url: z.string().url(), }); +const authAppSchema = applicationSchema.and( + z.object({ + access_token: z.string().optional().catch(undefined), + }), +); + const soapboxAuthSchema = z.object({ - app: applicationSchema.optional(), + app: authAppSchema.optional(), tokens: z.record(z.string(), tokenSchema), users: z.record(z.string(), authUserSchema), me: z.string().url().optional(), diff --git a/src/schemas/token.ts b/src/schemas/token.ts index b32019a5a..2feaf30bd 100644 --- a/src/schemas/token.ts +++ b/src/schemas/token.ts @@ -5,6 +5,8 @@ const tokenSchema = z.object({ token_type: z.string(), scope: z.string(), created_at: z.number(), + id: z.coerce.string().optional().catch(undefined), // Pleroma (primary key) + me: z.string().url().optional().catch(undefined), // Pleroma (ActivityPub ID of user) }); type Token = z.infer;