Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a :validation-fn option to jwt/unsign #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 7 additions & 7 deletions src/buddy/sign/jwt.clj
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,30 @@
(not-any? #{iss-claim} iss)
(not= iss-claim iss))))
(throw (ex-info (str "Issuer does not match " iss)
{:type :validation :cause :iss})))
{:type :validation :cause :iss})))

;; Check the `:aud` claim.
(when (and aud (let [aud-claim (:aud claims)]
(if (coll? aud-claim)
(not-any? #{aud} aud-claim)
(not= aud aud-claim))))
(throw (ex-info (str "Audience does not match " aud)
{:type :validation :cause :aud})))
{:type :validation :cause :aud})))

;; Check the `:exp` claim.
(when (and (:exp claims) (<= (:exp claims) (- now leeway)))
(throw (ex-info (format "Token is expired (%s)" (:exp claims))
{:type :validation :cause :exp})))
{:type :validation :cause :exp})))

;; Check the `:nbf` claim.
(when (and (:nbf claims) (> (:nbf claims) (+ now leeway)))
(throw (ex-info (format "Token is not yet valid (%s)" (:nbf claims))
{:type :validation :cause :nbf})))
{:type :validation :cause :nbf})))

;; Check the `:max-age` option.
(when (and (:iat claims) (number? max-age) (> (- now (:iat claims)) max-age))
(throw (ex-info (format "Token is older than max-age (%s)" max-age)
{:type :validation :cause :max-age})))
{:type :validation :cause :max-age})))
claims))

(defn- normalize-date-claims
Expand Down Expand Up @@ -106,14 +106,14 @@

(defn unsign
([message pkey] (unsign message pkey {}))
([message pkey {:keys [skip-validation] :or {skip-validation false} :as opts}]
([message pkey {:keys [skip-validation validation-fn] :or {skip-validation false validation-fn validate-claims} :as opts}]
(try
(let [claims (-> (jws/unsign message pkey opts)
(codecs/bytes->str)
(json/parse-string true))]
(if skip-validation
claims
(validate-claims claims opts)))
(validation-fn claims (dissoc opts :validation-fn))))
(catch com.fasterxml.jackson.core.JsonParseException e
(throw (ex-info "Message seems corrupt or manipulated."
{:type :validation :cause :signature}))))))
Expand Down