Why do I get "Invalid token" unless I invert projectId and clientId? - node.js

In order to setup Google Identity Toolkit for my Website I started with the Quick-start App for Node.js.
It worked fine on localhost.
But when I moved it to an actual app-engine instance it did not work anymore.
I got this error returned by gitkitClient.verifyGitkitToken():
Invalid token: Unable to verify the ID Token: Wrong recipient, payload
audience != requiredAudience
I found a very helpful post on stackoverflow about more or less the same issue, for Java instead of node.js: It looks like there is a mismatch between projectId and clientId.
I changed my gitkit-server-config.json file to swap the projectId and clientId values and it worked!
This sound very much like a major bug on google side, doesn't it?
Why does it work on localhost?
Will this be changed/fixed in the future?
Maybe the problem is in the tutorial?
I have a working solution for now, but I do not feel safe to keep it like that...
I hope a googler will read this!
[EDIT]
Following wyhao31's comment I had a closer look at the gitkitclient.js source code and both projectId and clientId are added to the same audiences array.
After more test I found out that you must only put the project ID ('my-project-name') in the gitkit-server-config.json file.
The nasty thing is that if you add it with a 'clientId' property name it is also working...

Based on your description, and if you read gitkit-server-config.json file like this
var gitkitClient = new GitkitClient(JSON.parse(fs.readFileSync('./gitkit-server-config.json')));
I think the problem might caused by using old nodejs client lib. Could you try to update your client lib to latest version(0.0.6)? Or you can use the code directly.

Related

#google-cloud/pubsub TypeError: Cannot read property 'status' of undefined

Installed the google cloud pub sub following instructions from the following link,
https://cloud.google.com/pubsub/docs/reference/libraries
Imported that as the following,
const { PubSub } = require("#google-cloud/pubsub");
const projectId = "projectXYZ";
const pubSubClient = new PubSub({ projectId });
export async function publishMessage(topicName, data) {
return await pubSubClient.topic(topicName).publish(JSON.stringify(data));
}
this leads to the following error,
Now, what I have observed is that the code below the import has no significance in this issue, since this error still appears even if I only import on the first line.
Am I missing something or required to install something more that just the package?
Any help is deeply appreciated.
Thank you in advance.
Library maintainer here! I'm guessing from the stack trace that you're using webpack on your app? Right now we don't have webpack and rollup support - there are a few weird things like what you see above. Your code looks okay. We'd like to get webpack and similar tools working at some point for server-side usage, but it hasn't gone very high on the priority list just yet. I think the previous assumption was that people were trying to use it in a web browser, which we don't recommend for a number of reasons (most prominently, security concerns with GCP credentials). But I've been seeing a lot of issues with users trying to use webpack on the server side to make their Cloud Functions more compact, which seems pretty legit. I'll bring it up in our next team sync.
If you're not using webpack, then that sounds like something that might be filed over at the issue tracker.

Docusign: Sample Code Cannot Connect to '/app-shell'?

I'm using the sample code provided by DocuSign in the Github project "qs-01-node-embed-signing-ceremony-master".
I've obtained and added my accessToken and accountId.
When the sample code is run via npm start, it displays an error message in the browser window:
Cannot GET /app-shell
Chrome dev tools shows this:
The sample code appears to contain no file named app-shell.
How can I correct this?
I looked at this sample code and tried it from scratch. I cannot find a file named workbox-core.dev.js in there and when I tried it - it worked just fine.
Please check if this is not something in your environment that may be causing this.
Try a new fresh clone from scratch just in case.
It looks like there are unaddressed caching anomalies with the demo code. Selecting "Empty Cache and Hard Reload" in Chrome fixed it.
After successfully signing the test doc, I still got the "Cannot GET /app-shell" message, but that glitch seems to be specific to the demo code.
Hopefully this is enough of a test for me to start implementing Docusign in my app.
Note: It appears that this demo was not built with create-react-app or other tech that auto-rebuilds and reloads the page when the source is edited.

Firebase-admin fails silently to write to realtime database from nodejs

I’m trying to get setup using Firebase Admin on my nodejs app running on my Raspberry Pi.
when I call;
admin.database().ref().child("myPath").push({date: new Date()});
I’m not receiving any errors but no data is showing up when looking in the Firebase console at my database either.
I also tried chaining on;
set({date: new Date()});
and
push().set({date: new Date()});
Both also fail to write with no errors.
I've verified that my rtdb url and the project id in the service account file are correct for my database. And I've tried both requireing the file as in the docs and passing the credential object directly to;
admin.credential.cert()
I only mention that I'm running on Raspberry Pi because the issue I’m seeing sounds very similar to this Stack Overflow post where Firebase Admin won’t write to db. I’m wondering if it’s not coincidental.
Any ideas what I might be missing?
please can you try this code .....push().set({'date': new Date()});. Thanks. I am guessing it's takes a String as key and even the new Date().toStirng() would be good if i am not wrong.
Thanks,

Implementing authentication with passport / node.js and getting error

I had already implemented authentication for the app I'm working on but am trying to refactor it based on a recent tutorial I did which I thought was very clear and also involved adding facebook / google / twitter auth which I would like to do.
So far, I've updated the user model and defined my local strategy for signup but when I go to try and run the app now, I'm getting an error. I have the app uploaded to github and wondered if somebody would be able to check it out and see where I'm going wrong. The error is pointing to a part of the index file until node_modules / express but I cannot work out what's up and I'd like to know before I proceed further.
Latest commit is under: https://github.com/DaveBage83/friendly-invention
Thanks!
A few things for you notice.
1 - Do not commit the node_modules folder. Once you have all your dependencies in the package.json file, the npm install will download all of for you again.
2 - This code is full of erros. I won't put everything here, I believe you find them by yourself. Otherwise, put the specific code here, so we can help you out.
About the question:
In you app.js file.
...
authRoutes = require('./routes/index')(app, passport)
...
If you look at the ./routes/index, you'll see that it is exporting a route object, witch does not expect the two parameters. (e.g. (app, passport)).
Hope it can still help you.

SendGrid API Key is not working. "The provided authorization grant is invalid, expired or revoked"

SendGrid seems to be preventing my node js server from sending emails.
I get this error message in the response on sending off an email:
The provided authorization grant is invalid, expired or revoked
I have an API key setup as well and have followed the documentation.
You need to use the API KEY GENERATED
DO NOT USE the API KEY ID
Sendgrid only displays the generated key once when you create it.
If you didn't record it somewhere when you created the key you'll need to create a new key and then you'll probably want to delete the old key since it would be useless if you don't know what it is.
FYI: The API key in the screenshot above is already deleted. I deleted it right away so please don't worry about me leaking that key.
This is a late answer and JAVA oriented.. But I simply copied the docs and didn't notice..
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
I just put my key in there and didn't see the getEnv. Silly of course.. but when trying to get things to work quickly...
For me I just had to generate a new API key. For some weird reason the former API key has become invalid, so I also added an alert for this case
In Sendgrid v3, I had the similar issue when using env variable in Node JS. If I use env variable, I get the above issue. But if I drop the string into the require process, it works.
Doesn't Work:
SENDGRID_API_KEY=SG.XXXXXXXXXXXXXXXXXXX
var sg = require('sendgrid')(process.env.SENDGRID_API_KEY);
Works
var sg = require('sendgrid')('SG.XXXXXXXXXXXXXXXXXXX');
Replace SG.XXXXXXXXXXXXXXXXXXX with API Key Generated (which you can only see once during key generation).
EDIT
Note : Make sure you don't save this to public repository. If you do anyone can use your API Key and also your account will be suspended by Sendgrid team temporarily until you remove it from repository.
In my case I was trying to debug the connection by using telnet and kept getting rejected.
Turns out that these two are not equivalent, echo will include \n in the encoded string.
echo 'apikey' | base64
printf 'apikey' | base64
So yeah, make sure you don't include the newline in the API key.
If you are using node js,
make sure you have the require('dotenv').config()line inside the file that needs the sendgrid/nodemailer module.
Without it, the sendgrid transporter will have an undefined value instead of the api_key.
Here's my solution:
Install the dotenv package: npm i dotenv
Go to the earliest entry point of your application(i.e index.js) and put this at the top:
const dotenv = require('dotenv').config();
Create a .env file and add SENDGRID_API_KEY=<YOUR_API_KEY>. NO quotes '' or "".
In your file which you use sendgrid, add this to the top:
const sgMail = require('#sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
Done.
It might be late for an answer but for people who are getting the same problem in spring boot
it might be caused at initialization when you are using env or property value
i was initializing the variable in the constructor before the value was loaded by spring and it was giving the same output. so either initialize it in the method you are calling the SendGrid function or do it after values are loaded
Instead of using,
api_key: ${process.env.SENDGRIDAPIKEY}
try,
api_key: "" + ${process.env.SENDGRIDAPIKEY} + ""
worked for me,
For me; I've just created a new API key and then it works again.
I'm %100 sure it was working with no any code change.
Maybe it is an issue with SendGrid or some sort of security action from SendGrid.
I had the same issue, it disappeared after I verified my email address and enabled 2FA.

Resources