garyf / json_web_token_ex

An Elixir implementation of the JSON Web Token (JWT) Standard, RFC 7519
MIT License
143 stars 50 forks source link

How to pass in proper RSA key? #24

Open munjalpatel opened 6 years ago

munjalpatel commented 6 years ago

Hello,

I am trying to verify a JWT token using RS256 and an RSA public key. However, I keep getting "ArgumentError"

What is the correct way to pass in RSA key?

This is what I am doing:

[{_, key, _}] = "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----\n"
|> :public_key.pem_decode

token = "__header__.__payload__.__sig__"
JsonWebToken.verify(token, %{alg: "RS256", key: key})

This is the error I get:

** (ArgumentError) argument error (crypto) :crypto.pkey_verify_nif(:rsa, :sha256, "header.payload.sig", <<7, 49, 241, 224, ..., 85, 4, 3, 19, ...>>, []) (crypto) crypto.erl:420: :crypto.verify/6 (json_web_token) lib/json_web_token/jws.ex:103: JsonWebToken.Jws.verified/3 (json_web_token) lib/json_web_token/jwt.ex:89: JsonWebToken.Jwt.verify/2

ghost commented 3 years ago

Also having this issue, got any solution?

barberj commented 2 years ago

This is how I'm doing it

defmodule .. do

@public_key JsonWebToken.Algorithm.RsaUtil.public_key(Application.get_env(:my_app, :pub_pem))

  def decode(jwt) do
    options = %{alg: "RS256", iss: _redacted, key: @public_key}

    JsonWebToken.verify(jwt, options)
    |> mockable(JwtClaims).verify(options)
  end
end