From 9338724460b3a86eefdd0085c43bd0b75e73a4e2 Mon Sep 17 00:00:00 2001 From: Marius Peter Date: Wed, 3 Dec 2025 20:57:26 +0100 Subject: Parameterize authentication credentials. --- authentication.rkt | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'authentication.rkt') diff --git a/authentication.rkt b/authentication.rkt index 9e9400b..9a15976 100644 --- a/authentication.rkt +++ b/authentication.rkt @@ -1,27 +1,33 @@ #lang racket -(provide make-auth-dispatch) +(provide authentication-credentials-defined? + make-auth-dispatch) (require web-server/http web-server/http/basic-auth) -(define ferti-user - (or (getenv "FERTI_USER") (error 'authentication "FERTI_USER environment variable is not set"))) -(define ferti-pass - (or (getenv "FERTI_PASS") (error 'authentication "FERTI_PASS environment variable is not set"))) +(define ferti-user (make-parameter (getenv "FERTI_USER"))) +(define ferti-pass (make-parameter (getenv "FERTI_PASS"))) + +(define (authentication-credentials-defined?) + (and (ferti-user) (ferti-pass))) (define (make-auth-dispatch handler) - (lambda (req) - (if (authorized? req) - (handler req) - (unauthorized-response)))) + (if (authentication-credentials-defined?) + (lambda (req) + (if (authorized? req) + (handler req) + (unauthorized-response))) + (error + 'authentication + "Undefined authentication credentials (FERTI_USER and FERTI_PASS environment variables)"))) (define (authorized? req) (match (request->basic-credentials req) [(cons user-b pass-b) (define user (bytes->string/utf-8 user-b)) (define pass (bytes->string/utf-8 pass-b)) - (and (string=? user ferti-user) (string=? pass ferti-pass))] + (and (string=? user (ferti-user)) (string=? pass (ferti-pass)))] [_ #f])) (define (unauthorized-response) -- cgit v1.2.3