Jim Cheung

Saturday, July 11, 2026

extract basic auth from headers

(defn- extract-basic-creds [req]
  (when-let [auth (get-in req [:headers "authorization"])]
    (when (str/starts-with? auth "Basic ")
      (try
        (-> (subs auth 6)
            base64-decode
            (str/split #":" 2))
        (catch Exception _ nil)))))

usage:

(if (and creds
         (= "admin" (first creds))
         (= password (second creds)))
  (hdlr req)
  (unauthorized))
Blog Archive
    Newer Entries
  • N/A