A Clojure library for JSON Web Token(JWT), forked from https://github.com/liquidz/clj-jwt
(ns foo
(:require
[com.timezynk.clj-jwt.core :refer [jwt sign to-str]]
[com.timezynk.clj-jwt.key :refer [private-key]]
[java-time.api :refer [days plus zoned-date-time]]))
(def claim
{:iss "foo"
:exp (plus (zoned-date-time) (days 1))
:iat (zoned-date-time)})
(def rsa-prv-key (private-key "rsa/private.key" "pass phrase"))
(def ec-prv-key (private-key "ec/private.key"))
;; plain JWT
(-> claim jwt to-str)
;; HMAC256 signed JWT
(-> claim jwt (sign :HS256 "secret") to-str)
;; RSA256 signed JWT
(-> claim jwt (sign :RS256 rsa-prv-key) to-str)
;; ECDSA256 signed JWT
(-> claim jwt (sign :ES256 ec-prv-key) to-str)
(ns foo
(:require
[com.timezynk.clj-jwt.core :refer [jwt sign str->jwt to-str verify]]
[com.timezynk.clj-jwt.key :refer [private-key public-key]]
[java-time.api :refer [days plus zoned-date-time]]))
(def claim
{:iss "foo"
:exp (plus (zoned-date-time) (days 1))
:iat (zoned-date-time)})
(def rsa-prv-key (private-key "rsa/private.key" "pass phrase"))
(def rsa-pub-key (public-key "rsa/public.key"))
(def ec-prv-key (private-key "ec/private.key"))
(def ec-pub-key (public-key "ec/public.key"))
;; verify plain JWT
(let [token (-> claim jwt to-str)]
(-> token str->jwt verify))
;; verify HMAC256 signed JWT
(let [token (-> claim jwt (sign :HS256 "secret") to-str)]
(-> token str->jwt (verify "secret")))
;; verify RSA256 signed JWT
(let [token (-> claim jwt (sign :RS256 rsa-prv-key) to-str)]
(-> token str->jwt (verify rsa-pub-key)))
;; verify ECDSA256 signed JWT
(let [token (-> claim jwt (sign :ES256 ec-prv-key) to-str)]
(-> token str->jwt (verify ec-pub-key)))
You can specify algorithm name (OPTIONAL) for more secure verification.
(ns foo
(:require
[com.timezynk.clj-jwt.core :refer [jwt sign str->jwt to-str verify]]))
;; verify with specified algorithm
(let [key "secret"
token (-> {:foo "bar"} jwt (sign :HS256 key) to-str)]
(-> token str->jwt (verify :HS256 key)) ;; => true
(-> token str->jwt (verify :none key))) ;; => false
(ns foo
(:require
[com.timezynk.clj-jwt.core :refer [jwt sign str->jwt to-str verify]]
[java-time.api :refer [days plus zoned-date-time]]))
(def claim
{:iss "foo"
:exp (plus (zoned-date-time) (days 1))
:iat (zoned-date-time)})
;; decode plain JWT
(let [token (-> claim jwt to-str)]
(println (-> token str->jwt :claims)))
;; decode signed JWT
(let [token (-> claim jwt (sign :HS256 "secret") to-str)]
(println (-> token str->jwt :claims)))
Can you improve this documentation? These fine people already did:
liquidz, Masashi Iizuka, Lars & Relevance PairEdit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close