Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
syuilo committed Feb 28, 2025
1 parent a3bba23 commit c63c346
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/backend/src/core/CreateSystemUserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { IdService } from '@/core/IdService.js';
import { MiUserKeypair } from '@/models/UserKeypair.js';
import { MiUsedUsername } from '@/models/UsedUsername.js';
import { DI } from '@/di-symbols.js';
import generateNativeUserToken from '@/misc/generate-native-user-token.js';
import { generateNativeUserToken } from '@/misc/token.js';
import { bindThis } from '@/decorators.js';

@Injectable()
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/core/SignupService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { MiUserProfile } from '@/models/UserProfile.js';
import { IdService } from '@/core/IdService.js';
import { MiUserKeypair } from '@/models/UserKeypair.js';
import { MiUsedUsername } from '@/models/UsedUsername.js';
import generateUserToken from '@/misc/generate-native-user-token.js';
import { generateNativeUserToken } from '@/misc/token.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { InstanceActorService } from '@/core/InstanceActorService.js';
import { bindThis } from '@/decorators.js';
Expand Down Expand Up @@ -74,7 +74,7 @@ export class SignupService {
}

// Generate secret
const secret = generateUserToken();
const secret = generateNativeUserToken();

// Check username duplication
if (await this.usersRepository.exists({ where: { usernameLower: username.toLowerCase(), host: IsNull() } })) {
Expand Down
7 changes: 0 additions & 7 deletions packages/backend/src/misc/is-native-token.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@

import { secureRndstr } from '@/misc/secure-rndstr.js';

// eslint-disable-next-line import/no-default-export
export default () => secureRndstr(16);
export const generateNativeUserToken = () => secureRndstr(16);

export const isNativeUserToken = (token: string) => token.length === 16;
4 changes: 2 additions & 2 deletions packages/backend/src/server/api/AuthenticateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { MiAccessToken } from '@/models/AccessToken.js';
import { MemoryKVCache } from '@/misc/cache.js';
import type { MiApp } from '@/models/App.js';
import { CacheService } from '@/core/CacheService.js';
import isNativeToken from '@/misc/is-native-token.js';
import { isNativeUserToken } from '@/misc/token.js';
import { bindThis } from '@/decorators.js';

export class AuthenticationError extends Error {
Expand Down Expand Up @@ -46,7 +46,7 @@ export class AuthenticateService implements OnApplicationShutdown {
return [null, null];
}

if (isNativeToken(token)) {
if (isNativeUserToken(token)) {
const user = await this.cacheService.localUserByNativeTokenCache.fetch(token,
() => this.usersRepository.findOneBy({ token }) as Promise<MiLocalUser | null>);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import bcrypt from 'bcryptjs';
import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import type { UsersRepository, UserProfilesRepository } from '@/models/_.js';
import generateUserToken from '@/misc/generate-native-user-token.js';
import { generateNativeUserToken } from '@/misc/token.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { DI } from '@/di-symbols.js';

Expand Down Expand Up @@ -49,7 +49,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
throw new Error('incorrect password');
}

const newToken = generateUserToken();
const newToken = generateNativeUserToken();

await this.usersRepository.update(me.id, {
token: newToken,
Expand Down

0 comments on commit c63c346

Please sign in to comment.