summaryrefslogtreecommitdiff
path: root/handlers.rkt
diff options
context:
space:
mode:
Diffstat (limited to 'handlers.rkt')
-rw-r--r--handlers.rkt38
1 files changed, 27 insertions, 11 deletions
diff --git a/handlers.rkt b/handlers.rkt
index 42ed13a..7304d18 100644
--- a/handlers.rkt
+++ b/handlers.rkt
@@ -14,19 +14,35 @@
"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 app-dispatch))
+
(define (wrap-basic-auth handler)
(lambda (req)
- (match (request->basic-credentials req)
- ;; credentials found → continue to dispatcher
- [(cons user pass) (handler req)]
- [else
- ;; no credentials → trigger auth challenge
- (response 401
- #"Unauthorized"
- (current-seconds)
- TEXT/HTML-MIME-TYPE
- (list (make-basic-auth-header "Ferti Private Area"))
- void)])))
+ (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-values (app-dispatch _)
(dispatch-rules [("ferti") #:method "get" ferti]
Copyright 2019--2026 Marius PETER