|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Code0 |
| 4 | + module Identities |
| 5 | + module Provider |
| 6 | + class Saml |
| 7 | + attr_reader :config_loader |
| 8 | + |
| 9 | + def initialize(config_loader) |
| 10 | + @config_loader = config_loader |
| 11 | + end |
| 12 | + |
| 13 | + def authorization_url |
| 14 | + request = OneLogin::RubySaml::Authrequest.new |
| 15 | + request.create(create_settings) |
| 16 | + |
| 17 | + request.instance_variable_get :@login_url |
| 18 | + end |
| 19 | + |
| 20 | + def load_identity(**params) |
| 21 | + response = OneLogin::RubySaml::Response.new(params[:SAMLResponse], |
| 22 | + { **config[:response_settings], settings: create_settings }) |
| 23 | + attributes = response.attributes |
| 24 | + |
| 25 | + Identity.new(config[:provider_name], |
| 26 | + response.name_id, |
| 27 | + find_attribute(attributes, config[:attribute_statements][:username]), |
| 28 | + find_attribute(attributes, config[:attribute_statements][:email]), |
| 29 | + find_attribute(attributes, config[:attribute_statements][:firstname]), |
| 30 | + find_attribute(attributes, config[:attribute_statements][:lastname])) |
| 31 | + end |
| 32 | + |
| 33 | + private |
| 34 | + |
| 35 | + def find_attribute(attributes, attribute_statements) |
| 36 | + attribute_statements.each do |statement| |
| 37 | + return attributes[statement] unless attributes[statement].nil? |
| 38 | + end |
| 39 | + nil |
| 40 | + end |
| 41 | + |
| 42 | + def create_settings |
| 43 | + if config[:metadata_url].nil? |
| 44 | + settings = OneLogin::RubySaml::Settings.new |
| 45 | + else |
| 46 | + idp_metadata_parser = OneLogin::RubySaml::IdpMetadataParser.new |
| 47 | + settings = idp_metadata_parser.parse_remote(config[:metadata_url]) |
| 48 | + end |
| 49 | + |
| 50 | + settings.name_identifier_format = "urn:oasis:names:tc:SAML:2.0:nameid-format:persistent" |
| 51 | + |
| 52 | + config[:settings].each do |key, value| |
| 53 | + settings.send(:"#{key}=", value) |
| 54 | + end |
| 55 | + settings |
| 56 | + end |
| 57 | + |
| 58 | + def config |
| 59 | + config = config_loader |
| 60 | + config = config_loader.call if config_loader.is_a?(Proc) |
| 61 | + |
| 62 | + # rubocop:disable Layout/LineLength |
| 63 | + config[:provider_name] ||= :saml |
| 64 | + config[:response_settings] ||= {} |
| 65 | + config[:settings] ||= {} |
| 66 | + config[:attribute_statements] ||= {} |
| 67 | + config[:attribute_statements][:username] ||= %w[username name http://schemas.goauthentik.io/2021/02/saml/username] |
| 68 | + config[:attribute_statements][:email] ||= %w[email mail http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress http://schemas.microsoft.com/ws/2008/06/identity/claims/emailaddress] |
| 69 | + config[:attribute_statements][:firstname] ||= %w[first_name firstname firstName http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname http://schemas.microsoft.com/ws/2008/06/identity/claims/givenname] |
| 70 | + config[:attribute_statements][:lastname] ||= %w[last_name lastname lastName http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname http://schemas.microsoft.com/ws/2008/06/identity/claims/surname] |
| 71 | + # rubocop:enable Layout/LineLength |
| 72 | + |
| 73 | + config |
| 74 | + end |
| 75 | + end |
| 76 | + end |
| 77 | + end |
| 78 | +end |
0 commit comments