liquidz / clj-jwt

Clojure library for JSON Web Token(JWT)
118 stars 31 forks source link

Support for decoding tokens #14

Closed kennyjwilli closed 9 years ago

kennyjwilli commented 9 years ago

Is there support for decoding tokens? If so, are there any docs for it? I have searched and searched for it and have found no such docs or functionality.

liquidz commented 9 years ago

You can use str->jwt function to decode tokens. I added sample codes to README.md (284727d)

(require '[clj-jwt.core :refer :all])

;; generate token
(def claim {:foo "bar"})
(def token (-> claim jwt (sign :HS256 "secret") to-str)) ; => eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIifQ.dtxWM6MIcgoeMgH87tGvsNDY6cHWL6MGW4LeYvnm1JA

;; decode token
(-> token str->jwt) ; => #clj_jwt.core.JWT{:header {:alg "HS256", :typ "JWT"}, :claims {:foo "bar"}, :signature "dtxWM6MIcgoeMgH87tGvsNDY6cHWL6MGW4LeYvnm1JA"}
(-> token str->jwt :claims :foo) ; => "bar"
(-> token str->jwt (verify "secret")) ; => true
kennyjwilli commented 9 years ago

Ah got it. Thanks!