-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/saml_integration'
- Loading branch information
Showing
21 changed files
with
387 additions
and
42 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
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,36 @@ | ||
class SamlIdpController < SamlIdp::IdpController | ||
layout false | ||
before_action :setup_saml_configuration | ||
|
||
private | ||
|
||
def idp_authenticate(email, password) | ||
User.find_and_check_user(email, password) ? User.find_active_user_by_email(email) : nil | ||
end | ||
|
||
def idp_make_saml_response(found_user) | ||
encode_response found_user | ||
end | ||
|
||
def idp_logout | ||
# user = User.by_email(saml_request.name_id) | ||
# user.logout | ||
end | ||
|
||
def setup_saml_configuration | ||
slug = params[:slug] | ||
org = Organisation.find_by_slug(slug) | ||
saml_url = "#{Figaro.env.gate_url}/#{slug}/saml" | ||
SamlIdp.configure do |config| | ||
config.x509_certificate = org.cert_key | ||
config.secret_key = org.cert_private_key | ||
config.organization_name = org.name | ||
config.organization_url = org.website | ||
config.base_saml_location = saml_url | ||
config.attribute_service_location = "#{saml_url}/attributes" | ||
config.single_service_post_location = "#{saml_url}/auth" | ||
config.single_logout_service_post_location = "#{saml_url}/logout" | ||
config.single_logout_service_redirect_location = "#{saml_url}/logout" | ||
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 |
---|---|---|
@@ -1,18 +1,71 @@ | ||
class Organisation < ActiveRecord::Base | ||
validates :name, :url, :email_domain, presence: true | ||
validates :name, :website, :domain, :country, :state, :address, | ||
:admin_email_address, :slug, presence: true | ||
validates :address, format: { | ||
with: /\A[a-zA-Z0-9\s]+\z/, | ||
message: 'Invalid - Only Alphabets, Space and Numbers Allowed', | ||
} | ||
validates :admin_email_address, email: true | ||
validates :slug, uniqueness: true | ||
|
||
attr_accessor :cert, :rsa_key | ||
|
||
UPDATE_KEYS = %w( | ||
name website domain country state address admin_email_address slug unit_name | ||
).freeze | ||
|
||
def self.find_by_slug(slug) | ||
Organisation.where(slug: slug).first | ||
end | ||
|
||
def self.setup(attrs = {}) | ||
attrs.stringify_keys! | ||
attrs = attrs.select { |k, _v| %w(name url email_domain).include?(k) } | ||
attrs = attrs.stringify_keys | ||
attrs = attrs.select { |k, _v| UPDATE_KEYS.include?(k) } | ||
org = Organisation.new(attrs) | ||
org.save if org.valid? | ||
org | ||
end | ||
|
||
def update_profile(attrs = {}) | ||
attrs.stringify_keys! | ||
attrs = attrs.select { |k, _v| %w(name url email_domain).include?(k) } | ||
attrs = attrs.stringify_keys | ||
attrs = attrs.select { |k, _v| UPDATE_KEYS.include?(k) } | ||
assign_attributes(attrs) | ||
save if valid? | ||
end | ||
|
||
def saml_setup? | ||
cert_fingerprint.present? && cert_key.present? && cert_private_key.present? | ||
end | ||
|
||
def setup_saml_certs | ||
return false unless persisted? | ||
require 'openssl' | ||
self.rsa_key = OpenSSL::PKey::RSA.new(2048) | ||
private_key = rsa_key.to_pem | ||
public_key = rsa_key.public_key | ||
subject = "/C=#{country}/ST=#{state}/L=#{address}/O=#{name}/OU=#{unit_name}/CN=#{domain}" | ||
self.cert = OpenSSL::X509::Certificate.new | ||
cert.subject = cert.issuer = OpenSSL::X509::Name.parse(subject) | ||
cert.not_before = Time.now | ||
cert.not_after = Time.now + 365 * 24 * 60 * 60 | ||
cert.public_key = public_key | ||
cert.serial = SecureRandom.random_number(10) | ||
cert.version = 2 | ||
ef = OpenSSL::X509::ExtensionFactory.new | ||
ef.subject_certificate = cert | ||
ef.issuer_certificate = cert | ||
cert.extensions = [ | ||
ef.create_extension('basicConstraints', 'CA:TRUE', true), | ||
ef.create_extension('subjectKeyIdentifier', 'hash'), | ||
] | ||
cert.add_extension ef.create_extension( | ||
'authorityKeyIdentifier', 'keyid:always,issuer:always' | ||
) | ||
cert.sign rsa_key, OpenSSL::Digest::SHA1.new | ||
update_attributes( | ||
cert_fingerprint: OpenSSL::Digest::SHA256.hexdigest(cert.to_der).scan(/../).join(':'), | ||
cert_key: cert.to_pem, | ||
cert_private_key: private_key | ||
) | ||
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,7 @@ | ||
class EmailValidator < ActiveModel::EachValidator | ||
def validate_each(record, attribute, value) | ||
unless value.to_s.match?(/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i) | ||
record.errors[attribute] << (options[:message] || 'is not an email') | ||
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
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.