Code Monkey home page Code Monkey logo

Comments (6)

moberwasserlechner avatar moberwasserlechner commented on August 10, 2024

Hey,

it's a quick fix I think I can easily do it myself and release a new beta today.

I use JEST for testing. Do you know a way to test for such errors? I would want to prevent such bugs in the future.

from generic-oauth2.

tsavo-vdb-knott avatar tsavo-vdb-knott commented on August 10, 2024

from generic-oauth2.

moberwasserlechner avatar moberwasserlechner commented on August 10, 2024

The new lib does not support conversion from arraybuffer to base64.

So I'm going to remove all base64 lib dependency completely and use the code from https://github.com/niklasvh/base64-arraybuffer directly.

I Have to add a few tests for that and will release the a new version tomorrow.

from generic-oauth2.

tsavo-vdb-knott avatar tsavo-vdb-knott commented on August 10, 2024

from generic-oauth2.

tsavo-vdb-knott avatar tsavo-vdb-knott commented on August 10, 2024

Tried to make a PR but it I don't have auth to do so - either way, I hope the following helps/saves you some time!

// ESM Base64 file.

root/base64/index.js (Or in your case probably some .TS version

let lookup = [];
let revLookup = [];
let Arr = typeof Uint8Array !== "undefined" ? Uint8Array : Array;

let code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
for (let i = 0, len = code.length; i < len; ++i) {
    lookup[i] = code[i];
    revLookup[code.charCodeAt(i)] = i;
}

// Support decoding URL-safe base64 strings, as Node.js does.
// See: https://en.wikipedia.org/wiki/Base64#URL_applications
revLookup["-".charCodeAt(0)] = 62;
revLookup["_".charCodeAt(0)] = 63;

export const getLens = b64 => {
    let len = b64.length;

    if (len % 4 > 0) {
        throw new Error("Invalid string. Length must be a multiple of 4");
    }

    // Trim off extra bytes after placeholder bytes are found
    // See: https://github.com/beatgammit/base64-js/issues/42
    let validLen = b64.indexOf("=");
    if (validLen === -1) validLen = len;

    let placeHoldersLen = validLen === len ? 0 : 4 - (validLen % 4);

    return [validLen, placeHoldersLen];
};

// base64 is 4/3 + up to two characters of the original data
export const byteLength = b64 => {
    let lens = getLens(b64);
    let validLen = lens[0];
    let placeHoldersLen = lens[1];
    return ((validLen + placeHoldersLen) * 3) / 4 - placeHoldersLen;
};

const _byteLength = (b64, validLen, placeHoldersLen) => {
    return ((validLen + placeHoldersLen) * 3) / 4 - placeHoldersLen;
};

export const toByteArray = b64 => {
    let tmp;
    let lens = getLens(b64);
    let validLen = lens[0];
    let placeHoldersLen = lens[1];

    let arr = new Arr(_byteLength(b64, validLen, placeHoldersLen));

    let curByte = 0;

    // if there are placeholders, only get up to the last complete 4 chars
    let len = placeHoldersLen > 0 ? validLen - 4 : validLen;

    for (let i = 0; i < len; i += 4) {
        tmp =
            (revLookup[b64.charCodeAt(i)] << 18) |
            (revLookup[b64.charCodeAt(i + 1)] << 12) |
            (revLookup[b64.charCodeAt(i + 2)] << 6) |
            revLookup[b64.charCodeAt(i + 3)];
        arr[curByte++] = (tmp >> 16) & 0xff;
        arr[curByte++] = (tmp >> 8) & 0xff;
        arr[curByte++] = tmp & 0xff;
    }

    if (placeHoldersLen === 2) {
        tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4);
        arr[curByte++] = tmp & 0xff;
    }

    if (placeHoldersLen === 1) {
        tmp =
            (revLookup[b64.charCodeAt(i)] << 10) |
            (revLookup[b64.charCodeAt(i + 1)] << 4) |
            (revLookup[b64.charCodeAt(i + 2)] >> 2);
        arr[curByte++] = (tmp >> 8) & 0xff;
        arr[curByte++] = tmp & 0xff;
    }

    return arr;
};

export const tripletToBase64 = num => {
    return lookup[(num >> 18) & 0x3f] + lookup[(num >> 12) & 0x3f] + lookup[(num >> 6) & 0x3f] + lookup[num & 0x3f];
};

export const encodeChunk = (uint8, start, end) => {
    let tmp;
    let output = [];
    for (let i = start; i < end; i += 3) {
        tmp = ((uint8[i] << 16) & 0xff0000) + ((uint8[i + 1] << 8) & 0xff00) + (uint8[i + 2] & 0xff);
        output.push(tripletToBase64(tmp));
    }
    return output.join("");
};

export const fromByteArray = uint8 => {
    let tmp;
    let len = uint8.length;
    let extraBytes = len % 3; // if we have 1 byte left, pad 2 bytes
    let parts = [];
    let maxChunkLength = 16383; // must be multiple of 3

    // go through the array every three bytes, we'll deal with trailing stuff later
    for (let i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
        parts.push(encodeChunk(uint8, i, i + maxChunkLength > len2 ? len2 : i + maxChunkLength));
    }

    // pad the end with zeros, but make sure to not forget the extra bytes
    if (extraBytes === 1) {
        tmp = uint8[len - 1];
        parts.push(lookup[tmp >> 2] + lookup[(tmp << 4) & 0x3f] + "==");
    } else if (extraBytes === 2) {
        tmp = (uint8[len - 2] << 8) + uint8[len - 1];
        parts.push(lookup[tmp >> 10] + lookup[(tmp >> 4) & 0x3f] + lookup[(tmp << 2) & 0x3f] + "=");
    }

    return parts.join("");
};

Then obviously as you know. The Web-Utils.ts

Line 162:

import {fromByteArray} from "../Base64-esm";

Line 178:

You'll no longer need the base64.someExport prefix.

const encoded = fromByteArray(new Uint8Array(buffer));

Hopefully this saves you some time.

Cheers!

-T

from generic-oauth2.

tsavo-vdb-knott avatar tsavo-vdb-knott commented on August 10, 2024

LOL, Im so dumb... I just saw your commit disregard the above... So sorry 🤦‍♂️

from generic-oauth2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.