如何针对 firebase 验证身份验证令牌?
How to validate an authentication token against firebase?
我不是指使用 firebase 的自定义身份验证。我需要的与在应用程序服务器中生成令牌并允许在 firebase 中访问的自定义身份验证略有不同。
实际上,例如,我正在尝试使用电子邮件和密码在 firebase 中进行身份验证,并且通过该身份验证能够访问某些应用程序服务器中的 restful 服务。
这可能吗 ?
我认为以某种方式可以在 firebase 身份验证后将令牌发送到应用程序服务器,并且该服务器将针对 firebase 验证 auth 令牌。
Client --------authenticates ------->> Firebase
Client <<--------auth token ---------- Firebase
Client --------- sends ------------->> Application server (NodeJS)
App Server ------- validates (auth token) ---->> Firebase
提前致谢。
您可以使用 iOS、Web 和 Android
中可用的异步 getToken 方法获取令牌
网址:
https://firebase.google.com/docs/reference/js/firebase.User#getToken
iOS:
https://firebase.google.com/docs/reference/ios/firebaseauth/interface_f_i_r_user#properties
并将该令牌发送到您的后端服务器,然后您可以使用服务器中的 verifyIdToken 方法来验证令牌并获取令牌的 uid
服务器方法
https://firebase.google.com/docs/auth/server#verify_id_tokens
客户端 -------- 验证 -------->> Firebase
import firebase from 'firebase/app';
import 'firebase/auth';
import 'firebase/firestore';
const googleAuthProvider = new firebase.auth.GoogleAuthProvider();
const auth = firebase.auth();
const authenticates = await auth.signInWithPopup(googleAuthProvider).then(user => user).catch(err => err)
客户端 <<--------authtoken ------------ Firebase
您将从 authenticates
响应中获取数据
authtoken = authenticates.credential.idToken
email = authenticates.user.email
...
客户端--------发送------------>>应用服务器(NodeJS)
const sends = await axios({
method: 'post',
url: `${API_BASE_URL}/request`,
headers: {
'Authorization': `Bearer ${authtoken}`,
},
data: {
from: next_cursor,
size: next_cursor + 100,
}
});
应用服务器 -------- 验证(授权令牌)---->> Firebase
我们将在初始化 firebase 身份验证时app_oauth2_client_id
import { OAuth2Client } from 'google-auth-library';
const oauth2Client = new OAuth2Client(process.env.app_oauth2_client_id);
function verifyOauth2Token(token) {
const ticket = await oauth2Client.verifyIdToken({
idToken: token,
audience: [process.env.app_oauth2_client_id]
});
return ticket.getPayload();
}
const tokenInfo = await verifyOauth2Token(token);
tokenInfor
{
iss: 'accounts.google.com',
azp: '671303332471-5n8014rorllmd09n8mmadesc2qidpda5.apps.googleusercontent.com',
aud: '671303332471-5n8014rorllmd09n8mmadesc2qidpda5.apps.googleusercontent.com',
sub: '100037911230177975416',
email: 'testapp@gmail.com',
email_verified: true,
at_hash: '3rxsMOftrr9NZWlBkYznuQ',
iat: 1635842823,
exp: 1635846423
}
我不是指使用 firebase 的自定义身份验证。我需要的与在应用程序服务器中生成令牌并允许在 firebase 中访问的自定义身份验证略有不同。 实际上,例如,我正在尝试使用电子邮件和密码在 firebase 中进行身份验证,并且通过该身份验证能够访问某些应用程序服务器中的 restful 服务。 这可能吗 ? 我认为以某种方式可以在 firebase 身份验证后将令牌发送到应用程序服务器,并且该服务器将针对 firebase 验证 auth 令牌。
Client --------authenticates ------->> Firebase
Client <<--------auth token ---------- Firebase
Client --------- sends ------------->> Application server (NodeJS)
App Server ------- validates (auth token) ---->> Firebase
提前致谢。
您可以使用 iOS、Web 和 Android
中可用的异步 getToken 方法获取令牌网址:
https://firebase.google.com/docs/reference/js/firebase.User#getToken
iOS: https://firebase.google.com/docs/reference/ios/firebaseauth/interface_f_i_r_user#properties
并将该令牌发送到您的后端服务器,然后您可以使用服务器中的 verifyIdToken 方法来验证令牌并获取令牌的 uid
服务器方法 https://firebase.google.com/docs/auth/server#verify_id_tokens
客户端 -------- 验证 -------->> Firebase
import firebase from 'firebase/app';
import 'firebase/auth';
import 'firebase/firestore';
const googleAuthProvider = new firebase.auth.GoogleAuthProvider();
const auth = firebase.auth();
const authenticates = await auth.signInWithPopup(googleAuthProvider).then(user => user).catch(err => err)
客户端 <<--------authtoken ------------ Firebase
您将从 authenticates
响应中获取数据
authtoken = authenticates.credential.idToken
email = authenticates.user.email
...
客户端--------发送------------>>应用服务器(NodeJS)
const sends = await axios({
method: 'post',
url: `${API_BASE_URL}/request`,
headers: {
'Authorization': `Bearer ${authtoken}`,
},
data: {
from: next_cursor,
size: next_cursor + 100,
}
});
应用服务器 -------- 验证(授权令牌)---->> Firebase
我们将在初始化 firebase 身份验证时app_oauth2_client_id
import { OAuth2Client } from 'google-auth-library';
const oauth2Client = new OAuth2Client(process.env.app_oauth2_client_id);
function verifyOauth2Token(token) {
const ticket = await oauth2Client.verifyIdToken({
idToken: token,
audience: [process.env.app_oauth2_client_id]
});
return ticket.getPayload();
}
const tokenInfo = await verifyOauth2Token(token);
tokenInfor
{
iss: 'accounts.google.com',
azp: '671303332471-5n8014rorllmd09n8mmadesc2qidpda5.apps.googleusercontent.com',
aud: '671303332471-5n8014rorllmd09n8mmadesc2qidpda5.apps.googleusercontent.com',
sub: '100037911230177975416',
email: 'testapp@gmail.com',
email_verified: true,
at_hash: '3rxsMOftrr9NZWlBkYznuQ',
iat: 1635842823,
exp: 1635846423
}