解除帐号关联

解除关联操作可能会从您的平台或 Google 发起,并且两者的一致链接状态的显示可提供最佳用户体验。对于 Google 帐号关联,支持令牌撤消端点或跨帐号保护是可选的。

帐号可以通过以下任一方式解除关联:

  • 用户请求
  • 未能为过期的刷新令牌续期
  • 您或 Google 发起的其他事件。例如,滥用和威胁检测服务导致帐号被暂停。

用户请求与 Google 解除关联

通过用户的 Google 帐号或应用发起的帐号解除关联会删除之前授予的所有访问令牌和刷新令牌,从而移除用户同意声明,并视需要调用令牌撤消端点(如果您选择实现的话)。

用户请求与您的平台解除关联

您应该为用户提供一种解除关联的机制,例如指向其帐号的网址。如果您不为用户提供解除关联的方法,请添加一个 Google 帐号链接,以便用户可以管理其关联帐号。

您可以选择实施风险共享和协作 (RISC),并通知用户帐号帐号关联状态的变化。这样,您的平台和 Google 都可以显示最新且一致的关联状态,而无需依赖刷新或访问令牌请求来更新关联状态,从而改善用户体验。

令牌过期

为了提供流畅的用户体验并避免服务中断,Google 会在其生命周期即将结束时尝试更新刷新令牌。在某些情况下,当有效的刷新令牌不可用时,可能需要用户同意才能重新关联帐号。

将平台设计为支持多个未过期的访问令牌和刷新令牌可以最大限度地减少集群环境之间的客户端-服务器交换中存在的竞态条件,避免用户中断,并最大限度地减少复杂的时间和错误处理场景。虽然最终一致,但在客户端-服务器令牌续订交换期间以及集群同步之前,旧令牌和新颁发的未过期令牌都可能会在短时间内使用。例如,在您发出新的访问令牌之后,但在接收和集群同步之前,Google 会向您的服务发出使用先前未过期的访问令牌的 Google 请求。建议使用刷新令牌轮替的替代安全措施。

其他事件

帐号可能会因各种其他原因解除关联,例如帐号处于闲置状态、暂停、恶意行为等。在这种情况下,您的平台和 Google 可以更好地管理用户帐号,并通过通知帐号和关联状态的变化来重新关联。

实现令牌撤消端点,以便 Google 进行调用,并使用 RISC 将您的令牌撤消事件通知 Google,以确保您的平台和 Google 保持一致的用户帐号关联状态。

令牌撤消端点

If you support an OAuth 2.0 token revocation endpoint, your platform can receive notifications from Google. This lets you inform users of link state changes, invalidate a token, and cleanup security credentials and authorization grants.

The request has the following form:

POST /revoke HTTP/1.1
Host: oauth2.example.com
Content-Type: application/x-www-form-urlencoded

client_id=GOOGLE_CLIENT_ID&client_secret=GOOGLE_CLIENT_SECRET&token=TOKEN&token_type_hint=refresh_token

Your token revocation endpoint must be able to handle the following parameters:

Revocation endpoint parameters
client_id A string that identifies the request origin as Google. This string must be registered within your system as Google's unique identifier.
client_secret A secret string that you registered with Google for your service.
token The token to be revoked.
token_type_hint (Optional) The type of token being revoked, either an access_token or refresh_token. If unspecified, defaults to access_token.

Return a response when the token is deleted or invalid. See the following for an example:

HTTP/1.1 200 Success
Content-Type: application/json;charset=UTF-8

If the token can't be deleted for any reason, return a 503 response code, as shown in the following example:

HTTP/1.1 503 Service Unavailable
Content-Type: application/json;charset=UTF-8
Retry-After: HTTP-date / delay-seconds

Google retries the request later or as requested by Retry-After.

跨帐号保护 (RISC)

如果您支持跨帐号保护,则您的平台可以在访问令牌或刷新令牌被撤消时通知 Google。这样一来,Google 就可以告知用户链接状态的变化、使令牌失效、清理安全凭据以及进行授权授权。

跨帐号保护基于 OpenID Foundation 开发的 RISC 标准

系统会使用安全事件令牌来通知 Google 令牌撤消情况。

解码后,令牌撤消事件如以下示例所示:

{
  "iss":"http://risc.example.com",
  "iat":1521068887,
  "aud":"google_account_linking",
  "jti":"101942095",
  "toe": "1508184602",
  "events": {
    "https://schemas.openid.net/secevent/oauth/event-type/token-revoked":{
      "subject_type": "oauth_token",
      "token_type": "refresh_token",
      "token_identifier_alg": "hash_SHA512_double",
      "token": "double SHA-512 hash value of token"
    }
  }
}

用于向 Google 通知令牌撤消事件的安全事件令牌必须符合下表中的要求:

令牌撤消事件
iss 颁发者声明:这是您托管的网址,在注册期间与 Google 共享。
aud Audience Claim:将 Google 标识为 JWT 接收者。它必须设置为 google_account_linking
jti JWT ID 声明:这是您为每个安全性事件令牌生成的唯一 ID。
iat Issued At Claim(在声明期间颁发):这是一个 NumericDate 值,表示此安全性事件令牌的创建时间。
toe Time of Event Claim:这是一个可选的 NumericDate 值,表示令牌撤消的时间。
exp 到期时间声明:请勿添加此字段,因为导致此通知的事件已经发生。
events
安全性事件声明:这是一个 JSON 对象,必须仅包含一个令牌撤消事件。
subject_type 此字段必须设置为 oauth_token
token_type 这是要撤消的令牌类型,可以是 access_tokenrefresh_token
token_identifier_alg 这是用于对令牌进行编码的算法,必须为 hash_SHA512_double
token 这是已撤消令牌的 ID。

如需详细了解字段类型和格式,请参阅 JSON Web 令牌 (JWT)