feat(mongodb): support OIDC browser authentication
This commit is contained in:
22 files changed
+1109
-24
No files matched your search
@@ -0,0 +1,22 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { mongoConnectionUsesOidc } from "@/lib/mongo/mongoConnectionOptions";
|
||||
|
||||
describe("mongoConnectionUsesOidc", () => {
|
||||
it("detects OIDC in structured URL params without case sensitivity", () => {
|
||||
expect(mongoConnectionUsesOidc("authSource=%24external&AUTHMECHANISM=mongodb-oidc")).toBe(true);
|
||||
});
|
||||
|
||||
it("detects OIDC in mongodb and mongodb+srv connection strings", () => {
|
||||
expect(mongoConnectionUsesOidc(undefined, "mongodb://localhost/app?authMechanism=MONGODB-OIDC")).toBe(true);
|
||||
expect(mongoConnectionUsesOidc(undefined, "mongodb+srv://cluster.example.com/app?authMechanism=MONGODB%2DOIDC#fragment")).toBe(true);
|
||||
});
|
||||
|
||||
it("does not classify non-OIDC or non-MongoDB URLs as OIDC", () => {
|
||||
expect(mongoConnectionUsesOidc("authMechanism=SCRAM-SHA-256")).toBe(false);
|
||||
expect(mongoConnectionUsesOidc(undefined, "postgres://localhost/app?authMechanism=MONGODB-OIDC")).toBe(false);
|
||||
});
|
||||
|
||||
it("treats a connection string as authoritative over stale form params", () => {
|
||||
expect(mongoConnectionUsesOidc("authMechanism=MONGODB-OIDC", "mongodb://localhost/app?authMechanism=SCRAM-SHA-256")).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -3,6 +3,20 @@ export function mongoUrlParam(urlParams: string | undefined, key: string): strin
|
||||
return params.get(key) || "";
|
||||
}
|
||||
|
||||
export function mongoConnectionUsesOidc(urlParams: string | undefined, connectionString?: string): boolean {
|
||||
const input = connectionString?.trim() || "";
|
||||
if (input) {
|
||||
if (!/^mongodb(?:\+srv)?:\/\//i.test(input)) return false;
|
||||
const queryStart = input.indexOf("?");
|
||||
if (queryStart < 0) return false;
|
||||
const fragmentStart = input.indexOf("#", queryStart + 1);
|
||||
const query = input.slice(queryStart + 1, fragmentStart < 0 ? undefined : fragmentStart);
|
||||
return mongoUrlParamCaseInsensitive(query, "authMechanism").toUpperCase() === "MONGODB-OIDC";
|
||||
}
|
||||
|
||||
return mongoUrlParamCaseInsensitive(urlParams, "authMechanism").toUpperCase() === "MONGODB-OIDC";
|
||||
}
|
||||
|
||||
export function setMongoUrlParam(urlParams: string | undefined, key: string, value: string): string {
|
||||
const params = parseMongoUrlParams(urlParams);
|
||||
const normalized = value.trim();
|
||||
@@ -84,3 +98,11 @@ export function mongodbAuthFailureHint(message: string): string {
|
||||
function parseMongoUrlParams(urlParams: string | undefined): URLSearchParams {
|
||||
return new URLSearchParams((urlParams || "").trim().replace(/^\?/, ""));
|
||||
}
|
||||
|
||||
function mongoUrlParamCaseInsensitive(urlParams: string | undefined, key: string): string {
|
||||
const expectedKey = key.toLowerCase();
|
||||
for (const [paramKey, value] of parseMongoUrlParams(urlParams)) {
|
||||
if (paramKey.toLowerCase() === expectedKey) return value;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
Reference in new issue
Block a user