-
Notifications
You must be signed in to change notification settings - Fork 4
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
Setup SSO for Admin app #3138
Draft
phil-l-brockwell
wants to merge
8
commits into
main
Choose a base branch
from
pb/setup-sso-for-admins
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Setup SSO for Admin app #3138
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
77f9c29
Set correct after_sign_out_path_for Admin users
phil-l-brockwell 5e2fe1d
Add omniauth gems
phil-l-brockwell b01b625
Add sso fields to Admin
phil-l-brockwell c3ec732
Configure OmniAuth::Strategies::DbtStaffSso
phil-l-brockwell bf8ac0b
Add FindAndUpdateWithAuth Interactor
phil-l-brockwell 72d143e
Replace existing Admin authorisation with OmniAuth
phil-l-brockwell c256da8
Remove redundant Admin endpoints
phil-l-brockwell 2d541fc
Setup strategy for development environment
phil-l-brockwell 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 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 |
---|---|---|
@@ -1,9 +1,2 @@ | ||
class Admins::ConfirmationsController < Devise::ConfirmationsController | ||
include PasswordSettable | ||
|
||
private | ||
|
||
def password_reset_path(token) | ||
edit_admin_password_path(reset_password_token: token, locals: { password_not_set: true }) | ||
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,33 @@ | ||
class Admins::OmniauthCallbacksController < Devise::OmniauthCallbacksController | ||
skip_before_action :verify_authenticity_token, only: :developer | ||
|
||
def dbt_staff_sso | ||
FindAndUpdateWithAuth.new(auth: request.env["omniauth.auth"], model: Admin).run.tap do |result| | ||
if result.success | ||
sign_in_and_redirect result.user, event: :authentication | ||
set_flash_message(:notice, :success, kind: "DBT Staff SSO") | ||
else | ||
set_flash_message(:notice, :error, kind: result.errors) | ||
redirect_to new_admin_session_url | ||
end | ||
end | ||
end | ||
|
||
def developer | ||
raise "developer strategy should only be used in development!" unless Rails.env.development? | ||
|
||
FindAndUpdateWithAuth.new(auth: request.env["omniauth.auth"], model: Admin).run.tap do |result| | ||
if result.success | ||
sign_in_and_redirect result.user, event: :authentication | ||
set_flash_message(:notice, :success, kind: "Developer SSO") | ||
else | ||
set_flash_message(:notice, :error, kind: result.errors) | ||
redirect_to new_admin_session_url | ||
end | ||
end | ||
end | ||
|
||
def failure | ||
redirect_to admin_root_path | ||
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,2 @@ | ||
class Admins::SessionsController < Devise::SessionsController | ||
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
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,52 @@ | ||
class FindAndUpdateWithAuth | ||
attr_reader :success, :errors, :user | ||
|
||
def initialize(auth:, model:) | ||
@model = model | ||
@sso_uid = auth.uid | ||
@sso_info = auth.info | ||
@sso_provider = auth.provider | ||
end | ||
|
||
def run | ||
@user = find_or_create_user | ||
@success = @user.persisted? | ||
@errors = @user.errors unless @success | ||
self | ||
end | ||
|
||
private | ||
|
||
# 1 - check if they have already set up sso; when they have update them to ensure we capture the current email/first_name/last_name | ||
# 2 - check if the user already existed prior to sso; when they do update them with the auth details | ||
# 3 - finally if they dont exist; create them from the auth details | ||
def find_or_create_user | ||
find_and_update_with_auth(sso_provider: @sso_provider, sso_uid: @sso_uid) || find_and_update_with_auth(email: @sso_info.email) || create_from_auth | ||
end | ||
|
||
def find_and_update_with_auth(conditions) | ||
raise "conditions should not be nil: #{conditions}" if conditions.compact.empty? | ||
|
||
existing = @model.find_by(**conditions) | ||
return unless existing | ||
|
||
@model.update( | ||
existing.id, | ||
sso_provider: @sso_provider, | ||
sso_uid: @sso_uid, | ||
email: @sso_info.email, | ||
first_name: @sso_info.first_name, | ||
last_name: @sso_info.last_name, | ||
) | ||
end | ||
|
||
def create_from_auth | ||
@model.create( | ||
email: @sso_info.email, | ||
first_name: @sso_info.first_name, | ||
last_name: @sso_info.last_name, | ||
sso_uid: @sso_uid, | ||
sso_provider: @sso_provider, | ||
) | ||
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
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
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,6 @@ | ||
class AddOmniauthToAdmins < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :admins, :sso_provider, :string | ||
add_column :admins, :sso_uid, :string | ||
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 |
---|---|---|
@@ -1,7 +1,6 @@ | ||
unless Admin.exists? | ||
admin_args = { | ||
email: "[email protected]", | ||
password: "^#ur9EkLm@1W+OaDvg", | ||
first_name: "First name", | ||
last_name: "Last name", | ||
confirmed_at: DateTime.now | ||
|
Oops, something went wrong.
Oops, something went wrong.
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.
Check failure
Code scanning / CodeQL
CSRF protection weakened or disabled High
Copilot Autofix AI about 1 month ago
To fix the CSRF vulnerability, we should re-enable CSRF protection for the
developer
action. This can be done by removing theskip_before_action :verify_authenticity_token, only: :developer
line. Additionally, we can add a safeguard to ensure that thedeveloper
action is only accessible in development environments by using a more secure approach, such as an environment-specific route constraint.