Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/npm_and_yarn/vite-6.0.11
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfy1339 authored Jan 27, 2025
2 parents 912cfdc + 59207f6 commit af4da10
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
15 changes: 5 additions & 10 deletions src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { isJWT } from "./is-jwt.js";
import type { Token, Authentication } from "./types.js";

const REGEX_IS_INSTALLATION_LEGACY = /^v1\./;
const REGEX_IS_INSTALLATION = /^ghs_/;
const REGEX_IS_USER_TO_SERVER = /^ghu_/;

export async function auth(token: Token): Promise<Authentication> {
const isApp = token.split(/\./).length === 3;
const isInstallation =
REGEX_IS_INSTALLATION_LEGACY.test(token) ||
REGEX_IS_INSTALLATION.test(token);
const isUserToServer = REGEX_IS_USER_TO_SERVER.test(token);
const isApp = isJWT(token);
const isInstallation = token.startsWith("v1.") || token.startsWith("ghs_");
const isUserToServer = token.startsWith("ghu_");

const tokenType = isApp
? "app"
Expand All @@ -21,7 +16,7 @@ export async function auth(token: Token): Promise<Authentication> {

return {
type: "token",
token: token,
token,
tokenType,
};
}
5 changes: 5 additions & 0 deletions src/is-jwt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const b64url = "(?:[a-zA-Z0-9_-]+)";
const sep = "\\.";
const jwtRE = new RegExp(`^${b64url}${sep}${b64url}${sep}${b64url}$`);

export const isJWT = jwtRE.test.bind(jwtRE);
26 changes: 26 additions & 0 deletions test/is-jwt-token.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { describe, test, expect } from "vitest";
import { isJWT } from "../src/is-jwt.js";

describe("isJWT", () => {
test("valid JWT", () => {
expect(isJWT("a.a.a")).toBe(true);
expect(
isJWT(
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOi0zMCwiZXhwIjo1NzAsImlzcyI6MX0.q3foRa78U3WegM5PrWLEh5N0bH1SD62OqW66ZYzArp95JBNiCbo8KAlGtiRENCIfBZT9ibDUWy82cI4g3F09mdTq3bD1xLavIfmTksIQCz5EymTWR5v6gL14LSmQdWY9lSqkgUG0XCFljWUglEP39H4yeHbFgdjvAYg3ifDS12z9oQz2ACdSpvxPiTuCC804HkPVw8Qoy0OSXvCkFU70l7VXCVUxnuhHnk8-oCGcKUspmeP6UdDnXk-Aus-eGwDfJbU2WritxxaXw6B4a3flTPojkYLSkPBr6Pi0H2-mBsW_Nvs0aLPVLKobQd4gqTkosX3967DoAG8luUMhrnxe8Q",
),
).toBe(true);
});
test("invalid JWT", () => {
expect(isJWT("")).toBe(false);
expect(isJWT(".")).toBe(false);
expect(isJWT("..")).toBe(false);
expect(isJWT("...")).toBe(false);
expect(isJWT("....")).toBe(false);
expect(isJWT("a.a.")).toBe(false);
expect(isJWT(".a.a")).toBe(false);
expect(isJWT("a..a")).toBe(false);
expect(isJWT("a/.a.a")).toBe(false);
expect(isJWT("a.a.a=")).toBe(false);
expect(isJWT("a.a.a==")).toBe(false);
});
});

0 comments on commit af4da10

Please sign in to comment.