0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-06 20:40:08 -05:00

refactor(core): remove redundant isBuffer('base64url') (#1749)

This commit is contained in:
Xiao Yijun 2022-08-08 15:33:12 +08:00 committed by GitHub
parent 00bab4c095
commit 5624ea423a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,26 +1,12 @@
/**
* Theses codes comes from [node-oidc-provider](https://github.com/panva/node-oidc-provider):
* - [initialize_keystore.js](https://github.com/panva/node-oidc-provider/blob/9da61e9c9dc6152cd1140d42ea06abe1d812c286/lib/helpers/initialize_keystore.js#L13-L36)
* - [base64url.js](https://github.com/panva/node-oidc-provider/blob/9da61e9c9dc6152cd1140d42ea06abe1d812c286/lib/helpers/base64url.js)
*/
import { createHash } from 'crypto';
import { JWK, KeyLike, exportJWK as joseExportJWK } from 'jose';
const fromBase64 = (base64: string) =>
base64.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
const encodeBuffer = (buf: Buffer) => {
const base64url = buf.toString('base64url');
if (Buffer.isEncoding('base64url')) {
return base64url;
}
return fromBase64(base64url);
};
const getCalculateKidComponents = (jwk: JWK) => {
switch (jwk.kty) {
case 'RSA':
@ -49,7 +35,7 @@ const getCalculateKidComponents = (jwk: JWK) => {
const calculateKid = (jwk: JWK) => {
const components = getCalculateKidComponents(jwk);
return encodeBuffer(createHash('sha256').update(JSON.stringify(components)).digest());
return createHash('sha256').update(JSON.stringify(components)).digest().toString('base64url');
};
export const exportJWK = async (key: KeyLike | Uint8Array): Promise<JWK> => {