diff --git a/src/app.module.ts b/src/app.module.ts index 0bc7d3c..9180627 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,9 +1,10 @@ +import 'dotenv/config'; import { Module } from '@nestjs/common'; import { MongooseModule } from '@nestjs/mongoose'; import { TasksModule } from './services/tasks/tasks.module'; import { ScheduleModule } from '@nestjs/schedule'; import { VideoModule } from './repositories/video/video.module'; -import { ConfigModule } from '@nestjs/config'; +import { ConfigModule, ConfigService } from '@nestjs/config'; import { PublishingModule } from './services/publishing/publishing.module'; import { CreatorModule } from './repositories/creator/creator.module'; import { UserModule } from './repositories/user/user.module'; @@ -72,7 +73,14 @@ const mongoUrl = process.env.CORE_MONGODB_URL || 'mongodb://mongo:27017'; UserAccountModule, ApiModule, VotingModule, - JwtModule, + JwtModule.registerAsync({ + imports: [ConfigModule], + inject: [ConfigService], + useFactory: async (configService: ConfigService) => ({ + secretOrPrivateKey: configService.get('JWT_PRIVATE_KEY'), + signOptions: { expiresIn: '30d' }, + }), + }), ], controllers: [], providers: [], diff --git a/src/services/api/api.module.ts b/src/services/api/api.module.ts index 48c8048..179b861 100644 --- a/src/services/api/api.module.ts +++ b/src/services/api/api.module.ts @@ -1,3 +1,4 @@ +import 'dotenv/config'; import { Module } from '@nestjs/common'; import { ApiController } from './api.controller'; import { AuthModule } from '../auth/auth.module'; @@ -8,6 +9,7 @@ import { EmailModule } from '../email/email.module'; import { LinkedAccountModule } from '../../repositories/linked-accounts/linked-account.module'; import { RequireHiveVerify } from './utils'; import { JwtModule } from '@nestjs/jwt'; +import { ConfigModule, ConfigService } from '@nestjs/config'; @Module({ imports: [ @@ -17,9 +19,13 @@ import { JwtModule } from '@nestjs/jwt'; HiveModule, LinkedAccountModule, EmailModule, - JwtModule.register({ - privateKey: process.env.JWT_PRIVATE_KEY, - signOptions: { expiresIn: '30d' }, + JwtModule.registerAsync({ + imports: [ConfigModule], + inject: [ConfigService], + useFactory: async (configService: ConfigService) => ({ + secretOrPrivateKey: configService.get('JWT_PRIVATE_KEY'), + signOptions: { expiresIn: '30d' }, + }), }), ], controllers: [ApiController], diff --git a/src/services/api/dto/LoginSingleton.dto.ts b/src/services/api/dto/LoginSingleton.dto.ts index be6ba88..c969be8 100644 --- a/src/services/api/dto/LoginSingleton.dto.ts +++ b/src/services/api/dto/LoginSingleton.dto.ts @@ -1,5 +1,13 @@ import { IsNotEmpty } from 'class-validator'; import { ApiProperty } from '@nestjs/swagger'; +import crypto from 'crypto'; +import { PrivateKey } from '@hiveio/dhive'; + +const privateKey = PrivateKey.fromSeed(crypto.randomBytes(32).toString('hex')); +const message = { account: 'sisygoboom', ts: Date.now() }; +const signature = privateKey + .sign(crypto.createHash('sha256').update(JSON.stringify(message)).digest()) + .toString(); /** * Data Transfer Object (DTO) for login singleton hive @@ -16,7 +24,7 @@ export class LoginSingletonHiveDto { ts: { type: 'number', description: 'Timestamp of the proof payload', - example: 1625158800, + example: Date.now(), }, account: { type: 'string', @@ -24,6 +32,7 @@ export class LoginSingletonHiveDto { example: 'user123', }, }, + example: message, }) proof_payload: { ts: number; @@ -37,7 +46,7 @@ export class LoginSingletonHiveDto { @ApiProperty({ description: 'Proof string for authentication', type: 'string', - example: 'proofString123', + example: signature, }) proof: string; } diff --git a/src/services/auth/auth.module.ts b/src/services/auth/auth.module.ts index 9ab5a58..7fa8417 100644 --- a/src/services/auth/auth.module.ts +++ b/src/services/auth/auth.module.ts @@ -1,8 +1,7 @@ -import 'dotenv/config'; import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; import { AuthService } from './auth.service'; import { PassportModule } from '@nestjs/passport'; -import { JwtModule, JwtService } from '@nestjs/jwt'; +import { JwtModule } from '@nestjs/jwt'; import { JwtStrategy, LocalStrategy } from './auth.strategy'; import { UserModule } from '../../repositories/user/user.module'; import { UserAccountModule } from '../../repositories/userAccount/user-account.module'; @@ -19,18 +18,25 @@ import { ConfigModule, ConfigService } from '@nestjs/config'; ConfigModule, UserModule, UserAccountModule, - UserModule, HiveAccountModule, HiveModule, EmailModule, SessionModule, PassportModule.register({ defaultStrategy: 'jwt' }), - JwtModule.register({ - privateKey: process.env.JWT_PRIVATE_KEY, - signOptions: { expiresIn: '30d' }, + JwtModule.registerAsync({ + imports: [ConfigModule], + inject: [ConfigService], + useFactory: async (configService: ConfigService) => { + const key = configService.get('JWT_PRIVATE_KEY'); + console.log(key); + return { + secretOrPrivateKey: key, + signOptions: { expiresIn: '30d' }, + }; + }, }), ], - providers: [AuthService, LocalStrategy, JwtService, ConfigService, JwtStrategy], + providers: [AuthService, LocalStrategy, JwtStrategy, ConfigService], controllers: [AuthController], exports: [AuthService], }) diff --git a/src/services/auth/auth.strategy.ts b/src/services/auth/auth.strategy.ts index aaccfaf..b61833d 100644 --- a/src/services/auth/auth.strategy.ts +++ b/src/services/auth/auth.strategy.ts @@ -5,6 +5,7 @@ import { Injectable, UnauthorizedException } from '@nestjs/common'; import { AuthService } from './auth.service'; import { ExtractJwt } from 'passport-jwt'; import 'dotenv/config'; +import { ConfigService } from '@nestjs/config'; @Injectable() export class LocalStrategy extends PassportStrategy(StrategyLocal) { @@ -23,8 +24,8 @@ export class LocalStrategy extends PassportStrategy(StrategyLocal) { @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { - constructor() { - const jwtPrivateKey = process.env.JWT_PRIVATE_KEY; + constructor(configService: ConfigService) { + const jwtPrivateKey = configService.get('JWT_PRIVATE_KEY'); if (!jwtPrivateKey) throw new Error('Missing JWT_PRIVATE_KEY in .env'); super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), diff --git a/src/services/uploader/uploading.module.ts b/src/services/uploader/uploading.module.ts index fe14229..97f1985 100644 --- a/src/services/uploader/uploading.module.ts +++ b/src/services/uploader/uploading.module.ts @@ -9,6 +9,7 @@ import { PublishingModule } from '../publishing/publishing.module'; import { HiveModule } from '../../repositories/hive/hive.module'; import { JwtModule } from '@nestjs/jwt'; import { RequireHiveVerify, UserDetailsInterceptor } from '../api/utils'; +import { ConfigModule, ConfigService } from '@nestjs/config'; @Module({ imports: [ @@ -17,9 +18,13 @@ import { RequireHiveVerify, UserDetailsInterceptor } from '../api/utils'; IpfsModule, PublishingModule, HiveModule, - JwtModule.register({ - privateKey: process.env.JWT_PRIVATE_KEY, - signOptions: { expiresIn: '30d' }, + JwtModule.registerAsync({ + imports: [ConfigModule], + inject: [ConfigService], + useFactory: async (configService: ConfigService) => ({ + secretOrPrivateKey: configService.get('JWT_PRIVATE_KEY'), + signOptions: { expiresIn: '30d' }, + }), }), ], controllers: [UploadingController],