Created
December 6, 2019 13:00
-
-
Save joellobo/f43ef0047968c6adebc79ff0beff9bd7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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