2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-03-18 12:36:00 +00:00
|
|
|
// This module is browser compatible.
|
2022-08-11 11:51:20 +00:00
|
|
|
|
|
|
|
/**
|
2023-12-13 02:57:59 +00:00
|
|
|
* Utilities for
|
2024-01-25 14:08:29 +00:00
|
|
|
* {@link https://datatracker.ietf.org/doc/html/rfc4648#section-5 | base64url}
|
2023-12-13 02:57:59 +00:00
|
|
|
* encoding and decoding.
|
2022-08-11 11:51:20 +00:00
|
|
|
*
|
|
|
|
* This module is browser compatible.
|
|
|
|
*
|
|
|
|
* @module
|
|
|
|
*/
|
2020-06-03 13:44:51 +00:00
|
|
|
|
2020-10-13 01:12:10 +00:00
|
|
|
import * as base64 from "./base64.ts";
|
2020-06-03 13:44:51 +00:00
|
|
|
|
2024-01-25 14:08:29 +00:00
|
|
|
/**
|
2020-06-03 13:44:51 +00:00
|
|
|
* Some variants allow or require omitting the padding '=' signs:
|
2021-08-03 06:32:41 +00:00
|
|
|
* https://en.wikipedia.org/wiki/Base64#The_URL_applications
|
2024-01-25 14:08:29 +00:00
|
|
|
*
|
2020-10-13 01:12:10 +00:00
|
|
|
* @param base64url
|
2020-06-03 13:44:51 +00:00
|
|
|
*/
|
2022-06-17 14:17:59 +00:00
|
|
|
function addPaddingToBase64url(base64url: string): string {
|
2020-06-03 13:44:51 +00:00
|
|
|
if (base64url.length % 4 === 2) return base64url + "==";
|
|
|
|
if (base64url.length % 4 === 3) return base64url + "=";
|
2020-07-14 19:24:17 +00:00
|
|
|
if (base64url.length % 4 === 1) {
|
2020-06-03 13:44:51 +00:00
|
|
|
throw new TypeError("Illegal base64url string!");
|
2020-07-14 19:24:17 +00:00
|
|
|
}
|
2020-06-03 13:44:51 +00:00
|
|
|
return base64url;
|
|
|
|
}
|
|
|
|
|
2020-10-13 01:12:10 +00:00
|
|
|
function convertBase64urlToBase64(b64url: string): string {
|
2021-07-26 12:08:13 +00:00
|
|
|
if (!/^[-_A-Z0-9]*?={0,2}$/i.test(b64url)) {
|
|
|
|
// Contains characters not part of base64url spec.
|
|
|
|
throw new TypeError("Failed to decode base64url: invalid character");
|
|
|
|
}
|
2020-10-13 01:12:10 +00:00
|
|
|
return addPaddingToBase64url(b64url).replace(/\-/g, "+").replace(/_/g, "/");
|
2020-06-03 13:44:51 +00:00
|
|
|
}
|
|
|
|
|
2023-10-11 07:19:48 +00:00
|
|
|
function convertBase64ToBase64url(b64: string) {
|
|
|
|
return b64.endsWith("=")
|
2023-10-02 01:51:11 +00:00
|
|
|
? b64.endsWith("==")
|
|
|
|
? b64.replace(/\+/g, "-").replace(/\//g, "_").slice(0, -2)
|
|
|
|
: b64.replace(/\+/g, "-").replace(/\//g, "_").slice(0, -1)
|
|
|
|
: b64.replace(/\+/g, "-").replace(/\//g, "_");
|
2023-10-11 07:19:48 +00:00
|
|
|
}
|
2020-06-03 13:44:51 +00:00
|
|
|
|
|
|
|
/**
|
2023-12-13 02:57:59 +00:00
|
|
|
* Convert data into a base64url-encoded string.
|
|
|
|
*
|
|
|
|
* @see {@link https://datatracker.ietf.org/doc/html/rfc4648#section-5}
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* import { encodeBase64Url } from "https://deno.land/std@$STD_VERSION/encoding/base64url.ts";
|
|
|
|
*
|
|
|
|
* encodeBase64Url(new TextEncoder().encode("foobar")); // "Zm9vYmFy"
|
|
|
|
* ```
|
2020-06-03 13:44:51 +00:00
|
|
|
*/
|
2023-09-21 09:29:13 +00:00
|
|
|
export function encodeBase64Url(
|
|
|
|
data: ArrayBuffer | Uint8Array | string,
|
|
|
|
): string {
|
|
|
|
return convertBase64ToBase64url(base64.encodeBase64(data));
|
2020-06-03 13:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-12-13 02:57:59 +00:00
|
|
|
* Decodes a given base64url-encoded string.
|
|
|
|
*
|
|
|
|
* @see {@link https://datatracker.ietf.org/doc/html/rfc4648#section-5}
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* import { decodeBase64Url } from "https://deno.land/std@$STD_VERSION/encoding/base64url.ts";
|
|
|
|
*
|
|
|
|
* decodeBase64Url("Zm9vYmFy"); // Uint8Array(6) [ 102, 111, 111, 98, 97, 114 ]
|
|
|
|
* ```
|
2020-06-03 13:44:51 +00:00
|
|
|
*/
|
2023-09-21 09:29:13 +00:00
|
|
|
export function decodeBase64Url(b64url: string): Uint8Array {
|
|
|
|
return base64.decodeBase64(convertBase64urlToBase64(b64url));
|
2020-06-03 13:44:51 +00:00
|
|
|
}
|