From a0fd9b1f8c333aa9987442bbce27e1cd04a03b01 Mon Sep 17 00:00:00 2001 From: gered Date: Sat, 27 Dec 2014 18:10:32 -0500 Subject: [PATCH] add support for automatically including anti-forgery tokens in POST reqs --- project.clj | 3 ++- .../clj_browserchannel_messaging/server.clj | 9 ++++---- .../clj_browserchannel_messaging/client.cljs | 22 ++++++++++++++++--- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/project.clj b/project.clj index 2edf086..e10347e 100644 --- a/project.clj +++ b/project.clj @@ -7,7 +7,8 @@ :dependencies [[org.clojure/clojure "1.6.0"] [org.clojure/clojurescript "0.0-2371" :scope "provided"] [org.clojure/core.async "0.1.346.0-17112a-alpha"] - [net.thegeez/clj-browserchannel-server "0.1.0"]] + [net.thegeez/clj-browserchannel-server "0.1.0"] + [prismatic/dommy "1.0.0"]] :source-paths ["target/generated/src/clj" "src/clj"] :resource-paths ["target/generated/src/cljs" "src/cljs"] diff --git a/src/clj/clj_browserchannel_messaging/server.clj b/src/clj/clj_browserchannel_messaging/server.clj index 6ae994d..0674186 100644 --- a/src/clj/clj_browserchannel_messaging/server.clj +++ b/src/clj/clj_browserchannel_messaging/server.clj @@ -1,9 +1,10 @@ (ns clj-browserchannel-messaging.server (:refer-clojure :exclude [send]) - (:require [clojure.edn :as edn] - [clojure.core.async :refer [chan pub sub tag with a name of 'anti-forgery-token', + the value of this meta tag will be automatically included under the + X-CSRF-Token HTTP header on all BrowserChannel POST requests to work with + any CSRF protection your web app uses (e.g. Ring's wrap-anti-forgery + middleware)." [& {:keys [base middleware]}] - (let [base (or base "/browserchannel")] + (let [base (or base "/browserchannel") + anti-forgery-token (get-anti-forgery-token)] (register-middleware! middleware) (events/listen js/window "unload" @@ -173,6 +187,8 @@ ; this seems to help prevent premature session timeouts from occuring (vs the default of 3) (set! goog.net.BrowserChannel/BACK_CHANNEL_MAX_RETRIES 20) (.setHandler browser-channel (->handler)) + (if anti-forgery-token + (.setExtraHeaders browser-channel (js-obj "X-CSRF-Token" anti-forgery-token))) (.connect browser-channel (str base "/test") (str base "/bind"))))