-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2769 from sciencehistory/microsoft_sso
Microsoft SSO ( keywords Entra / Azure / OmniAuth / Oauth )
- Loading branch information
Showing
24 changed files
with
622 additions
and
102 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
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 |
---|---|---|
|
@@ -350,7 +350,7 @@ Configuration for solr_wrapper is at `./.solr_wrapper.yml` | |
|
||
### Account management | ||
|
||
We shouldn't have to use account management rake tasks as much, since there is now admin web interface for creating and editing accounts. But they are still there, as they can be convenient for setting up a dev environment or perhaps bootstrapping a production environment with an admin account, or in general automating things involving users. | ||
We shouldn't have to use account management rake tasks as much, since we provide an admin web interface for creating and editing accounts. But they are still there, as they can be convenient for setting up a dev environment or perhaps bootstrapping a production environment with an admin account, or in general automating things involving users. | ||
|
||
```shell | ||
./bin/rake scihist:user:create[[email protected]] | ||
|
@@ -369,6 +369,11 @@ This can be useful if we need to do some maintenance that doesn't bring down the | |
|
||
This feature was in our v1 sufia-based app, we copied it over. | ||
|
||
|
||
## Using Microsoft SSO | ||
It's possible to configure the app to use Microsoft single sign-on (SSO) instead of standard email-and-password authentication. | ||
Details are in [a separate README file](config/initializers/MICROSOFT_SSO_README.md). | ||
|
||
## Thanks | ||
|
||
<img src="https://www.browserstack.com/images/layout/browserstack-logo-600x315.png" width="280"/> | ||
|
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,70 @@ | ||
# This controller provides methods used to authenticate a user using | ||
# Microsoft Single Sign On / Entra / Azure. | ||
# Links to more documentation are at config/initializers/devise.rb. | ||
# | ||
# Note that if ScihistDigicoll::Env.lookup(:log_in_using_microsoft_sso) is not set to true, | ||
# this *entire* controller is turned off in config/routes.rb . | ||
# (See devise_for :users in that file.) | ||
class AuthController < Devise::OmniauthCallbacksController | ||
|
||
before_action :maybe_redirect_back, only: [:passthru, :entra_id] | ||
|
||
# This method signs a user in after they authenticate with Microsoft SSO. | ||
def entra_id | ||
email = request.env['omniauth.auth']['info']['email'] | ||
@user = User.where('email ILIKE ?', "%#{ User.sanitize_sql_like(email) }%").first | ||
|
||
unless @user&.persisted? | ||
flash[:alert] = "You can't currently log in to the Digital Collections. Please contact a Digital Collections administrator." | ||
redirect_back(fallback_location: root_path) | ||
return | ||
end | ||
|
||
if @user.locked_out? | ||
flash[:alert] = "Sorry, this user is not allowed to log in." | ||
redirect_back(fallback_location: root_path) | ||
return | ||
end | ||
|
||
flash[:notice] = 'Signed in successfully.' | ||
sign_in_and_redirect @user, event: :authentication | ||
end | ||
|
||
# Log a user out of the digital collections, | ||
# *then* log them out of Microsoft SSO. | ||
def sso_logout | ||
# There should not be a route to this method unless ScihistDigicoll::Env.lookup(:log_in_using_microsoft_sso). | ||
raise "This method should be unreachable." unless ScihistDigicoll::Env.lookup(:log_in_using_microsoft_sso) | ||
sign_out current_user | ||
redirect_to sso_logout_path, allow_other_host: true | ||
end | ||
|
||
private | ||
|
||
# We need to provide a default path for newly signed-in users. | ||
# Usual login paths do not call this method, but when | ||
# the SSO setup is misconfigured, | ||
# this method does sometimes get called, resulting in a 500 error. | ||
# Instead, we send users to the root path. | ||
def new_session_path *args | ||
flash[:notice] = "This URL is not meant for regular users." | ||
root_path | ||
end | ||
|
||
def sso_logout_path | ||
@logout_path ||= OmniAuth::Strategies::EntraId::BASE_URL + | ||
"/common/oauth2/v2.0/logout" + | ||
"?post_logout_redirect_uri=" + | ||
ScihistDigicoll::Env.lookup(:app_url_base) + | ||
root_path | ||
end | ||
|
||
def maybe_redirect_back | ||
unless ScihistDigicoll::Env.lookup(:log_in_using_microsoft_sso) | ||
flash[:alert] = "Sorry, you can't log in this way." | ||
redirect_back(fallback_location: root_path) | ||
return | ||
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,13 @@ | ||
# To minimize confusion, let's make password manipulation unavailable if we're currently managing | ||
# auth using Microsoft. These passwords are irrelevant and will just cause confusion, since the user likely | ||
# has a totally different password in Entra. | ||
class PasswordsController < Devise::PasswordsController | ||
before_action :maybe_redirect_back | ||
private | ||
def maybe_redirect_back | ||
return unless ScihistDigicoll::Env.lookup(:log_in_using_microsoft_sso) | ||
flash[:alert] = "Passwords are managed in Microsoft SSO now." | ||
redirect_back(fallback_location: root_path) | ||
return | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Microsoft SSO | ||
By default, when you install the app, users log in using a combination of a username and password. | ||
|
||
If you want, though, you can try using Microsoft’s Entra to authenticate users instead. | ||
- [This PR](https://github.com/sciencehistory/scihist_digicoll/pull/2769) has a lot of details and context. | ||
- Authentication is provided by two gems, `omniauth-entra-id` and `omniauth-rails_csrf_protection`. | ||
- A feature switch, ENV setting `:log_in_using_microsoft_sso`, determines whether the app uses Microsoft SSO to sign in or not. This is turned off by default, so if you want to use it in Dev, you will need to add some env variables (see “using SSO in dev”). | ||
- The Microsoft auth provider is configured with three more ENV settings: | ||
- :microsoft_sso_client_id identifies the app to Microsoft SSO; | ||
- :microsoft_sso_client_secret authenticates the app to Microsoft SSO; | ||
- :microsoft_sso_tenant_id identifies the Microsoft SSO directory the app wants to check (namely the Institute one. This ID is the same for dev, staging and prod.) | ||
- Most of the configuration is done in two files: | ||
- config/initializers/devise.rb | ||
- config/routes.rb | ||
|
||
## Using SSO in a development environment | ||
Single sign-on is turned off by default in development. If you want to try using SSO in development, you can temporarily add something like the following to your local_env.yml file: | ||
- log_in_using_microsoft_sso: true | ||
- microsoft_sso_client_id: [...] | ||
- microsoft_sso_client_secret: [...] | ||
- microsoft_sso_tenant_id: [...] |
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.