使用自定义令牌进行 Firebase 身份验证 [英] Firebase authentication with custom token
问题描述
我有一个 firebase 项目,我试图从我的 rails 服务器进行身份验证,并使用库 ruby-jwt 创建自定义令牌,正如文档中所说,但我一直收到相同的错误:
I have a firebase project which Im trying to authenticate from my rails server creating a custom token with the library ruby-jwt as it says on the docs, but i keep getting the same error:
auth/invalid-custom-token,自定义token格式不正确.请检查文档.
auth/invalid-custom-token, The custom token format is incorrect. Please check the documentation.
credentials.json
来自我在google console中创建的服务账号,uid是从前端发送到api的.
The credentials.json
is from the service account I made in google console, uid is sent from the front end to the api.
def generate_auth_token(uid)
now_seconds = Time.now.to_i
credentials = JSON.parse(File.read("credentials.json"))
private_key = OpenSSL::PKey::RSA.new credentials["private_key"]
payload = {
:iss => credentials["client_email"],
:sub => credentials["client_email"],
:aud => 'https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit',
:iat => now_seconds,
:exp => now_seconds+(60*60), # Maximum expiration time is one hour
:uid => uid.to_s,
:claims => {:premium_account => true}
}
JWT.encode(payload, private_key, 'RS256')
end
在 jwt.io 中是这样的
it looks like this in jwt.io
{
"iss": "defered@defered.iam.gserviceaccount.com",
"sub": "defered@defered.iam.gserviceaccount.com",
"aud": "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit",
"iat": 1486824545,
"exp": 1486828145,
"uid": "4",
"claims": {
"premium_account": true
}
}
推荐答案
我找到了一种更好的身份验证方法,我只是发送 firebase 给你的令牌,并使用我需要的信息在 Rails 上验证它,仅此而已.
I found a better way to authenticate, I'm just sending the token that firebase gives you and verifying it on rails with the information I need and that's it.
这篇关于使用自定义令牌进行 Firebase 身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!