2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { AccessTokensRepository, AppsRepository, UsersRepository } from '@/models/index.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import type { CacheableLocalUser, ILocalUser } from '@/models/entities/User.js';
|
|
|
|
import type { AccessToken } from '@/models/entities/AccessToken.js';
|
|
|
|
import { Cache } from '@/misc/cache.js';
|
|
|
|
import type { App } from '@/models/entities/App.js';
|
|
|
|
import { UserCacheService } from '@/core/UserCacheService.js';
|
|
|
|
import isNativeToken from '@/misc/is-native-token.js';
|
2022-12-04 06:03:09 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
export class AuthenticationError extends Error {
|
|
|
|
constructor(message: string) {
|
|
|
|
super(message);
|
|
|
|
this.name = 'AuthenticationError';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class AuthenticateService {
|
2022-09-18 18:11:50 +00:00
|
|
|
private appCache: Cache<App>;
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.accessTokensRepository)
|
|
|
|
private accessTokensRepository: AccessTokensRepository,
|
|
|
|
|
|
|
|
@Inject(DI.appsRepository)
|
|
|
|
private appsRepository: AppsRepository,
|
|
|
|
|
|
|
|
private userCacheService: UserCacheService,
|
|
|
|
) {
|
2022-09-18 18:11:50 +00:00
|
|
|
this.appCache = new Cache<App>(Infinity);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-12-03 10:42:05 +00:00
|
|
|
public async authenticate(token: string | null | undefined): Promise<[CacheableLocalUser | null | undefined, AccessToken | null | undefined]> {
|
2022-09-17 18:27:08 +00:00
|
|
|
if (token == null) {
|
|
|
|
return [null, null];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isNativeToken(token)) {
|
|
|
|
const user = await this.userCacheService.localUserByNativeTokenCache.fetch(token,
|
|
|
|
() => this.usersRepository.findOneBy({ token }) as Promise<ILocalUser | null>);
|
|
|
|
|
|
|
|
if (user == null) {
|
|
|
|
throw new AuthenticationError('user not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
return [user, null];
|
|
|
|
} else {
|
|
|
|
const accessToken = await this.accessTokensRepository.findOne({
|
|
|
|
where: [{
|
|
|
|
hash: token.toLowerCase(), // app
|
|
|
|
}, {
|
|
|
|
token: token, // miauth
|
|
|
|
}],
|
|
|
|
});
|
|
|
|
|
|
|
|
if (accessToken == null) {
|
|
|
|
throw new AuthenticationError('invalid signature');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.accessTokensRepository.update(accessToken.id, {
|
|
|
|
lastUsedAt: new Date(),
|
|
|
|
});
|
|
|
|
|
|
|
|
const user = await this.userCacheService.localUserByIdCache.fetch(accessToken.userId,
|
|
|
|
() => this.usersRepository.findOneBy({
|
|
|
|
id: accessToken.userId,
|
|
|
|
}) as Promise<ILocalUser>);
|
|
|
|
|
|
|
|
if (accessToken.appId) {
|
2022-09-18 18:11:50 +00:00
|
|
|
const app = await this.appCache.fetch(accessToken.appId,
|
2022-09-17 18:27:08 +00:00
|
|
|
() => this.appsRepository.findOneByOrFail({ id: accessToken.appId! }));
|
|
|
|
|
|
|
|
return [user, {
|
|
|
|
id: accessToken.id,
|
|
|
|
permission: app.permission,
|
|
|
|
} as AccessToken];
|
|
|
|
} else {
|
|
|
|
return [user, accessToken];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|