Skip to content

Instantly share code, notes, and snippets.

@tolitius
Created March 30, 2020 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tolitius/80c3a04ef41451f18f934aed97473fff to your computer and use it in GitHub Desktop.
Save tolitius/80c3a04ef41451f18f934aed97473fff to your computer and use it in GitHub Desktop.
how to use jwks with buddy
(require '[jsonista.core :as json]
'[org.httpkit.client :as http]
'[buddy.core.keys :as keys]
'[buddy.sign.jwt :as jwt])
(def mapper (json/object-mapper {:decode-key-fn keyword}))
;; if done fo real => check for http/get error
(defn jwks->pubkey [jwks-url]
(-> @(http/get jwks-url)
:body
(json/read-value mapper)
:keys
first
keys/jwk->public-key))
(defn validate-token [pkey token claims]
(try
(jwt/unsign token pkey claims)
(catch Throwable t
{:error true
:details (ex-data t)})))
(def token "eyJhbG.....")
(-> (jwks->pubkey "https://YOUR_DOMAIN/.well-known/jwks.json")
(validate-token token {:alg :rs256}))
;; => {:scope ["app:area:read"],
;; :client_id "gitpod",
;; :iss "https://YOUR_DOMAIN",
;; :exp 1585616824}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment