Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/fdc_ts/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]))

Expand Down Expand Up @@ -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)))))
Expand Down
14 changes: 14 additions & 0 deletions test/fdc_ts/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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+))

Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand Down