66 lines
1.5 KiB
TypeScript
66 lines
1.5 KiB
TypeScript
import path from "path";
|
|
import fs from 'fs';
|
|
import { Result } from "sim-shared/domain/Result.js";
|
|
import { PgClient } from "sim-shared/infrastructure/PgClient.js";
|
|
|
|
type TokenLine = {
|
|
id: number,
|
|
url: string,
|
|
user_name: string,
|
|
pass: string,
|
|
brand_id: string,
|
|
cert_file: string,
|
|
key_file: string,
|
|
ca_file: string,
|
|
p12_file: string,
|
|
cert_password: string,
|
|
token: string
|
|
}
|
|
|
|
/**
|
|
* Repositorio para usar los tokens guardados en la bdd de intranet o en un archivo
|
|
*/
|
|
export class LegacyJWTTokenRepository {
|
|
constructor(
|
|
// En prod (no deberia usarse) tiene que apuntar a intranet
|
|
private pgClient: PgClient
|
|
) {
|
|
}
|
|
|
|
public async getTokenFromDB(): Promise<Result<string, string>> {
|
|
const query = "SELECT * FROM alai_api_credentials;"
|
|
|
|
try {
|
|
const res = await this.pgClient.query<TokenLine>(query)
|
|
if (res.rowCount == 0) {
|
|
return {
|
|
error: "Error recuperando el token actual"
|
|
}
|
|
} else {
|
|
return {
|
|
data: res.rows[0].token
|
|
}
|
|
}
|
|
} catch (e) {
|
|
return {
|
|
error: String(e)
|
|
}
|
|
}
|
|
}
|
|
|
|
public static getTokenFromFile(dir: string, file: string): Result<string, string> {
|
|
try {
|
|
const tokenPath = path.join(dir, file)
|
|
const fileContent = fs.readFileSync(tokenPath).toString()
|
|
return {
|
|
data: fileContent
|
|
}
|
|
} catch (e) {
|
|
return {
|
|
error: "[d!] No se ha podido leer el archivo del token de debug" + String(e)
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|