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

Argument error when generating token #247

Closed
pedroseabra1091 opened this issue Sep 16, 2019 · 8 comments
Closed

Argument error when generating token #247

pedroseabra1091 opened this issue Sep 16, 2019 · 8 comments

Comments

@pedroseabra1091
Copy link

pedroseabra1091 commented Sep 16, 2019

When generating the token while defining the signer explicitly I get:

** (ArgumentError) argument error: [[], {:jose_jws, {:jose_jws_alg_rsa_pkcs1_v1_5, :RS256}, :undefined, %{"typ" => "JWT"}}, %{"aud" => "Joken", "exp" => 1568637168, "iat" => 1568629968, "iss" => "Joken", "jti" => "2n2e9990n2g0g8v1fg0000b3", "nbf" => 1568629968}]
    (jose) src/jose_jwt.erl:184: :jose_jwt.sign/3
    (joken) lib/joken/signer.ex:119: Joken.Signer.sign/2
    (joken) lib/joken.ex:364: Joken.encode_and_sign/3
    (joken) lib/joken.ex:210: Joken.generate_and_sign/4

I am currently creating the signer through the config file like stated in (https://hexdocs.pm/joken/assymetric_cryptography_signers.html#content):

config :joken,
  rsa_key: [
    signer_alg: "RS256",
    key_pem: "
    -----BEGIN PUBLIC KEY-----
    ....
    -----END PUBLIC KEY-----"
  ]
@victorolinasc
Copy link
Collaborator

Hi @pedroseabra1091

It seems that your PEM is not properly formatted. You are using single quotes (") for a multi line string. That preserves the spaces in between.

Try setting your PEM like:

config :joken,
  rsa_key: [
    signer_alg: "RS256",
    key_pem: """
    -----BEGIN PUBLIC KEY-----
    ....
    -----END PUBLIC KEY-----
    """
  ]

It is important that the final triple quotes be aligned with where each starts so that Elixir will trim it automatically.

Please tell me if that works

@pedroseabra1091
Copy link
Author

pedroseabra1091 commented Sep 16, 2019

Hey @victorolinasc !
Still breaks, however, the problem isn't the same. Now when I try to generate_and_sign after creating the signer with parse_config(:rsa_key) the following error pops:

14:36:26.873 [error] Process #PID<0.306.0> raised an exception
** (Joken.Error) Can't sign your token because couldn't create a signer.

To create a signer we need a key in config.exs. You can define
a key in your config.exs in several ways: (...)

Could it be a badly sanitized private key?

@victorolinasc
Copy link
Collaborator

That is strange....

Try this please:

%Joken.Signer{} = signer = Joken.Signer.parse_config(:rsa_key)
defmodule MyToken do
    use Joken.Config
    def token_config, do: %{}
end
token = "xxxxx"
MyToken.verify_and_validate(token, signer)

@pedroseabra1091
Copy link
Author

pedroseabra1091 commented Sep 16, 2019

I already figured it out. I was missing the token_config argument in the generate_and_sign. I was only sending the extra claims and the signer

@victorolinasc
Copy link
Collaborator

Nice! Can I close this then?

@pedroseabra1091
Copy link
Author

pedroseabra1091 commented Sep 16, 2019

Actually one more question, how do I need to format the private key in an ENV variable in order for Elixir to trim it correctly?

@victorolinasc
Copy link
Collaborator

It must be a valid PEM string. PEM can be split into multi lines or not. THe issue you had first was that your multi-line PEM had spaces at the beginning of the lines because of the single quote.

So, if the PEM is on an ENV variable it just needs to be a valid PEM file.

@victorolinasc
Copy link
Collaborator

@pedroseabra1091 I'll be closing this one. If you need any help feel free to re-open it.

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