Skip to content

Commit fe09489

Browse files
committed
IIRR-30(auth): Enforce user token check on all controllers
Add a before_action in ApplicationController to check for the presence of a user token in the session, rendering the login page if missing. Remove redundant token check from PuzzlesController. Allow SessionsController to skip this check for authentication purposes. Ref: https://ombulabs.atlassian.net/browse/IIRR-30
1 parent 912ff92 commit fe09489

6 files changed

Lines changed: 40 additions & 5 deletions

File tree

app/controllers/application_controller.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@ class ApplicationController < ActionController::Base
22
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
33
allow_browser versions: :modern
44
before_action :check_session_expiry
5+
before_action :check_user_token
56

67
private
78

9+
def check_user_token
10+
unless session[:user_token]
11+
render "puzzles/login"
12+
end
13+
end
14+
815
def check_session_expiry
916
if session[:expires_at].present? && Time.current > session[:expires_at]
1017
reset_session

app/controllers/puzzles_controller.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
class PuzzlesController < ApplicationController
22
def index
3-
unless session[:user_token]
4-
render "login"
5-
end
6-
73
@pending_puzzles = Puzzle.pending
84
@approved_puzzles = Puzzle.approved
95
@rejected_puzzles = Puzzle.rejected

app/controllers/sessions_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
class SessionsController < ApplicationController
2+
skip_before_action :check_user_token
3+
24
def create
35
auth = request.env["omniauth.auth"]
46
user_email = auth.info.email
57

6-
domain_allowlist = ENV.fetch("DOMAIN_ALLOWLIST").split(",").map(&:strip)
8+
domain_allowlist = ENV.fetch("DOMAIN_ALLOWLIST", "").split(",").map(&:strip)
79
if domain_allowlist.present?
810
unless domain_allowlist.any? { |domain| user_email.end_with?("@#{domain}") }
911
reset_session

config/environments/test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,9 @@
5050

5151
# Raise error when a before_action's only/except options reference missing actions.
5252
config.action_controller.raise_on_missing_callback_actions = true
53+
54+
# Once you have enabled test mode, all requests to OmniAuth will be short circuited to use
55+
# the mock authentication hash.
56+
# See: https://github.com/omniauth/omniauth/wiki/Integration-Testing
57+
OmniAuth.config.test_mode = true
5358
end

test/controllers/puzzles_controller_test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
class PuzzlesControllerTest < ActionDispatch::IntegrationTest
44
test "should get index" do
5+
sign_in
56
get puzzles_path
67
assert_response :success
78
end
89

910
test "should show error message when editing puzzle with invalid data" do
1011
puzzle = puzzles(:one)
1112

13+
sign_in
1214
patch puzzle_path(puzzle), params: {
1315
puzzle: {
1416
question: "",
@@ -27,6 +29,7 @@ class PuzzlesControllerTest < ActionDispatch::IntegrationTest
2729
test "should successfully update puzzle with valid data" do
2830
puzzle = puzzles(:one)
2931

32+
sign_in
3033
patch puzzle_path(puzzle), params: {
3134
puzzle: {
3235
question: "Updated question",

test/test_helper.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,28 @@ class TestCase
1010
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
1111
fixtures :all
1212

13+
# Global setup to be run before each test
14+
setup do
15+
OmniAuth.config.mock_auth[:google] = nil
16+
end
17+
1318
# Add more helper methods to be used by all tests here...
19+
def sign_in
20+
auth = {
21+
provider: "google_oauth2",
22+
uid: "123456789",
23+
info: {
24+
email: "cooper@ombulabs.com"
25+
},
26+
credentials: {
27+
token: "token"
28+
}
29+
}
30+
31+
OmniAuth.config.add_mock(:google, auth)
32+
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google]
33+
34+
post sessions_path, params: { provider: :google }
35+
end
1436
end
1537
end

0 commit comments

Comments
 (0)