From 62dede02eaf93a6ca08983bbf84a8a71e67fa6eb Mon Sep 17 00:00:00 2001 From: MeiMei <30769358+mei23@users.noreply.github.com> Date: Sun, 18 Jul 2021 00:53:16 +0900 Subject: [PATCH] =?UTF-8?q?API=20Authenticate=E3=81=A7DB=E6=8E=A5=E7=B6=9A?= =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E3=81=AA=E3=81=A9=E3=81=8C=E7=99=BA?= =?UTF-8?q?=E7=94=9F=E3=81=99=E3=82=8B=E3=81=A8=E3=83=AD=E3=82=B0=E3=82=A2?= =?UTF-8?q?=E3=82=A6=E3=83=88=E3=81=95=E3=81=9B=E3=82=89=E3=82=8C=E3=81=A6?= =?UTF-8?q?=E3=81=97=E3=81=BE=E3=81=86=E3=81=AE=E3=82=92=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?=20Fix=20#7603=20(#7604)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/account.ts | 2 +- src/server/api/api-handler.ts | 18 +++++++++++------- src/server/api/authenticate.ts | 13 ++++++++++--- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/client/account.ts b/src/client/account.ts index 102269a0d..2b860b3dd 100644 --- a/src/client/account.ts +++ b/src/client/account.ts @@ -47,7 +47,7 @@ function fetchAccount(token): Promise { }) .then(res => { // When failed to authenticate user - if (res.status !== 200 && res.status < 500) { + if (res.status >= 400 && res.status < 500) { return signout(); } diff --git a/src/server/api/api-handler.ts b/src/server/api/api-handler.ts index 80a4fd97c..cbace8917 100644 --- a/src/server/api/api-handler.ts +++ b/src/server/api/api-handler.ts @@ -1,7 +1,7 @@ import * as Koa from 'koa'; import { IEndpoint } from './endpoints'; -import authenticate from './authenticate'; +import authenticate, { AuthenticationError } from './authenticate'; import call from './call'; import { ApiError } from './error'; @@ -37,11 +37,15 @@ export default (endpoint: IEndpoint, ctx: Koa.Context) => new Promise((res) => { }).catch((e: ApiError) => { reply(e.httpStatusCode ? e.httpStatusCode : e.kind === 'client' ? 400 : 500, e); }); - }).catch(() => { - reply(403, new ApiError({ - message: 'Authentication failed. Please ensure your token is correct.', - code: 'AUTHENTICATION_FAILED', - id: 'b0a7f5f8-dc2f-4171-b91f-de88ad238e14' - })); + }).catch(e => { + if (e instanceof AuthenticationError) { + reply(403, new ApiError({ + message: 'Authentication failed. Please ensure your token is correct.', + code: 'AUTHENTICATION_FAILED', + id: 'b0a7f5f8-dc2f-4171-b91f-de88ad238e14' + })); + } else { + reply(500, new ApiError()); + } }); }); diff --git a/src/server/api/authenticate.ts b/src/server/api/authenticate.ts index 6ea5a111b..bba4db4ac 100644 --- a/src/server/api/authenticate.ts +++ b/src/server/api/authenticate.ts @@ -8,7 +8,14 @@ import { Cache } from '@/misc/cache'; // ref. https://github.com/typeorm/typeorm/blob/master/docs/caching.md const cache = new Cache(1000 * 60 * 60); -export default async (token: string): Promise<[User | null | undefined, AccessToken | null | undefined]> => { +export class AuthenticationError extends Error { + constructor(message: string) { + super(message); + this.name = 'AuthenticationError'; + } +} + +export default async (token: string): Promise<[User | null | undefined, App | null | undefined]> => { if (token == null) { return [null, null]; } @@ -24,7 +31,7 @@ export default async (token: string): Promise<[User | null | undefined, AccessTo .findOne({ token }); if (user == null) { - throw new Error('user not found'); + throw new AuthenticationError('user not found'); } cache.set(token, user); @@ -41,7 +48,7 @@ export default async (token: string): Promise<[User | null | undefined, AccessTo }); if (accessToken == null) { - throw new Error('invalid signature'); + throw new AuthenticationError('invalid signature'); } AccessTokens.update(accessToken.id, {