Skip to content

Commit

Permalink
Merge pull request #76 from donghquinn/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
donghquinn authored Mar 14, 2024
2 parents 1a299b0 + 68e8b15 commit 5e624fc
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 171 deletions.
68 changes: 0 additions & 68 deletions src/libraries/client/decrypt.lib.ts

This file was deleted.

52 changes: 0 additions & 52 deletions src/libraries/client/encrypt.lib.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/libraries/mailer/mail.utils.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/modules/client/client.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ClientController } from '@controllers/client/client.ctl';
import { ClientSearchController } from '@controllers/client/search.ctl';
import { ClientStarController } from '@controllers/client/star.ctl';
import { CryptoModule } from '@modules/crypto.module';
import { MailerModule } from '@modules/mailer.module';
import { Module } from '@nestjs/common';
import { ClientProvider } from 'providers/client/client.pvd';
Expand All @@ -12,6 +13,6 @@ import { ClientPrismaModule } from './client-prisma.module';
@Module({
controllers: [ClientController, ClientSearchController, ClientStarController],
providers: [ClientProvider, ClientSearchProvider, ClientStarProvider],
imports: [AccountManagerModule, ClientPrismaModule, MailerModule],
imports: [AccountManagerModule, ClientPrismaModule, MailerModule, CryptoModule],
})
export class ClientModule {}
8 changes: 8 additions & 0 deletions src/modules/crypto.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';
import { CryptoProvider } from 'providers/crypto.pvd';

@Module({
providers: [CryptoProvider],
exports: [CryptoProvider],
})
export class CryptoModule {}
22 changes: 10 additions & 12 deletions src/providers/client/client.pvd.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { ClientError, NoUserError } from '@errors/client.error';
import { PasswordError } from '@errors/password.error';
import { comparePassword, decrypt } from '@libraries/client/decrypt.lib';
import { cryptData } from '@libraries/client/encrypt.lib';
import { Injectable } from '@nestjs/common';
import { ClientLogger } from '@utils/logger.util';
import { MailerProvider } from 'providers/mailer.pvd';
import { CryptoProvider } from 'providers/crypto.pvd';
import { clearIntervalAsync, setIntervalAsync } from 'set-interval-async';
import { AccountManager } from '../account-manager.pvd';
import { ClientPrismaLibrary } from './client-prisma.pvd';
Expand All @@ -14,7 +12,7 @@ export class ClientProvider {
constructor(
private readonly prisma: ClientPrismaLibrary,
private readonly accountManager: AccountManager,
private readonly mailer: MailerProvider,
private readonly crypto: CryptoProvider,
) {}

async checkEmailandSignup(email: string, password: string, name: string) {
Expand All @@ -27,7 +25,7 @@ export class ClientProvider {
email,
});

const { encodedData: encodedPassword, encodedToken: passwordToken } = cryptData(password);
const { encodedData: encodedPassword, encodedToken: passwordToken } = this.crypto.cryptData(password);

const uuid = await this.prisma.insertNewClient(email, name, encodedPassword, passwordToken);

Expand Down Expand Up @@ -61,7 +59,7 @@ export class ClientProvider {

const { password: foundPassword, password_token: passwordToken, uuid } = userInfo;

const isMatch = comparePassword(password, foundPassword, passwordToken);
const isMatch = this.crypto.comparePassword(password, foundPassword, passwordToken);

if (!isMatch) {
ClientLogger.error('[LOGIN] Password Matching Given Password is Not Match. Reject.');
Expand All @@ -70,7 +68,7 @@ export class ClientProvider {
}

// Set User Info into REDIS
const { encodedData: encodedEmail, encodedToken: encodedEmailToken } = cryptData(email);
const { encodedData: encodedEmail, encodedToken: encodedEmailToken } = this.crypto.cryptData(email);

ClientLogger.debug('[ACCOUNT] Searching User Info: %o', {
encodedEmail,
Expand Down Expand Up @@ -145,7 +143,7 @@ export class ClientProvider {

const { token, uuid } = foundKey;

const email = decrypt(encodedEmail, token);
const email = this.crypto.decrypt(encodedEmail, token);

await this.prisma.updateClientLoginStatus(email, uuid, false);

Expand Down Expand Up @@ -185,7 +183,7 @@ export class ClientProvider {
});

const { token, uuid } = foundKey;
const email = decrypt(encodedEmail, token);
const email = this.crypto.decrypt(encodedEmail, token);

const result = await this.prisma.getMyPageInfo(email, uuid);

Expand Down Expand Up @@ -213,7 +211,7 @@ export class ClientProvider {

const { token: redisToken } = loginInfo;

const email = decrypt(encodedEmail, redisToken);
const email = this.crypto.decrypt(encodedEmail, redisToken);

// 찾기
const result = await this.prisma.selectUserInfo(email);
Expand All @@ -228,11 +226,11 @@ export class ClientProvider {

const { password: dbPassword, password_token: token, uuid } = result;

const isMatch = comparePassword(password, dbPassword, token);
const isMatch = this.crypto.comparePassword(password, dbPassword, token);

if (!isMatch) throw new PasswordError('[CHANGE_PASS] Changing Password', 'Password Is Not Match');

const { encodedData: encodedPassword, encodedToken: passwordToken } = cryptData(newPassword);
const { encodedData: encodedPassword, encodedToken: passwordToken } = this.crypto.cryptData(newPassword);

await this.prisma.updateNewPassword(uuid, encodedPassword, passwordToken);

Expand Down
28 changes: 13 additions & 15 deletions src/providers/client/search.pvd.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { ClientError, NoUserError } from '@errors/client.error';
import { NoValidateKeyError, PasswordError } from '@errors/password.error';
import { comparePassword, decrypt, decryptPassword } from '@libraries/client/decrypt.lib';
import { cryptData, encryptOriginalPassword } from '@libraries/client/encrypt.lib';
import { Injectable } from '@nestjs/common';
import { ClientLogger } from '@utils/logger.util';
import { createSearchPasswordMailcontent } from '@libraries/mailer/mail.utils';
import { randomBytes } from 'crypto';
import { AccountManager } from 'providers/account-manager.pvd';
import { CryptoProvider } from 'providers/crypto.pvd';
import { MailerProvider } from 'providers/mailer.pvd';
import { ClientPrismaLibrary } from './client-prisma.pvd';

Expand All @@ -15,6 +13,7 @@ export class ClientSearchProvider {
constructor(
private readonly prisma: ClientPrismaLibrary,
private readonly accountManager: AccountManager,
private readonly crypto: CryptoProvider,
private readonly mailer: MailerProvider,
) {}

Expand All @@ -35,13 +34,11 @@ export class ClientSearchProvider {
const randomKey = randomBytes(8).toString('hex');
// const { encodedData: encodedPassword, dataToken: passwordToken } = cryptData(randomPassword);

const mailContent = createSearchPasswordMailcontent(randomKey);

const { password, password_token: token } = result;

await this.accountManager.setTempData(randomKey, email, password, token);

await this.mailer.sendMail(email, 'Search Password', mailContent);
await this.mailer.sendSearchPassword(email, 'Search Password', randomKey);

ClientLogger.info('[SEARCH_PASS] Sending New Password Complete');

Expand Down Expand Up @@ -74,9 +71,9 @@ export class ClientSearchProvider {

const { password, token } = tempItem;

const rawPassword = decrypt(password, token);
const rawPassword = this.crypto.decrypt(password, token);

const encryptedPassword = encryptOriginalPassword(rawPassword);
const encryptedPassword = this.crypto.encryptOriginalPassword(rawPassword);

ClientLogger.info('[VALIDATE_KEY] Validate Password Searching Key Complete');

Expand Down Expand Up @@ -115,18 +112,19 @@ export class ClientSearchProvider {
throw new NoUserError('[CHANGE_PASS] Login', 'No Matching Info. Please Make sure you Logged Out Before.');
}

const decryptedPassword = decryptPassword(password);
const decryptedPassword = this.crypto.decryptPassword(password);

const { password: dbPassword, password_token: token, uuid } = result;

const dbRawPassword = decrypt(dbPassword, token);
const dbRawPassword = this.crypto.decrypt(dbPassword, token);

if (decryptedPassword !== dbRawPassword)
throw new PasswordError('[CHANGE_PASS] Changing Password', 'Password Is Not Match');

const decryptedNewPassword = decryptPassword(newPassword);
const decryptedNewPassword = this.crypto.decryptPassword(newPassword);

const { encodedData: encodedNewPassword, encodedToken: passwordNewToken } = cryptData(decryptedNewPassword);
const { encodedData: encodedNewPassword, encodedToken: passwordNewToken } =
this.crypto.cryptData(decryptedNewPassword);

await this.prisma.updateNewPassword(uuid, encodedNewPassword, passwordNewToken);

Expand Down Expand Up @@ -162,7 +160,7 @@ export class ClientSearchProvider {

const { token: emailToken } = loginInfo;

const email = decrypt(encodedEmail, emailToken);
const email = this.crypto.decrypt(encodedEmail, emailToken);

// 찾기
const result = await this.prisma.findUser(email);
Expand All @@ -177,11 +175,11 @@ export class ClientSearchProvider {

const { password: dbPassword, password_token: token, uuid } = result;

const isMatch = comparePassword(password, dbPassword, token);
const isMatch = this.crypto.comparePassword(password, dbPassword, token);

if (!isMatch) throw new PasswordError('[CHANGE_PASS] Changing Password', 'Password Is Not Match');

const { encodedData: encodedPassword, encodedToken: passwordToken } = cryptData(newPassword);
const { encodedData: encodedPassword, encodedToken: passwordToken } = this.crypto.cryptData(newPassword);

await this.prisma.updateNewPassword(uuid, encodedPassword, passwordToken);

Expand Down
Loading

0 comments on commit 5e624fc

Please sign in to comment.