antarctic 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ export declare function encodeBase64(bytes: Uint8Array): string;
2
+ export declare function encodeBase64urlNoPadding(bytes: Uint8Array): string;
3
+ export declare function decodeBase64urlIgnorePadding(encoded: string): Uint8Array;
@@ -0,0 +1,39 @@
1
+ const base64urlAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
2
+ function encodeBinaryString(bytes) {
3
+ let binary = "";
4
+ // Chunked to stay well under the argument limit for large inputs.
5
+ for (let i = 0; i < bytes.length; i += 0x8000) {
6
+ binary += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
7
+ }
8
+ return binary;
9
+ }
10
+ export function encodeBase64(bytes) {
11
+ return btoa(encodeBinaryString(bytes));
12
+ }
13
+ export function encodeBase64urlNoPadding(bytes) {
14
+ let result = "";
15
+ for (let i = 0; i < bytes.length; i += 3) {
16
+ let buffer = 0;
17
+ let bits = 0;
18
+ for (let j = 0; j < 3 && i + j < bytes.length; j++) {
19
+ buffer = (buffer << 8) | bytes[i + j];
20
+ bits += 8;
21
+ }
22
+ for (let j = 0; j < 4 && bits > 0; j++) {
23
+ bits -= 6;
24
+ const index = bits >= 0 ? (buffer >> bits) & 0x3f : (buffer << -bits) & 0x3f;
25
+ result += base64urlAlphabet[index];
26
+ }
27
+ }
28
+ return result;
29
+ }
30
+ export function decodeBase64urlIgnorePadding(encoded) {
31
+ const normalized = encoded.replaceAll("-", "+").replaceAll("_", "/").replaceAll("=", "");
32
+ const padded = normalized + "=".repeat((4 - (normalized.length % 4)) % 4);
33
+ const binary = atob(padded);
34
+ const bytes = new Uint8Array(binary.length);
35
+ for (let i = 0; i < binary.length; i++) {
36
+ bytes[i] = binary.charCodeAt(i);
37
+ }
38
+ return bytes;
39
+ }
package/dist/jwt.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export declare function decodeJWT(jwt: string): object;
2
+ export declare function createJWTSignatureMessage(headerJSON: string, payloadJSON: string): Uint8Array;
3
+ export declare function encodeJWT(headerJSON: string, payloadJSON: string, signature: Uint8Array): string;
package/dist/jwt.js ADDED
@@ -0,0 +1,38 @@
1
+ import { decodeBase64urlIgnorePadding, encodeBase64urlNoPadding } from "./encoding.js";
2
+ export function decodeJWT(jwt) {
3
+ const parts = jwt.split(".");
4
+ if (parts.length !== 3) {
5
+ throw new Error("Invalid JWT");
6
+ }
7
+ let jsonPayload;
8
+ try {
9
+ jsonPayload = new TextDecoder().decode(decodeBase64urlIgnorePadding(parts[1]));
10
+ }
11
+ catch {
12
+ throw new Error("Invalid JWT: Invalid base64url encoding");
13
+ }
14
+ let payload;
15
+ try {
16
+ payload = JSON.parse(jsonPayload);
17
+ }
18
+ catch {
19
+ throw new Error("Invalid JWT: Invalid JSON encoding");
20
+ }
21
+ if (typeof payload !== "object" || payload === null) {
22
+ throw new Error("Invalid JWT: Invalid payload");
23
+ }
24
+ return payload;
25
+ }
26
+ export function createJWTSignatureMessage(headerJSON, payloadJSON) {
27
+ const encoder = new TextEncoder();
28
+ const encodedHeader = encodeBase64urlNoPadding(encoder.encode(headerJSON));
29
+ const encodedPayload = encodeBase64urlNoPadding(encoder.encode(payloadJSON));
30
+ return encoder.encode(encodedHeader + "." + encodedPayload);
31
+ }
32
+ export function encodeJWT(headerJSON, payloadJSON, signature) {
33
+ const encoder = new TextEncoder();
34
+ const encodedHeader = encodeBase64urlNoPadding(encoder.encode(headerJSON));
35
+ const encodedPayload = encodeBase64urlNoPadding(encoder.encode(payloadJSON));
36
+ const encodedSignature = encodeBase64urlNoPadding(signature);
37
+ return encodedHeader + "." + encodedPayload + "." + encodedSignature;
38
+ }
package/dist/oauth2.js CHANGED
@@ -1,4 +1,4 @@
1
- import * as encoding from "@oslojs/encoding";
1
+ import { encodeBase64urlNoPadding } from "./encoding.js";
2
2
  import * as sha2 from "@oslojs/crypto/sha2";
3
3
  export class OAuth2Tokens {
4
4
  data;
@@ -53,15 +53,15 @@ export class OAuth2Tokens {
53
53
  }
54
54
  export function createS256CodeChallenge(codeVerifier) {
55
55
  const codeChallengeBytes = sha2.sha256(new TextEncoder().encode(codeVerifier));
56
- return encoding.encodeBase64urlNoPadding(codeChallengeBytes);
56
+ return encodeBase64urlNoPadding(codeChallengeBytes);
57
57
  }
58
58
  export function generateCodeVerifier() {
59
59
  const randomValues = new Uint8Array(32);
60
60
  crypto.getRandomValues(randomValues);
61
- return encoding.encodeBase64urlNoPadding(randomValues);
61
+ return encodeBase64urlNoPadding(randomValues);
62
62
  }
63
63
  export function generateState() {
64
64
  const randomValues = new Uint8Array(32);
65
65
  crypto.getRandomValues(randomValues);
66
- return encoding.encodeBase64urlNoPadding(randomValues);
66
+ return encodeBase64urlNoPadding(randomValues);
67
67
  }
package/dist/oidc.js CHANGED
@@ -1,7 +1,7 @@
1
- import * as jwt from "@oslojs/jwt";
1
+ import { decodeJWT } from "./jwt.js";
2
2
  export function decodeIdToken(idToken) {
3
3
  try {
4
- return jwt.decodeJWT(idToken);
4
+ return decodeJWT(idToken);
5
5
  }
6
6
  catch (e) {
7
7
  throw new Error("Invalid ID token", {
@@ -1,4 +1,4 @@
1
- import * as jwt from "@oslojs/jwt";
1
+ import { createJWTSignatureMessage, encodeJWT } from "../jwt.js";
2
2
  import { createOAuth2Request, sendTokenRequest } from "../request.js";
3
3
  import { decodeIdToken } from "../oidc.js";
4
4
  import { consumeOAuthState, generateOAuthState, parseCallbackQuery, profileId, profileString, requireAuthConfig, requireProviderOption, resolveAuthConfig, resolveScopes, saveOAuthState } from "../auth.js";
@@ -99,8 +99,8 @@ export class Apple {
99
99
  const signature = new Uint8Array(await crypto.subtle.sign({
100
100
  name: "ECDSA",
101
101
  hash: "SHA-256"
102
- }, privateKey, jwt.createJWTSignatureMessage(headerJSON, payloadJSON)));
103
- const token = jwt.encodeJWT(headerJSON, payloadJSON, signature);
102
+ }, privateKey, createJWTSignatureMessage(headerJSON, payloadJSON)));
103
+ const token = encodeJWT(headerJSON, payloadJSON, signature);
104
104
  return token;
105
105
  }
106
106
  }
package/dist/request.js CHANGED
@@ -1,4 +1,4 @@
1
- import * as encoding from "@oslojs/encoding";
1
+ import { encodeBase64 } from "./encoding.js";
2
2
  import { OAuth2Tokens } from "./oauth2.js";
3
3
  import { trimLeft, trimRight } from "./utils.js";
4
4
  export function joinURIAndPath(base, ...path) {
@@ -24,7 +24,7 @@ export function createOAuth2Request(endpoint, body) {
24
24
  }
25
25
  export function encodeBasicCredentials(username, password) {
26
26
  const bytes = new TextEncoder().encode(`${username}:${password}`);
27
- return encoding.encodeBase64(bytes);
27
+ return encodeBase64(bytes);
28
28
  }
29
29
  export async function sendTokenRequest(request) {
30
30
  let response;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "antarctic",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "High-level OAuth 2.0 clients for popular providers, forked from Arctic",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -35,8 +35,6 @@
35
35
  "vitest": "1.6.0"
36
36
  },
37
37
  "dependencies": {
38
- "@oslojs/crypto": "1.0.1",
39
- "@oslojs/encoding": "1.1.0",
40
- "@oslojs/jwt": "0.2.0"
38
+ "@oslojs/crypto": "1.0.1"
41
39
  }
42
40
  }
File without changes