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.
  • Loading branch information
gerdint committed Aug 15, 2023
1 parent e18a062 commit ad4fb13
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 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
11 changes: 7 additions & 4 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 @@ -43,14 +44,16 @@
(Integer/parseInt n)
n))

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

(defn- format-access-token
[{{: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))
seconds-from-now-to-date))
refresh_token (assoc :refresh-token refresh_token)
id_token (assoc :id-token id_token))))

Expand Down
24 changes: 12 additions & 12 deletions test/ring/middleware/oauth2_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(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]]
Expand Down Expand Up @@ -62,9 +61,9 @@
: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))))

(deftest test-redirect-uri
(fake/with-fake-routes
Expand All @@ -75,12 +74,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 (#'oauth2/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 +129,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 (#'oauth2/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 +197,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 (#'oauth2/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 +206,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 +225,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 (#'oauth2/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 ad4fb13

Please sign in to comment.