parent
6d1d7b5366
commit
62dede02ea
|
@ -47,7 +47,7 @@ function fetchAccount(token): Promise<Account> {
|
||||||
})
|
})
|
||||||
.then(res => {
|
.then(res => {
|
||||||
// When failed to authenticate user
|
// When failed to authenticate user
|
||||||
if (res.status !== 200 && res.status < 500) {
|
if (res.status >= 400 && res.status < 500) {
|
||||||
return signout();
|
return signout();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import * as Koa from 'koa';
|
import * as Koa from 'koa';
|
||||||
|
|
||||||
import { IEndpoint } from './endpoints';
|
import { IEndpoint } from './endpoints';
|
||||||
import authenticate from './authenticate';
|
import authenticate, { AuthenticationError } from './authenticate';
|
||||||
import call from './call';
|
import call from './call';
|
||||||
import { ApiError } from './error';
|
import { ApiError } from './error';
|
||||||
|
|
||||||
|
@ -37,11 +37,15 @@ export default (endpoint: IEndpoint, ctx: Koa.Context) => new Promise((res) => {
|
||||||
}).catch((e: ApiError) => {
|
}).catch((e: ApiError) => {
|
||||||
reply(e.httpStatusCode ? e.httpStatusCode : e.kind === 'client' ? 400 : 500, e);
|
reply(e.httpStatusCode ? e.httpStatusCode : e.kind === 'client' ? 400 : 500, e);
|
||||||
});
|
});
|
||||||
}).catch(() => {
|
}).catch(e => {
|
||||||
reply(403, new ApiError({
|
if (e instanceof AuthenticationError) {
|
||||||
message: 'Authentication failed. Please ensure your token is correct.',
|
reply(403, new ApiError({
|
||||||
code: 'AUTHENTICATION_FAILED',
|
message: 'Authentication failed. Please ensure your token is correct.',
|
||||||
id: 'b0a7f5f8-dc2f-4171-b91f-de88ad238e14'
|
code: 'AUTHENTICATION_FAILED',
|
||||||
}));
|
id: 'b0a7f5f8-dc2f-4171-b91f-de88ad238e14'
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
reply(500, new ApiError());
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -8,7 +8,14 @@ import { Cache } from '@/misc/cache';
|
||||||
// ref. https://github.com/typeorm/typeorm/blob/master/docs/caching.md
|
// ref. https://github.com/typeorm/typeorm/blob/master/docs/caching.md
|
||||||
const cache = new Cache<User>(1000 * 60 * 60);
|
const cache = new Cache<User>(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) {
|
if (token == null) {
|
||||||
return [null, null];
|
return [null, null];
|
||||||
}
|
}
|
||||||
|
@ -24,7 +31,7 @@ export default async (token: string): Promise<[User | null | undefined, AccessTo
|
||||||
.findOne({ token });
|
.findOne({ token });
|
||||||
|
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
throw new Error('user not found');
|
throw new AuthenticationError('user not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
cache.set(token, user);
|
cache.set(token, user);
|
||||||
|
@ -41,7 +48,7 @@ export default async (token: string): Promise<[User | null | undefined, AccessTo
|
||||||
});
|
});
|
||||||
|
|
||||||
if (accessToken == null) {
|
if (accessToken == null) {
|
||||||
throw new Error('invalid signature');
|
throw new AuthenticationError('invalid signature');
|
||||||
}
|
}
|
||||||
|
|
||||||
AccessTokens.update(accessToken.id, {
|
AccessTokens.update(accessToken.id, {
|
||||||
|
|
Loading…
Reference in New Issue