summaryrefslogtreecommitdiff
path: root/handlers.rkt
diff options
context:
space:
mode:
authorMarius Peter <dev@marius-peter.com>2025-11-30 18:39:57 +0100
committerMarius Peter <dev@marius-peter.com>2025-11-30 18:39:57 +0100
commit60ccbcc94bf66692bcc1c2db21d5244cc40146ee (patch)
tree68fa7247a7cc81f893732b0ce6697bd4b510c736 /handlers.rkt
parenteae3703c7b86dc8630f60d45f2734336da757eea (diff)
Factor out authentication logic from handlers.
Diffstat (limited to 'handlers.rkt')
-rw-r--r--handlers.rkt35
1 files changed, 4 insertions, 31 deletions
diff --git a/handlers.rkt b/handlers.rkt
index 6141426..001d5a1 100644
--- a/handlers.rkt
+++ b/handlers.rkt
@@ -1,12 +1,12 @@
#lang racket
-(provide secured-dispatch
+(provide production-dispatch
fapg-url)
(require web-server/dispatch
web-server/http
web-server/formlets
- web-server/http/basic-auth
+ "authentication.rkt"
"views.rkt"
"formlets.rkt"
"models/user.rkt"
@@ -17,35 +17,8 @@
"models/fertilizer-product.rkt"
"services/nnls.rkt")
-(define ferti-user
- (or (getenv "FERTI_USER") (error 'ferti "FERTI_USER environment variable is not set")))
-(define ferti-pass
- (or (getenv "FERTI_PASS") (error 'ferti "FERTI_PASS environment variable is not set")))
-
-(define (secured-dispatch)
- (wrap-basic-auth fapg-dispatch))
-
-(define (wrap-basic-auth handler)
- (lambda (req)
- (if (authorized? req)
- (handler req)
- (unauthorized-response))))
-
-(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))]
- [_ #f]))
-
-(define (unauthorized-response)
- (response 401
- #"Unauthorized"
- (current-seconds)
- TEXT/HTML-MIME-TYPE
- (list (make-basic-auth-header (format "Basic Auth Test: ~a" (gensym))))
- void))
+(define (production-dispatch)
+ (make-auth-dispatch fapg-dispatch))
(define-values (fapg-dispatch fapg-url)
(dispatch-rules
Copyright 2019--2026 Marius PETER