reCAPTCHA (Ruby 側) の煩雑な処理をモジュール化
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 全体の処理 | |
# | |
# スコアを得るまでの流れ: | |
# | |
# View 側: | |
# 1. reCAPTCHA を設置する (JS 処理) | |
# 2. ログインフォームの submit に合わせて g-recaptcha-response にデータを乗せる | |
# | |
# Rails 側: | |
# 1. client を生成 | |
# 1. event を生成 (準備) | |
# 2. assessment を生成 (準備) | |
# 3. request を生成 (準備) | |
# 2. client.create_assessment(generate_assessment) でスコアを含む結果を得る | |
# コントローラに include されることを想定 | |
# reCAPTCHA Enterprise サービスに評価を作成してもらってスコアを判定できるようにする | |
module RecaptchaEnterprise | |
module Common | |
private | |
# チェックボックス方式 | |
def checkbox_site_key | |
variable_part = | |
case Rails.env | |
when 'test' | |
:test | |
when 'development' | |
:dev | |
when 'production' | |
:prod | |
end | |
site_key = | |
Rails.application | |
.credentials | |
.public_send("recaptcha_v2_#{variable_part}!")[:site_key] | |
# AWSなど... デプロイ先での環境変数設定があれば そちらを優先する | |
ENV['RECAPTCHA_SECRET_KEY_CHECKBOX'] || site_key | |
end | |
# スコア方式 | |
def score_site_key | |
variable_part = | |
case Rails.env | |
when 'test' | |
:test | |
when 'development' | |
:dev | |
when 'production' | |
:prod | |
end | |
site_key = | |
Rails.application | |
.credentials | |
.public_send("recaptcha_v3_#{variable_part}!")[:site_key] | |
# AWSなど... デプロイ先での環境変数設定があれば そちらを優先する | |
ENV['RECAPTCHA_SECRET_KEY_SCORE'] || site_key | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module RecaptchaEnterprise | |
private | |
include Common | |
# assessment として送信するためのイベントを作成 | |
def generate_event | |
# スコア方式, チェックボックス方式による site_key 呼び出しの切り替え | |
site_key = | |
case params['recaptcha-type'] | |
when 'score' | |
score_site_key | |
when 'check' | |
checkbox_site_key | |
else | |
nil | |
end | |
::Google::Cloud::RecaptchaEnterprise::V1beta1::Event.new( | |
token: params['g-recaptcha-response'], | |
site_key: site_key, | |
expected_action: 'LOGIN' | |
) | |
end | |
# request.assessment= に渡すインスタンスを作成 | |
def generate_assessment | |
assessment = ::Google::Cloud::RecaptchaEnterprise::V1beta1::Assessment.new | |
assessment.event = generate_event | |
assessment | |
end | |
def generate_request | |
request = ::Google::Cloud::RecaptchaEnterprise::V1beta1::CreateAssessmentRequest.new | |
request.parent = Rails.application.credentials.recaptcha![:project_id] | |
request.assessment = generate_assessment | |
request | |
end | |
# スコアを含む情報を得るためのクライアントを作成 | |
def generate_client | |
credentials = JSON.parse(Rails.application.credentials.recaptcha_service_account!) | |
::Google::Cloud::RecaptchaEnterprise::V1beta1::RecaptchaEnterpriseService::Client.configure do |config| | |
# HINT: String, Hash, そのほか Google が用意したデータ形式が有効 | |
config.credentials = credentials | |
end | |
::Google::Cloud::RecaptchaEnterprise::V1beta1::RecaptchaEnterpriseService::Client.new | |
end | |
# View 側から与えられた情報をもとに、スコアを返す | |
def recaptcha_score | |
return @recaptcha_score if @recaptcha_score.present? | |
client = generate_client | |
@recaptcha_score ||= | |
client.create_assessment(generate_request) | |
.score | |
@recaptcha_score | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment