|  | 
|  | 1 | +# frozen_string_literal: true | 
|  | 2 | + | 
|  | 3 | +module Code0 | 
|  | 4 | +  module Identities | 
|  | 5 | +    module Provider | 
|  | 6 | +      class Oidc < BaseOauth | 
|  | 7 | +        def token_url | 
|  | 8 | +          config[:token_url] | 
|  | 9 | +        end | 
|  | 10 | + | 
|  | 11 | +        def token_payload(code) | 
|  | 12 | +          { code: code, | 
|  | 13 | +            grant_type: "authorization_code", | 
|  | 14 | +            redirect_uri: config[:redirect_uri], | 
|  | 15 | +            client_id: config[:client_id], | 
|  | 16 | +            client_secret: config[:client_secret] } | 
|  | 17 | +        end | 
|  | 18 | + | 
|  | 19 | +        def user_details_url | 
|  | 20 | +          config[:user_details_url] | 
|  | 21 | +        end | 
|  | 22 | + | 
|  | 23 | +        def authorization_url | 
|  | 24 | +          config[:user_details_url] | 
|  | 25 | +        end | 
|  | 26 | + | 
|  | 27 | +        def create_identity(response, *) | 
|  | 28 | +          body = response.parsed_response | 
|  | 29 | + | 
|  | 30 | +          Identity.new(config[:provider_name], | 
|  | 31 | +                       find_attribute(body, config[:attribute_statements][:identifier]), | 
|  | 32 | +                       find_attribute(body, config[:attribute_statements][:username]), | 
|  | 33 | +                       find_attribute(body, config[:attribute_statements][:email]), | 
|  | 34 | +                       find_attribute(body, config[:attribute_statements][:firstname]), | 
|  | 35 | +                       find_attribute(body, config[:attribute_statements][:lastname])) | 
|  | 36 | +        end | 
|  | 37 | + | 
|  | 38 | +        def config | 
|  | 39 | +          config = super | 
|  | 40 | + | 
|  | 41 | +          # rubocop:disable Layout/LineLength | 
|  | 42 | +          config[:provider_name] ||= :oidc | 
|  | 43 | +          config[:attribute_statements] ||= {} | 
|  | 44 | +          config[:attribute_statements][:identifier] ||= %w[sub id identifier] | 
|  | 45 | +          config[:attribute_statements][:username] ||= %w[username name login] | 
|  | 46 | +          config[:attribute_statements][:email] ||= %w[email mail] | 
|  | 47 | +          config[:attribute_statements][:firstname] ||= %w[first_name firstname firstName givenname given_name givenName] | 
|  | 48 | +          config[:attribute_statements][:lastname] ||= %w[last_name lastname lastName family_name familyName familyname] | 
|  | 49 | +          # rubocop:enable Layout/LineLength | 
|  | 50 | + | 
|  | 51 | +          config | 
|  | 52 | +        end | 
|  | 53 | + | 
|  | 54 | +        def find_attribute(attributes, attribute_statements) | 
|  | 55 | +          attribute_statements.each do |statement| | 
|  | 56 | +            return attributes[statement] unless attributes[statement].nil? | 
|  | 57 | +          end | 
|  | 58 | +          nil | 
|  | 59 | +        end | 
|  | 60 | +      end | 
|  | 61 | +    end | 
|  | 62 | +  end | 
|  | 63 | +end | 
0 commit comments