Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/scrawn/biller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Generated by scrawn tag sync. Do not edit manually.
import { createScrawn } from "@scrawn/core";
import { TAGS, EXPRESSIONS } from "./pricerefs.ts";

Expand Down
1 change: 0 additions & 1 deletion examples/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"outDir": "./dist",
"baseUrl": ".",
"paths": {
"@scrawn/core": ["../packages/scrawn/src/index.ts"]
},
Expand Down
2 changes: 1 addition & 1 deletion packages/scrawn/proto
Submodule proto updated 1 files
+1 −1 buf.gen.yaml
15 changes: 9 additions & 6 deletions packages/scrawn/src/core/auth/apiKeyAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import { ScrawnValidationError } from "../errors/index.js";
const log = new ScrawnLogger("Auth");

/**
* API key format: sk_ followed by 16 alphanumeric characters
* @example 'sk_abc123def456ghi7'
* API key format: scrn_<role>_ followed by 32 alphanumeric characters.
* Roles: dash (dashboard), live (production), test
* @example 'scrn_live_abc123def456ghi789jkl012mno345pq'
*/
export type ApiKeyFormat = `scrn_${string}`;

const API_KEY_REGEX = /^scrn_(dash|live|test)_[a-zA-Z0-9]{32}$/;

/**
* Type guard to validate API key format
*/
export function isValidApiKey(key: string): key is ApiKeyFormat {
return /^scrn_[a-zA-Z0-9]{32}$/.test(key);
return API_KEY_REGEX.test(key);
}

/**
Expand All @@ -25,11 +28,11 @@ export function validateApiKey(key: string): ApiKeyFormat {
if (!isValidApiKey(key)) {
log.error(`Invalid API key format: "${key}".`);
throw new ScrawnValidationError(
"Invalid API key format. Expected format: scrn_<32 alphanumeric characters>",
"Invalid API key format. Expected format: scrn_<dash|live|test>_<32 alphanumeric characters>",
{
details: {
providedKey: key.substring(0, 10) + "...",
expectedFormat: "scrn_<32 alphanumeric characters>",
providedKey: key.substring(0, 14) + "...",
expectedFormat: "scrn_<role>_<32 alphanumeric characters>",
},
}
);
Expand Down
2 changes: 1 addition & 1 deletion packages/scrawn/tests/unit/auth/apiKeyAuth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "../../../src/core/auth/apiKeyAuth.js";
import { ScrawnValidationError } from "../../../src/core/errors/index.js";

const validKey = "scrn_1234567890abcdef1234567890abcdef";
const validKey = "scrn_live_1234567890abcdef1234567890abcdef";

describe("apiKeyAuth", () => {
it("validates api key format", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/scrawn/tests/unit/scrawn/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Scrawn } from "../../../src/core/scrawn.js";
import { RegisterEventResponse } from "../../../src/gen/event/v1/event_pb.js";
import { ScrawnError, ScrawnValidationError } from "../../../src/core/errors/index.js";

const validKey = "scrn_1234567890abcdef1234567890abcdef";
const validKey = "scrn_live_1234567890abcdef1234567890abcdef";

const requestMock = vi.fn(async () => {
const response = new RegisterEventResponse();
Expand Down
2 changes: 1 addition & 1 deletion packages/scrawn/tests/unit/scrawn/scrawn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
ScrawnValidationError,
} from "../../../src/core/errors/index.js";

const validKey = "scrn_1234567890abcdef1234567890abcdef";
const validKey = "scrn_live_1234567890abcdef1234567890abcdef";

const requestMock = vi.fn();
const addPayloadMock = vi.fn(function (this: unknown, payload: unknown) {
Expand Down
1 change: 0 additions & 1 deletion packages/scrawn/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"baseUrl": ".",
"paths": {}
},
"include": ["src/**/*"],
Expand Down
Loading