0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Rails OmniAuthで本番だけGoogleログインが失敗する原因と対応(AuthenticityError)

Posted at

ログに出ていたエラー

WARN -- omniauth: Attack prevented by OmniAuth::AuthenticityTokenProtection
ERROR -- omniauth: Authentication failure! authenticity_error: OmniAuth::AuthenticityError, Forbidden

原因

OmniAuth 2.x はデフォルトでリクエストをPOSTにするため、
RailsのCSRFトークン検証と整合が取れないと AuthenticityError になる。

本番だけ発生したのは、
Cookie/SameSite/HTTPS などの環境差で CSRF token が一致しなかった可能性が高い。

対応

OmniAuth 2.x に内蔵されている CSRF 保護を明示的に設定。

追加したinitializer

config/initializers/omniauth.rb

OmniAuth.config.request_validation_phase =
  OmniAuth::AuthenticityTokenProtection.new(key: :_csrf_token)

補足

omniauth-rails_csrf_protection というgemもあるが、OmniAuth 2.x では内蔵保護で十分なので今回は採用しなかった。

参考記事

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?