diff --git a/src/fdc_ts/core.clj b/src/fdc_ts/core.clj index d322417..14d95d5 100644 --- a/src/fdc_ts/core.clj +++ b/src/fdc_ts/core.clj @@ -17,6 +17,7 @@ [compojure.core :refer [defroutes ANY GET PUT POST context]] [compojure.route :as route] [cheshire.core :as json] + [clojure.string :as str] [clj-time [core :as t][coerce :as tc][format :as tf][predicates :as tp]] [schema.core :as s])) @@ -55,7 +56,11 @@ ;TODO Move put to separate module (defn auth [token project-tokens project ctx] - (let [request-token (get-in ctx [:request :headers "auth-token"])] +; TODO remove auth-token header + (let [header-token (get-in ctx [:request :headers "auth-token"]) + auth-raw (first (remove nil? [(get-in ctx [:request :headers "Authorization"]) " "])) + auth-token (last (str/split auth-raw #" ")) + request-token (first (remove str/blank? [header-token auth-token]))] (and (some? request-token) (or (= (token env) request-token) (= (get-in (json/parse-string (project-tokens env)) [project]) request-token))))) diff --git a/test/fdc_ts/core_test.clj b/test/fdc_ts/core_test.clj index 5465b5d..86a2271 100644 --- a/test/fdc_ts/core_test.clj +++ b/test/fdc_ts/core_test.clj @@ -63,6 +63,9 @@ (defn- with-valid-pub-token [request] (mock/header request "auth-token" +valid-pub-token+)) +(defn- with-valid-bearer-pub-token [request] + (mock/header request "Authorization" (str "Bearer " +valid-pub-token+))) + (defn- with-valid-meta-token [request] (mock/header request "auth-token" +valid-meta-token+)) @@ -260,6 +263,14 @@ #(let [response (handler (with-valid-pub-token put-publish-deployment))] (is (= 201 (:status response)))))) +(deftest should-accept-put-deployment-with-bearer-token + (with-redefs-fn + {#'deployment/insert-deployment + (fn [data] + )} + #(let [response (handler (with-valid-bearer-pub-token put-publish-deployment))] + (is (= 201 (:status response)))))) + ;;;; auth (deftest auth-should-work @@ -270,6 +281,9 @@ ; General token match -> authorized (is (= (core/auth :auth-token-publish :auth-token-project "foo" {:request {:headers {"auth-token" "test-token-pub"}}}) true)) + ; General token as Bearer match -> authorized + (is (= (core/auth :auth-token-publish :auth-token-project "foo" {:request {:headers {"Authorization" "Bearer test-token-pub"}}}) + true)) ; Project token match -> authorized (is (= (core/auth :auth-token-publish :auth-token-project "foo" {:request {:headers {"auth-token" "test-token-foo"}}}) true))