Skip to content

Commit

Permalink
feat: removes atob usage and fixes #24
Browse files Browse the repository at this point in the history
  • Loading branch information
polvallverdu committed Mar 10, 2024
1 parent 574956b commit 4f493f4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules vitest run"
},
"dependencies": {
"@cfworker/base64url": "^1.12.5",
"@tsndr/cloudflare-worker-jwt": "^2.5.1",
"itty-router": "^4.0.27",
"zod": "^3.22.4"
Expand Down
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

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

7 changes: 3 additions & 4 deletions src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { decode } from "@cfworker/base64url";

export type RegistryTokenCapability = "push" | "pull";
export type RegistryAuthProtocolTokenPayload = {
username: string;
Expand Down Expand Up @@ -38,10 +40,7 @@ export function stripUsernamePasswordFromHeader(r: Request): [string, string] |
}

// Decodes the base64 value and performs unicode normalization.
// @see https://datatracker.ietf.org/doc/html/rfc7613#section-3.3.2 (and #section-4.2.2)
// @see https://dev.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/normalize
const buffer = Uint8Array.from(atob(encoded), (character) => character.charCodeAt(0));
const decoded = new TextDecoder().decode(buffer).normalize();
const decoded = decode(encoded);

// The username & password are split by the first colon.
//=> example: "username:password"
Expand Down
7 changes: 2 additions & 5 deletions src/token.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { decode } from "@cfworker/base64url";
import jwt from "@tsndr/cloudflare-worker-jwt";
import {
RegistryTokenCapability,
Expand All @@ -8,11 +9,7 @@ import {

export function importKeyFromBase64(key: string): JsonWebKey {
// Decodes the base64 value and performs unicode normalization.
// @see https://datatracker.ietf.org/doc/html/rfc7613#section-3.3.2 (and #section-4.2.2)
// @see https://dev.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/normalize
const buffer = Uint8Array.from(atob(key), (character) => character.charCodeAt(0));
const decoded = new TextDecoder().decode(buffer).normalize();
return JSON.parse(decoded);
return JSON.parse(decode(key));
}

export async function newRegistryTokens(jwtPublicKey: string): Promise<RegistryTokens> {
Expand Down

0 comments on commit 4f493f4

Please sign in to comment.