Implement MiAuth

This commit is contained in:
syuilo 2020-03-28 11:24:37 +09:00
parent 608b8bb741
commit 6be127e18b
19 changed files with 330 additions and 48 deletions

View file

@ -15,7 +15,7 @@ import signin from './private/signin';
import discord from './service/discord';
import github from './service/github';
import twitter from './service/twitter';
import { Instances } from '../../models';
import { Instances, AccessTokens, Users } from '../../models';
// Init app
const app = new Koa();
@ -73,6 +73,28 @@ router.get('/v1/instance/peers', async ctx => {
ctx.body = instances.map(instance => instance.host);
});
router.post('/miauth/:session/check', async ctx => {
const token = await AccessTokens.findOne({
session: ctx.params.session
});
if (token && !token.fetched) {
AccessTokens.update(token.id, {
fetched: true
});
ctx.body = {
ok: true,
token: token.token,
user: await Users.pack(token.userId, null, { detail: true })
};
} else {
ctx.body = {
ok: false,
};
}
});
// Return 404 for unknown API
router.all('*', async ctx => {
ctx.status = 404;