Skip to content

Commit

Permalink
Change all java.util.UUID to clojure.core uuid utilities (#276)
Browse files Browse the repository at this point in the history
Co-authored-by: Iain Wood <iain.wood@flexiana.com>
  • Loading branch information
gmsvalente and Iain Wood authored Dec 21, 2023
1 parent c2d9587 commit 58ae122
Show file tree
Hide file tree
Showing 20 changed files with 97 additions and 143 deletions.
8 changes: 4 additions & 4 deletions doc/How-To.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ To implement login, you need to [use the session interceptor](tutorials.md#inter

```clojure
(let [;; Create a unique ID
session-id (UUID/randomUUID)]
session-id (random-uuid)]
;; Store a new session in session storage
(add! session-storage session-id {:session-id session-id})
;; Make sure session-id is part of the response
Expand Down Expand Up @@ -64,7 +64,7 @@ you already have this injected, you can modify your create session function like
(let [;; Get user from database result
user (-> state :response-data :db-data first)
;; Create session
session-id (UUID/randomUUID)]
session-id (random-uuid)]
;; Store the new session in session storage. Notice the addition of user.
(add! session-storage session-id (assoc user :session-id session-id))
;; Make sure session-id is part of the response
Expand All @@ -82,7 +82,7 @@ Be sure to remove user's password and any other sensitive information before sto
;; Remove password for session storage
(dissoc :users/password))
;; Create session id
session-id (UUID/randomUUID)]
session-id (random-uuid)]
;; Store the new session in session storage
(add! session-storage session-id (assoc user :session-id session-id))
;; Make sure session-id is part of the response
Expand All @@ -101,7 +101,7 @@ Next, we check if the credentials are correct, so we use an `if` statement.
;; Remove password for session storage
(dissoc :users/password))
;; Create session ID
session-id (UUID/randomUUID)]
session-id (random-uuid)]
;; Store the new session in session storage
(add! session-storage session-id (assoc user :session-id session-id))
;; Make sure session-id is part of the response
Expand Down
4 changes: 2 additions & 2 deletions doc/tutorials.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ Adding to the previous examples:
After the session addition, it updates the user's last-login value in the database."
[state]
(if (valid-credentials? state)
(let [new-session-id (str (UUID/randomUUID))
(let [new-session-id (str (random-uuid))
session-backend (-> state :deps :session-backend)
{:users/keys [id] :as user} (-> state :response-data :db-data first)]
(remove-from-session-store! session-backend id)
Expand Down Expand Up @@ -584,7 +584,7 @@ ping:
(defn ping [deps]
(let [channel (get-in deps [:events-channel :channel])]
(async/>!! channel {:type :ping
:id (str (UUID/randomUUID))
:id (str (random-uuid))
:timestamp (.getTime (Date.))})))

(defn ->system
Expand Down
10 changes: 5 additions & 5 deletions docs/0.3.0/How-To.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ <h2><a href="#login-implementation" name="login-implementation"></a>Login implem
</code></pre>
<p>To implement login, you need to <a href="tutorials.md#interceptor-overriding">use the session interceptor</a> in</p>
<pre><code class="clojure">(let [;; Create a unique ID
session-id (UUID/randomUUID)]
session-id (random-uuid)]
;; Store a new session in session storage
(add! session-storage session-id {:session-id session-id})
;; Make sure session-id is part of the response
Expand All @@ -45,7 +45,7 @@ <h2><a href="#login-implementation" name="login-implementation"></a>Login implem
<pre><code class="clojure">(let [;; Get user from database result
user (-&gt; state :response-data :db-data first)
;; Create session
session-id (UUID/randomUUID)]
session-id (random-uuid)]
;; Store the new session in session storage. Notice the addition of user.
(add! session-storage session-id (assoc user :session-id session-id))
;; Make sure session-id is part of the response
Expand All @@ -60,7 +60,7 @@ <h2><a href="#login-implementation" name="login-implementation"></a>Login implem
;; Remove password for session storage
(dissoc :users/password))
;; Create session id
session-id (UUID/randomUUID)]
session-id (random-uuid)]
;; Store the new session in session storage
(add! session-storage session-id (assoc user :session-id session-id))
;; Make sure session-id is part of the response
Expand All @@ -76,7 +76,7 @@ <h2><a href="#login-implementation" name="login-implementation"></a>Login implem
;; Remove password for session storage
(dissoc :users/password))
;; Create session ID
session-id (UUID/randomUUID)]
session-id (random-uuid)]
;; Store the new session in session storage
(add! session-storage session-id (assoc user :session-id session-id))
;; Make sure session-id is part of the response
Expand Down Expand Up @@ -282,4 +282,4 @@ <h3><a href="#data-ownership" name="data-ownership"></a>Data ownership</h3>
(-&gt; state
(assoc :query (delete-query state))
(assoc-in [:request-data :restriction-fn] restriction-fn))))
</code></pre></div></div></div></body></html>
</code></pre></div></div></div></body></html>
11 changes: 4 additions & 7 deletions examples/acl/src/backend/app/interceptors/load_user.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
(ns interceptors.load-user
(:require
[honeysql.helpers :refer [select from where]]
[xiana.db :as db])
(:import
(java.util
UUID)))
[xiana.db :as db]))

(defn ->role
[user]
Expand All @@ -18,7 +15,7 @@

(defn valid-user?
[{:keys [request] :as state}]
(let [user-id (UUID/fromString (get-in request [:headers :authorization]))
(let [user-id (parse-uuid (get-in request [:headers :authorization]))
datasource (get-in state [:deps :db :datasource])
query (-> {}
(select :*)
Expand All @@ -31,8 +28,8 @@
(def load-user!
{:enter (fn [{:keys [request] :as state}]
(let [guest-user {:users/role :guest
:users/id (UUID/randomUUID)}
session-id (get-in request [:headers :session-id] (UUID/randomUUID))
:users/id (random-uuid)}
session-id (get-in request [:headers :session-id] (random-uuid))
user (try (let [valid-user (valid-user? state)]
(if (empty? valid-user)
guest-user
Expand Down
15 changes: 6 additions & 9 deletions examples/acl/src/backend/app/models/comments.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,21 @@
columns
values
sset]
:as helpers])
(:import
(java.util
UUID)))
:as helpers]))

(defn fetch-query
[{{{id :id} :query-params} :request
:as state}]
(assoc state :query (cond->
(from (select :*) :comments)
id (where [:= :id (UUID/fromString id)]))))
id (where [:= :id (parse-uuid id)]))))

(defn add-query
[{{user-id :users/id} :session-data
{{content :content post-id :post_id} :body-params} :request
:as state}]
(let [pid (try (UUID/fromString post-id)
(catch Exception _ (UUID/randomUUID)))]
(let [pid (try (parse-uuid post-id)
(catch Exception _ (random-uuid)))]
(assoc state :query (-> (insert-into :comments)
(columns :content :post_id :user_id)
(values [[content pid user-id]])))))
Expand All @@ -35,12 +32,12 @@
{{content :content} :body-params} :request
:as state}]
(assoc state :query (-> (helpers/update :comments)
(where [:= :id (UUID/fromString id)])
(where [:= :id (parse-uuid id)])
(sset {:content content}))))

(defn delete-query
[{{{id :id} :params} :request
:as state}]
(assoc state :query (cond->
(delete-from :comments)
id (where [:= :id (UUID/fromString id)]))))
id (where [:= :id (parse-uuid id)]))))
17 changes: 7 additions & 10 deletions examples/acl/src/backend/app/models/posts.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@
left-join
sset
delete-from]
:as helpers])
(:import
(java.util
UUID)))
:as helpers]))

(defn fetch-query
[{{{id :id} :query-params} :request
:as state}]
(assoc state :query (cond->
(from (select :*) :posts)
id (where [:= :id (UUID/fromString id)]))))
id (where [:= :id (parse-uuid id)]))))

(defn add-query
[{{user-id :users/id} :session-data
Expand All @@ -35,28 +32,28 @@
(let [content (or (get-in state [:request :params :content])
(get-in state [:request :body-params :content]))]
(assoc state :query (-> (helpers/update :posts)
(where [:= :id (UUID/fromString id)])
(where [:= :id (parse-uuid id)])
(sset {:content content})))))

(defn delete-query
[{{{id :id} :query-params} :request
:as state}]
(assoc state :query (cond->
(delete-from :posts)
id (where [:= :id (UUID/fromString id)]))))
id (where [:= :id (parse-uuid id)]))))

(defn fetch-by-ids-query
[{{{ids :ids} :body-params} :request
:as state}]
(assoc state :query (cond->
(from (select :*) :posts)
ids (where [:in :id (map #(UUID/fromString %) [ids])])
(coll? ids) (where [:in :id (map #(UUID/fromString %) ids)]))))
ids (where [:in :id (map #(parse-uuid %) [ids])])
(coll? ids) (where [:in :id (map #(parse-uuid %) ids)]))))

(defn fetch-with-comments-query
[{{{id :id} :query-params} :request
:as state}]
(assoc state :query (cond-> (-> (select :*)
(from :posts)
(left-join :comments [:= :posts.id :comments.post_id]))
id (where [:= :posts.id (UUID/fromString id)]))))
id (where [:= :posts.id (parse-uuid id)]))))
17 changes: 7 additions & 10 deletions examples/acl/src/backend/app/models/users.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
left-join
values
sset]
:as helpers])
(:import
(java.util
UUID)))
:as helpers]))

(defn ->store-user
[m]
Expand All @@ -28,7 +25,7 @@
:as state}]
(assoc state :query (cond->
(from (select :*) :users)
id (where [:= :id (UUID/fromString id)]))))
id (where [:= :id (parse-uuid id)]))))

(defn add-query
[{{body :body-params} :request
Expand All @@ -42,15 +39,15 @@
{body :body-params} :request
:as state}]
(assoc state :query (-> (helpers/update :users)
(where [:= :id (UUID/fromString id)])
(sset (core/update (->store-user body) :id #(UUID/fromString %))))))
(where [:= :id (parse-uuid id)])
(sset (core/update (->store-user body) :id #(parse-uuid %))))))

(defn delete-query
[{{{id :id} :query-params} :request
:as state}]
(assoc state :query (cond->
(delete-from :users)
id (where [:= :id (UUID/fromString id)]))))
id (where [:= :id (parse-uuid id)]))))

(defn fetch-with-post-comments-query
[{{{id :id} :query-params} :request
Expand All @@ -59,12 +56,12 @@
(from :users)
(left-join :posts [:= :posts.user_id :users.id])
(merge-left-join :comments [:= :posts.id :comments.post_id]))
id (where [:= :users.id (UUID/fromString id)]))))
id (where [:= :users.id (parse-uuid id)]))))

(defn fetch-with-post-query
[{{{id :id} :query-params} :request
:as state}]
(assoc state :query (cond-> (-> (select :*)
(from :users)
(left-join :posts [:= :posts.user_id :users.id]))
id (where [:= :users.id (UUID/fromString id)]))))
id (where [:= :users.id (parse-uuid id)]))))
7 changes: 2 additions & 5 deletions examples/sessions/src/backend/app/controllers/login.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
(:require
[clojure.data.json :as json]
[ring.util.request :refer [body-string]]
[xiana.session :as session])
(:import
(java.util
UUID)))
[xiana.session :as session]))

(def db
[{:id 1
Expand All @@ -31,7 +28,7 @@
(json/read-str :key-fn keyword))
(throw (ex-message "Missing body")))
user (find-user (:email rbody))
session-id (UUID/randomUUID)
session-id (random-uuid)
session-data {:session-id session-id
:user (dissoc user :password)}]
(if (and user (= (:password user) (:password rbody)))
Expand Down
19 changes: 8 additions & 11 deletions examples/sessions/src/backend/app/interceptors.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
(ns app.interceptors
(:require
[xiana.session :refer [delete! fetch]])
(:import
(java.util
UUID)))
[xiana.session :refer [delete! fetch]]))

(def require-logged-in
{:name ::require-logged-in
Expand Down Expand Up @@ -32,13 +29,13 @@
query-params :query-params} :request
:as state}]
(try (let [session-backend (-> state :deps :session-backend)
session-id (UUID/fromString (or (some->> headers
:session-id)
(some->> cookies
:session-id
:value)
(some->> query-params
:SESSIONID)))
session-id (parse-uuid (or (some->> headers
:session-id)
(some->> cookies
:session-id
:value)
(some->> query-params
:SESSIONID)))
session-data (fetch session-backend session-id)]
(assoc state :session-data (assoc session-data :session-id session-id)))
(catch Exception _ ; TODO: catch more specific exception or rethink that
Expand Down
9 changes: 3 additions & 6 deletions examples/sessions/test/integration_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
[next.jdbc :as jdbc]
[xiana.commons :as commons]
[xiana.config :as config]
[xiana.db :as db])
(:import
(java.util
UUID)))
[xiana.db :as db]))

(defn merge-connection
[config]
Expand Down Expand Up @@ -85,7 +82,7 @@
(is (= {"first-name" "Piotr", "id" 1, "email" "piotr@example.com", "last-name" "Developer"}
user)
"User has been logged in")
(is (true? (try (UUID/fromString body-session-id)
(is (true? (try (parse-uuid body-session-id)
true
(catch Exception _ false)))
"and has UUID session id")
Expand Down Expand Up @@ -132,7 +129,7 @@
(is (= {"first-name" "Piotr", "id" 1, "email" "piotr@example.com", "last-name" "Developer"}
user)
"User is logged in")
(is (true? (try (UUID/fromString session-id)
(is (true? (try (parse-uuid session-id)
true
(catch Exception _ false)))
"and has UUID session id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
[tick.core :as t])
(:import
(java.sql
Timestamp)
(java.util
UUID)))
Timestamp)))

(defn- clean-pg
[obj]
Expand Down Expand Up @@ -36,7 +34,7 @@
creator (-> state :session-data :users/id)
event {:payload p
:resource (name (:resource params))
:resource-id (UUID/fromString (:id params))
:resource-id (parse-uuid (:id params))
:modified-at (Timestamp/from (t/now))
:action action
:creator creator}]
Expand Down
Loading

0 comments on commit 58ae122

Please sign in to comment.