Firebase 3 Auth + Node + jsonwebtoken:自定义令牌无效

时间:2016-05-30 04:56:17

标签: javascript node.js jwt firebase-authentication

我在Node.js v4.4.5上运行了自己的OAuth 2服务器,包含所有这些内容:

"bcrypt-nodejs": "0.0.3",
"body-parser": "^1.15.1",
"cors": "^2.7.1",
"express": "~4.1.1",
"firebase": "^3.0.3",
"mongoose": "^4.4.19",
"oauth2orize": "^1.3.0",
"passport": "^0.3.2",
"passport-jwt": "^2.0.0",
"passport-local": "^1.0.0"
"jsonwebtoken": "^7.0.0"

我可以登录并获得一个JWT,根据jwt.io,可以解码,但我无法验证,因为我没有公钥。

我得到JWT并将其发送到signInWithCustomToken,然后我从https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken?key=AIzaSy收到错误....

{
        "error": {
        "errors": [
        {
            "domain": "global",
            "reason": "invalid",
            "message": "INVALID_CUSTOM_TOKEN"
        }
        ],
        "code": 400,
        "message": "INVALID_CUSTOM_TOKEN"
    }
}

在我的 auth.js 控制器中,我创建了JWT,如下所示:

var token = jwt.sign(
    {
        iss: jwt_config.client_email,
        sub: jwt_config.client_email,
        aud: 'https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit',
        iat: iat,
        exp: exp,
        uid: user._id,
        claims: {
            username: user.username, 
            user_id: user._id, 
            roles: user.role
        }
    },
    jwt_config.private_key, 
    {
        algorithm: 'RS256'
    }
);
res.json({success: true, token: token});

我将服务帐户数据加载到jwt_config:

var jwt_config = require('../config/Firebase-68824d8xxxxx.json');

这是违规的JWT:

  

eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJwcm9qZWN0QHByb2plY3QtODE5NTMxNDA4ODMzNzM0MzUwMy5pYW0uZ3NlcnZpY2VhY2NvdW50LmNvbSIsInN1YiI6InByb2plY3RAcHJvamVjdC04MTk1MzE0MDg4MzM3MzQzNTAzLmlhbS5nc2VydmljZWFjY291bnQuY29tIiwiYXVkIjoiaHR0cHM6Ly9pZGVudGl0eXRvb2xraXQuZ29vZ2xlYXBpcy5jb20vZ29vZ2xlLmlkZW50aXR5LmlkZW50aXR5dG9vbGtpdC52MS5JZGVudGl0eVRvb2xraXQiLCJpYXQiOjE0NjQ1ODIzMDEsImV4cCI6MTQ2NDY5MDMwMSwidWlkIjoiNTc0YTJmNWJmODRhZTMwOTdkMjNiZWQ1IiwiY2xhaW1zIjp7InVzZXJuYW1lIjoiamxwZWxsaWNlciIsInVzZXJfaWQiOiI1NzRhMmY1YmY4NGFlMzA5N2QyM2JlZDUiLCJyb2xlcyI6IlN0dWRlbnQifX0.gDghMzXc2sFP4XbMhrU2kxo7u0yte4PT03AbeyzT8oGLYJEHJOAY-kZT0l_fGWDi68AfVCaEiHeFj1a3-M95i4NRTQErWyGPuqqS9ii2m0mDaBseFZumk5iTTWiqY8Tpo6_7fkWGsuM7fnuXjaMKV8jkOWS913EE3DQmXHT5bwPe-KY-xTFxU0P3VPWzbgw5T5lExmzpv0x78Fr-RCy45QhfJ3IeVa-Pyhnp4_NY9VAi1naJLpwKKo7aVq3uLujMK8ViNSgfdXTqI9VNq7KrdgqSKnpdoZ2ph_J6fHBnHtAhV6F_Iy_FyC7Zg1EyC_4vMpBJuMx5UYzy6f1Gm0wvHw

当我在mi登录页面收到它时,我将其发送到Firebase:

firebase.initializeApp(config);
$.ajax({
    type: 'post',
    dataType: 'json',
    url: 'http://ec2-52...compute.amazonaws.com:3000/api/authenticate',
    data: $('#loginForm').serialize(),
    complete: function(result) {
        result = result.responseJSON;
        // console.log(JSON.stringify(result, null, 4));
        var success = result.success;
        if (success) {
            var data = result.token;

            firebase.auth().signInWithCustomToken(data).catch(function(error) {
                // Handle Errors here.
                var errorCode = error.code;
                var data = error.message;
                // ...
            });

        } else {
            var data = result.message;
        }
        $('#token').html(data);
    }
});

这是错误弹出的地方。

我很难过,我该怎么办?

我应该研究一个不同的模块来创建JWT吗?

有没有办法知道JWT的具体错误?

谢谢!

2 个答案:

答案 0 :(得分:2)

好吧,我做到了! 事实证明,在我的声明中,username和user_id是对象! 所以,通过这样做:

claims: {
    username: String(user.username), 
    user_id: Strint(user._id), 
    roles: user.role
}

它有效!

希望有人觉得这很有帮助。

答案 1 :(得分:0)

在类似的情况下,在服务器端创建的令牌已创建,有效期为2小时。创建令牌时没有错误,只有在客户端尝试使用它时才会出错。将到期时间更改为一小时会修复"无效的自定义令牌"问题