erlang-jose icon indicating copy to clipboard operation
erlang-jose copied to clipboard

Verifying JWT with the RS256 algorithm using public key

Open truonglx-56 opened this issue 7 years ago • 3 comments

Can i read public key from file and verify token?

truonglx-56 avatar Apr 21 '17 02:04 truonglx-56

Yes, there are a few functions provided for reading JSON Web Keys from files:

Here is a list of examples of each of the signing and verification operations by algorithm type: https://hexdocs.pm/jose/JOSE.JWS.html#module-examples

If you're using RS256, for example, you might do the following:

public_jwk = JOSE.JWK.from_openssh_key_file("my-rsa-key.pub")
case JOSE.JWT.verify_strict(public_jwk, ["RS256"], token) do
  {true, jwt, _jws} ->
    # use the verified jwt claims however you would like...
    # NOTE: only the signature has been verified,
    #       things like token expiration are not part of this library
  _ ->
    # invalid token, do something else
end

Please let me know whether that works for you or if you have any other questions.

potatosalad avatar Apr 22 '17 12:04 potatosalad

read_key(RsaKey) -> case jose_jwk:from_pem_file(RsaKey) of {error, _} -> {undefined}; RsaPrivateKey -> jose_jwk:to_public(RsaPrivateKey) end.

Work for me, but when i check a expired token with: jose_jwt:verify_strict(RsaPublicKey, [<<"RS512">>], Token) return {true, _ , _ }.Second, when i change data in a claim, verify return {flase,,} ,although i didn't change key and use algorithm "RS512" .This is a bug or my fault?

truonglx-56 avatar Apr 24 '17 04:04 truonglx-56

Same with me, expired token seems to work.

imranismail avatar Nov 15 '17 17:11 imranismail