fix: don't use full host url in storage key

This commit is contained in:
Daniel Roe 2022-11-27 18:22:39 +00:00
parent 0192d3eeb7
commit f1a5108a06
No known key found for this signature in database
GPG key ID: 22D5008E4F5D9B55
3 changed files with 13 additions and 11 deletions

View file

@ -1,5 +1,5 @@
import { stringifyQuery } from 'ufo'
import { HOST_DOMAIN, getApp } from '~/server/shared'
import { HOST_DOMAIN, HOST_URL, getApp } from '~/server/shared'
export default defineEventHandler(async (event) => {
const server = event.context.params.server
@ -13,7 +13,7 @@ export default defineEventHandler(async (event) => {
const query = stringifyQuery({
client_id: app.client_id,
scope: 'read write follow push',
redirect_uri: `${HOST_DOMAIN}/api/${server}/oauth`,
redirect_uri: `${HOST_URL}/api/${server}/oauth`,
response_type: 'code',
})
const url = `https://${server}/oauth/authorize?${query}`

View file

@ -1,5 +1,5 @@
import { stringifyQuery } from 'vue-router'
import { HOST_DOMAIN, getApp } from '~/server/shared'
import { HOST_DOMAIN, HOST_URL, getApp } from '~/server/shared'
export default defineEventHandler(async (event) => {
const server = event.context.params.server
@ -17,13 +17,13 @@ export default defineEventHandler(async (event) => {
body: {
client_id: app.client_id,
client_secret: app.client_secret,
redirect_uri: `${HOST_DOMAIN}/api/${server}/oauth`,
redirect_uri: `${HOST_URL}/api/${server}/oauth`,
grant_type: 'authorization_code',
code,
scope: 'read write follow push',
},
})
const url = `${HOST_DOMAIN}/signin/callback?${stringifyQuery({ server, token: result.access_token })}`
const url = `${HOST_URL}/signin/callback?${stringifyQuery({ server, token: result.access_token })}`
await sendRedirect(event, url, 302)
})

View file

@ -3,6 +3,7 @@ import _fs from 'unstorage/drivers/fs'
// @ts-expect-error unstorage needs to provide backwards-compatible subpath types
import _kv from 'unstorage/drivers/cloudflare-kv-http'
import { isCI } from 'std-env'
import { parseURL } from 'ufo'
import { $fetch } from 'ohmyfetch'
import type { Storage } from 'unstorage'
@ -13,8 +14,9 @@ import type { AppInfo } from '~/types'
import { APP_NAME } from '~/constants'
const runtimeConfig = useRuntimeConfig()
export const HOST_DOMAIN = runtimeConfig.deployUrl
export const HOST_URL = runtimeConfig.deployUrl
|| (process.dev || !isCI ? 'http://localhost:5314' : 'https://elk.zone')
export const HOST_DOMAIN = parseURL(HOST_URL).host!
const fs = _fs as typeof import('unstorage/dist/drivers/fs')['default']
const kv = _kv as typeof import('unstorage/dist/drivers/cloudflare-kv-http')['default']
@ -33,10 +35,10 @@ else {
})))
}
async function fetchAppInfo(host: string, server: string) {
async function fetchAppInfo(server: string) {
const redirect_uris = [
'urn:ietf:wg:oauth:2.0:oob',
`${host}/api/${server}/oauth`,
`${HOST_URL}/api/${server}/oauth`,
].join('\n')
const app: AppInfo = await $fetch(`https://${server}/api/v1/apps`, {
@ -54,11 +56,11 @@ const serverKey = (host: string, server: string) => `servers:${host}:${server}.j
export async function getApp(host: string, server: string) {
const key = serverKey(host, server)
if (await storage.hasItem(key))
return storage.getItem(key) as Promise<AppInfo>
try {
const appInfo = await fetchAppInfo(host, server)
if (await storage.hasItem(key))
return await storage.getItem(key) as Promise<AppInfo>
const appInfo = await fetchAppInfo(server)
await storage.setItem(key, appInfo)
return appInfo
}