summaryrefslogtreecommitdiff
path: root/authentication.rkt
blob: 9e9400b516f05df8272bf562f4160d72dcb5a96b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#lang racket

(provide 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 (make-auth-dispatch 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))
Copyright 2019--2026 Marius PETER