Skip to content

Commit

Permalink
Implement map-with-meta-token and map-check-meta-token
Browse files Browse the repository at this point in the history
  • Loading branch information
verberktstan committed Jan 23, 2024
1 parent 753268a commit 1907c13
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
13 changes: 13 additions & 0 deletions src/swark/authom.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
#?(:cljs (catch :default nil)
:clj (catch Throwable _ nil))))

(defn map-with-meta-token
"Returns the map `m` with the hashed token in it's metadata. Only accepts a map and primary-key must be present in map `m`."
[m primary-key & [pass secret :as args]]
(-> m map? assert)
(-> m (get primary-key) assert)
(merge (apply with-meta-token (select-keys m [primary-key]) args) m))

(comment
;; TODO: Turn fiddle code into tests
(-> {:user/id 123} with-meta-token meta)
Expand All @@ -53,6 +60,12 @@
(when (= token (apply ->hash item args))
item)))

(defn map-check-meta-token
[m primary-key & [pass secret :as args]]
(-> m map? assert)
(-> m (get primary-key) assert)
(apply check-meta-token (select-keys m [primary-key]) args))

(comment
;; TODO: Turn fiddle code into tests
(let [user (with-meta-token {:user/id 123} "password" "SECRET")]
Expand Down
25 changes: 17 additions & 8 deletions test/swark/authom_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,28 @@
(:require [clojure.test :as t]
[swark.authom :as sut]))

(def ITEM #:user{:id 123 :fullname "User Name"})
(def WITH-TOKEN (-> ITEM (select-keys [:user/id]) (sut/with-meta-token "password" "SECRET")))
(def TOKEN -301775488)
(def ITEM [:some :data])
(def WITH-TOKEN (-> ITEM (sut/with-meta-token "password")))
(def TOKEN 1446530582)

(def USER #:user{:id 123 :fullname "User Name"})
(def USER-WITH-TOKEN (-> USER (sut/map-with-meta-token :user/id "password" "SECRET")))
(def USER-TOKEN -301775488)

(t/deftest meta-token
(t/testing "Returns the token if stored in metadata"
(t/is (= TOKEN (sut/meta-token WITH-TOKEN))))
(t/is (-> WITH-TOKEN sut/meta-token #{TOKEN}))
(t/is (-> USER-WITH-TOKEN sut/meta-token #{USER-TOKEN})))
(t/testing "Returns nil if the token is NOT stored in metadata"
(t/is (nil? (sut/meta-token ITEM)))))
(t/is (-> ITEM sut/meta-token nil?))
(t/is (-> USER sut/meta-token nil?))))

(t/deftest check-meta-token
(t/testing "Returns a truethy value when password and secret match"
(t/is (sut/check-meta-token WITH-TOKEN "password" "SECRET")))
(t/is (-> WITH-TOKEN (sut/check-meta-token "password")))
(t/is (-> USER-WITH-TOKEN (sut/map-check-meta-token :user/id "password" "SECRET"))))
(t/testing "Returns a falsey value when password and/or secret do not match"
(t/is (not (sut/check-meta-token WITH-TOKEN "wrong-password" "SECRET")))
(t/is (not (sut/check-meta-token WITH-TOKEN "password" "WRONG_SECRET")))))
(t/is (-> WITH-TOKEN (sut/check-meta-token "wrong-password" "SECRET") not))
(t/is (-> WITH-TOKEN (sut/check-meta-token "password" "WRONG_SECRET") not))
(t/is (-> USER-WITH-TOKEN (sut/map-check-meta-token :user/id "wrong-password" "SECRET") not))
(t/is (-> USER-WITH-TOKEN (sut/map-check-meta-token :user/id "password" "WRONG_SECRET") not))))

0 comments on commit 1907c13

Please sign in to comment.