chore: format code
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
const base64url = require('base64-url');
|
const base64url = require('base64-url')
|
||||||
const createHash = require('crypto').createHash;
|
const createHash = require('crypto').createHash
|
||||||
const forge = require('node-forge');
|
const forge = require('node-forge')
|
||||||
const NodeRSA = require('node-rsa');
|
const NodeRSA = require('node-rsa')
|
||||||
|
|
||||||
const PRIVATE_KEY_PEM =
|
const PRIVATE_KEY_PEM =
|
||||||
'-----BEGIN RSA PRIVATE KEY-----\n' +
|
'-----BEGIN RSA PRIVATE KEY-----\n' +
|
||||||
@@ -30,7 +30,7 @@ const PRIVATE_KEY_PEM =
|
|||||||
'JEgWBQKBgQDKD+2Yh1/rUzu15lbPH0JSpozUinuFjePieR/4n+5CtEUxWJ2f0WeK\n' +
|
'JEgWBQKBgQDKD+2Yh1/rUzu15lbPH0JSpozUinuFjePieR/4n+5CtEUxWJ2f0WeK\n' +
|
||||||
's4XWWf2qgUccjpiGju2UR840mgWROoZ8BfSTd5tg1F7bo0HMgu2hu0RIRpZcRhsA\n' +
|
's4XWWf2qgUccjpiGju2UR840mgWROoZ8BfSTd5tg1F7bo0HMgu2hu0RIRpZcRhsA\n' +
|
||||||
'Cd0GrJvf1t0QIdDCXAy+RpgU1SLSq4Q6Lomc0WA5C5nBw9RKEUOV9A==\n' +
|
'Cd0GrJvf1t0QIdDCXAy+RpgU1SLSq4Q6Lomc0WA5C5nBw9RKEUOV9A==\n' +
|
||||||
'-----END RSA PRIVATE KEY-----\n';
|
'-----END RSA PRIVATE KEY-----\n'
|
||||||
|
|
||||||
const PUBLIC_KEY_PEM =
|
const PUBLIC_KEY_PEM =
|
||||||
'-----BEGIN PUBLIC KEY-----\n' +
|
'-----BEGIN PUBLIC KEY-----\n' +
|
||||||
@@ -41,84 +41,84 @@ const PUBLIC_KEY_PEM =
|
|||||||
'qXHP6AwKZXpT6jCzjzq9uyHxVcudqw6j0kQw48/A5A6AN5fIVy1cKnd0sKdqRX1N\n' +
|
'qXHP6AwKZXpT6jCzjzq9uyHxVcudqw6j0kQw48/A5A6AN5fIVy1cKnd0sKdqRX1N\n' +
|
||||||
'UqVoiOrO4jaDB1IdLD+YmRE/JjOHsWIMElYCPxKqnsNo6VCslGX/ziinArHhqRBr\n' +
|
'UqVoiOrO4jaDB1IdLD+YmRE/JjOHsWIMElYCPxKqnsNo6VCslGX/ziinArHhqRBr\n' +
|
||||||
'HwIDAQAB\n' +
|
'HwIDAQAB\n' +
|
||||||
'-----END PUBLIC KEY-----\n';
|
'-----END PUBLIC KEY-----\n'
|
||||||
|
|
||||||
const createCertificate = ({
|
const createCertificate = ({
|
||||||
publicKey,
|
publicKey,
|
||||||
privateKey,
|
privateKey,
|
||||||
jwksOrigin,
|
jwksOrigin
|
||||||
}) => {
|
}) => {
|
||||||
const cert = forge.pki.createCertificate();
|
const cert = forge.pki.createCertificate()
|
||||||
cert.publicKey = publicKey;
|
cert.publicKey = publicKey
|
||||||
cert.serialNumber = '123';
|
cert.serialNumber = '123'
|
||||||
const attrs = [
|
const attrs = [
|
||||||
{
|
{
|
||||||
name: 'commonName',
|
name: 'commonName',
|
||||||
value: `${jwksOrigin}`,
|
value: `${jwksOrigin}`
|
||||||
},
|
}
|
||||||
];
|
]
|
||||||
cert.validity.notBefore = new Date();
|
cert.validity.notBefore = new Date()
|
||||||
cert.validity.notAfter = new Date();
|
cert.validity.notAfter = new Date()
|
||||||
cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 1);
|
cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 1)
|
||||||
cert.setSubject(attrs);
|
cert.setSubject(attrs)
|
||||||
cert.setIssuer(attrs);
|
cert.setIssuer(attrs)
|
||||||
cert.sign(privateKey);
|
cert.sign(privateKey)
|
||||||
return forge.pki.certificateToPem(cert)
|
return forge.pki.certificateToPem(cert)
|
||||||
};
|
}
|
||||||
|
|
||||||
const getCertThumbprint = (certificate) => {
|
const getCertThumbprint = (certificate) => {
|
||||||
const shasum = createHash('sha1');
|
const shasum = createHash('sha1')
|
||||||
const der = Buffer.from(certificate).toString('binary');
|
const der = Buffer.from(certificate).toString('binary')
|
||||||
shasum.update(der);
|
shasum.update(der)
|
||||||
return shasum.digest('base64')
|
return shasum.digest('base64')
|
||||||
};
|
}
|
||||||
|
|
||||||
const createKeyPair = () => {
|
const createKeyPair = () => {
|
||||||
const privateKey = forge.pki.privateKeyFromPem(PRIVATE_KEY_PEM);
|
const privateKey = forge.pki.privateKeyFromPem(PRIVATE_KEY_PEM)
|
||||||
const publicKey = forge.pki.publicKeyFromPem(PUBLIC_KEY_PEM);
|
const publicKey = forge.pki.publicKeyFromPem(PUBLIC_KEY_PEM)
|
||||||
return {
|
return {
|
||||||
privateKey,
|
privateKey,
|
||||||
publicKey,
|
publicKey
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
const bnToB64 = (bn) => {
|
const bnToB64 = (bn) => {
|
||||||
let hex = BigInt(bn).toString(16);
|
let hex = BigInt(bn).toString(16)
|
||||||
if (hex.length % 2) {
|
if (hex.length % 2) {
|
||||||
hex = '0' + hex;
|
hex = '0' + hex
|
||||||
}
|
}
|
||||||
|
|
||||||
const bin = [];
|
const bin = []
|
||||||
let i = 0;
|
let i = 0
|
||||||
let d;
|
let d
|
||||||
let b;
|
let b
|
||||||
while (i < hex.length) {
|
while (i < hex.length) {
|
||||||
d = parseInt(hex.slice(i, i + 2), 16);
|
d = parseInt(hex.slice(i, i + 2), 16)
|
||||||
b = String.fromCharCode(d);
|
b = String.fromCharCode(d)
|
||||||
bin.push(b);
|
bin.push(b)
|
||||||
i += 2;
|
i += 2
|
||||||
}
|
}
|
||||||
|
|
||||||
return Buffer.from(bin.join(''), 'binary').toString('base64');
|
return Buffer.from(bin.join(''), 'binary').toString('base64')
|
||||||
};
|
}
|
||||||
|
|
||||||
const setup = (jwksOrigin) => {
|
const setup = (jwksOrigin) => {
|
||||||
const {privateKey, publicKey} = createKeyPair();
|
const { privateKey, publicKey } = createKeyPair()
|
||||||
const certPem = createCertificate({
|
const certPem = createCertificate({
|
||||||
jwksOrigin,
|
jwksOrigin,
|
||||||
privateKey,
|
privateKey,
|
||||||
publicKey,
|
publicKey
|
||||||
});
|
})
|
||||||
const certDer = forge.util.encode64(
|
const certDer = forge.util.encode64(
|
||||||
forge.asn1
|
forge.asn1
|
||||||
.toDer(forge.pki.certificateToAsn1(forge.pki.certificateFromPem(certPem)))
|
.toDer(forge.pki.certificateToAsn1(forge.pki.certificateFromPem(certPem)))
|
||||||
.getBytes()
|
.getBytes()
|
||||||
);
|
)
|
||||||
const thumbprint = base64url.encode(getCertThumbprint(certDer));
|
const thumbprint = base64url.encode(getCertThumbprint(certDer))
|
||||||
|
|
||||||
const helperKey = new NodeRSA();
|
const helperKey = new NodeRSA()
|
||||||
helperKey.importKey(forge.pki.privateKeyToPem(privateKey));
|
helperKey.importKey(forge.pki.privateKeyToPem(privateKey))
|
||||||
const {n: modulus, e: exponent} = helperKey.exportKey('components');
|
const { n: modulus, e: exponent } = helperKey.exportKey('components')
|
||||||
|
|
||||||
return {
|
return {
|
||||||
privateKey: forge.pki.privateKeyToPem(privateKey),
|
privateKey: forge.pki.privateKeyToPem(privateKey),
|
||||||
@@ -127,6 +127,6 @@ const setup = (jwksOrigin) => {
|
|||||||
exponent: bnToB64(exponent),
|
exponent: bnToB64(exponent),
|
||||||
modulus: modulus.toString('base64')
|
modulus: modulus.toString('base64')
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
module.exports = setup;
|
module.exports = setup
|
||||||
|
|||||||
Reference in New Issue
Block a user