cleanup and add some comments

This commit is contained in:
flynn 2019-06-09 01:00:26 +02:00
parent f220856e39
commit 1fc8b9ee5d
2 changed files with 16 additions and 21 deletions

View file

@ -48,7 +48,9 @@
(defn admin-access [req]
(contains? (get-in req [:session :user :roles]) :admin))
(def rules [{:pattern #"^/admin/.*"
(def rules
"The authentication rules"
[{:pattern #"^/admin/.*"
:handler admin-access}
; TODO add other auth schemes
;{:pattern [#"^/$" #"^/oauth/.*"]
@ -57,26 +59,33 @@
; :handler user-access}
])
(defn on-error [request response]
(defn on-auth-error
[request response]
(error-page
{:status 403
:title (str "Access to " (:uri request) " is not authorised")}))
(defn wrap-restricted [handler]
(defn wrap-restricted
"Example of how to wrap a route or handling in an authentication scheme"
[handler]
(restrict handler {:handler authenticated?
:on-error on-error}))
:on-error on-auth-error}))
(defn wrap-auth [handler]
(defn wrap-auth
"Installs the session backend on ring"
[handler]
(let [backend (session-backend)]
(-> handler
(wrap-authentication backend)
(wrap-authorization backend))))
(defn wrap-base [handler]
(defn wrap-base
"The all default middleware functions. These get applied to every route."
[handler]
(-> ((:middleware defaults) handler)
wrap-auth
(wrap-access-rules {:rules rules
:on-error on-error})
:on-error on-auth-error})
wrap-webjars
wrap-flash
(wrap-session {:cookie-attrs {:http-only true}})

View file

@ -66,17 +66,3 @@
(log/warn (:cause (Throwable->map e)))
(-> (found "/")
(assoc :flash {:error "An error occurred, please try again."})))))))))
;(catch [:status 401] _
; (error-page {:status 401
; :title "Error authenticating"
; :message "Please contact your system administrator to fix this issue"}))
; TODO catch using
;(defn multiple-status-endpoint [req]
; (let [resp (do-external-request req)]
; (condp = (:status resp)
; 201 (println ok)
; 401 (println error))))