Skip to content

Commit

Permalink
allow custom challenge render config
Browse files Browse the repository at this point in the history
  • Loading branch information
jrochkind committed Feb 25, 2025
1 parent 60cc36d commit a0524dd
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ def self._bot_detect_passed_good?(request)


def challenge
# possible custom render to choose layouts or templates, but normally
# we just do default rails render and this proc is empty.
# if self.bot_challenge_config.challenge_renderer
# instance_eval &self.bot_challenge_config.challenge_renderer
# end
#
render template: "optional/some_template", layout: "optional_layout"
end

def verify_challenge
Expand Down
3 changes: 3 additions & 0 deletions app/models/bot_challenge_page/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ def initialize(**values)
# actions from protection.
attribute :allow_exempt, default: ->(controller, config) { false }

# replace with say `->() { render layout: 'something' }`, or `render "somedir/some_template"`
attribute :challenge_renderer, default: nil


# rate limit per subnet, following lehigh's lead, although we use a smaller
# subnet: /24 for IPv4, and /72 for IPv6
Expand Down
22 changes: 22 additions & 0 deletions spec/controllers/bot_challenge_page_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
it "renders and includes expected values" do
get :challenge

expect(response).to have_http_status(200)
expect(response.body).to include I18n.t("bot_challenge_page.title")
expect(response.body).to include I18n.t("bot_challenge_page.blurb_html")

Expand All @@ -18,6 +19,27 @@
expect(errorTemplate).to be_present
expect(errorTemplate.text).to include I18n.t("bot_challenge_page.error")
end

describe "with custom render" do
around do |example|
orig = BotChallengePage::BotChallengePageController.bot_challenge_config
BotChallengePage::BotChallengePageController.bot_challenge_config.challenge_renderer =
lambda { render "optional/some_template", layout: "optional/some_layout" }

example.run

BotChallengePage::BotChallengePageController.bot_challenge_config = orig
end

it "renders and includes custom templates" do
get :challenge

expect(response).to have_http_status(200)

expect(response.body).to include("We are in optional some layout.")
expect(response.body).to include("Some Template Rendered")
end
end
end

describe "#verify_challenge" do
Expand Down
7 changes: 7 additions & 0 deletions spec/dummy/app/views/layouts/optional_layout.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>

<body>
We are in optional some layout.
<%= yield %>
</body>
</html>
21 changes: 21 additions & 0 deletions spec/dummy/app/views/optional/some_template.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<h1>Some Template Rendered</h1>

<%= render "bot_challenge_page/turnstile_widget_placeholder" %>

<template id="botChallengePageErrorTemplate">
<div class="alert alert-danger" role="alert">
<i class="fa fa-exclamation-triangle" aria-hidden="true"></i>
<%= t('bot_challenge_page.error') %>
</div>
</template>

<template id="botChallengePageStillAroundTemplate" data-still_around_delay_ms="<%= still_around_delay_ms %>">
<div class="alert alert-info" role="alert">
<i class="fa fa-info-circle" aria-hidden="true"></i>
<%= t('bot_challenge_page.still_around') %>
</div>
</template>

<script src="<%= cf_turnstile_js_url %>" async defer></script>

<%= render "bot_challenge_page/local_turnstile_script_tag" %>

0 comments on commit a0524dd

Please sign in to comment.