0
0
Fork 0
This commit is contained in:
Xeltica 2020-08-04 12:13:41 +09:00
commit 326384957c
22 changed files with 4176 additions and 0 deletions

58
src/router.ts Normal file
View file

@ -0,0 +1,58 @@
import { Context, DefaultState } from 'koa';
import Router from 'koa-router';
import { die } from './die';
import { v4 as uuid } from 'uuid';
import { config } from './config';
import axios from 'axios';
export const router = new Router<DefaultState, Context>();
export const sessionHostCache: Record<string, string> = { };
router.get('/', async ctx => {
await ctx.render('index');
});
router.get('/login', async ctx => {
const host = ctx.query.host as string | undefined;
if (!host) {
await die(ctx, 'ホストを空欄にしてはいけない');
return;
}
const session = uuid();
const name = encodeURI('みす廃あらーと');
const permission = encodeURI('write:notes');
const callback = encodeURI(`${config.url}/miauth`);
const url = `https://${host}/miauth/${session}?name=${name}&callback=${callback}&permission=${permission}`;
sessionHostCache[session] = host;
ctx.redirect(url);
});
router.get('/miauth', async ctx => {
const session = ctx.query.session as string | undefined;
if (!session) {
await die(ctx, 'セッションが見つからなかった');
return;
}
const host = sessionHostCache[session];
if (!host) {
await die(ctx, '問題が発生しました。お手数ですが、最初からやり直してください。');
}
const url = `https://${host}/api/miauth/${session}/check`;
const { token, user } = (await axios.post(url)).data;
ctx.body = { ok: true, user };
});
router.get('/legacy-auth', async ctx => {
await die(ctx, 'coming soon');
});
// Return 404 for other pages
router.all('(.*)', async ctx => {
ctx.status = 404;
await die(ctx, 'ページが見つかりませんでした');
});