2023-07-27 08:31:52 +03:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-06-10 07:45:11 +03:00
|
|
|
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
|
2022-09-17 21:27:08 +03:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-15 08:28:29 +03:00
|
|
|
import type { AccessTokensRepository, AppsRepository, UsersRepository } from '@/models/_.js';
|
2023-09-20 05:33:36 +03:00
|
|
|
import type { MiLocalUser } from '@/models/User.js';
|
|
|
|
import type { MiAccessToken } from '@/models/AccessToken.js';
|
2023-04-04 09:56:47 +03:00
|
|
|
import { MemoryKVCache } from '@/misc/cache.js';
|
2023-09-20 05:33:36 +03:00
|
|
|
import type { MiApp } from '@/models/App.js';
|
2023-04-04 11:32:09 +03:00
|
|
|
import { CacheService } from '@/core/CacheService.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import isNativeToken from '@/misc/is-native-token.js';
|
2022-12-04 08:03:09 +02:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
export class AuthenticationError extends Error {
|
|
|
|
constructor(message: string) {
|
|
|
|
super(message);
|
|
|
|
this.name = 'AuthenticationError';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable()
|
2023-06-10 07:45:11 +03:00
|
|
|
export class AuthenticateService implements OnApplicationShutdown {
|
2023-08-16 11:51:28 +03:00
|
|
|
private appCache: MemoryKVCache<MiApp>;
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.accessTokensRepository)
|
|
|
|
private accessTokensRepository: AccessTokensRepository,
|
|
|
|
|
|
|
|
@Inject(DI.appsRepository)
|
|
|
|
private appsRepository: AppsRepository,
|
|
|
|
|
2023-04-04 11:32:09 +03:00
|
|
|
private cacheService: CacheService,
|
2022-09-17 21:27:08 +03:00
|
|
|
) {
|
2023-08-16 11:51:28 +03:00
|
|
|
this.appCache = new MemoryKVCache<MiApp>(Infinity);
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
|
|
|
|
2022-12-04 08:03:09 +02:00
|
|
|
@bindThis
|
2023-08-16 11:51:28 +03:00
|
|
|
public async authenticate(token: string | null | undefined): Promise<[MiLocalUser | null, MiAccessToken | null]> {
|
2022-09-17 21:27:08 +03:00
|
|
|
if (token == null) {
|
|
|
|
return [null, null];
|
|
|
|
}
|
2023-07-08 01:08:16 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
if (isNativeToken(token)) {
|
2023-04-04 11:32:09 +03:00
|
|
|
const user = await this.cacheService.localUserByNativeTokenCache.fetch(token,
|
2023-08-16 11:51:28 +03:00
|
|
|
() => this.usersRepository.findOneBy({ token }) as Promise<MiLocalUser | null>);
|
2023-07-08 01:08:16 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
if (user == null) {
|
|
|
|
throw new AuthenticationError('user not found');
|
|
|
|
}
|
2023-07-08 01:08:16 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
return [user, null];
|
|
|
|
} else {
|
|
|
|
const accessToken = await this.accessTokensRepository.findOne({
|
|
|
|
where: [{
|
|
|
|
hash: token.toLowerCase(), // app
|
|
|
|
}, {
|
|
|
|
token: token, // miauth
|
|
|
|
}],
|
|
|
|
});
|
2023-07-08 01:08:16 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
if (accessToken == null) {
|
|
|
|
throw new AuthenticationError('invalid signature');
|
|
|
|
}
|
2023-07-08 01:08:16 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
this.accessTokensRepository.update(accessToken.id, {
|
|
|
|
lastUsedAt: new Date(),
|
|
|
|
});
|
2023-07-08 01:08:16 +03:00
|
|
|
|
2023-04-04 11:32:09 +03:00
|
|
|
const user = await this.cacheService.localUserByIdCache.fetch(accessToken.userId,
|
2022-09-17 21:27:08 +03:00
|
|
|
() => this.usersRepository.findOneBy({
|
|
|
|
id: accessToken.userId,
|
2023-08-16 11:51:28 +03:00
|
|
|
}) as Promise<MiLocalUser>);
|
2023-07-08 01:08:16 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
if (accessToken.appId) {
|
2022-09-18 21:11:50 +03:00
|
|
|
const app = await this.appCache.fetch(accessToken.appId,
|
2022-09-17 21:27:08 +03:00
|
|
|
() => this.appsRepository.findOneByOrFail({ id: accessToken.appId! }));
|
2023-07-08 01:08:16 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
return [user, {
|
|
|
|
id: accessToken.id,
|
|
|
|
permission: app.permission,
|
2023-08-16 11:51:28 +03:00
|
|
|
} as MiAccessToken];
|
2022-09-17 21:27:08 +03:00
|
|
|
} else {
|
|
|
|
return [user, accessToken];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-10 07:45:11 +03:00
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public dispose(): void {
|
|
|
|
this.appCache.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public onApplicationShutdown(signal?: string | undefined): void {
|
|
|
|
this.dispose();
|
|
|
|
}
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|