Skip to content

Commit

Permalink
Fix server config updating (#974)
Browse files Browse the repository at this point in the history
  • Loading branch information
sceuick authored Jul 14, 2024
1 parent 4f694af commit fe77c5f
Show file tree
Hide file tree
Showing 14 changed files with 226 additions and 173 deletions.
4 changes: 2 additions & 2 deletions common/types/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export namespace AppSchema {
imagesHost: string
imagesModels: ImageModel[]

googleClientId: string

ttsHost: string
ttsApiKey: string
ttsAccess: 'off' | 'users' | 'subscribers' | 'admins'
Expand Down Expand Up @@ -174,8 +176,6 @@ export namespace AppSchema {
/** @todo remove after next deployment */
tier?: AppSchema.SubscriptionTier
serverConfig?: Configuration

googleClientId: string | undefined
}

export type ChatMode = 'standard' | 'adventure'
Expand Down
1 change: 1 addition & 0 deletions srv/api/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const updateConfiguration = handle(async ({ body }) => {
ttsApiKey: 'string?',
imagesModels: ['any'],
supportEmail: 'string',
googleClientId: 'string',
},
body
)
Expand Down
2 changes: 0 additions & 2 deletions srv/api/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ export async function getAppConfig(user?: AppSchema.User) {
openRouter: { models: openRouter },
subs,
serverConfig: configuration,
googleClientId:
!!config.google.clientId && !!config.google.secret ? config.google.clientId : undefined,
}
}

Expand Down
25 changes: 19 additions & 6 deletions srv/api/user/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { OAuthScope, oauthScopes } from '/common/types'
import { patreon } from './patreon'
import { getSafeUserConfig } from './settings'
import { OAuth2Client } from 'google-auth-library'
import { config } from '/srv/config'
import { createAccessToken, toSafeUser } from '/srv/db/user'

const GOOGLE = new OAuth2Client()
Expand All @@ -31,13 +30,15 @@ export const login = handle(async (req) => {
export const oathGoogleLogin = handle(async ({ log, body }) => {
assertValid({ token: 'string' }, body)

if (!config.google.secret) {
const config = await store.admin.getServerConfiguration().catch(() => undefined)

if (!config?.googleClientId) {
throw new StatusError('Not allowed', 405)
}

const token = await GOOGLE.verifyIdToken({
idToken: body.token,
audience: config.google.clientId,
audience: config.googleClientId,
})

const payload = token.getPayload()
Expand All @@ -63,21 +64,33 @@ export const oathGoogleLogin = handle(async ({ log, body }) => {
})

export const unlinkGoogleAccount = handle(async ({ userId }) => {
const user = await store.users.updateUser(userId, { google: null as any })
return { user }
const user = await store.users.getUser(userId)
if (!user) throw new StatusError('User not found', 404)

if (!user.google?.sub) throw new StatusError('Account not linked with Google', 400)
if (user.google.sub === user._id)
throw new StatusError('Account registered using Google - Cannot be unlinked', 400)

const next = await store.users.updateUser(userId, { google: null as any })
return { user: next }
})

export const linkGoogleAccount = handle(async ({ body, userId }) => {
assertValid({ token: 'string' }, body)

const user = await store.users.getUser(userId)
const config = await store.admin.getServerConfiguration().catch(() => undefined)

if (!config?.googleClientId) {
throw new StatusError('Not allowed', 405)
}

if (!user) throw new StatusError('Unauthorized: Account not found', 401)
if (user.google?.sub) throw new StatusError('Account already linked to Google', 400)

const token = await GOOGLE.verifyIdToken({
idToken: body.token,
audience: config.google.clientId,
audience: config.googleClientId,
})

const payload = token.getPayload()
Expand Down
5 changes: 0 additions & 5 deletions srv/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,6 @@ export const config = {
chatCounts: !!env('CHAT_COUNTS', ''),
},

google: {
clientId: env('GOOGLE_CLIENT_ID', ''),
secret: env('GOOGLE_CLIENT_SECRET', ''),
},

patreon: {
redirect: env('PATREON_REDIRECT_URI', 'http://localhost:1234/oauth/patreon'),
campaign_id: env('PATREON_CAMPAIGN_ID', ''),
Expand Down
1 change: 1 addition & 0 deletions srv/db/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export async function getServerConfiguration() {
ttsHost: '',
maxGuidanceTokens: 1000,
maxGuidanceVariables: 15,
googleClientId: '',
}

await db('configuration').insertOne(next)
Expand Down
Loading

0 comments on commit fe77c5f

Please sign in to comment.