add SecureService

This commit is contained in:
Loïs Mansot 2023-09-21 14:09:06 +02:00
parent 92f10dd407
commit c4dcc16027
No known key found for this signature in database
GPG Key ID: 8CF1F4150DDA726D
2 changed files with 68 additions and 3 deletions

View File

@ -73,11 +73,16 @@ export class BackendVariables {
@IsNotEmpty() @IsNotEmpty()
public readonly MAILCHIMP_API_KEY!: string; public readonly MAILCHIMP_API_KEY!: string;
@IsNotEmpty()
public readonly SECURE_API_KEY!: string;
@IsNotEmpty()
public readonly SECURE_API_BASE_URL!: string;
@IsNotEmpty() @IsNotEmpty()
public readonly ENV!: string; public readonly ENV!: string;
public constructor() { public constructor() {
dotenv.config(); dotenv.config();
this.DATABASE_PORT = process.env["DATABASE_PORT"]!; this.DATABASE_PORT = process.env["DATABASE_PORT"]!;
this.DATABASE_HOST = process.env["DATABASE_HOST"]!; this.DATABASE_HOST = process.env["DATABASE_HOST"]!;
@ -102,10 +107,11 @@ export class BackendVariables {
this.ACCESS_TOKEN_SECRET = process.env["ACCESS_TOKEN_SECRET"]!; this.ACCESS_TOKEN_SECRET = process.env["ACCESS_TOKEN_SECRET"]!;
this.REFRESH_TOKEN_SECRET = process.env["REFRESH_TOKEN_SECRET"]!; this.REFRESH_TOKEN_SECRET = process.env["REFRESH_TOKEN_SECRET"]!;
this.MAILCHIMP_API_KEY = process.env["MAILCHIMP_API_KEY"]!; this.MAILCHIMP_API_KEY = process.env["MAILCHIMP_API_KEY"]!;
this.SECURE_API_KEY = process.env["SECURE_API_KEY"]!;
this.SECURE_API_BASE_URL = process.env["SECURE_API_BASE_URL"]!;
this.ENV = process.env["ENV"]!; this.ENV = process.env["ENV"]!;
} }
public async validate(groups?: string[]) { public async validate(groups?: string[]) {
const validationOptions = groups ? { groups } : undefined; const validationOptions = groups ? { groups } : undefined;
try { try {

View File

@ -0,0 +1,59 @@
import BaseService from "@Services/BaseService";
import { Service } from "typedi";
import { BackendVariables } from "@Common/config/variables/Variables";
@Service()
export default class SecureService extends BaseService {
constructor(protected variables: BackendVariables) {
super();
}
/**
* @description : anchor a sequence of hashes
* @throws {Error} If secure job cannot be created
*/
public async anchor(hash_sources: string[]) {
const url = new URL(this.variables.SECURE_API_BASE_URL.concat("/flows/v2/anchor"));
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
apiKey: this.variables.SECURE_API_KEY,
},
body: JSON.stringify({
hash_sources,
callback_url: "",
callback_config: {},
}),
});
return await response.json();
}
/**
* @description : verify if a sequence of hashes is anchored
* @throws {Error} If secure job cannot be found
*/
public async verify(hash_sources: string[]) {
const params = new URLSearchParams();
hash_sources.forEach((hash) => {
params.append("hash_sources", hash);
});
const url = new URL(this.variables.SECURE_API_BASE_URL.concat("/flows/v2/verify?").concat(params.toString()));
const response = await fetch(url, {
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
apiKey: this.variables.SECURE_API_KEY,
},
});
return await response.json();
}
}