-
Notifications
You must be signed in to change notification settings - Fork 77
Implementing SSO #230
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
Open
kevin-atnos
wants to merge
11
commits into
master
Choose a base branch
from
sso
base: master
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.
Open
Implementing SSO #230
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
aba80bf
Implementing SSO
kevin-atnos 2976f81
Add redis session store
brunto 5233a2e
fix queries N+1
brunto 5a804a8
Update saml_controller.rb
kevin-atnos 37469f5
Add missing allow host and unlock_access
brunto 6d7b00f
Missing condition
brunto 30ab8c8
Update README.md
kevin-atnos f6cfc6c
Update README.md
kevin-atnos 1a96665
fix: README
kevin-atnos 47116c4
fix: update SSO redirection to use environment variable
brunto 3373b56
Add IdP certificate fingerprint settings (#346)
brunto 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
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 |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| class SamlController < Doorkeeper::TokensController | ||
| # skip_before_action :doorkeeper_authorize! | ||
|
|
||
| def metadata | ||
| meta = OneLogin::RubySaml::Metadata.new | ||
| render xml: meta.generate(settings, true) | ||
| end | ||
|
|
||
| def sso | ||
| request = OneLogin::RubySaml::Authrequest.new | ||
| redirect_to(request.create(settings), allow_other_host: true) | ||
| end | ||
|
|
||
| def consume | ||
| response = OneLogin::RubySaml::Response.new(params[:SAMLResponse], settings:) | ||
|
|
||
| if response.is_valid? | ||
| email = response.name_id | ||
| session[:nameid] = response.name_id | ||
| user = User.find_by("LOWER(email) = ?", email.strip.downcase) | ||
| if user | ||
| user.unlock_access! | ||
| else | ||
| password = [*'0'..'9', *'a'..'z', *'A'..'Z', *'!'..'?'].sample(16).join | ||
| user = User.create!(email:, password:, password_confirmation: password) | ||
|
|
||
| user.is_user = true | ||
| user.unlock_access! | ||
| user.save | ||
| end | ||
| sign_in(:user, user) | ||
|
|
||
| doorkeeper_app = Doorkeeper::Application.first | ||
| access_token = Doorkeeper::AccessToken.find_or_create_for( | ||
| application: doorkeeper_app, | ||
| resource_owner: user.id, | ||
| scopes: Doorkeeper::OAuth::Scopes.from_array(%w[public]) | ||
| ) | ||
|
|
||
| # redirect_to frontrnd | ||
| redirect_to "#{ENV['SSO_FRONTEND_REDIRECTION']}/#/?sso_token=#{access_token.token}", allow_other_host: true | ||
| else | ||
| logger.info "Response Invalid. Errors: #{response.errors}" | ||
| @errors = response.errors | ||
| redirect_to ENV['SSO_FRONTEND_REDIRECTION'], allow_other_host: true | ||
| end | ||
| end | ||
|
|
||
| def logout | ||
| logout_request = OneLogin::RubySaml::Logoutrequest.new | ||
| session[:transaction_id] = logout_request.uuid | ||
|
|
||
| logger.info "New SP SLO for User ID: '#{session[:nameid]}', Transaction ID: '#{session[:transaction_id]}'" | ||
|
|
||
| settings.name_identifier_value = session[:nameid] if settings.name_identifier_value.nil? | ||
|
|
||
| redirect_to(logout_request.create(settings), allow_other_host: true) | ||
| end | ||
|
|
||
| # Handle the SLO response from the IdP | ||
| # GET /saml/slo | ||
| def slo | ||
| logout_response = if session.has_key? :transaction_id | ||
| OneLogin::RubySaml::Logoutresponse.new(params[:SAMLResponse], settings, | ||
| matches_request_id: session[:transaction_id]) | ||
| else | ||
| OneLogin::RubySaml::Logoutresponse.new(params[:SAMLResponse], settings) | ||
| end | ||
| logger.info "LogoutResponse is: #{logout_response}" | ||
|
|
||
| # Validate the SAML Logout Response | ||
| if !logout_response.validate | ||
| logger.error 'The SAML Logout Response is invalid' | ||
| else | ||
| # Actually log out this session | ||
| logger.info "SLO completed for '#{session[:nameid]}'" | ||
| session[:nameid] = nil | ||
| session[:transaction_id] = nil | ||
|
|
||
| redirect_to ENV['SSO_FRONTEND_REDIRECTION'], allow_other_host: true | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def settings | ||
| settings = OneLogin::RubySaml::Settings.new | ||
| url_base = "#{request.protocol}#{request.host_with_port}" | ||
|
|
||
| # settings.soft = true | ||
| settings.issuer = "#{url_base}/saml/metadata" | ||
| settings.assertion_consumer_service_url = "#{url_base}/saml/acs" | ||
| settings.assertion_consumer_logout_service_url = "#{url_base}/saml/slo" | ||
|
|
||
| # IdP section | ||
| settings.idp_entity_id = ENV['IDP_ENTITY_ID'] | ||
| settings.idp_sso_target_url = ENV['IDP_SSO_TARGET_URL'] | ||
| settings.idp_slo_target_url = ENV['IDP_SLO_TARGET_URL'] | ||
| settings.idp_cert_fingerprint = ENV['IDP_CERT_FINGERPRINT'] | ||
| settings.idp_cert_fingerprint_algorithm = ENV['IDP_CERT_FINGERPRINT_ALGORITHM'] | ||
| settings.name_identifier_format = 'urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress' | ||
|
|
||
| settings | ||
| 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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| Rails.application.config.session_store :redis_store, | ||
| url: "#{ENV.fetch('REDIS_URL', 'redis://127.0.0.1:6379')}/0/session", | ||
| expire_after: 30.days, | ||
| key: "_piaback_session_#{Rails.env}", | ||
| domain: ENV.fetch("DOMAIN_NAME", "localhost"), | ||
| threadsafe: true, | ||
| secure: Rails.env.production?, | ||
| same_site: :lax, | ||
| httponly: true | ||
|
|
||
| Rails.application.config.middleware.use ActionDispatch::Cookies | ||
| Rails.application.config.middleware.use Rails.application.config.session_store, Rails.application.config.session_options |
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
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.
Uh oh!
There was an error while loading. Please reload this page.