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

Can't sign a claims map #30

Closed
rcanepa opened this issue Jun 10, 2016 · 3 comments
Closed

Can't sign a claims map #30

rcanepa opened this issue Jun 10, 2016 · 3 comments

Comments

@rcanepa
Copy link

rcanepa commented Jun 10, 2016

I added to my project the dependency [buddy/buddy-sign "1.1.0"] to implement authentication features. However, for some reason this simple example doesn't work (from the repl):

(require '[buddy.sign.jws :as jws])
;; this work
(jws/sign "a" "secret")
;; => "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.YQ.fbSW5D1FbwwcFijKn6aSghRHIwcpE102OKAvkfxsxls"

;; this doesn't work
(jws/sign {:a 100} "secret") ;; throws an error

And this is the error message:
IllegalArgumentException No implementation of method: :-to-bytes of protocol: #'buddy.core.codecs/IByteArray found for class: clojure.lang.PersistentArrayMap clojure.core/-cache-protocol-fn (core_deftype.clj:568)

I am getting a similar error if I try to sign an integer:
IllegalArgumentException No implementation of method: :-to-bytes of protocol: #'buddy.core.codecs/IByteArray found for class: java.lang.Long clojure.core/-cache-protocol-fn (core_deftype.clj:568)

Looking the sign implementation (from cursive), I see that it has a pre clause to enforce the use of a map as the claims container, which contradicts the results I am getting.

(defn sign
  "Sign arbitrary length string/byte array using
  json web token/signature."
  ;; The exp nbf and iat keys in the options are deprecated
  ;; and will be removed in the next version.
  [claims pkey & [{:keys [alg typ] :or {alg :hs256 typ :jws} :as opts}]]
  {:pre [(map? claims)]}
  (let [header (encode-header alg typ)
        claims (encode-claims claims opts)
        signature (calculate-signature {:key pkey
                                        :alg alg
                                        :header header
                                        :claims claims})]
    (str/join "." [header claims signature])))

Any idea of what could be the problem?

@rcanepa
Copy link
Author

rcanepa commented Jun 10, 2016

It seems that I found the problem. I am using the old API right?... here https://github.com/funcool/buddy-sign/blob/master/CHANGES.adoc I found that I should use the jwt namespace instead of the jws for my purposes. Is that right?

@niwinz
Copy link
Member

niwinz commented Jun 11, 2016

Yes, is that right, jws and jwe namespaces are low-level api that works with bytes payload as RFC specifies.

@niwinz niwinz closed this as completed Jun 11, 2016
@rcanepa
Copy link
Author

rcanepa commented Jun 11, 2016

Thanks!... everything worked great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants