Vonage / vonage-node-sdk

Vonage API client for Node.js. API support for SMS, Voice, Text-to-Speech, Numbers, Verify (2FA) and more.
Apache License 2.0
375 stars 178 forks source link

[Bug]: Error: secretOrPrivateKey must be an asymmetric key when using RS256 #900

Closed yuridamata closed 4 months ago

yuridamata commented 5 months ago

Node Version

20.x

Platform

Linux

SDK Version

3.12

Code Sample

const { Vonage } = require('@vonage/server-sdk')

const vonage = new Vonage({
  apiKey: MY_KEY,
  apiSecret: MY_SECRET,
  applicationId: MY_APP_ID,
  privateKey: "./private.key"
})

const ANSWER_URL = 'https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json'

vonage.voice.createOutboundCall({
  to: [{
    type: 'phone',
    number: "5561981299885"
  }],
  from: {
    type: 'phone',
    number: "11910862363"
  },
  answer_url: [ANSWER_URL]
})
  .then(resp => console.log(resp))
  .catch(err => console.error(err));

Expected Behavior

Make a call to the number.

Actual Behavior

Getting this error on the console:

Error: secretOrPrivateKey must be an asymmetric key when using RS256

manchuck commented 5 months ago

@yuridamata Before initializing the Vonage class, can your read in the file using fs.readFileSync('./private.key') and using that result instead?

const fs = require('fs');

const keyBuffer = fs.readFileSync('./private.key');

const vonage = new Vonage({
  apiKey: MY_KEY,
  apiSecret: MY_SECRET,
  applicationId: MY_APP_ID,
  privateKey: keyBuffer,
})
AI-General commented 4 months ago

Your answer is right @manchuck

manchuck commented 4 months ago

@AI-General There have been other reports of devs having this issue as well. I can't seem to reproduce it on my end. I was thinking that it could be a permission issue (which is why I was asking if you could do the readFileSync as that would throw an exception), but that is not the issue here. I will try to do some more debugging to see where the failure is.

AI-General commented 4 months ago

@manchuck I think it is clear bug. It is not relate to permission

danielnitu commented 4 months ago

We recently started getting this error as well, but only in the production environment. We use an env variable to store the key as a one line string and were able to fix it replacing all occurrences of \n with the newline \n before using the key.

const PRIVATE_KEY = process.env.PRIVATE_KEY.replace(/\\n/g, '\n');

const vonage = new Vonage(
  new Auth({
    applicationId: APP_ID,
    privateKey: PRIVATE_KEY,
  }),
);
manchuck commented 4 months ago

@AI-General Yes it is a clear bug, but I am unable to reproduce the issue when I pass in just the path to the key. Sorry If that didn't come across in my last comment.

@danielnitu I will check to see if line endings are causing the issue

danisOktaxi commented 4 months ago

Facing same issue after upgrade to 3.12.2

Error: secretOrPrivateKey must be an asymmetric key when using RS256 at module.exports (\node_modules\jsonwebtoken\sign.js:130:22) at tokenGenerate (\node_modules\@vonage\jwt\dist\tokenGenerate.js:97:12) at Auth.createBearerHeader (\node_modules\@vonage\auth\dist\auth.js:140:54) at Voice.addAuthenticationToRequest (\node_modules\@vonage\server-client\dist\client.js:82:52) at Voice.prepareRequest (\node_modules\@vonage\server-client\dist\client.js:256:30) at Voice.sendRequest (\node_modules\@vonage\server-client\dist\client.js:212:34) at Voice.sendRequestWithData (\node_modules\@vonage\server-client\dist\client.js:196:27) at Voice.sendPostRequest (\node_modules\@vonage\server-client\dist\client.js:169:21) at Voice.createOutboundCall (\node_modules\@vonage\voice\dist\voice.js:206:33)

manchuck commented 4 months ago

We have found the issue! It was a small logic error when deciding how to read the key. The fix has been published with v3.13.1 of the server-sdk

https://www.npmjs.com/package/@vonage/server-sdk/v/3.13.1