Skip to content

Commit cd21b71

Browse files
committed
PING et backendconfig
1 parent 5f2d8ca commit cd21b71

File tree

10 files changed

+158
-24
lines changed

10 files changed

+158
-24
lines changed

docs/DAEMON.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
# Communication avec sesame-daemon
2+
Voir doc sesame-daemon

src/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { PasswdModule } from './passwd/passwd.module';
55
import { BullModule } from '@nestjs/bullmq';
66
import { AuthModule } from './auth/auth.module';
77
import {ConfigModule, ConfigService} from '@nestjs/config';
8+
import { BackendsModule } from './backends/backends.module';
89

910
import config from './config'
1011

@@ -14,6 +15,7 @@ import config from './config'
1415
load: [config]
1516
}),
1617
PasswdModule,
17-
AuthModule]
18+
AuthModule,
19+
BackendsModule]
1820
})
1921
export class AppModule {}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { BackendsController } from './backends.controller';
3+
import { BackendsService } from './backends.service';
4+
5+
describe('BackendsController', () => {
6+
let controller: BackendsController;
7+
8+
beforeEach(async () => {
9+
const module: TestingModule = await Test.createTestingModule({
10+
controllers: [BackendsController],
11+
providers: [BackendsService],
12+
}).compile();
13+
14+
controller = module.get<BackendsController>(BackendsController);
15+
});
16+
17+
it('should be defined', () => {
18+
expect(controller).toBeDefined();
19+
});
20+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {Body, Controller, Logger, Post,Get, Res, UseGuards} from '@nestjs/common';
2+
import {BackendsService} from './backends.service';
3+
import {ChangePasswordDto} from "../passwd/dto/change-password.dto";
4+
import {Queue, QueueEvents} from "bullmq";
5+
import {ApiBearerAuth, ApiOperation, ApiResponse, ApiTags} from "@nestjs/swagger";
6+
import {AuthGuard} from "@nestjs/passport";
7+
import {Response} from "express";
8+
import {PasswdService} from "../passwd/passwd.service";
9+
10+
@Controller('backends')
11+
@ApiTags('backends')
12+
export class BackendsController {
13+
private readonly logger = new Logger(BackendsController.name);
14+
15+
constructor(private backendsService: BackendsService) {
16+
}
17+
18+
@Get('list')
19+
@ApiOperation({summary: 'List backends from daemon'})
20+
@ApiResponse({status: 200, description: 'List ok '})
21+
@ApiBearerAuth()
22+
@UseGuards(AuthGuard("api-key"))
23+
async list(@Res() res: Response): Promise<Response> {
24+
const backend = await this.backendsService.list()
25+
return res.status(200).json(backend);
26+
}
27+
@Get('alive')
28+
@ApiOperation({summary: 'test backends '})
29+
@ApiResponse({status: 200, description: 'command executed '})
30+
@ApiBearerAuth()
31+
@UseGuards(AuthGuard("api-key"))
32+
async alive(@Res() res: Response): Promise<Response> {
33+
const backend = await this.backendsService.alive()
34+
return res.status(200).json(backend);
35+
}
36+
}

src/backends/backends.module.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Module } from '@nestjs/common';
2+
import { BackendsService } from './backends.service';
3+
import { BackendsController } from './backends.controller';
4+
import {ConfigModule} from "@nestjs/config";
5+
6+
@Module({
7+
imports:[ConfigModule],
8+
controllers: [BackendsController],
9+
providers: [BackendsService],
10+
})
11+
export class BackendsModule {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { BackendsService } from './backends.service';
3+
4+
describe('BackendsService', () => {
5+
let service: BackendsService;
6+
7+
beforeEach(async () => {
8+
const module: TestingModule = await Test.createTestingModule({
9+
providers: [BackendsService],
10+
}).compile();
11+
12+
service = module.get<BackendsService>(BackendsService);
13+
});
14+
15+
it('should be defined', () => {
16+
expect(service).toBeDefined();
17+
});
18+
});

src/backends/backends.service.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Injectable } from '@nestjs/common';
2+
import Redis from "ioredis";
3+
import crypto from "crypto";
4+
import {ConfigService} from "@nestjs/config";
5+
import {Queue, QueueEvents} from "bullmq";
6+
7+
@Injectable()
8+
export class BackendsService {
9+
10+
constructor(private readonly configService:ConfigService) {}
11+
async list() {
12+
const redisConfig={host:this.configService.get('redis.host'),port:this.configService.get('redis.port')}
13+
const queue=new Queue(this.configService.get('nameQueue'),{connection:redisConfig})
14+
const queueEvents = new QueueEvents(this.configService.get('nameQueue'),{connection: redisConfig})
15+
const job=await queue.add('LISTBACKEND',"x")
16+
queueEvents.on('failed',(errors)=>{
17+
console.log(errors)
18+
})
19+
return await job.waitUntilFinished(queueEvents,30000)
20+
}
21+
async alive() {
22+
const redisConfig={host:this.configService.get('redis.host'),port:this.configService.get('redis.port')}
23+
const queue=new Queue(this.configService.get('nameQueue'),{connection:redisConfig})
24+
const queueEvents = new QueueEvents(this.configService.get('nameQueue'),{connection: redisConfig})
25+
const job=await queue.add('PING',"x")
26+
queueEvents.on('failed',(errors)=>{
27+
console.log(errors)
28+
})
29+
return await job.waitUntilFinished(queueEvents,30000)
30+
}
31+
32+
33+
34+
35+
}

src/passwd/passwd.controller.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class PasswdController {
2323

2424
@Post('change')
2525
@ApiOperation({summary: 'change password'})
26-
@ApiResponse({status: 201, description: 'Password has been successfully changed.'})
26+
@ApiResponse({status: 200, description: 'Password has been successfully changed.'})
2727
@ApiResponse({status: 403, description: 'Old password wrong'})
2828
@ApiResponse({status: 500, description: 'Backend error'})
2929
@ApiBearerAuth()
@@ -35,27 +35,27 @@ export class PasswdController {
3535
data.data.uid = cpwd.uid
3636
this.logger.log('call passwd change for : ' + cpwd.uid)
3737
if (data.data.status === 0) {
38-
return res.status(201).json(data);
38+
return res.status(200).json(data);
3939
} else {
4040
if (data.data.status === 1) {
4141
return res.status(403).json(data);
4242
}
43-
this.logger.log('ERROR : ' + data)
44-
return res.status(201).json(data);
43+
return res.status(200).json(data);
4544
}
4645
}
4746

4847
@Post('gettoken')
4948
@ApiOperation({summary: 'ask token for reseting password'})
50-
@ApiResponse({status: 201, description: 'Token', content: {}})
49+
@ApiResponse({status: 200, description: 'Token', content: {}})
5150
@ApiResponse({status: 500, description: 'Backend error'})
5251
@ApiBearerAuth()
5352
@UseGuards(AuthGuard("api-key"))
5453
async gettoken(@Body() asktoken: AskTokenDto, @Res() res: Response): Promise<Response> {
54+
this.logger.log('GetToken for : ' + asktoken.uid)
5555
const data = await this.passwdService.askToken(asktoken)
5656
const ret = {token: data}
5757
console.log(ret)
58-
return res.status(201).json(ret);
58+
return res.status(200).json(ret);
5959
}
6060
@Post('verifytoken')
6161
@ApiOperation({summary: 'ask token for reseting password'})
@@ -64,29 +64,32 @@ export class PasswdController {
6464
@ApiBearerAuth()
6565
@UseGuards(AuthGuard("api-key"))
6666
async verifyToken(@Body() token:VerifyTokenDto ,@Res() res: Response): Promise<Response> {
67+
this.logger.log('Verify token : ' + token.token)
6768
const ok=await this.passwdService.verifyToken(token.token)
69+
console.log('reponse : ' + ok)
6870
if (ok === true){
69-
return res.status(201).json(token)
71+
console.log('reponse : 200')
72+
return res.status(200).json({'status':0})
7073
}else{
71-
return res.status(500).json(token)
74+
return res.status(200).json({'status':1})
7275
}
7376

7477
}
7578
@Post('reset')
7679
@ApiOperation({summary: 'reset password'})
77-
@ApiResponse({status: 201, description: 'Reset OK'})
80+
@ApiResponse({status: 200, description: 'Reset OK'})
7881
@ApiResponse({status: 500, description: 'Reset KO'})
7982
@ApiBearerAuth()
8083
@UseGuards(AuthGuard("api-key"))
8184
async reset(@Body() data:ResetPasswordDto,@Res()res:Response):Promise<Response>{
82-
const tokenData=await this.passwdService.decryptToken(data.token)
83-
if (Object.keys(tokenData).length === 0){
84-
return res.status(500).json({'status':1,'error':'invalid token'})
85-
}else{
86-
87-
return res.status(200).json({'status':0,'error':''})
88-
85+
//this.logger.log('password reset : ' + data)
86+
const resetData=await this.passwdService.reset(data)
87+
//this.logger.log(resetData)
88+
if (resetData.status === 0){
89+
return res.status(200).json(resetData)
8990

91+
}else{
92+
return res.status(200).json({'status':1,'error':'invalid token'})
9093
}
9194

9295
}

src/passwd/passwd.service.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ export class PasswdService {
3737
}
3838
async verifyToken(token){
3939
const data=await this.decryptToken(token)
40-
console.log('r : '+JSON.stringify(data))
40+
console.log('r (verifyToken service) : ')
41+
console.log(data)
42+
console.log('longueur' + Object.keys(data).length)
4143
if (Object.keys(data).length === 0){
4244
return false
4345
}else{
46+
console.log('return true')
4447
return true
4548
}
4649

@@ -61,21 +64,26 @@ export class PasswdService {
6164
decipher.setAuthTag(Buffer.from(cypherData.tag, 'base64'));
6265
let plaintext = decipher.update(token, 'base64', 'ascii');
6366
console.log('texte : ' + plaintext)
67+
//delete key
68+
//redis.del([token])
6469
return JSON.parse(plaintext)
6570
}else{
6671
return {}
6772
}
6873
}
6974
async reset(data:ResetPasswordDto){
7075
const tokenData=await this.decryptToken(data.token)
76+
console.log(tokenData)
7177
if (Object.keys(tokenData).length === 0){
72-
return false
78+
return {'status':1,'error':'invalid token'}
7379
}
7480
const redisConfig={host:this.configService.get('redis.host'),port:this.configService.get('redis.port')}
7581
const queue=new Queue(this.configService.get('nameQueue'),{connection:redisConfig})
7682
const queueEvents = new QueueEvents(this.configService.get('nameQueue'),{connection: redisConfig})
77-
const job=await queue.add('RESETPWD',tokenData)
83+
const backendData={'uid':tokenData.uid,'newPassword':data.newPassword}
84+
const job=await queue.add('RESETPWD',backendData)
7885
queueEvents.on('failed',(errors)=>{
86+
console.log('Erreur queue')
7987
console.log(errors)
8088
})
8189
return await job.waitUntilFinished(queueEvents,30000)

src/swagger.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {DocumentBuilder, SwaggerModule} from "@nestjs/swagger";
22
export default function swagger(app){
33
const config = new DocumentBuilder()
4-
.setTitle('Password manager')
5-
.setDescription('Change reset password')
6-
.setVersion('1.0')
7-
.addTag('passwd')
4+
.setTitle('Sesame Orchestrator API')
5+
.setDescription('Sesame')
6+
.setVersion('0.1')
7+
.addTag('sesame-orchestrator')
88
.addBearerAuth({type: 'http',scheme : 'bearer'})
99
.build();
1010
const document = SwaggerModule.createDocument(app, config);

0 commit comments

Comments
 (0)