-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathoauth.tsx
394 lines (366 loc) · 11 KB
/
oauth.tsx
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import { zValidator } from "@hono/zod-validator";
import { eq } from "drizzle-orm";
import { escape } from "es-toolkit";
import { type Context, Hono } from "hono";
import { cors } from "hono/cors";
import { z } from "zod";
import { Layout } from "./components/Layout";
import { db } from "./db";
import { loginRequired } from "./login";
import {
createAccessToken,
createAuthorizationCode,
createClientCredential,
} from "./oauth/helpers";
import type { Variables } from "./oauth/middleware";
import { scopesSchema } from "./oauth/validators";
import {
type Account,
type AccountOwner,
type Application,
type Scope,
accessTokens,
applications,
scopeEnum,
} from "./schema";
import { renderCustomEmojis } from "./text";
import { uuid } from "./uuid";
const app = new Hono<{ Variables: Variables }>();
app.get(
"/authorize",
zValidator(
"query",
z.object({
response_type: z.enum(["code"]),
client_id: z.string(),
redirect_uri: z.string().url(),
scope: scopesSchema.optional(),
state: z.string().optional(),
}),
),
loginRequired,
async (c) => {
const data = c.req.valid("query");
const application = await db.query.applications.findFirst({
where: eq(applications.clientId, data.client_id),
});
if (application == null) return c.json({ error: "invalid_client_id" }, 400);
const scopes = data.scope ?? ["read"];
if (scopes.some((s) => !application.scopes.includes(s))) {
return c.json({ error: "invalid_scope" }, 400);
}
if (!application.redirectUris.includes(data.redirect_uri)) {
return c.json({ error: "invalid_redirect_uri" }, 400);
}
const accountOwners = await db.query.accountOwners.findMany({
with: { account: true },
});
return c.html(
<AuthorizationPage
accountOwners={accountOwners}
application={application}
redirectUri={data.redirect_uri}
scopes={scopes}
state={data.state}
/>,
);
},
);
interface AuthorizationPageProps {
accountOwners: (AccountOwner & { account: Account })[];
application: Application;
redirectUri: string;
scopes: Scope[];
state?: string;
}
function AuthorizationPage(props: AuthorizationPageProps) {
return (
<Layout title={`Hollo: Authorize ${props.application.name}`}>
<hgroup>
<h1>Authorize {props.application.name}</h1>
<p>Do you want to authorize this application to access your account?</p>
</hgroup>
<p>It allows the application to:</p>
<ul>
{props.scopes.map((scope) => (
<li key={scope}>
<code>{scope}</code>
</li>
))}
</ul>
<form action="/oauth/authorize" method="post">
<p>Choose an account to authorize:</p>
{props.accountOwners.map((accountOwner, i) => {
const accountName = renderCustomEmojis(
escape(accountOwner.account.name),
accountOwner.account.emojis,
);
return (
<label>
<input
type="radio"
name="account_id"
value={accountOwner.id}
checked={i === 0}
/>
{/* biome-ignore lint/security/noDangerouslySetInnerHtml: xss protected */}
<strong dangerouslySetInnerHTML={{ __html: accountName }} />
<p style="margin-left: 1.75em; margin-top: 0.25em;">
<small>{accountOwner.account.handle}</small>
</p>
</label>
);
})}
<input
type="hidden"
name="application_id"
value={props.application.id}
/>
<input type="hidden" name="redirect_uri" value={props.redirectUri} />
<input type="hidden" name="scopes" value={props.scopes.join(" ")} />
{props.state != null && (
<input type="hidden" name="state" value={props.state} />
)}
<div role="group">
{props.redirectUri !== "urn:ietf:wg:oauth:2.0:oob" && (
<button
type="submit"
class="secondary"
name="decision"
value="deny"
>
Deny
</button>
)}
<button type="submit" name="decision" value="allow">
Allow
</button>
</div>
</form>
</Layout>
);
}
app.post(
"/authorize",
loginRequired,
zValidator(
"form",
z.object({
account_id: uuid,
application_id: uuid,
redirect_uri: z.string().url(),
scopes: scopesSchema,
state: z.string().optional(),
decision: z.enum(["allow", "deny"]),
}),
),
async (c) => {
const form = c.req.valid("form");
const application = await db.query.applications.findFirst({
where: eq(applications.id, form.application_id),
});
if (application == null) {
return c.notFound();
}
if (form.scopes.some((s) => !application.scopes.includes(s))) {
return c.json({ error: "invalid_scope" }, 400);
}
if (!application.redirectUris.includes(form.redirect_uri)) {
return c.json({ error: "invalid_redirect_uri" }, 400);
}
const url = new URL(form.redirect_uri);
if (form.decision === "deny") {
url.searchParams.set("error", "access_denied");
url.searchParams.set(
"error_description",
"The resource owner or authorization server denied the request.",
);
} else {
const code = await createAuthorizationCode(
application.id,
form.account_id,
form.scopes,
);
if (form.redirect_uri === "urn:ietf:wg:oauth:2.0:oob") {
return c.html(
<AuthorizationCodePage application={application} code={code} />,
);
}
url.searchParams.set("code", code);
if (form.state != null) {
url.searchParams.set("state", form.state);
}
}
return c.redirect(url.href);
},
);
interface AuthorizationCodePageProps {
application: Application;
code: string;
}
function AuthorizationCodePage(props: AuthorizationCodePageProps) {
return (
<Layout title={"Hollo: Authorization Code"}>
<hgroup>
<h1>Authorization Code</h1>
<p>Here is your authorization code.</p>
</hgroup>
<pre>{props.code}</pre>
<p>
Copy this code and paste it into <em>{props.application.name}</em>.
</p>
</Layout>
);
}
const tokenRequestSchema = z.object({
grant_type: z.enum(["authorization_code", "client_credentials"]),
code: z.string().optional(),
client_id: z.string(),
client_secret: z.string(),
redirect_uri: z.string().url().optional(),
scope: scopesSchema.optional(),
});
app.post("/token", cors(), async (c) => {
let form: z.infer<typeof tokenRequestSchema>;
const contentType = c.req.header("Content-Type");
if (
contentType === "application/json" ||
contentType?.match(/^application\/json\s*;/)
) {
const json = await c.req.json();
const result = await tokenRequestSchema.safeParseAsync(json);
if (!result.success) {
return c.json({ error: "Invalid request", zod_error: result.error }, 400);
}
form = result.data;
} else {
const formData = await c.req.parseBody();
const result = await tokenRequestSchema.safeParseAsync(formData);
if (!result.success) {
return c.json({ error: "Invalid request", zod_error: result.error }, 400);
}
form = result.data;
}
const application = await db.query.applications.findFirst({
where: eq(applications.clientId, form.client_id),
});
if (application == null || application.clientSecret !== form.client_secret) {
return c.json(
{
error: "invalid_client",
error_description:
"Client authentication failed due to unknown client, " +
"no client authentication included, or unsupported authentication " +
"method.",
},
401,
);
}
if (form.scope?.some((s) => !application.scopes.includes(s))) {
return c.json(
{
error: "invalid_scope",
error_description:
"The requested scope is invalid, unknown, or malformed.",
},
400,
);
}
if (form.grant_type === "authorization_code") {
if (form.code == null) {
return c.json(
{
error: "invalid_request",
error_description: "The authorization code is required.",
},
400,
);
}
if (!form.redirect_uri) {
return c.json(
{
error: "invalid_request",
error_description:
"The authorization code grant flow requires a redirect URI.",
},
400,
);
}
const token = await db.query.accessTokens.findFirst({
where: eq(accessTokens.code, form.code),
with: { application: true },
});
if (token == null || token.grant_type !== "authorization_code") {
return c.json(
{
error: "invalid_grant",
error_description:
"The provided authorization code is invalid, expired, revoked, " +
"does not match the redirection URI used in the authorization " +
"request, or was issued to another client.",
},
400,
);
}
// Validate that the redirect URI given is registered with the Application
// (since we"re not tracking Access Grants which would bind the redirect URI
// to the code)
if (!token.application.redirectUris.includes(form.redirect_uri)) {
return c.json(
{
error: "invalid_request",
error_description: "Invalid redirect URI.",
},
400,
);
}
const accessToken = await createAccessToken(token);
return c.json({
access_token: accessToken.token,
token_type: accessToken.type,
scope: accessToken.scope,
created_at: accessToken.createdAt,
});
}
if (form.grant_type === "client_credentials") {
const clientCredential = await createClientCredential(
application,
form.scope,
);
return c.json({
access_token: clientCredential.token,
token_type: clientCredential.type,
scope: clientCredential.scope,
created_at: clientCredential.createdAt,
});
}
return c.json(
{
error: "unsupported_grant_type",
error_description:
"The authorization grant type is not supported by the authorization server.",
},
400,
);
});
export async function oauthAuthorizationServer(c: Context) {
const url = new URL(c.req.url);
return c.json({
issuer: new URL("/", url).href,
authorization_endpoint: new URL("/oauth/authorize", url).href,
token_endpoint: new URL("/oauth/token", url).href,
// Not yet supported by Hollo:
// "revocation_endpoint": "",
scopes_supported: scopeEnum.enumValues,
response_types_supported: ["code"],
response_modes_supported: ["query"],
grant_types_supported: ["authorization_code", "client_credentials"],
token_endpoint_auth_methods_supported: [
"client_secret_post",
// Not supported by Hollo:
// "client_secret_basic",
],
app_registration_endpoint: new URL("/api/v1/apps", url).href,
});
}
export default app;