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

JWT ArgumentError (JOSE) #95

Closed
mthomps4 opened this issue Mar 22, 2021 · 4 comments
Closed

JWT ArgumentError (JOSE) #95

mthomps4 opened this issue Mar 22, 2021 · 4 comments

Comments

@mthomps4
Copy link

Unsure what is missing here.
Looking for advice.

Deps

      {:goth, "~> 1.3-rc"},
      {:hackney, "~> 1.17"},
      {:google_api_compute, "~> 0.34.0"}

application.ex

  def start(_type, _args) do
    credentials =
      "GOOGLE_APPLICATION_CREDENTIALS_JSON"
      |> System.fetch_env!()
      |> Jason.decode!()

    source = {:service_account, credentials, []}
...
      {Goth, name: Toolbox.Goth, source: source}

Error Trace

** (Mix) Could not start application toolbox: Toolbox.Application.start(:normal, []) returned an error: shutdown: failed to start child: Goth.Server
    ** (EXIT) an exception was raised:
        ** (ArgumentError) argument error: [[], {:jose_jws, {:jose_jws_alg_rsa_pkcs1_v1_5, :RS256}, :undefined, %{"typ" => "JWT"}}, %{"aud" => "https://oauth2.googleapis.com/token,", "exp" => 1616384563, "iat" => 1616380963, "iss" => "name@appid.iam.gserviceaccount.com,", "scope" => "https://www.googleapis.com/auth/cloud-platform"}]
            (jose 1.11.1) src/jwt/jose_jwt.erl:184: :jose_jwt.sign/3
            (goth 1.3.0-rc.0) lib/goth/token.ex:200: Goth.Token.jwt/2
            (goth 1.3.0-rc.0) lib/goth/token.ex:149: Goth.Token.request/1
            (goth 1.3.0-rc.0) lib/goth/token.ex:109: Goth.Token.fetch/1
            (goth 1.3.0-rc.0) lib/goth/server.ex:49: Goth.Server.init/1
      ```
@wojtekmach
Copy link
Collaborator

Thanks for the report. I could reproduce this error by putting "private_key" => "bad" (or "") instead of a proper private key in the PEM format in my credentials. Could you double-check that your credentials are valid?

@mthomps4
Copy link
Author

Thanks for the quick response.

🤔
I took the JSON file and did a Node stringify
It looks ok at first, but on second glance the special characters may be throwing it off \ + \n

\"private_key\":\"-----BEGIN PRIVATE KEY-----\\\MIIEvg...+43\\\\asdf\n ....

Any info on how you got your creds to a full string for ENV?
Fairly certain that's it.

@mthomps4
Copy link
Author

Update:
Re-stringified.

Noticed that the line breaks had an extra \
find \\n replace with \n

Was able to run {:ok, token} = Goth.fetch(Toolbox.Goth) successfully! 🎉

@wojtekmach
Copy link
Collaborator

Great, glad its fixed. Im gonna go ahead and close this but if folks keep running into this we can probably at least improve the error message.

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