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