megutech

自身の備忘録として主にWEBサーバー周りの技術について投稿しています。

Ruby on RailsでreCAPTCHA Enterprise を使いたい

無料版と有料版のreCAPTCHAとがありますが、使い方は一緒かと思ったら違ってちょっと大変だったお話。

環境

Service Version
Ruby 2.7.4
Ruby on Rails 6.1
recaptcha 5.8.1

前提

Gemはrecaptchaを使います。

通常のreCAPTCHAなら下記用意しておけば、後はドキュメント通りでOKです。

Recaptcha.configure do |config|
  config.site_key  = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
  config.secret_key = '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'

  # Uncomment the following lines if you are using the Enterprise API:
  # config.enterprise = true
  # config.enterprise_api_key = 'AIzvFyE3TU-g4K_Kozr9F1smEzZSGBVOfLKyupA'
  # config.enterprise_project_id = 'my-project'
end

でもenterpriseにはsecret_keyがありません。

代わりにenterprise_api_keyを使います。

ドキュメントにはコメントを外しましょうとはありますが、secret_keyは不要ですとは無かったのでsecret_keyを探して右往左往してしまいました。。

というわけでenterpriseでは以下になります。

Recaptcha.configure do |config|
  config.site_key  = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'

  config.enterprise = true
  config.enterprise_api_key = 'AIzvFyE3TU-xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
  config.enterprise_project_id = 'my-project'
end

各キーの場所

ついでなのでキーのそれぞれの場所も記しておきます。

これまた探すのに手間取って右往左往しましたので。。。

site_key

GCPの「セキュリティ」から「reCAPTCHAT Enterprise」を選択し、キーを作成してください。

enterprise_api_key

GCPの「APIとサービス」からAPIキーを発行してください。

キーの制限としては「reCAPTCHA Enterprise API」を選択しておいてください。

enterprise_project_id

ダッシュボードの「プロジェクト情報」に記載のプロジェクトIDです。

所感

折角recaptchaにEnterprise用の機能まで備わっているのに、分かりづらくてもったいない。。

公式は公式で google-cloud-recaptcha_enterprise を押してきていて、やたらと混乱してしまった。。。