diff options
Diffstat (limited to 'authentication.rkt')
| -rw-r--r-- | authentication.rkt | 26 |
1 files changed, 16 insertions, 10 deletions
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) |