Skip to content

Commit

Permalink
refactor(oidc): remove base64.js dependency (#1292) (release)
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-chervet committed Feb 10, 2024
1 parent cc60cf0 commit 2511648
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 16 deletions.
3 changes: 1 addition & 2 deletions packages/oidc-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@axa-fr/oidc-client",
"version": "7.17.0",
"version": "7.16.0",
"private": false,
"type": "module",
"main": "./dist/index.umd.cjs",
Expand All @@ -27,7 +27,6 @@
"@testing-library/jest-dom": "6.4.2",
"@testing-library/react": "14.2.1",
"@vitest/coverage-v8": "1.2.2",
"base64-js": "^1.5.1",
"cpy": "11.0.0",
"cpy-cli": "^5.0.0",
"rimraf": "5.0.5",
Expand Down
10 changes: 3 additions & 7 deletions packages/oidc-client/src/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as base64 from 'base64-js';
import {uint8ToUrlBase64} from "./jwt";


const cryptoInfo = () => {
const hasCrypto = typeof window !== 'undefined' && !!(window.crypto as any);
Expand All @@ -16,11 +17,6 @@ const bufferToString = (buffer: Uint8Array) => {
return state.join('');
};

const urlSafe = (buffer: Uint8Array): string => {
const encoded = base64.fromByteArray(new Uint8Array(buffer));
return encoded.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
};

export const generateRandom = (size: number) => {
const buffer = new Uint8Array(size);
const { hasCrypto } = cryptoInfo();
Expand Down Expand Up @@ -48,7 +44,7 @@ export function textEncodeLite(str: string) {
export function base64urlOfHashOfASCIIEncodingAsync(code: string):Promise<string> {
return new Promise((resolve, reject) => {
crypto.subtle.digest('SHA-256', textEncodeLite(code)).then(buffer => {
return resolve(urlSafe(new Uint8Array(buffer)));
return resolve(uint8ToUrlBase64(new Uint8Array(buffer)));
}, error => reject(error));
});
}
Expand Down
6 changes: 2 additions & 4 deletions packages/oidc-client/src/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,16 @@ function utf8ToBinaryString(str) {
const escstr = encodeURIComponent(str);
// replaces any uri escape sequence, such as %0A,
// with binary escape, such as 0x0A
const binstr = escstr.replace(/%([0-9A-F]{2})/g, function (match, p1) {
return escstr.replace(/%([0-9A-F]{2})/g, function (match, p1) {
return String.fromCharCode(parseInt(p1, 16));
});

return binstr;
}

// Uint8Array to URL Safe Base64
//
// the shortest distant between two encodings... binary string
// @ts-ignore
function uint8ToUrlBase64(uint8) {
export const uint8ToUrlBase64 =(uint8: Uint8Array) => {
let bin = '';
// @ts-ignore
uint8.forEach(function(code) {
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2511648

Please sign in to comment.