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:
parent
00bab4c095
commit
5624ea423a
1 changed files with 1 additions and 15 deletions
|
@ -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> => {
|
||||
|
|
Loading…
Reference in a new issue