-
Notifications
You must be signed in to change notification settings - Fork 401
/
token.ts
59 lines (49 loc) · 1.49 KB
/
token.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// https://docs.joinmastodon.org/methods/oauth/#token
import { cors } from 'wildebeest/backend/src/utils/cors'
import * as errors from 'wildebeest/backend/src/errors'
import type { Env } from 'wildebeest/backend/src/types/env'
import { type Database, getDatabase } from 'wildebeest/backend/src/database'
import { readBody } from 'wildebeest/backend/src/utils/body'
import { getClientById } from 'wildebeest/backend/src/mastodon/client'
type Body = {
code: string | null
}
export const onRequest: PagesFunction<Env, any> = async ({ request, env }) => {
return handleRequest(await getDatabase(env), request)
}
export async function handleRequest(db: Database, request: Request): Promise<Response> {
const headers = {
...cors(),
'content-type': 'application/json; charset=utf-8',
}
if (request.method === 'OPTIONS') {
return new Response('', { headers })
}
let data: Body = { code: null }
try {
data = await readBody<Body>(request)
} catch (err) {
// ignore error
}
let code = data.code
if (!code) {
const url = new URL(request.url)
code = url.searchParams.get('code')
}
if (!code) {
return errors.notAuthorized('missing authorization')
}
const parts = code.split('.')
const clientId = parts[0]
const client = await getClientById(db, clientId)
if (client === null) {
return errors.clientUnknown()
}
const res = {
access_token: code,
token_type: 'Bearer',
scope: client.scopes,
created_at: (Date.now() / 1000) | 0,
}
return new Response(JSON.stringify(res), { headers })
}