Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase 3.0 Tokens : [Error: Firebase Auth ID token has no "kid" claim]

I'm currently developing a node.js service with firebase 3.0 that is called by a web application that uses firebase 2.4.

I'm sending the current user Firebase ID token (Auth.$getAuth().token) in my header call and trying to validade this token with

var idToken = req.headers["x-access-token"];
auth.verifyIdToken(idToken).then(function(decodedToken) {
    var uid = decodedToken.sub;
    console.log(decodedToken);
}, function(error){
    console.log(error);
});

But I'm getting:

[Error: Firebase Auth ID token has no "kid" claim]

getAuth():

enter image description here


UPDATE

I'have just tested generating and validating the token on the server side and I'm getting the same problem.

var auth = firebase.auth();
var token = auth.createCustomToken(userId, {"premium_account": true});
console.log(token);
auth.verifyIdToken(token).then(function(decodedToken) {
     console.log(decodedToken);
}, function(error){
     console.log(error);
});

Any suggestions?


UPDATE 2: [SOLUTION]

The problem in my case was that the Tokens generated with AngularFire 2.X.X are not compatible with the Firebase 3.X.X that is running in my server. So after digging into some thoughts that people wrote here and in this google group topic the workaround was to use jsonwebtoken as follows:

var jwt = require('jsonwebtoken');
jwt.verify(idToken, fbKey, function(err, decoded) {
   if (!err){ console.log(decoded); }
});

You can find the fbKey accessing the new firebase console and going into Settings -> Project Settings -> Database.

like image 693
adolfosrs Avatar asked May 21 '16 01:05

adolfosrs


People also ask

How do I verify my Firebase custom token?

To do so securely, after a successful sign-in, send the user's ID token to your server using HTTPS. Then, on the server, verify the integrity and authenticity of the ID token and retrieve the uid from it. You can use the uid transmitted in this way to securely identify the currently signed-in user on your server.

How do you refresh a Firebase token?

You can refresh a Firebase ID token by issuing an HTTP POST request to the securetoken.googleapis.com endpoint. The refresh token's grant type, always "refresh_token". A Firebase Auth refresh token. The number of seconds in which the ID token expires.

What happens when Firebase token expires?

So if the underlying session expires, or the user logs out, or the Firebase auth. token that is automatically generated by Simple Login expires, this callback will be invoked with user=null, error=null to indicate that the user is logged out, and you should attempt to reauthenticate. I hope that makes sense and helps!


2 Answers

The documentation states that a Firebase ID Token is not the same as a Custom Token, and that verifyIdToken() is not intended for verifying tokens generated with generateCustomToken().

Old style custom tokens still seem to work (signed with a database secret instead of a service account private key). You can generate and verify these yourself using firebase-token-generator.js and/or jsonwebtoken.js.

Copied from Firebase Project > Settings > Database > Secrets

Create custom database authentication tokens using a legacy Firebase token generator. At least one secret must exist at all times.

like image 184
user1881056 Avatar answered Sep 27 '22 15:09

user1881056


Seems like there is no way to use firebase's createCustomToken and verifyIdToken in a couple.

Method createCustomToken uses method sign from jsonwebtoken module which does not put "kid" claim in header section of jwt by default. And createCustomToken does not put it itself.

I suppose at this time you can use jsonwebtoken module directly to generate token with own key id.

[email protected]

[email protected]

like image 29
Lenar Masitow Avatar answered Sep 27 '22 16:09

Lenar Masitow