Skip to content

Commit

Permalink
fix: Add basic user-agent to all mastodon-bound requests (#2277)
Browse files Browse the repository at this point in the history
  • Loading branch information
untitaker authored Nov 27, 2024
1 parent 008248e commit 302da09
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions server/api/[server]/oauth/[origin].ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { stringifyQuery } from 'ufo'

import { defaultUserAgent } from '~/server/utils/shared'

export default defineEventHandler(async (event) => {
let { server, origin } = getRouterParams(event)
server = server.toLocaleLowerCase().trim()
Expand All @@ -24,6 +26,9 @@ export default defineEventHandler(async (event) => {
try {
const result: any = await $fetch(`https://${server}/oauth/token`, {
method: 'POST',
headers: {
'user-agent': defaultUserAgent,
},
body: {
client_id: app.client_id,
client_secret: app.client_secret,
Expand Down
6 changes: 6 additions & 0 deletions server/utils/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { env } from '#build-info'
// @ts-expect-error virtual import
import { driver } from '#storage-config'

import { version } from '~/config/env'
import { APP_NAME } from '~/constants'
import type { AppInfo } from '~/types'

Expand Down Expand Up @@ -48,9 +49,14 @@ export function getRedirectURI(origin: string, server: string) {
return `${origin}/api/${server}/oauth/${encodeURIComponent(origin)}`
}

export const defaultUserAgent = `${APP_NAME}/${version}`

async function fetchAppInfo(origin: string, server: string) {
const app: AppInfo = await $fetch(`https://${server}/api/v1/apps`, {
method: 'POST',
headers: {
'user-agent': defaultUserAgent,
},
body: {
client_name: APP_NAME + (env !== 'release' ? ` (${env})` : ''),
website: 'https://elk.zone',
Expand Down

0 comments on commit 302da09

Please sign in to comment.