LoginSignup
9
6

More than 1 year has passed since last update.

Google APIのOAuthトークンの失効について

Last updated at Posted at 2022-10-13

Google APIのOAuthトークンの有効期限/失効について、挙動・仕様で少し気になる点があったため公式ドキュメントを漁ることにした。いくつか注意しておきたいポイントがあったので、メモとして残しておく。

公式ドキュメント: https://developers.google.com/identity/protocols/oauth2

Refresh Tokenは複数発行でき、一定数を超えると古いものから無効化される

Limits apply to the number of refresh tokens that are issued per client-user combination, and per user across all clients, and these limits are different. If your application requests enough refresh tokens to go over one of the limits, older refresh tokens stop working.

新しくTokenを発行しても、古いTokenは有効であり続ける(使われないと6ヶ月後に失効する 後述)。古いTokenを無効化するには明示的にRevokeする必要がある。

Refresh Tokenが無効化されるパターン

  • The user has revoked your app's access.
  • The refresh token has not been used for six months.
  • The user changed passwords and the refresh token contains Gmail scopes.
  • The user account has exceeded a maximum number of granted (live) refresh tokens.
  • The user belongs to a Google Cloud Platform organization that has session control policies in effect.
  • ユーザーがアクセス権を削除した場合(Googleアカウントのセキュリティ設定から削除可能)
  • Refresh Tokenが6ヶ月使用されなかった場合
  • TokenのScopeにGmailが含まれている状態でユーザがパスワードを変更した場合
  • 発行されたRefresh Token(有効なもの)の数が制限を超えた場合
  • Session control policiesが有効なGoogle Cloud Platform organizationに所属している場合

A Google Cloud Platform project with an OAuth consent screen configured for an external user type and a publishing status of "Testing" is issued a refresh token expiring in 7 days.

プロジェクトの設定が外部ユーザへ開放されていて、かつTesting状態の場合はRefresh Tokenは7日間で失効する

There is currently a limit of 100 refresh tokens per Google Account per OAuth 2.0 client ID. If the limit is reached, creating a new refresh token automatically invalidates the oldest refresh token without warning. This limit does not apply to service accounts.

1クライアント(アプリケーション)あたり、サービスアカウントを除いて100個までRefresh Tokenを生成できる。それ以上生成しようとすると古いものから失効していく。

アクセス権の削除

  • アクセス権を削除すると、Refresh Tokenが無効化され、また有効期限内のAccess Tokenを使ってもAPIを呼び出すと400 Bad Requestが発生するようになる。ただし、後述の通りSDK経由でAPIを呼び出した場合、失効後もしばらくエラーが返ってこないことがあるので注意。
  • アカウント設定画面からアクセス権を削除した場合、発行されているすべてのRefresh Tokenが無効化される。個別に無効化することはできない。キーのローテーションをしたいような場合に、この機能は使えない。

Tokenの無効化

  • Refresh TokenもしくはAccess Tokenはアプリケーション側から無効化することもできる。
  • Token単位でRevokeできるので、複数Tokenが発行されていて特定のTokenだけ無効化したいような場合はこの機能を使う。

おまけ: Tokenが失効しているはずなのにAPIからエラーが返ってこない

Refresh Tokenを失効させたはずなのに、expiryを過ぎたAccess Tokenを使ってSDK経由でAPIを呼び出すとレスポンスが返ってきてしまうことがある。調べてみたところどうやらSDK内部でレスポンスをキャッシュする機構があるようで、パラメータの内容を変えてAPIを叩き直してみたところ無事エラーが返ることを確認できた。若干厄介な挙動だ。

9
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
6