Skip to content

Commit

Permalink
Avoid using any bundlers but use tsc instead (#192)
Browse files Browse the repository at this point in the history
Co-authored-by: Jon Koops <jonkoops@gmail.com>
  • Loading branch information
frederikprijck and jonkoops committed Oct 27, 2023
1 parent 57f52de commit 1113ee4
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 1,444 deletions.
33 changes: 0 additions & 33 deletions lib/base64-url-decode.ts

This file was deleted.

36 changes: 34 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { base64UrlDecode } from "./base64-url-decode.js";

export interface JwtDecodeOptions {
header?: boolean;
}
Expand All @@ -24,6 +22,40 @@ export class InvalidTokenError extends Error {}

InvalidTokenError.prototype.name = "InvalidTokenError";

function b64DecodeUnicode(str: string) {
return decodeURIComponent(
atob(str).replace(/(.)/g, (m, p) => {
let code = p.charCodeAt(0).toString(16).toUpperCase();
if (code.length < 2) {
code = "0" + code;
}
return "%" + code;
})
);
}

function base64UrlDecode(str: string) {
let output = str.replace(/-/g, "+").replace(/_/g, "/");
switch (output.length % 4) {
case 0:
break;
case 2:
output += "==";
break;
case 3:
output += "=";
break;
default:
throw new Error("base64 string is not of the correct length");
}

try {
return b64DecodeUnicode(output);
} catch (err) {
return atob(output);
}
}

export function jwtDecode<T = JwtHeader>(
token: string,
options: JwtDecodeOptions & { header: true }
Expand Down
Loading

0 comments on commit 1113ee4

Please sign in to comment.