Solicitud de tokens correcta

This commit is contained in:
2026-01-27 12:31:37 +01:00
parent e8c278aa41
commit fc6b995ebd
2 changed files with 32 additions and 24 deletions

View File

@@ -80,24 +80,24 @@ export class JWTToken<T> {
}) {
const strHeader = JSON.stringify(args.header)
const base64Header = Buffer.from(strHeader).toString("base64url")
let msg = base64Header
let token = base64Header
if (args.payload != undefined) {
const strPayload = JSON.stringify(args.payload)
const base64payload = Buffer.from(strPayload).toString("base64url")
msg += ("." + base64payload)
token += ("." + base64payload)
}
if (args.sigantureData != undefined) {
const base64signature = signJWT({
algorythm: args.sigantureData.algorythm,
privateKey: args.sigantureData.privateKey,
data: msg
data: token
}).toString("base64url")
msg += ("." + base64signature)
token += ("." + base64signature)
}
console.log("JWT", msg)
return token
}

View File

@@ -8,6 +8,7 @@ import {
} from "#shared/domain/JWT"
import axios, { AxiosError } from "axios";
import { sign } from "node:crypto"
import { ClientRequest } from "node:http";
type GrantAccessRequestBody = {
grant_type: string,
@@ -95,33 +96,40 @@ export class JWTService {
console.warn("Se está intentado conseguir un token sin expirar el anterior")
}
console.log("headers", addIATHeaders(DEFAULT_HEADERS))
console.log("body", DEFAULT_BODY)
console.log("keypath", __dirname + "/../obj.pem")
const httpheaders = addIATHeaders(DEFAULT_HEADERS)
const jwtHeaders = {
alg: "RS256",
typ: "JWT",
kid: env.OBJ_KID
}
const jwtData = addIATHeaders({
sub: env.OBJ_CLIENT_ID,
iss: env.OBJ_CLIENT_ID,
aud: "https://idp.docapost.io/auth/realms/GETWAY",
jti: Date.now().toString(),
})
const key = fs.readFileSync(__dirname + "/../obj.pem", "utf8")
const msg = Buffer.from("test")
const signature = sign(
"sha256",
Buffer.from(msg),
key
)
JWTToken.fromParts({
header: { alg: "RS256", typ: "JWT", kid: "1234" },
payload: {
"iss": "savefamily_rest_ws",
"aud": "https://idp.docapost.io/auth/realms/GETWAY",
},
const token = JWTToken.fromParts({
header: jwtHeaders,
payload: jwtData,
sigantureData: {
algorythm: "sha256",
privateKey: key
}
})
console.log("signature", signature.toString("base64url"))
return;
const bodyWithtoken = {
...DEFAULT_BODY,
client_assertion: token
}
console.log("body", bodyWithtoken)
const req = axios.post(GET_TOKEN_URL,
DEFAULT_BODY,
{
...DEFAULT_BODY,
client_assertion: token
},
{
headers: addIATHeaders(DEFAULT_HEADERS)
}