diff --git a/src/framework/components/interceptors.clj b/src/framework/components/interceptors.clj index 7b3897cf..af05e24b 100644 --- a/src/framework/components/interceptors.clj +++ b/src/framework/components/interceptors.clj @@ -1,6 +1,5 @@ (ns framework.components.interceptors (:require - [clojure.core :as core] [clojure.walk :refer [keywordize-keys]] [framework.acl.core :as acl] [framework.components.interceptors.muuntaja :as m-int] @@ -37,6 +36,8 @@ :role :guest}))) (defn require-logged-in + "Tries to extract userId from headers/authorization, and adds it as user/id into session-data. + If fails, it executes 'or-else' parameter function. By default it inserts a new guest user" ([] (require-logged-in guest)) ([or-else] @@ -48,6 +49,9 @@ (or-else state)))})) (def session-interceptor + ":enter Extracts session-id from headers. Tries to get last session with the same id from session backend + :leave Stores actual session in session backend + TODO session Time To Leave" {:enter (fn [{request :request :as state}] (let [sessions-backend (-> state :deps @@ -76,6 +80,10 @@ #(keywordize-keys ((par/wrap-params identity) %)))))}) (defn db-access + "Runs HoneySQL query provided in (:query state) + Injects the result to (state [:response-data :db-data]) + Optional parameter function: executed on :enter, if session interceptors creates new-session key in session data + for instance fetching user from db by it's id" ([] {:leave (fn [{query :query :as state}] @@ -92,16 +100,20 @@ (xiana/ok state)))))) (def view + "Executes view function to create response" {:leave (fn [{view :view :as state}] (view state))}) (defn muuntaja + "Muuntaja interceptor wrapped into xiana monad" ([] (muuntaja m-int/muun-instance)) ([instance] (wrap/interceptor instance))) (defn acl-restrict + ":enter checks access control + :leave place for tightening db query via provided owner-fn" ([] (acl-restrict nil)) ([m] @@ -116,6 +128,7 @@ state)))})) (def side-effect + "Place for business logic based on db-data, before rendering view" {:leave (fn [{f :side-effect :as state}] (if f (f state) diff --git a/src/framework/components/interceptors/muuntaja.clj b/src/framework/components/interceptors/muuntaja.clj index df6d6646..dc8d66f0 100644 --- a/src/framework/components/interceptors/muuntaja.clj +++ b/src/framework/components/interceptors/muuntaja.clj @@ -1,4 +1,5 @@ (ns framework.components.interceptors.muuntaja + "Prepare default munntaja instance" (:require [clojure.data.xml :as xml] [clojure.string] diff --git a/src/framework/components/interceptors/wrap.clj b/src/framework/components/interceptors/wrap.clj index 4c255d3d..e39a675b 100644 --- a/src/framework/components/interceptors/wrap.clj +++ b/src/framework/components/interceptors/wrap.clj @@ -3,6 +3,7 @@ [xiana.core :as xiana])) (defn interceptor + "Interceptor wrapper to use xiana monad" [in] (cond-> {} (:enter in) (assoc :enter (fn [state] (xiana/ok ((:enter in) state)))) @@ -10,6 +11,7 @@ (:error in) (assoc :error (fn [state] (xiana/error ((:error in) state)))))) (defn middleware->enter + "add middleware as :enter of interceptor" ([middleware] (middleware->enter {} middleware)) ([interceptor middleware] @@ -17,6 +19,7 @@ (xiana/ok (assoc state :request ((middleware identity) req))))))) (defn middleware->leave + "add middleware as :leave of interceptor" ([middleware] (middleware->leave {} middleware)) ([interceptor middleware] diff --git a/src/framework/components/runner.clj b/src/framework/components/runner.clj index 6efc9402..63561f46 100644 --- a/src/framework/components/runner.clj +++ b/src/framework/components/runner.clj @@ -8,6 +8,8 @@ (xiana/ok state))) (defn run + "Executes interceptors around a function. Be aware of raw recursion + TODO: refactor it to use reduce instead of recursion" ([state action] (run state [] action)) ([state interceptors action] diff --git a/src/framework/components/session/auth.clj b/src/framework/components/session/auth.clj deleted file mode 100644 index ce928592..00000000 --- a/src/framework/components/session/auth.clj +++ /dev/null @@ -1,9 +0,0 @@ -(ns framework.components.session.auth - (:require - [xiana.core :as xiana])) - -(defn require-logged-in - [state] - (if (get-in state [:session-data :user]) - (xiana/ok state) - (xiana/error (assoc state :response {:status 401 :body "Unauthorized"})))) diff --git a/src/framework/components/session/middleware.clj b/src/framework/components/session/middleware.clj deleted file mode 100644 index 725883be..00000000 --- a/src/framework/components/session/middleware.clj +++ /dev/null @@ -1,30 +0,0 @@ -(ns framework.components.session.middleware - (:require - [framework.components.session.backend :refer [fetch add! delete!]] - [xiana.core :as xiana])) - -(defn session-middleware - [{request :request :as state}] - (let [sessions-backend (-> state - :deps - :session - :session-backend) - session-id (get-in request [:headers "session-id"]) - user (fetch sessions-backend session-id)] - (xiana/ok (assoc-in state [:session-data :user] user)))) - -(defn auth-middleware - [state] - (let [sessions-backend (-> state - :deps - :session - :session-backend) - login-data (-> state - :login-data) - logout-data (-> state - :logout-data)] - (when login-data - (add! sessions-backend (:session-id login-data) login-data)) - (when logout-data - (delete! sessions-backend (:session-id logout-data))) - (xiana/ok state)))