Add OAuth controller

This commit is contained in:
Alex Gleason 2023-03-04 21:36:53 -06:00
parent 8ed662321d
commit b36b8ea7d2
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 29 additions and 3 deletions

22
src/api/oauth.ts Normal file
View File

@ -0,0 +1,22 @@
import { validator, z } from '@/deps.ts';
const createTokenSchema = z.object({
password: z.string(),
});
const createTokenController = validator('json', (value, c) => {
const result = createTokenSchema.safeParse(value);
if (result.success) {
return c.json({
access_token: result.data.password,
token_type: 'Bearer',
scope: 'read write follow push',
created_at: Math.floor(new Date().getTime() / 1000),
});
} else {
return c.json({ error: 'Invalid request' }, 400);
}
});
export { createTokenController };

View File

@ -1,7 +1,8 @@
import { Hono } from '@/deps.ts';
import { appVerifyCredentials, createAppController } from './api/apps.ts';
import { appVerifyCredentials, createAppController } from './api/apps.ts';
import instanceController from './api/instance.ts';
import { createTokenController } from './api/oauth.ts';
const app = new Hono();
@ -10,4 +11,6 @@ app.get('/api/v1/instance', instanceController);
app.get('/api/v1/apps/verify_credentials', appVerifyCredentials);
app.post('/api/v1/apps', createAppController);
app.post('/oauth/token', createTokenController);
export default app;

View File

@ -1,3 +1,4 @@
import { Context, Hono } from 'https://deno.land/x/hono@v3.0.2/mod.ts';
export { Hono };
import { Context, Hono, validator } from 'https://deno.land/x/hono@v3.0.2/mod.ts';
export { Hono, validator };
export { z } from 'https://deno.land/x/zod@v3.20.5/mod.ts';
export type { Context };