Search code examples
sessioncookiesclojuremiddlewarering

Change cookie attributes according to the request params


I'm creating a proxy server with several middle-wares, one of them is ring 'wrap-session'. I would like to be able to change dynamically the cookie attributes (max-age) that 'wrap-session' gets according to the request params. this is the proxy creation:

(defn- make-server
  [port service-spec auth-app backend-bouncer]
  (let [backend (session-backend {})
        proxy-handler         (make-proxy-handler service-spec auth-app backend-bouncer)
        bam-auth-handler      (buddy.auth.middleware/wrap-authentication proxy-handler backend)
        wrap-session-handler  (wrap-session bam-auth-handler {:cookie-name "myCookie" :cookie-attrs {:max-age 3600}})]
    (jetty/run-jetty wrap-session-handler {:port (or port 3000)})))

how can I do it?


Solution

  • Apparently there was no other way but to rewrite wrap-session and another and to copy the private method session-options to my file and called it local-session-option, this is how I have done it:

    (defn- local-session-options
      [options cookie-store]
      {:store        (options :store cookie-store)
       :cookie-name  (options :cookie-name "ring-session")
       :cookie-attrs (merge {:path "/"
                             :http-only true}
                            (options :cookie-attrs)
                            (if-let [root (options :root)]
                              {:path root}))})
    
    
    
    (defn local-wrap-session
      [handler]
      (let [store (mem/memory-store)]
        (fn [request]
            (let [cookie-max_age (get-max-age request) ; add additional setting and calculation here
                  options (local-session-options {:cookie-name "name" :cookie-attrs {:max-age cookie-max_age}} store)
                  new-request (rms/session-request request options)]
              (-> (handler new-request)
                  (rms/session-response new-request options))))))