diff --git a/lib/code0/identities/identity_provider.rb b/lib/code0/identities/identity_provider.rb index 329ed58..3712e14 100644 --- a/lib/code0/identities/identity_provider.rb +++ b/lib/code0/identities/identity_provider.rb @@ -9,12 +9,12 @@ def initialize @providers = {} end - def add_provider(provider_type, config_loader) - add_named_provider provider_type, provider_type, config_loader + def add_provider(provider_type, config) + add_named_provider provider_type, provider_type, config end - def add_named_provider(provider_id, provider_type, config_loader) - provider = Identities::Provider.const_get(provider_type.capitalize).new(config_loader) + def add_named_provider(provider_id, provider_type, config) + provider = Identities::Provider.const_get(provider_type.capitalize).new(config) providers[provider_id] = provider end diff --git a/lib/code0/identities/provider/base_oauth.rb b/lib/code0/identities/provider/base_oauth.rb index e2681e8..347411a 100644 --- a/lib/code0/identities/provider/base_oauth.rb +++ b/lib/code0/identities/provider/base_oauth.rb @@ -66,6 +66,13 @@ def check_response(response) def create_identity(*) raise NotImplementedError end + + def config + if config_loader.is_a?(Proc) + return config_loader.call + end + config_loader + end end end end diff --git a/lib/code0/identities/provider/discord.rb b/lib/code0/identities/provider/discord.rb index c535828..371d039 100644 --- a/lib/code0/identities/provider/discord.rb +++ b/lib/code0/identities/provider/discord.rb @@ -9,7 +9,6 @@ def token_url end def token_payload(code) - config = config_loader.call { code: code, grant_type: "authorization_code", redirect_uri: config[:redirect_uri], @@ -22,7 +21,6 @@ def user_details_url end def authorization_url - config = config_loader.call "https://discord.com/oauth2/authorize?client_id=#{config[:client_id]}&response_type=code&redirect_uri=#{URI.encode_uri_component(config[:redirect_uri])}&scope=identify+openid+email" end diff --git a/lib/code0/identities/provider/github.rb b/lib/code0/identities/provider/github.rb index 057f165..efc97dc 100644 --- a/lib/code0/identities/provider/github.rb +++ b/lib/code0/identities/provider/github.rb @@ -9,7 +9,6 @@ def token_url end def token_payload(code) - config = config_loader.call { code: code, redirect_uri: config[:redirect_uri], client_id: config[:client_id], @@ -21,7 +20,6 @@ def user_details_url end def authorization_url - config = config_loader.call "https://github.com/login/oauth/authorize?client_id=#{config[:client_id]}&redirect_uri=#{URI.encode_uri_component(config[:redirect_uri])}&scope=read:user+user:email" end diff --git a/lib/code0/identities/provider/gitlab.rb b/lib/code0/identities/provider/gitlab.rb index ca37920..12526e3 100644 --- a/lib/code0/identities/provider/gitlab.rb +++ b/lib/code0/identities/provider/gitlab.rb @@ -5,7 +5,6 @@ module Identities module Provider class Gitlab < BaseOauth def base_url - config = config_loader.call config[:base_url] end @@ -14,7 +13,6 @@ def token_url end def token_payload(code) - config = config_loader.call { code: code, grant_type: "authorization_code", redirect_uri: config[:redirect_uri], @@ -27,7 +25,6 @@ def user_details_url end def authorization_url - config = config_loader.call # rubocop:disable Layout/LineLength base_url + "/oauth/authorize?client_id=#{config[:client_id]}&response_type=code&redirect_uri=#{URI.encode_uri_component(config[:redirect_uri])}&scope=read_user" # rubocop:enable Layout/LineLength diff --git a/lib/code0/identities/provider/google.rb b/lib/code0/identities/provider/google.rb index 82247e7..378ab4d 100644 --- a/lib/code0/identities/provider/google.rb +++ b/lib/code0/identities/provider/google.rb @@ -13,7 +13,6 @@ def token_url end def token_payload(code) - config = config_loader.call { code: code, grant_type: "authorization_code", @@ -28,7 +27,6 @@ def user_details_url end def authorization_url - config = config_loader.call # rubocop:disable Layout/LineLength base_url + "/o/oauth2/v2/auth?client_id=#{config[:client_id]}&response_type=code&redirect_uri=#{URI.encode_www_form_component(config[:redirect_uri])}&scope=openid%20email%20profile" # rubocop:enable Layout/LineLength diff --git a/lib/code0/identities/provider/microsoft.rb b/lib/code0/identities/provider/microsoft.rb index 348329d..fdf3466 100644 --- a/lib/code0/identities/provider/microsoft.rb +++ b/lib/code0/identities/provider/microsoft.rb @@ -13,7 +13,6 @@ def token_url end def token_payload(code) - config = config_loader.call { code: code, grant_type: "authorization_code", redirect_uri: config[:redirect_uri], @@ -26,7 +25,6 @@ def user_details_url end def authorization_url - config = config_loader.call "https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize?client_id=#{config[:client_id]}&response_type=code&redirect_uri=#{config[:redirect_uri]}&response_mode=query&scope=email%20profile%20openid" end diff --git a/spec/code0/identities/identity_provider_spec.rb b/spec/code0/identities/identity_provider_spec.rb index d92ab9b..93025e9 100644 --- a/spec/code0/identities/identity_provider_spec.rb +++ b/spec/code0/identities/identity_provider_spec.rb @@ -7,14 +7,14 @@ describe "#add_provider" do it "adds the correct class" do - instance.add_provider :google, -> {} + instance.add_provider :google, {} expect(instance.providers).to match(google: an_instance_of(Code0::Identities::Provider::Google)) end end describe "#load_identity" do it "calls the right provider" do - provider = Code0::Identities::Provider::Google.new(-> {}) + provider = Code0::Identities::Provider::Google.new({}) allow(provider).to receive(:load_identity) instance.providers[:google] = provider instance.load_identity(:google, { test: 1 }) diff --git a/spec/code0/identities/provider/discord_spec.rb b/spec/code0/identities/provider/discord_spec.rb index 5a85ff9..7d84452 100644 --- a/spec/code0/identities/provider/discord_spec.rb +++ b/spec/code0/identities/provider/discord_spec.rb @@ -2,13 +2,11 @@ RSpec.describe Code0::Identities::Provider::Discord do subject(:service_response) do - described_class.new(lambda { - { + described_class.new({ redirect_uri: redirect_uri, client_id: client_id, client_secret: client_secret - } - }).load_identity(code: code) + }).load_identity(code: code) end let(:redirect_uri) { SecureRandom.hex } @@ -34,7 +32,7 @@ end end - context "when everything is valid" do + shared_examples "when everything is valid" do let(:access_token) { SecureRandom.hex } let(:response_body) { { id: 1, username: "name", email: "example@code0.tech" }.to_json } @@ -65,4 +63,21 @@ expect(service_response.email).to eq("example@code0.tech") end end + + context "when config is Proc" do + subject(:service_response) do + described_class.new(-> {{ + redirect_uri: redirect_uri, + client_id: client_id, + client_secret: client_secret + }}).load_identity(code: code) + end + it_behaves_like "when everything is valid" + end + + context 'when config is a hash' do + it_behaves_like "when everything is valid" + end + + end