From f213ca52a61612ea9c7c823330fd4ecb21200d1e Mon Sep 17 00:00:00 2001 From: carlgu Date: Fri, 25 Oct 2024 10:14:11 +0200 Subject: [PATCH 1/3] Add http basic auth integration for a list of credentials --- CHANGELOG.md | 6 ++++++ src/next/middleware/http-auth.ts | 35 ++++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 58662ad..4870c26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +2.3.1 +===== + +* (improvement) Add method `integrateHttpBasicAuth()` for handling a list of Http Basic Auth Credentials +* (deprecation) Deprecate `handleHttpBasicAuth` method + 2.3.0 ===== diff --git a/src/next/middleware/http-auth.ts b/src/next/middleware/http-auth.ts index e050f21..53426ca 100644 --- a/src/next/middleware/http-auth.ts +++ b/src/next/middleware/http-auth.ts @@ -1,7 +1,14 @@ import type {NextRequest} from "next/server"; +export interface Credentials { + username: string; + password: string; +} + /** * Handles basic authentication + * + * @deprecated Use the new method 'integrateHttpBasicAuth' allowing a list of credentials to check against instead */ export function handleHttpBasicAuth ( request: NextRequest, @@ -10,6 +17,24 @@ export function handleHttpBasicAuth ( responseText: string = "Auth required", realmLabel: string = "Secure Area", ) : Response | undefined +{ + return integrateHttpBasicAuth( + request, + [{username: username, password: password}], + responseText, + realmLabel + ); +} + +/** + * Handles basic authentication + */ +export function integrateHttpBasicAuth ( + request: NextRequest, + users: Credentials[], + responseText: string = "Auth required", + realmLabel: string = "Secure Area", +) : Response | undefined { const auth = request.headers.get("authorization"); const authToken = /^Basic (?.*?)$/.exec(auth || ""); @@ -20,10 +45,12 @@ export function handleHttpBasicAuth ( .toString() .split(":"); - if (givenUser === username && givenPassword === password) - { - return; - } + users.forEach((item) => { + if (givenUser === item.username && givenPassword === item.password) + { + return; + } + }) } return new Response(responseText, { From 17038f97b2b9db24f87aa7ad705b5773145d9304 Mon Sep 17 00:00:00 2001 From: carlgu Date: Fri, 25 Oct 2024 10:35:41 +0200 Subject: [PATCH 2/3] Review feedback --- UPGRADE.md | 1 + src/next/middleware/http-auth.ts | 14 +++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index f38a104..70ee740 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -2,6 +2,7 @@ ========== * The `createEvent` helper was removed, use `new CustomEvent()` directly. +* The `handleHttpBasicAuth()` method was removed, use `integrateHttpBasicAuth()` directly 1.x to 2.0 diff --git a/src/next/middleware/http-auth.ts b/src/next/middleware/http-auth.ts index 53426ca..71db2ad 100644 --- a/src/next/middleware/http-auth.ts +++ b/src/next/middleware/http-auth.ts @@ -20,7 +20,7 @@ export function handleHttpBasicAuth ( { return integrateHttpBasicAuth( request, - [{username: username, password: password}], + [{username, password}], responseText, realmLabel ); @@ -45,12 +45,12 @@ export function integrateHttpBasicAuth ( .toString() .split(":"); - users.forEach((item) => { - if (givenUser === item.username && givenPassword === item.password) - { - return; - } - }) + const hasValidUser = users.some((item : Credentials) => givenUser === item.username && givenPassword === item.password); + + if (hasValidUser) + { + return; + } } return new Response(responseText, { From 12d55e2735f3fecfb6333a87cc1b5e5592cc0981 Mon Sep 17 00:00:00 2001 From: Jannik Date: Fri, 25 Oct 2024 10:38:56 +0200 Subject: [PATCH 3/3] CS --- src/next/middleware/http-auth.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/next/middleware/http-auth.ts b/src/next/middleware/http-auth.ts index 71db2ad..898686d 100644 --- a/src/next/middleware/http-auth.ts +++ b/src/next/middleware/http-auth.ts @@ -45,7 +45,7 @@ export function integrateHttpBasicAuth ( .toString() .split(":"); - const hasValidUser = users.some((item : Credentials) => givenUser === item.username && givenPassword === item.password); + const hasValidUser = users.some(item => givenUser === item.username && givenPassword === item.password); if (hasValidUser) {