hub*_*rdr 9 firebase google-oauth firebase-security
我们正在使用带有Google身份验证的firebase.我们选择Google是因为我们的应用程序会调用Google API.我们使用从firebase返回的授权有效负载中包含的access_token授权这些api调用.但是,我们无法确定如何在access_token到期后刷新它.根据Google的说法,我们应该假设access_token可能因各种原因而失效.
因此,(据我所知),我们需要一种方法来刷新此令牌而不强制用户重新授权.理想情况下,我可以在请求firebase auth时请求离线access_type但是我没有看到如何做到这一点(再次触发firebase.authWithOAuthPopup(...),我们绝对不想做用户会话显然仍然有效.
是否可以通过Firebase获取离线access_type Google oauth令牌,以便Google返回refresh_token(https://developers.google.com/accounts/docs/OAuth2WebServer#formingtheurl)?使用refresh_token,我想我可以为api调用获取一个新的access_token.
我试过这个,但绝对不支持:
this.firebase.authWithOAuthPopup("google", this.authenticateGoogle.bind(this), {
access_type: 'offline', <-- not passed to Google
scope: 'https://www.googleapis.com/auth/userinfo.profile, https://www.googleapis.com/auth/devstorage.read_write'
});
Run Code Online (Sandbox Code Playgroud)
所有拨打https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=abcd的电话都会将access_type显示为在线.
谢谢
使服务器端实施要求最小化的解决方案。
TL:DR; 使用Google网站登录库来生成身份验证凭据。使用身份验证凭据登录Firebase,然后将脱机访问交换代码发布到您的服务器。
<script src="https://apis.google.com/js/platform.js?onload=loadAuth2" async defer></script>
<script>
function loadAuth2 () {
gapi.load('auth2', function() {
gapi.auth2.init({
client_id: 'your firebase Web client ID',
cookie_policy: 'single_host_origin',
scope: 'profile ...'
});
});
}
</script>
Run Code Online (Sandbox Code Playgroud)
注意:作用域应该是所需访问范围的以空格分隔的列表。
假设Firebase已加载,我的登录单击处理程序为:
<script>
function login() {
const auth = gapi.auth2.getAuthInstance();
auth.then(() => {
auth.grantOfflineAccess({
'redirect_uri': 'postmessage',
'prompt': 'concent',
'approval_prompt': 'force',
}).then(offlineAccessExchangeCode => {
// send offline access exchange code to server ...
const authResp = auth.currentUser.get().getAuthResponse();
const credential = firebase.auth.GoogleAuthProvider.credential(authResp.id_token);
return firebase.auth().signInWithCredential(credential);
}).then(user => {
// do the thing kid!
});
});
}
</script>
Run Code Online (Sandbox Code Playgroud)
调用auth.grantOfflineAccess
with 'redirect_uri': 'postmessage'
会使Google auth2
库通过将该身份验证凭据传递回您的Web应用window.postMessage
。请参阅此处以获取auth2
库参考。
在我的应用程序的其他地方,我正在侦听Firebase身份验证状态的更改。
firebase.auth().onAuthStateChanged(user => {
if (user) {
// navigate to logged in state
} else {
// navigate to login page
}
});
Run Code Online (Sandbox Code Playgroud)
我POST
将服务器offlineAccessExchangeCode
(看起来像{"code": "..."}
)连接到我的服务器,以交换当前已认证用户的凭证,其中包括刷新令牌。虽然您可以访问firebase.auth().currentUser.refreshToken
此令牌的客户端对我而言不起作用(也许有人可以告诉我,我在这里错了:D)
我的Python服务器端代码如下。请注意,Google SDK是为大多数Google服务自动生成的,因此以下代码应可以轻松转换为它们支持的任何语言。
from oauth2client import client
// ...
// assuming flask
@app.route("/google/auth/exchange", methods=['POST'])
def google_auth_exchange():
auth_code = request.get_json()['code']
credentials = client.credentials_from_clientsecrets_and_code(
'config/client_secret.json', ['profile', '...'], auth_code)
print(credentials.refresh_token)
Run Code Online (Sandbox Code Playgroud)
就是这样。如果您需要脱机访问,我会假设您具有服务器或某些服务器端代码,因此希望实现路由距离理想的解决方案不太远。
注意: GCLID解析器是我目前正在从事的项目,需要这样做。
归档时间: |
|
查看次数: |
2544 次 |
最近记录: |