-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AdminAuthentication middleware for authenticating admins
Resolves a potential issue where a non-authenticated user could enumerate modules by checking for 307 vs 404 responses. Ensures that client apps get authentication correct.
- Loading branch information
Showing
18 changed files
with
181 additions
and
87 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
katalyst-koi (4.13.2) | ||
katalyst-koi (4.14.0) | ||
bcrypt | ||
importmap-rails | ||
katalyst-content | ||
|
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,9 +31,6 @@ def authenticate_local_admins(value) | |
helper :all | ||
|
||
layout -> { turbo_frame_layout || "koi/application" } | ||
|
||
before_action :authenticate_local_admin, if: -> { Koi::Controller::IsAdminController.authenticate_local_admins } | ||
before_action :authenticate_admin, unless: :admin_signed_in? | ||
end | ||
|
||
class << self | ||
|
@@ -47,10 +44,14 @@ def authenticate_local_admin | |
|
||
session[:admin_user_id] = | ||
Admin::User.where(email: %W[#{ENV.fetch('USER', nil)}@katalyst.com.au [email protected]]).first&.id | ||
|
||
flash.delete(:redirect) if (redirect = flash[:redirect]) | ||
|
||
redirect_to(redirect || admin_dashboard_path, status: :see_other) | ||
end | ||
|
||
def authenticate_admin | ||
redirect_to new_admin_session_path, status: :temporary_redirect | ||
def authenticate_local_admins? | ||
IsAdminController.authenticate_local_admins | ||
end | ||
|
||
def turbo_frame_layout | ||
|
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
# Describe your gem and declare its dependencies: | ||
Gem::Specification.new do |s| | ||
s.name = "katalyst-koi" | ||
s.version = "4.13.2" | ||
s.version = "4.14.0" | ||
s.authors = ["Katalyst Interactive"] | ||
s.email = ["[email protected]"] | ||
|
||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
module Koi | ||
module Middleware | ||
class AdminAuthentication | ||
def initialize(app) | ||
@app = app | ||
end | ||
|
||
def call(env) | ||
if env["PATH_INFO"].starts_with?("/admin") | ||
admin_call(env) | ||
else | ||
@app.call(env) | ||
end | ||
end | ||
|
||
def admin_call(env) | ||
request = ActionDispatch::Request.new(env) | ||
session = ActionDispatch::Request::Session.find(request) | ||
|
||
if requires_authentication?(request) && !authenticated?(session) | ||
# Set the redirection path for returning the user to their requested path after login | ||
if request.get? | ||
request.flash[:redirect] = request.fullpath | ||
request.commit_flash | ||
end | ||
|
||
[303, { "Location" => "/admin/session/new" }, []] | ||
else | ||
@app.call(env) | ||
end | ||
end | ||
|
||
private | ||
|
||
def requires_authentication?(request) | ||
!request.path.starts_with?("/admin/session") | ||
end | ||
|
||
def authenticated?(session) | ||
session[:admin_user_id].present? | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe "admin authentication" do | ||
describe "GET /admin/dashboard" do | ||
subject { action && response } | ||
|
||
let(:action) { get "/admin/dashboard" } | ||
|
||
it { is_expected.to have_http_status(:see_other).and(redirect_to("/admin/session/new")) } | ||
end | ||
|
||
describe "GET /admin/guess" do | ||
subject { action && response } | ||
|
||
let(:action) { get "/admin/guess" } | ||
|
||
it { is_expected.to have_http_status(:see_other).and(redirect_to("/admin/session/new")) } | ||
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
Oops, something went wrong.