fix: use x-api-key for api key sessions
This commit is contained in:
@@ -25,6 +25,10 @@ import { createGenericOAuthProviders } from "./oauth";
|
||||
import { authPerformanceConfig } from "./performance";
|
||||
import { buildAuthSms, sendSms } from "./sms";
|
||||
|
||||
type ApiKeyGetterContext = {
|
||||
headers?: Headers;
|
||||
};
|
||||
|
||||
export function createApiKeyConfigurations(env: Env) {
|
||||
const performance = authPerformanceConfig(env);
|
||||
|
||||
@@ -34,6 +38,8 @@ export function createApiKeyConfigurations(env: Env) {
|
||||
defaultPrefix: "cfw_",
|
||||
requireName: true,
|
||||
enableMetadata: true,
|
||||
enableSessionForAPIKeys: true,
|
||||
customAPIKeyGetter: (ctx: ApiKeyGetterContext) => getApiKeyFromRequestHeaders(ctx.headers),
|
||||
deferUpdates: performance.apiKeyDeferUpdates,
|
||||
rateLimit: {
|
||||
enabled: true,
|
||||
@@ -57,6 +63,10 @@ export function createApiKeyConfigurations(env: Env) {
|
||||
];
|
||||
}
|
||||
|
||||
export function getApiKeyFromRequestHeaders(headers: Headers | undefined): string | null {
|
||||
return headers?.get("x-api-key") ?? null;
|
||||
}
|
||||
|
||||
export function createAuthPlugins(env: Env): BetterAuthPlugin[] {
|
||||
const plugins: BetterAuthPlugin[] = [
|
||||
openAPI(),
|
||||
|
||||
@@ -11,7 +11,11 @@ import {
|
||||
import { buildAuthEmail, sendEmail } from "../src/email";
|
||||
import { createGenericOAuthProviders, createSocialProviders } from "../src/oauth";
|
||||
import { authPerformanceConfig } from "../src/performance";
|
||||
import { createApiKeyConfigurations, createAuthPlugins } from "../src/plugins";
|
||||
import {
|
||||
createApiKeyConfigurations,
|
||||
createAuthPlugins,
|
||||
getApiKeyFromRequestHeaders,
|
||||
} from "../src/plugins";
|
||||
import { buildAuthSms, sendSms } from "../src/sms";
|
||||
|
||||
const env: Env = {
|
||||
@@ -237,6 +241,32 @@ describe("auth plugins", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("enables API keys as get-session credentials only from x-api-key", () => {
|
||||
const configurations = createApiKeyConfigurations({
|
||||
BETTER_AUTH_URL: "http://localhost:8788",
|
||||
TRUSTED_ORIGINS: "http://localhost:8787",
|
||||
});
|
||||
|
||||
expect(configurations).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
configId: "default",
|
||||
enableSessionForAPIKeys: true,
|
||||
customAPIKeyGetter: expect.any(Function),
|
||||
}),
|
||||
]),
|
||||
);
|
||||
expect(getApiKeyFromRequestHeaders(new Headers({ "x-api-key": "cfw_secret" }))).toBe(
|
||||
"cfw_secret",
|
||||
);
|
||||
expect(
|
||||
getApiKeyFromRequestHeaders(new Headers({ authorization: "Bearer cfw_secret" })),
|
||||
).toBeNull();
|
||||
expect(
|
||||
getApiKeyFromRequestHeaders(new Headers({ authorization: "Bearer better-auth-session" })),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("allows disabling api key deferred updates", () => {
|
||||
const configurations = createApiKeyConfigurations({
|
||||
BETTER_AUTH_URL: "http://localhost:8788",
|
||||
|
||||
Reference in New Issue
Block a user