簡體   English   中英

Firebase 3 Auth + Node + jsonwebtoken:自定義令牌無效

[英]Firebase 3 Auth + Node + jsonwebtoken: Invalid custom token

我有自己的運行在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..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的具體錯誤是什么?

謝謝!

好吧,我做到了! 事實證明,在我的聲明中,username和user_id是對象! 所以,通過這樣做:

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

有用!

希望有人覺得這很有幫助。

在類似的情況下,在服務器端創建的令牌已創建,有效期為2小時。 創建令牌時沒有錯誤,只有在客戶端嘗試使用它時才會出錯。 將到期時間更改為一小時修復了“無效自定義令牌”問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM