Skip to content
This repository has been archived by the owner on Nov 18, 2023. It is now read-only.

Commit

Permalink
Argon2 support
Browse files Browse the repository at this point in the history
Implement Argon2 hashing support
  • Loading branch information
丈槍由紀 committed Aug 4, 2023
1 parent a8d45d4 commit 9729327
Show file tree
Hide file tree
Showing 17 changed files with 32 additions and 20 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

**🌎 **[Misskey](https://misskey-hub.net/)** is an open source, decentralized social media platform that's free forever! 🚀**

**Note** This fork adds argon2 support, so it's possible to migrate from Foundkey and Firefish.

---

<a href="https://misskey-hub.net/instances.html">
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"lodash": "4.17.21"
},
"dependencies": {
"argon2": "^0.30.2",
"execa": "7.1.1",
"gulp": "4.0.2",
"gulp-cssnano": "2.1.3",
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/core/CreateSystemUserService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { randomUUID } from 'node:crypto';
import { Inject, Injectable } from '@nestjs/common';
import bcrypt from 'bcryptjs';
import * as argon2 from 'argon2';
import { IsNull, DataSource } from 'typeorm';
import { genRsaKeyPair } from '@/misc/gen-key-pair.js';
import { User } from '@/models/entities/User.js';
Expand All @@ -27,8 +28,7 @@ export class CreateSystemUserService {
const password = randomUUID();

// Generate hash of password
const salt = await bcrypt.genSalt(8);
const hash = await bcrypt.hash(password, salt);
const hash = argon2.hash(password);

// Generate secret
const secret = generateNativeUserToken();
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/core/SignupService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { generateKeyPair } from 'node:crypto';
import { Inject, Injectable } from '@nestjs/common';
import bcrypt from 'bcryptjs';
import * as argon2 from 'argon2';
import { DataSource, IsNull } from 'typeorm';
import { DI } from '@/di-symbols.js';
import type { UsedUsernamesRepository, UsersRepository } from '@/models/index.js';
Expand Down Expand Up @@ -63,8 +64,7 @@ export class SignupService {
}

// Generate hash of password
const salt = await bcrypt.genSalt(8);
hash = await bcrypt.hash(password, salt);
hash = argon2.hash(password);
}

// Generate secret
Expand Down
3 changes: 2 additions & 1 deletion packages/backend/src/server/api/SigninApiService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { randomBytes } from 'node:crypto';
import { Inject, Injectable } from '@nestjs/common';
import bcrypt from 'bcryptjs';
import * as argon2 from 'argon2';
import * as OTPAuth from 'otpauth';
import { IsNull } from 'typeorm';
import { DI } from '@/di-symbols.js';
Expand Down Expand Up @@ -122,7 +123,7 @@ export class SigninApiService {
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: user.id });

// Compare password
const same = await bcrypt.compare(password, profile.password!);
const same = return argon2.verify(profile.password, password);

const fail = async (status?: number, failure?: { id: string }) => {
// Append signin history
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/server/api/SignupApiService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Inject, Injectable } from '@nestjs/common';
import bcrypt from 'bcryptjs';
import * as argon2 from 'argon2';
import { IsNull } from 'typeorm';
import { DI } from '@/di-symbols.js';
import type { RegistrationTicketsRepository, UsedUsernamesRepository, UserPendingsRepository, UserProfilesRepository, UsersRepository, RegistrationTicket } from '@/models/index.js';
Expand Down Expand Up @@ -155,8 +156,7 @@ export class SignupApiService {
const code = secureRndstr(16, { chars: L_CHARS });

// Generate hash of password
const salt = await bcrypt.genSalt(8);
const hash = await bcrypt.hash(password, salt);
const hash = argon2.hash(password);

const pendingUser = await this.userPendingsRepository.insert({
id: this.idService.genId(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Inject, Injectable } from '@nestjs/common';
import bcrypt from 'bcryptjs';
import * as argon2 from 'argon2';
import { Endpoint } from '@/server/api/endpoint-base.js';
import type { UsersRepository, UserProfilesRepository } from '@/models/index.js';
import { DI } from '@/di-symbols.js';
Expand Down Expand Up @@ -57,7 +58,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
const passwd = secureRndstr(8);

// Generate hash of password
const hash = bcrypt.hashSync(passwd);
const hash = argon2.hash(passwd);

await this.userProfilesRepository.update({
userId: user.id,
Expand Down
3 changes: 2 additions & 1 deletion packages/backend/src/server/api/endpoints/i/2fa/key-done.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { promisify } from 'node:util';
import bcrypt from 'bcryptjs';
import * as argon2 from 'argon2';
import cbor from 'cbor';
import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
Expand Down Expand Up @@ -56,7 +57,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: me.id });

// Compare password
const same = await bcrypt.compare(ps.password, profile.password!);
const same = argon2.verify(profile.password, ps.password);

if (!same) {
throw new Error('incorrect password');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { promisify } from 'node:util';
import * as crypto from 'node:crypto';
import bcrypt from 'bcryptjs';
import * as argon2 from 'argon2';
import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import type { UserProfilesRepository, AttestationChallengesRepository } from '@/models/index.js';
Expand Down Expand Up @@ -41,7 +42,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: me.id });

// Compare password
const same = await bcrypt.compare(ps.password, profile.password!);
const same = argon2.verify(profile.password, ps.password);

if (!same) {
throw new Error('incorrect password');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import bcrypt from 'bcryptjs';
import * as argon2 from 'argon2';
import * as OTPAuth from 'otpauth';
import * as QRCode from 'qrcode';
import { Inject, Injectable } from '@nestjs/common';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import bcrypt from 'bcryptjs';
import * as argon2 from 'argon2';
import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import type { UserProfilesRepository, UserSecurityKeysRepository } from '@/models/index.js';
Expand Down Expand Up @@ -38,7 +39,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: me.id });

// Compare password
const same = await bcrypt.compare(ps.password, profile.password!);
const same = argon2.verify(profile.password, ps.password);

if (!same) {
throw new Error('incorrect password');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import bcrypt from 'bcryptjs';
import * as argon2 from 'argon2';
import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
Expand Down Expand Up @@ -34,7 +35,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: me.id });

// Compare password
const same = await bcrypt.compare(ps.password, profile.password!);
const same = argon2.verify(profile.password, ps.password);

if (!same) {
throw new Error('incorrect password');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import bcrypt from 'bcryptjs';
import * as argon2 from 'argon2';
import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import type { UserProfilesRepository } from '@/models/index.js';
Expand Down Expand Up @@ -30,15 +31,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: me.id });

// Compare password
const same = await bcrypt.compare(ps.currentPassword, profile.password!);
const same = argon2.verify(profile.password, ps.currentPassword);

if (!same) {
throw new Error('incorrect password');
}

// Generate hash of password
const salt = await bcrypt.genSalt(8);
const hash = await bcrypt.hash(ps.newPassword, salt);
const hash = argon2.hash(ps.newPassword);

await this.userProfilesRepository.update(me.id, {
password: hash,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import bcrypt from 'bcryptjs';
import * as argon2 from 'argon2';
import { Inject, Injectable } from '@nestjs/common';
import type { UsersRepository, UserProfilesRepository } from '@/models/index.js';
import { Endpoint } from '@/server/api/endpoint-base.js';
Expand Down Expand Up @@ -39,7 +40,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
}

// Compare password
const same = await bcrypt.compare(ps.password, profile.password!);
const same = argon2.verify(profile.password, ps.password);

if (!same) {
throw new Error('incorrect password');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import bcrypt from 'bcryptjs';
import * as argon2 from 'argon2';
import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import type { UsersRepository, UserProfilesRepository } from '@/models/index.js';
Expand Down Expand Up @@ -39,7 +40,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: me.id });

// Compare password
const same = await bcrypt.compare(ps.password, profile.password!);
const same = argon2.verify(profile.password, ps.password);

if (!same) {
throw new Error('incorrect password');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: me.id });

// Compare password
const same = await bcrypt.compare(ps.password, profile.password!);
const same = argon2.verify(profile.password, ps.password);

if (!same) {
throw new ApiError(meta.errors.incorrectPassword);
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/server/api/endpoints/reset-password.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import bcrypt from 'bcryptjs';
import * as argon2 from 'argon2';
import { Inject, Injectable } from '@nestjs/common';
import type { UserProfilesRepository, PasswordResetRequestsRepository } from '@/models/index.js';
import { Endpoint } from '@/server/api/endpoint-base.js';
Expand Down Expand Up @@ -46,8 +47,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
}

// Generate hash of password
const salt = await bcrypt.genSalt(8);
const hash = await bcrypt.hash(ps.password, salt);
const hash = argon2.hash(ps.password);

await this.userProfilesRepository.update(req.userId, {
password: hash,
Expand Down

0 comments on commit 9729327

Please sign in to comment.