2022-02-27 11:07:39 +09:00
|
|
|
import bcrypt from 'bcryptjs';
|
|
|
|
import { generateKeyPair } from 'node:crypto';
|
|
|
|
import generateUserToken from './generate-native-user-token.js';
|
|
|
|
import { User } from '@/models/entities/user.js';
|
|
|
|
import { Users, UsedUsernames } from '@/models/index.js';
|
|
|
|
import { UserProfile } from '@/models/entities/user-profile.js';
|
2020-01-30 04:37:25 +09:00
|
|
|
import { getConnection } from 'typeorm';
|
2022-02-27 11:07:39 +09:00
|
|
|
import { genId } from '@/misc/gen-id.js';
|
|
|
|
import { toPunyNullable } from '@/misc/convert-host.js';
|
|
|
|
import { UserKeypair } from '@/models/entities/user-keypair.js';
|
|
|
|
import { usersChart } from '@/services/chart/index.js';
|
|
|
|
import { UsedUsername } from '@/models/entities/used-username.js';
|
2020-01-30 04:37:25 +09:00
|
|
|
|
2021-10-08 13:37:02 +09:00
|
|
|
export async function signup(opts: {
|
|
|
|
username: User['username'];
|
|
|
|
password?: string | null;
|
|
|
|
passwordHash?: UserProfile['password'] | null;
|
|
|
|
host?: string | null;
|
|
|
|
}) {
|
|
|
|
const { username, password, passwordHash, host } = opts;
|
|
|
|
let hash = passwordHash;
|
|
|
|
|
2020-01-30 04:37:25 +09:00
|
|
|
// Validate username
|
2022-02-19 14:05:32 +09:00
|
|
|
if (!Users.validateLocalUsername(username)) {
|
2020-01-30 04:37:25 +09:00
|
|
|
throw new Error('INVALID_USERNAME');
|
|
|
|
}
|
|
|
|
|
2021-10-08 13:37:02 +09:00
|
|
|
if (password != null && passwordHash == null) {
|
|
|
|
// Validate password
|
2022-02-19 14:05:32 +09:00
|
|
|
if (!Users.validatePassword(password)) {
|
2021-10-08 13:37:02 +09:00
|
|
|
throw new Error('INVALID_PASSWORD');
|
|
|
|
}
|
2020-01-30 04:37:25 +09:00
|
|
|
|
2021-10-08 13:37:02 +09:00
|
|
|
// Generate hash of password
|
|
|
|
const salt = await bcrypt.genSalt(8);
|
|
|
|
hash = await bcrypt.hash(password, salt);
|
|
|
|
}
|
2020-01-30 04:37:25 +09:00
|
|
|
|
|
|
|
// Generate secret
|
|
|
|
const secret = generateUserToken();
|
|
|
|
|
|
|
|
// Check username duplication
|
|
|
|
if (await Users.findOne({ usernameLower: username.toLowerCase(), host: null })) {
|
|
|
|
throw new Error('DUPLICATED_USERNAME');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check deleted username duplication
|
|
|
|
if (await UsedUsernames.findOne({ username: username.toLowerCase() })) {
|
|
|
|
throw new Error('USED_USERNAME');
|
|
|
|
}
|
|
|
|
|
|
|
|
const keyPair = await new Promise<string[]>((res, rej) =>
|
|
|
|
generateKeyPair('rsa', {
|
|
|
|
modulusLength: 4096,
|
|
|
|
publicKeyEncoding: {
|
|
|
|
type: 'spki',
|
2021-12-09 23:58:30 +09:00
|
|
|
format: 'pem',
|
2020-01-30 04:37:25 +09:00
|
|
|
},
|
|
|
|
privateKeyEncoding: {
|
|
|
|
type: 'pkcs8',
|
|
|
|
format: 'pem',
|
|
|
|
cipher: undefined,
|
2021-12-09 23:58:30 +09:00
|
|
|
passphrase: undefined,
|
|
|
|
},
|
2020-01-30 04:37:25 +09:00
|
|
|
} as any, (err, publicKey, privateKey) =>
|
|
|
|
err ? rej(err) : res([publicKey, privateKey])
|
|
|
|
));
|
|
|
|
|
|
|
|
let account!: User;
|
|
|
|
|
|
|
|
// Start transaction
|
|
|
|
await getConnection().transaction(async transactionalEntityManager => {
|
|
|
|
const exist = await transactionalEntityManager.findOne(User, {
|
|
|
|
usernameLower: username.toLowerCase(),
|
2021-12-09 23:58:30 +09:00
|
|
|
host: null,
|
2020-01-30 04:37:25 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
if (exist) throw new Error(' the username is already used');
|
|
|
|
|
|
|
|
account = await transactionalEntityManager.save(new User({
|
|
|
|
id: genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
username: username,
|
|
|
|
usernameLower: username.toLowerCase(),
|
|
|
|
host: toPunyNullable(host),
|
|
|
|
token: secret,
|
2020-03-31 08:07:10 +09:00
|
|
|
isAdmin: (await Users.count({
|
|
|
|
host: null,
|
|
|
|
})) === 0,
|
2020-01-30 04:37:25 +09:00
|
|
|
}));
|
|
|
|
|
|
|
|
await transactionalEntityManager.save(new UserKeypair({
|
|
|
|
publicKey: keyPair[0],
|
|
|
|
privateKey: keyPair[1],
|
2021-12-09 23:58:30 +09:00
|
|
|
userId: account.id,
|
2020-01-30 04:37:25 +09:00
|
|
|
}));
|
|
|
|
|
|
|
|
await transactionalEntityManager.save(new UserProfile({
|
|
|
|
userId: account.id,
|
|
|
|
autoAcceptFollowed: true,
|
|
|
|
password: hash,
|
|
|
|
}));
|
|
|
|
|
|
|
|
await transactionalEntityManager.save(new UsedUsername({
|
|
|
|
createdAt: new Date(),
|
|
|
|
username: username.toLowerCase(),
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
usersChart.update(account, true);
|
|
|
|
|
|
|
|
return { account, secret };
|
|
|
|
}
|