简体   繁体   English

如何让Google登录令牌有效期超过1小时?

[英]How to make Google sign-in token valid for longer than 1 hour?

I have implemented google sign-in successfully. 我已成功实施Google登录。

I am able to authenticate user and in response I receive token. 我能够对用户进行身份验证,并在回复时收到令牌。 However the token expires in 1 hour. 但令牌在1小时后到期。

expires_in: "3600"

I tried searching in the docs - https://developers.google.com/identity/sign-in/web/reference - but cannot find a paramenter to extend the lifespan of the token. 我尝试在文档中搜索 - https://developers.google.com/identity/sign-in/web/reference - 但找不到参数来延长令牌的生命周期。

在此输入图像描述


What I'm actually trying to do? 我实际上想做什么?

https://developers.google.com/identity/sign-in/web/backend-auth https://developers.google.com/identity/sign-in/web/backend-auth

after a user successfully signs in, send the user's ID token to your server using HTTPS 用户成功登录后,使用HTTPS将用户的ID令牌发送到您的服务器

I'm sending token with each request to the server: 我正在向服务器发送每个请求的令牌:

endpoint/get?access_token=" + access_token

And then on the server I'm calling https://www.googleapis.com/oauth2/v3/tokeninfo 然后在服务器上我打电话给https://www.googleapis.com/oauth2/v3/tokeninfo

So I have a token, every request is authenticated, but after 1 hour of working the tokeninfo method returns false and I need to re-authenticate the user. 所以我有一个令牌,每个请求都经过身份验证,但在工作1小时后, tokeninfo方法返回false ,我需要重新验证用户身份。

In my code I circumvented that by storing all the historical access_tokens and if client uses old token I check against historical data and manually issue new token using refresh_token (one of my permissions is to grant offline access) 在我的代码中,我通过存储所有历史access_tokens规避,如果客户端使用旧令牌,我会检查历史数据并使用refresh_token手动发出新令牌(我的一个权限是授予离线访问权限)


So yes, I'd be very interested to know: 是的,我很想知道:

  • How to expand lifespan of the access_token ? 如何扩展access_token生命周期?

OR 要么

  • Given the limited lifespan how to ensure requests are authenticated on the backend? 鉴于有限的生命周期如何确保请求在后端进行身份验证?

As @DaImTo noted, you can't extend the life of an access_token. 正如@DaImTo所指出的那样,你无法延长access_token的生命周期。 You can get a new one using a refresh_token, but often if you're trying to do this client side and have a server, you should re-think your approach. 您可以使用refresh_token获取一个新的,但通常如果您尝试执行此客户端并拥有服务器,则应重新考虑您的方法。

It sounds like there are two "authentications" that you're doing here - the client authenticating against the server, and the server authenticating against the Google service. 听起来你在这里有两个“身份验证” - 客户端对服务器进行身份验证,服务器根据Google服务进行身份验证。 Right now, the server should be holding onto the refresh token - so it can always re-authenticate against Google. 现在,服务器应该持有刷新令牌 - 因此它总是可以重新对Google进行身份验证。 It sounds like you're wrestling with how to authenticate your client against the server after the auth_token timeout. 听起来你正在努力解决在auth_token超时后如何针对服务器验证客户端的问题。

In general, the client shouldn't send the access_token to the server, nor the refresh_token. 通常,客户端不应将access_token发送到服务器,也不应发送refresh_token。 What it does is during the first sign-in, the client gets a one-time code (from Google) which it hands to the server. 它的作用是在第一次登录时,客户端获得一次性代码(来自Google),并将其交给服务器。 The server uses this to talk to Google and get the access_token and refresh_token, confirming the user has authenticated themselves, and then sends something (usually a cookie) back to the client saying "ok, I've authenticated you. Here is how you keep authenticating yourself for the rest of our conversation." 服务器使用它来与Google交谈并获取access_token和refresh_token,确认用户已经验证了自己,然后将一些内容(通常是cookie)发送回客户端,说“好的,我已经验证了你。这是你如何保持在剩下的谈话中验证自己。“

That later action is pretty standard and is unrelated to oauth itself. 后来的行动非常标准,与oauth本身无关。 The client and server then communicate as they always do - no oauth stuff is exchanged at all, you're relying on the cookie (or equivalent) to keep up the client-server authentication. 然后客户端和服务器一如既往地进行通信 - 根本不交换任何oauth东西,你依靠cookie(或等价物)来保持客户端 - 服务器身份验证。 The server continues to use the auth token and refresh token to talk to Google. 服务器继续使用身份验证令牌和刷新令牌与Google通信。

https://developers.google.com/identity/sign-in/web/server-side-flow I think is the best guide to this at the moment. https://developers.google.com/identity/sign-in/web/server-side-flow我认为目前是最好的指南。 Or at least it is the best one I can find at the moment. 或者至少它是我目前能找到的最好的一个。 It has a good diagram, at least. 至少它有一个很好的图表。

The key point is that you're exchanging the brilliantly named "code" with the server (what I was calling the "one-time code"). 关键是你要与服务器交换出色的“代码”(我称之为“一次性代码”)。 Once you have done that, the server authenticates you with Google - and it then has the access/refresh tokens and you communicate with the server without having to pass those. 完成后,服务器会通过Google对您进行身份验证 - 然后它具有访问/刷新令牌,您可以与服务器通信而无需传递这些令牌。

Access tokens are short lived and only last for one hour this is not something you can extend. 访问令牌是短暂的,只持续一个小时,这不是你可以扩展的东西。

What you need to do is take the refresh token and get a new access token. 您需要做的是获取刷新令牌并获取新的访问令牌。

example: 例:

You take the refresh_token that you got from your initial request and HTTP Post it to: Note: grant_type=refresh_token 您将从初始请求获得的refresh_token和HTTP发布到:发布: grant_type = refresh_token

https://accounts.google.com/o/oauth2/token https://accounts.google.com/o/oauth2/token
client_id={ClientId}.apps.googleusercontent.com&client_secret={ClientSecret}&refresh_token=1/ffYmfI0sjR54Ft9oupubLzrJhD1hZS5tWQcyAvNECCA&grant_type=refresh_token CLIENT_ID = {客户端Id} .apps.googleusercontent.com&client_secret = {ClientSecret}&refresh_token = 1 / ffYmfI0sjR54Ft9oupubLzrJhD1hZS5tWQcyAvNECCA&grant_type = refresh_token

response 响应

{
"access_token" : "ya29.1.AADtN_XK16As2ZHlScqOxGtntIlevNcasMSPwGiE3pe5ANZfrmJTcsI3ZtAjv4sDrPDRnQ",
"token_type" : "Bearer",
"expires_in" : 3600
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM