-
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.
- Loading branch information
Showing
12 changed files
with
164 additions
and
18 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
class SamlSessionsController < ApplicationController | ||
skip_before_action :authentication_required | ||
skip_forgery_protection only: :create | ||
|
||
def new | ||
saml_request = OneLogin::RubySaml::Authrequest.new | ||
redirect_to saml_request.create(saml_settings), allow_other_host: true | ||
end | ||
|
||
def create | ||
saml_response = OneLogin::RubySaml::Response.new(params[:SAMLResponse]) | ||
saml_response.settings = saml_settings | ||
|
||
if saml_response.is_valid? | ||
user = find_or_create_user(saml_response) | ||
session[:user_id] = user.id | ||
if user.admin? | ||
redirect_to users_path, notice: "Logged in!" | ||
else | ||
redirect_to root_url, notice: "Logged in!" | ||
end | ||
else | ||
redirect_to new_session_path, alert: "Could not sign you in via SSO" | ||
end | ||
end | ||
|
||
private | ||
|
||
def find_or_create_user(saml_response) | ||
User.find_or_create_by!(email: saml_response.nameid) do |user| | ||
user.first_name = "SAML" | ||
user.last_name = "User" | ||
end | ||
end | ||
|
||
def saml_settings | ||
settings = Saml.new.settings | ||
settings.assertion_consumer_service_url = saml_session_url | ||
settings | ||
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,14 @@ | ||
class Saml | ||
def self.configured? | ||
Rails.configuration.hdm[:saml].present? | ||
end | ||
|
||
def initialize | ||
@hdm_settings = Rails.configuration.hdm[:saml] | ||
@hdm_settings[:name_identifier_format] ||= "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress" | ||
end | ||
|
||
def settings | ||
OneLogin::RubySaml::Settings.new(@hdm_settings) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<% if Ldap.configured? || Saml.configured? %> | ||
<ul class="nav nav-tabs"> | ||
<li class="nav-item"> | ||
<%= link_to "Local login", login_path, class: "nav-link #{"active" if local_assigns[:active] == :local}" %> | ||
</li> | ||
<% if Ldap.configured? %> | ||
<li class="nav-item"> | ||
<%= link_to "LDAP login", new_ldap_session_path, class: "nav-link #{"active" if local_assigns[:active] == :ldap}" %> | ||
</li> | ||
<% end %> | ||
<% if Saml.configured? %> | ||
<li class="nav-item"> | ||
<%= link_to new_saml_session_path, class: "nav-link" do %> | ||
<%= icon("box-arrow-up-right") %> | ||
SAML SSO login | ||
<% end %> | ||
</li> | ||
<% end %> | ||
</ul> | ||
<% 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require "test_helper" | ||
|
||
class SamlSessionsControllerTest < ActionDispatch::IntegrationTest | ||
setup do | ||
Rails.configuration.hdm[:saml] = SAML_TEST_CONFIG.dup | ||
end | ||
|
||
teardown do | ||
Rails.configuration.hdm.delete(:saml) | ||
end | ||
|
||
test "#new redirects to SSO" do | ||
get new_saml_session_path | ||
|
||
assert_redirected_to %r{\Ahttps://testsso} | ||
end | ||
|
||
test "#create with successful SSO redirects to root_path" do | ||
stubbed_saml_response(valid: true) do | ||
post saml_session_path | ||
assert_redirected_to root_path | ||
end | ||
end | ||
|
||
test "#create with failed SSO redirects to login page" do | ||
stubbed_saml_response(valid: false) do | ||
post saml_session_path | ||
assert_redirected_to new_session_path | ||
end | ||
end | ||
|
||
private | ||
|
||
def stubbed_saml_response(valid: true, &block) | ||
saml_response = Minitest::Mock.new | ||
saml_response.expect(:settings=, true, [OneLogin::RubySaml::Settings]) | ||
saml_response.expect(:is_valid?, valid) | ||
saml_response.expect(:nameid, "testuser@example.com") | ||
OneLogin::RubySaml::Response.stub(:new, saml_response, &block) | ||
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,20 @@ | ||
require 'test_helper' | ||
|
||
class SamlTest < ActiveSupport::TestCase | ||
test "::configured? checks if configuration exists" do | ||
Rails.configuration.hdm[:saml] = SAML_TEST_CONFIG.dup | ||
assert Saml.configured? | ||
Rails.configuration.hdm.delete(:saml) | ||
assert_not Saml.configured? | ||
end | ||
|
||
test "#settings correctly configures ruby-saml" do | ||
Rails.configuration.hdm[:saml] = SAML_TEST_CONFIG.dup | ||
settings = Saml.new.settings | ||
assert_equal "hdm-test", settings.sp_entity_id | ||
assert_equal "https://testsso", settings.idp_sso_service_url | ||
assert_equal "test", settings.idp_cert_fingerprint | ||
assert_equal "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress", settings.name_identifier_format | ||
Rails.configuration.hdm.delete(:saml) | ||
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