Merge branch 'migrate-accounts-to-idb' into sw-notification-action

This commit is contained in:
tamaina 2021-07-19 15:53:49 +09:00
commit e436d2e47d
33 changed files with 500 additions and 152 deletions

View file

@ -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());
}
});
});

View file

@ -2,36 +2,30 @@ import isNativeToken from './common/is-native-token';
import { User } from '../../models/entities/user';
import { Users, AccessTokens, Apps } from '../../models';
import { AccessToken } from '../../models/entities/access-token';
import { Cache } from '@/misc/cache';
// TODO: TypeORMのカスタムキャッシュプロバイダを使っても良いかも
// ref. https://github.com/typeorm/typeorm/blob/master/docs/caching.md
const cache = new Cache<User>(1000 * 60 * 60);
export class AuthenticationError extends Error {
constructor(message: string) {
super(message);
this.name = 'AuthenticationError';
}
}
export default async (token: string): Promise<[User | null | undefined, AccessToken | null | undefined]> => {
export default async (token: string): Promise<[User | null | undefined, App | null | undefined]> => {
if (token == null) {
return [null, null];
}
if (isNativeToken(token)) {
const cached = cache.get(token);
if (cached) {
return [cached, null];
}
// Fetch user
const user = await Users
.findOne({ token });
if (user == null) {
throw new Error('user not found');
throw new AuthenticationError('user not found');
}
cache.set(token, user);
return [user, null];
} else {
// TODO: cache
const accessToken = await AccessTokens.findOne({
where: [{
hash: token.toLowerCase() // app
@ -41,7 +35,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, {

View file

@ -6,6 +6,7 @@ import { Users, Followings, Notifications } from '../../../../models';
import { User } from '../../../../models/entities/user';
import { insertModerationLog } from '../../../../services/insert-moderation-log';
import { doPostSuspend } from '../../../../services/suspend-user';
import { publishUserEvent } from '@/services/stream';
export const meta = {
tags: ['admin'],
@ -43,6 +44,11 @@ export default define(meta, async (ps, me) => {
targetId: user.id,
});
// Terminate streaming
if (Users.isLocalUser(user)) {
publishUserEvent(user.id, 'terminate', {});
}
(async () => {
await doPostSuspend(user).catch(e => {});
await unFollowAll(user).catch(e => {});

View file

@ -3,6 +3,7 @@ import * as bcrypt from 'bcryptjs';
import define from '../../define';
import { Users, UserProfiles } from '../../../../models';
import { doPostSuspend } from '../../../../services/suspend-user';
import { publishUserEvent } from '@/services/stream';
export const meta = {
requireCredential: true as const,
@ -30,4 +31,7 @@ export default define(meta, async (ps, user) => {
await doPostSuspend(user).catch(e => {});
await Users.delete(user.id);
// Terminate streaming
publishUserEvent(user.id, 'terminate', {});
});

View file

@ -1,6 +1,6 @@
import $ from 'cafy';
import * as bcrypt from 'bcryptjs';
import { publishMainStream } from '../../../../services/stream';
import { publishMainStream, publishUserEvent } from '../../../../services/stream';
import generateUserToken from '../../common/generate-native-user-token';
import define from '../../define';
import { Users, UserProfiles } from '../../../../models';
@ -36,4 +36,9 @@ export default define(meta, async (ps, user) => {
// Publish event
publishMainStream(user.id, 'myTokenRegenerated');
// Terminate streaming
setTimeout(() => {
publishUserEvent(user.id, 'terminate', {});
}, 5000);
});

View file

@ -2,6 +2,7 @@ import $ from 'cafy';
import define from '../../define';
import { AccessTokens } from '../../../../models';
import { ID } from '@/misc/cafy-id';
import { publishUserEvent } from '@/services/stream';
export const meta = {
requireCredential: true as const,
@ -19,6 +20,12 @@ export default define(meta, async (ps, user) => {
const token = await AccessTokens.findOne(ps.tokenId);
if (token) {
AccessTokens.delete(token.id);
await AccessTokens.delete({
id: ps.tokenId,
userId: user.id,
});
// Terminate streaming
publishUserEvent(user.id, 'terminate');
}
});

View file

@ -46,6 +46,13 @@ export default async (ctx: Koa.Context) => {
return;
}
if (user.isSuspended) {
ctx.throw(403, {
error: 'user is suspended'
});
return;
}
const profile = await UserProfiles.findOneOrFail(user.id);
// Compare password

View file

@ -92,6 +92,11 @@ export default class Connection {
this.userProfile = body;
break;
case 'terminate':
this.wsConnection.close();
this.dispose();
break;
default:
break;
}

View file

@ -22,6 +22,11 @@ module.exports = (server: http.Server) => {
// (現状はエラーがキャッチされておらずサーバーのログに流れて邪魔なので)
const [user, app] = await authenticate(q.i as string);
if (user?.isSuspended) {
request.reject(400);
return;
}
const connection = request.accept();
const ev = new EventEmitter();