Created
March 30, 2020 23:23
-
-
Save tolitius/80c3a04ef41451f18f934aed97473fff to your computer and use it in GitHub Desktop.
how to use jwks with buddy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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