-
Notifications
You must be signed in to change notification settings - Fork 0
Consolidate auth callbacks and fix Slack controller issues #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,16 @@ | ||
| class ApplicationController < ActionController::Base | ||
| # Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has. | ||
| allow_browser versions: :modern | ||
| before_action :check_session_expiry | ||
| before_action :check_user_token | ||
| before_action :authenticate! | ||
|
|
||
| private | ||
|
|
||
| def check_user_token | ||
| unless session[:user_token] | ||
| render "puzzles/login" | ||
| end | ||
| end | ||
|
|
||
| def check_session_expiry | ||
| def authenticate! | ||
| if session[:expires_at].present? && Time.current > session[:expires_at] | ||
| reset_session | ||
| render "puzzles/login" | ||
| else | ||
| session[:expires_at] = 1.hour.from_now | ||
| render "puzzles/login" and return | ||
| end | ||
| session[:expires_at] = 1.hour.from_now | ||
| render "puzzles/login" unless session[:user_token] | ||
| end | ||
| end |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,12 @@ | ||
| require "test_helper" | ||
|
|
||
| class SessionsControllerTest < ActionDispatch::IntegrationTest | ||
| # test "the truth" do | ||
| # assert true | ||
| # end | ||
| test "completes OAuth even when session has expired" do | ||
| sign_in | ||
|
|
||
| travel_to 2.hours.from_now do | ||
| sign_in | ||
| assert_redirected_to root_path | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| require "test_helper" | ||
|
|
||
| class Slack::PuzzlesControllerTest < ActionDispatch::IntegrationTest | ||
| setup do | ||
| ENV["SLACK_SIGNING_SECRET"] = "test_signing_secret" | ||
| end | ||
|
|
||
| test "rejects request with invalid Slack signature" do | ||
| post slack_puzzle_path, params: { payload: puzzle_payload }, | ||
| headers: slack_headers(secret: "wrong_secret") | ||
|
|
||
| assert_response :unauthorized | ||
| end | ||
|
|
||
| test "rejects request with expired Slack timestamp" do | ||
| post slack_puzzle_path, params: { payload: puzzle_payload }, | ||
| headers: slack_headers(timestamp: Time.now.to_i - 400) | ||
|
|
||
| assert_response :unauthorized | ||
| end | ||
|
|
||
| test "renders ok when puzzle fails validation" do | ||
| params = { payload: puzzle_payload(question: "") } | ||
|
|
||
| post slack_puzzle_path, params: params, | ||
| headers: slack_headers(body: params.to_query) | ||
|
|
||
| assert_response :ok | ||
| end | ||
|
|
||
| test "renders ok even when Slack notification fails after puzzle is saved" do | ||
| original = Slack::ApplicationController.instance_method(:send_message) | ||
| Slack::ApplicationController.define_method(:send_message) { |*| nil } | ||
|
|
||
| params = { payload: puzzle_payload(question: "What is unique about Ruby's blocks?") } | ||
|
|
||
| post slack_puzzle_path, params: params, | ||
| headers: slack_headers(body: params.to_query) | ||
|
|
||
| assert_response :ok | ||
| ensure | ||
| Slack::ApplicationController.define_method(:send_message, original) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def puzzle_payload(question: "What is Ruby?") | ||
| { | ||
| user: { id: "U123" }, | ||
| view: { | ||
| state: { | ||
| values: { | ||
| question: { question: { value: question } }, | ||
| answer: { answer: { selected_option: { value: "ruby" } } }, | ||
| explanation: { explanation: { value: "It is a programming language." } }, | ||
| link: { link: { value: nil } } | ||
| } | ||
| } | ||
| } | ||
| }.to_json | ||
| end | ||
|
|
||
| def slack_headers(secret: ENV["SLACK_SIGNING_SECRET"], timestamp: Time.now.to_i, body: "") | ||
| ts = timestamp.to_s | ||
| sig = "v0=" + OpenSSL::HMAC.hexdigest("SHA256", secret, "v0:#{ts}:#{body}") | ||
| { "X-Slack-Request-Timestamp" => ts, "X-Slack-Signature" => sig } | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we still want to respond with the unprocessable entity error? I suppose it depends on what slack expects us to give back to it in case of an error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, we never should have added that
head :unprocessable_entitythat is a controller's responsibility, thesend_messageis just part of the process