Git Product home page Git Product logo

Comments (12)

astuyve avatar astuyve commented on August 30, 2024 2

I had the same issue with my PEM key set in env variable. I could dump the ENV, copy and paste the var into a node console, and everything would work - but somehow I would get this error in my logs.

I'll keep playing with it and post again if I find the solution.

from app.js.

gr2m avatar gr2m commented on August 30, 2024 1

Thank you, much appreciated

from app.js.

tnolet avatar tnolet commented on August 30, 2024 1

Ok found it. Environment variables don't respect \n encoding so you need to explicitly replace them...sigh...

This works for me.

  1. Store your private key with \n as the new line, as per the documentation.
GITHUB_APP_PRIVATE_KEY=-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEArwgRs2okqD9NC3IrzYmFx82A66eZzkaa+bFBeDwxh0L6MiuI\n/Ngm+9Yq+
... bla bla bla more stuff ....
yMYPNcQu62afq9oBVV1OY0/U5x454eVk6h\nKOz+TQ10QWEkIgkJGl+8aB3iYPzmzSqoCXYy1tu4
ReT\nhRaYHTbStLF4z29OjqkUMsy3KO6REkWqbsEnLkqvRYSRVsKXLM0K\n-----END RSA PRIVATE KEY-----\n
  1. Read the variable and do a global replace
const app = new App({
  id: process.env.GITHUB_APP_ID,
  privateKey: process.env.GITHUB_APP_PRIVATE_KEY.replace(/\\n/g, '\n')
})

Sorry @gr2m had nothing to do with the repo or code.
Also see https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node

from app.js.

thellimist avatar thellimist commented on August 30, 2024 1

In .env I had my variable as

GITHUB_PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY----- .....-----END RSA PRIVATE KEY-----"

changed it to

GITHUB_PRIVATE_KEY=-----BEGIN RSA PRIVATE KEY----- .....-----END RSA PRIVATE KEY-----

by removing the " characters then did .replace(/\\n/g, '\n') and it fixed

from app.js.

wolfy1339 avatar wolfy1339 commented on August 30, 2024

This doesn't seem like an issue with this package, since I have tested the same method as you, and no problem at all with this package or the upstream jsonwebtoken package.

The following is my code that I have tested;

const key = fs.readFileSync('./private-key.pem').toString();

const app = new App({ id: process.env.APP_ID, privateKey: key });
const jwt = app.getSignedJsonWebToken();

It seems as though the docs need updating, you don't need to do anything with the private key string

from app.js.

tnolet avatar tnolet commented on August 30, 2024

Having the same issue. Saving to disk is not really an option I'm afraid. Need to read this from an ENV variable.

from app.js.

wolfy1339 avatar wolfy1339 commented on August 30, 2024

Unfortunately this issue only comes up when using environment variables. The only thing we can do now is discourage their use until a solution can be found

from app.js.

gr2m avatar gr2m commented on August 30, 2024

If someone could provide a reproducible test case that would be great. This package does not read out any environment variables, so the problem seems to be out of scope? Maybe log out the key before passing it to the privateKey and see if it's formatted correctly?

from app.js.

gr2m avatar gr2m commented on August 30, 2024

Thanks @tnolet, that's very helpful. As this might be a common mistake, I wonder if we can provide a more helpful error message? Maybe we can recognize the wrong formatting before trying to generate the JWT and throw a helpful error?

from app.js.

tnolet avatar tnolet commented on August 30, 2024

@gr2m just add a little callout somewhere in the docs. That should be enough. Don't clutter the code with stuff that doesn't belong there.

from app.js.

gr2m avatar gr2m commented on August 30, 2024

If only everyone would read the docs 😭

Maybe the fix is as simple as adding a .replace(/\\n/g, '\n') to the code, with a comment referencing this issue?

from app.js.

gr2m avatar gr2m commented on August 30, 2024

This library is being deprecated in favor of https://github.com/octokit/auth-app.js/.

If anyone can reproduce the problem discussed here, I'd very much appreciate a pull request with a failing test to https://github.com/octokit/auth-app.js/ 🙏

from app.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.