From f1a5108a06e9290f0b8d156e50a55698a1871aa8 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 27 Nov 2022 18:22:39 +0000 Subject: [PATCH] fix: don't use full host url in storage key --- server/api/[server]/login.ts | 4 ++-- server/api/[server]/oauth.ts | 6 +++--- server/shared.ts | 14 ++++++++------ 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/server/api/[server]/login.ts b/server/api/[server]/login.ts index 8d8101af..1ae8fe9f 100644 --- a/server/api/[server]/login.ts +++ b/server/api/[server]/login.ts @@ -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}` diff --git a/server/api/[server]/oauth.ts b/server/api/[server]/oauth.ts index cc7168cc..95df8f8a 100644 --- a/server/api/[server]/oauth.ts +++ b/server/api/[server]/oauth.ts @@ -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) }) diff --git a/server/shared.ts b/server/shared.ts index f0f55d18..b3f6fa02 100644 --- a/server/shared.ts +++ b/server/shared.ts @@ -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 try { - const appInfo = await fetchAppInfo(host, server) + if (await storage.hasItem(key)) + return await storage.getItem(key) as Promise + const appInfo = await fetchAppInfo(server) await storage.setItem(key, appInfo) return appInfo }