diff options
Diffstat (limited to 'packages/backend/src/server/api/integration/GithubServerService.ts')
| -rw-r--r-- | packages/backend/src/server/api/integration/GithubServerService.ts | 99 |
1 files changed, 43 insertions, 56 deletions
diff --git a/packages/backend/src/server/api/integration/GithubServerService.ts b/packages/backend/src/server/api/integration/GithubServerService.ts index 98d6230749..2fd20bf831 100644 --- a/packages/backend/src/server/api/integration/GithubServerService.ts +++ b/packages/backend/src/server/api/integration/GithubServerService.ts @@ -1,9 +1,9 @@ import { Inject, Injectable } from '@nestjs/common'; import Redis from 'ioredis'; -import Router from '@koa/router'; import { OAuth2 } from 'oauth'; import { v4 as uuid } from 'uuid'; import { IsNull } from 'typeorm'; +import { FastifyInstance, FastifyRequest, FastifyReply, FastifyPluginOptions } from 'fastify'; import type { Config } from '@/config.js'; import type { UserProfilesRepository, UsersRepository } from '@/models/index.js'; import { DI } from '@/di-symbols.js'; @@ -12,8 +12,8 @@ import type { ILocalUser } from '@/models/entities/User.js'; import { GlobalEventService } from '@/core/GlobalEventService.js'; import { MetaService } from '@/core/MetaService.js'; import { UserEntityService } from '@/core/entities/UserEntityService.js'; +import { FastifyReplyError } from '@/misc/fastify-reply-error.js'; import { SigninService } from '../SigninService.js'; -import type Koa from 'koa'; @Injectable() export class GithubServerService { @@ -36,21 +36,18 @@ export class GithubServerService { private metaService: MetaService, private signinService: SigninService, ) { + this.create = this.create.bind(this); } - public create() { - const router = new Router(); - - router.get('/disconnect/github', async ctx => { - if (!this.compareOrigin(ctx)) { - ctx.throw(400, 'invalid origin'); - return; + public create(fastify: FastifyInstance, options: FastifyPluginOptions, done: (err?: Error) => void) { + fastify.get('/disconnect/github', async (request, reply) => { + if (!this.compareOrigin(request)) { + throw new FastifyReplyError(400, 'invalid origin'); } - const userToken = this.getUserToken(ctx); + const userToken = this.getUserToken(request); if (!userToken) { - ctx.throw(400, 'signin required'); - return; + throw new FastifyReplyError(400, 'signin required'); } const user = await this.usersRepository.findOneByOrFail({ @@ -66,13 +63,13 @@ export class GithubServerService { integrations: profile.integrations, }); - ctx.body = 'GitHubの連携を解除しました :v:'; - // Publish i updated event this.globalEventService.publishMainStream(user.id, 'meUpdated', await this.userEntityService.pack(user, user, { detail: true, includeSecrets: true, })); + + return 'GitHubの連携を解除しました :v:'; }); const getOath2 = async () => { @@ -90,16 +87,14 @@ export class GithubServerService { } }; - router.get('/connect/github', async ctx => { - if (!this.compareOrigin(ctx)) { - ctx.throw(400, 'invalid origin'); - return; + fastify.get('/connect/github', async (request, reply) => { + if (!this.compareOrigin(request)) { + throw new FastifyReplyError(400, 'invalid origin'); } - const userToken = this.getUserToken(ctx); + const userToken = this.getUserToken(request); if (!userToken) { - ctx.throw(400, 'signin required'); - return; + throw new FastifyReplyError(400, 'signin required'); } const params = { @@ -111,10 +106,10 @@ export class GithubServerService { this.redisClient.set(userToken, JSON.stringify(params)); const oauth2 = await getOath2(); - ctx.redirect(oauth2!.getAuthorizeUrl(params)); + reply.redirect(oauth2!.getAuthorizeUrl(params)); }); - router.get('/signin/github', async ctx => { + fastify.get('/signin/github', async (request, reply) => { const sessid = uuid(); const params = { @@ -123,7 +118,7 @@ export class GithubServerService { state: uuid(), }; - ctx.cookies.set('signin_with_github_sid', sessid, { + reply.cookies.set('signin_with_github_sid', sessid, { path: '/', secure: this.config.url.startsWith('https'), httpOnly: true, @@ -132,27 +127,25 @@ export class GithubServerService { this.redisClient.set(sessid, JSON.stringify(params)); const oauth2 = await getOath2(); - ctx.redirect(oauth2!.getAuthorizeUrl(params)); + reply.redirect(oauth2!.getAuthorizeUrl(params)); }); - router.get('/gh/cb', async ctx => { - const userToken = this.getUserToken(ctx); + fastify.get('/gh/cb', async (request, reply) => { + const userToken = this.getUserToken(request); const oauth2 = await getOath2(); if (!userToken) { - const sessid = ctx.cookies.get('signin_with_github_sid'); + const sessid = request.cookies.get('signin_with_github_sid'); if (!sessid) { - ctx.throw(400, 'invalid session'); - return; + throw new FastifyReplyError(400, 'invalid session'); } - const code = ctx.query.code; + const code = request.query.code; if (!code || typeof code !== 'string') { - ctx.throw(400, 'invalid session'); - return; + throw new FastifyReplyError(400, 'invalid session'); } const { redirect_uri, state } = await new Promise<any>((res, rej) => { @@ -162,9 +155,8 @@ export class GithubServerService { }); }); - if (ctx.query.state !== state) { - ctx.throw(400, 'invalid session'); - return; + if (request.query.state !== state) { + throw new FastifyReplyError(400, 'invalid session'); } const { accessToken } = await new Promise<{ accessToken: string }>((res, rej) => @@ -184,8 +176,7 @@ export class GithubServerService { 'Authorization': `bearer ${accessToken}`, })) as Record<string, unknown>; if (typeof login !== 'string' || typeof id !== 'string') { - ctx.throw(400, 'invalid session'); - return; + throw new FastifyReplyError(400, 'invalid session'); } const link = await this.userProfilesRepository.createQueryBuilder() @@ -194,17 +185,15 @@ export class GithubServerService { .getOne(); if (link == null) { - ctx.throw(404, `@${login}と連携しているMisskeyアカウントはありませんでした...`); - return; + throw new FastifyReplyError(404, `@${login}と連携しているMisskeyアカウントはありませんでした...`); } - this.signinService.signin(ctx, await this.usersRepository.findOneBy({ id: link.userId }) as ILocalUser, true); + return this.signinService.signin(request, reply, await this.usersRepository.findOneBy({ id: link.userId }) as ILocalUser, true); } else { - const code = ctx.query.code; + const code = request.query.code; if (!code || typeof code !== 'string') { - ctx.throw(400, 'invalid session'); - return; + throw new FastifyReplyError(400, 'invalid session'); } const { redirect_uri, state } = await new Promise<any>((res, rej) => { @@ -214,9 +203,8 @@ export class GithubServerService { }); }); - if (ctx.query.state !== state) { - ctx.throw(400, 'invalid session'); - return; + if (request.query.state !== state) { + throw new FastifyReplyError(400, 'invalid session'); } const { accessToken } = await new Promise<{ accessToken: string }>((res, rej) => @@ -238,8 +226,7 @@ export class GithubServerService { })) as Record<string, unknown>; if (typeof login !== 'string' || typeof id !== 'string') { - ctx.throw(400, 'invalid session'); - return; + throw new FastifyReplyError(400, 'invalid session'); } const user = await this.usersRepository.findOneByOrFail({ @@ -260,29 +247,29 @@ export class GithubServerService { }, }); - ctx.body = `GitHub: @${login} を、Misskey: @${user.username} に接続しました!`; - // Publish i updated event this.globalEventService.publishMainStream(user.id, 'meUpdated', await this.userEntityService.pack(user, user, { detail: true, includeSecrets: true, })); + + return `GitHub: @${login} を、Misskey: @${user.username} に接続しました!`; } }); - return router; + done(); } - private getUserToken(ctx: Koa.BaseContext): string | null { - return ((ctx.headers['cookie'] ?? '').match(/igi=(\w+)/) ?? [null, null])[1]; + private getUserToken(request: FastifyRequest): string | null { + return ((request.headers['cookie'] ?? '').match(/igi=(\w+)/) ?? [null, null])[1]; } - private compareOrigin(ctx: Koa.BaseContext): boolean { + private compareOrigin(request: FastifyRequest): boolean { function normalizeUrl(url?: string): string { return url ? url.endsWith('/') ? url.substr(0, url.length - 1) : url : ''; } - const referer = ctx.headers['referer']; + const referer = request.headers['referer']; return (normalizeUrl(referer) === normalizeUrl(this.config.url)); } |