2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-01-06 06:28:46 -06:00
|
|
|
class DiscourseConnectProvider < DiscourseConnectBase
|
2019-07-26 09:37:23 -05:00
|
|
|
class BlankSecret < RuntimeError; end
|
2018-12-19 03:22:10 -06:00
|
|
|
|
|
|
|
def self.parse(payload, sso_secret = nil)
|
|
|
|
set_return_sso_url(payload)
|
2019-07-26 09:37:23 -05:00
|
|
|
if sso_secret.blank? && self.sso_secret.blank?
|
|
|
|
host = URI.parse(@return_sso_url).host
|
2021-02-08 04:04:33 -06:00
|
|
|
Rails.logger.warn("SSO failed; website #{host} is not in the `discourse_connect_provider_secrets` site settings")
|
2019-07-26 09:37:23 -05:00
|
|
|
raise BlankSecret
|
|
|
|
end
|
2018-12-19 03:22:10 -06:00
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.set_return_sso_url(payload)
|
|
|
|
parsed = Rack::Utils.parse_query(payload)
|
|
|
|
decoded = Base64.decode64(parsed["sso"])
|
|
|
|
decoded_hash = Rack::Utils.parse_query(decoded)
|
|
|
|
|
2020-05-12 19:11:22 -05:00
|
|
|
raise ParseError unless decoded_hash.key? 'return_sso_url'
|
2018-12-19 03:22:10 -06:00
|
|
|
@return_sso_url = decoded_hash['return_sso_url']
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.sso_secret
|
2021-02-08 04:04:33 -06:00
|
|
|
return nil unless @return_sso_url && SiteSetting.enable_discourse_connect_provider
|
2018-12-19 03:22:10 -06:00
|
|
|
|
2021-02-08 04:04:33 -06:00
|
|
|
provider_secrets = SiteSetting.discourse_connect_provider_secrets.split(/[|\n]/)
|
2018-12-19 03:22:10 -06:00
|
|
|
provider_secrets_hash = Hash[*provider_secrets]
|
|
|
|
return_url_host = URI.parse(@return_sso_url).host
|
|
|
|
# moves wildcard domains to the end of hash
|
|
|
|
sorted_secrets = provider_secrets_hash.sort_by { |k, _| k }.reverse.to_h
|
|
|
|
|
|
|
|
secret = sorted_secrets.select do |domain, _|
|
|
|
|
WildcardDomainChecker.check_domain(domain, return_url_host)
|
|
|
|
end
|
|
|
|
secret.present? ? secret.values.first : nil
|
|
|
|
end
|
|
|
|
end
|