Skip to content

Instantly share code, notes, and snippets.

@joellobo
Created December 6, 2019 13:00
Show Gist options
  • Save joellobo/f43ef0047968c6adebc79ff0beff9bd7 to your computer and use it in GitHub Desktop.
Save joellobo/f43ef0047968c6adebc79ff0beff9bd7 to your computer and use it in GitHub Desktop.
import fs from "fs";
import path from "path";
import jwa from "jwa";
import "dotenv/config";
const JWT = (function() {
let keystore = {
nomeArquivo: null,
diretorio: null
};
function validar(tokenTexto) {
let jwtValido = false;
if (tokenTexto) {
try {
let itemCertificadoAtualBuffer = this.carregar(keystore);
let tokenJWT = this.parse(tokenTexto);
let alg = jwa(tokenJWT.header.alg);
jwtValido = alg.verify(
tokenJWT.input,
tokenJWT.signature,
itemCertificadoAtualBuffer
);
return jwtValido;
} catch (erroJWT) {
jwtValido = false;
}
} else {
return jwtValido;
}
}
function carregar({ nomeArquivo, diretorio }) {
console.log(
nomeArquivo,
diretorio,
!nomeArquivo,
!nomeArquivo && !diretorio
);
if (nomeArquivo && diretorio) {
let arquivo = path.parse(nomeArquivo);
if (arquivo.ext === ".p12") {
let arquivoPath = path.join(diretorio, nomeArquivo);
console.log(arquivoPath);
return fs.readFileSync(arquivoPath);
}
}
return null;
}
function parse(token) {
let tokenJWT = null;
if (token) {
tokenJWT = {};
let tempArray = token.split(".");
tokenJWT.header = JSON.parse(Buffer.from(tempArray[0], "base64"));
tokenJWT.payload = JSON.parse(Buffer.from(tempArray[1], "base64"));
tokenJWT.signature = tempArray[2];
tokenJWT.input = tempArray[0] + "." + tempArray[1];
console.log(`tokenJWT:${tokenJWT}`);
return tokenJWT;
}
return tokenJWT;
}
return (function(keystore) {
var oQuery = new Function();
oQuery.keystore = keystore;
oQuery.validar = validar;
oQuery.carregar = carregar;
oQuery.parse = parse;
return oQuery;
});
})();
export default JWT;
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
import fs from "fs";
import path from "path";
import jwa from "jwa";
import "dotenv/config";
const JWT = {
keystore = {
nomeArquivo: null,
diretorio: null
},
validar(tokenTexto) {
let jwtValido = false;
if (tokenTexto) {
try {
let itemCertificadoAtualBuffer = this.carregar(keystore);
let tokenJWT = this.parse(tokenTexto);
let alg = jwa(tokenJWT.header.alg);
jwtValido = alg.verify(
tokenJWT.input,
tokenJWT.signature,
itemCertificadoAtualBuffer
);
return jwtValido;
} catch (erroJWT) {
jwtValido = false;
}
} else {
return jwtValido;
}
},
carregar({ nomeArquivo, diretorio }) {
console.log(
nomeArquivo,
diretorio,
!nomeArquivo,
!nomeArquivo && !diretorio
);
if (nomeArquivo && diretorio) {
let arquivo = path.parse(nomeArquivo);
if (arquivo.ext === ".p12") {
let arquivoPath = path.join(diretorio, nomeArquivo);
console.log(arquivoPath);
return fs.readFileSync(arquivoPath);
}
}
return null;
},
parse(token) {
let tokenJWT = null;
if (token) {
tokenJWT = {};
let tempArray = token.split(".");
tokenJWT.header = JSON.parse(Buffer.from(tempArray[0], "base64"));
tokenJWT.payload = JSON.parse(Buffer.from(tempArray[1], "base64"));
tokenJWT.signature = tempArray[2];
tokenJWT.input = tempArray[0] + "." + tempArray[1];
console.log(`tokenJWT:${tokenJWT}`);
return tokenJWT;
}
return tokenJWT;
}
};
export default JWT;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment