Skip to content

Commit

Permalink
Replace clj-time with native java.time (fixes #27)
Browse files Browse the repository at this point in the history
Requires Java 8 due to use of java.time.

Implement changes from review feedback
  • Loading branch information
gerdint committed Aug 28, 2023
1 parent e18a062 commit a4aac7c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
1 change: 0 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
:dependencies [[org.clojure/clojure "1.7.0"]
[cheshire "5.10.1"]
[clj-http "3.12.3"]
[clj-time "0.15.2"]
[ring/ring-core "1.9.4"]]
:profiles
{:dev {:dependencies [[clj-http-fake "1.0.3"]
Expand Down
12 changes: 6 additions & 6 deletions src/ring/middleware/oauth2.clj
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
(ns ring.middleware.oauth2
(:require [clj-http.client :as http]
[clj-time.core :as time]
[clojure.string :as str]
[crypto.random :as random]
[ring.util.codec :as codec]
[ring.util.request :as req]
[ring.util.response :as resp]))
[ring.util.response :as resp])
(:import [java.time Instant]
[java.util Date]))

(defn- redirect-uri [profile request]
(-> (req/request-url request)
Expand Down Expand Up @@ -47,10 +48,9 @@
[{{:keys [access_token expires_in refresh_token id_token] :as body} :body}]
(-> {:token access_token
:extra-data (dissoc body :access_token :expires_in :refresh_token :id_token)}
(cond-> expires_in (assoc :expires (-> expires_in
coerce-to-int
time/seconds
time/from-now))
(cond-> expires_in (assoc :expires (-> (Instant/now)
(.plusSeconds (coerce-to-int expires_in))
(Date/from)))
refresh_token (assoc :refresh-token refresh_token)
id_token (assoc :id-token id_token))))

Expand Down
31 changes: 18 additions & 13 deletions test/ring/middleware/oauth2_test.clj
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
(ns ring.middleware.oauth2-test
(:require [clj-http.fake :as fake]
[clj-time.core :as time]
[clojure.string :as str]
[clojure.test :refer :all]
[ring.middleware.oauth2 :as oauth2 :refer [wrap-oauth2]]
[ring.mock.request :as mock]
[ring.middleware.params :refer [wrap-params]]
[ring.util.codec :as codec]))
[ring.util.codec :as codec])
(:import [java.time Instant]
[java.util Date]))

(def test-profile
{:authorize-uri "https://example.com/oauth2/authorize"
Expand Down Expand Up @@ -62,9 +63,12 @@
:body "{\"access_token\":\"defdef\",\"expires_in\":3600,\"foo\":\"bar\"}"})

(defn approx-eq [a b]
(time/within?
(time/interval (time/minus a (time/seconds 1)) (time/plus a (time/seconds 1)))
b))
(let [a-ms (.getTime a)
b-ms (.getTime b)]
(< (- a-ms 1000) b-ms (+ a-ms 1000))))

(defn seconds-from-now-to-date [secs]
(-> (Instant/now) (.plusSeconds secs) (Date/from)))

(deftest test-redirect-uri
(fake/with-fake-routes
Expand All @@ -75,12 +79,12 @@
(assoc :session {::oauth2/state "xyzxyz"})
(assoc :query-params {"code" "abcabc", "state" "xyzxyz"}))
response (test-handler request)
expires (-> 3600 time/seconds time/from-now)]
expires (seconds-from-now-to-date 3600)]
(is (= 302 (:status response)))
(is (= "/" (get-in response [:headers "Location"])))
(is (map? (-> response :session ::oauth2/access-tokens)))
(is (= "defdef" (-> response :session ::oauth2/access-tokens :test :token)))
(is (approx-eq (-> 3600 time/seconds time/from-now)
(is (approx-eq expires
(-> response :session ::oauth2/access-tokens :test :expires)))
(is (= {:foo "bar"} (-> response :session ::oauth2/access-tokens :test :extra-data)))))

Expand Down Expand Up @@ -130,12 +134,13 @@
request (-> (mock/request :get "/oauth2/test/callback")
(assoc :session {::oauth2/state "xyzxyz"})
(assoc :query-params {"code" "abcabc", "state" "xyzxyz"}))
response (handler request)]
response (handler request)
expires (seconds-from-now-to-date 3600)]
(is (= 302 (:status response)))
(is (= "/" (get-in response [:headers "Location"])))
(is (map? (-> response :session ::oauth2/access-tokens)))
(is (= "defdef" (-> response :session ::oauth2/access-tokens :test :token)))
(is (approx-eq (-> 3600 time/seconds time/from-now)
(is (approx-eq expires
(-> response :session ::oauth2/access-tokens :test :expires)))))))

(deftest test-access-tokens-key
Expand Down Expand Up @@ -197,7 +202,7 @@
(assoc :session {::oauth2/state "xyzxyz"})
(assoc :query-params {"code" "abcabc", "state" "xyzxyz"}))
response (test-handler request)
expires (-> 3600 time/seconds time/from-now)]
expires (seconds-from-now-to-date 3600)]
(is (= 302 (:status response)))
(is (= "/" (get-in response [:headers "Location"])))
(is (map? (-> response :session ::oauth2/access-tokens)))
Expand All @@ -206,7 +211,7 @@
:test :refresh-token)))
(is (= "abc.def.ghi" (-> response :session ::oauth2/access-tokens
:test :id-token)))
(is (approx-eq (-> 3600 time/seconds time/from-now)
(is (approx-eq expires
(-> response :session ::oauth2/access-tokens :test :expires)))))))

(def openid-response-with-string-expires
Expand All @@ -225,10 +230,10 @@
(assoc :session {::oauth2/state "xyzxyz"})
(assoc :query-params {"code" "abcabc" "state" "xyzxyz"}))
response (test-handler request)
expires (-> 3600 time/seconds time/from-now)]
expires (seconds-from-now-to-date 3600)]
(is (= 302 (:status response)))
(is (= "/" (get-in response [:headers "Location"])))
(is (approx-eq (-> 3600 time/seconds time/from-now)
(is (approx-eq expires
(-> response :session ::oauth2/access-tokens :test :expires)))))))

(defn redirect-handler [{:keys [oauth2/access-tokens]}]
Expand Down

0 comments on commit a4aac7c

Please sign in to comment.