From aedbb4c32947c9e6a50a9ea28132da21d24ba383 Mon Sep 17 00:00:00 2001 From: Dario P Date: Sun, 25 Aug 2024 14:41:43 +0200 Subject: [PATCH 1/6] add a check to also allow hashes instead of lambda blocks --- lib/code0/identities/identity_provider.rb | 8 +++--- lib/code0/identities/provider/base_oauth.rb | 7 ++++++ lib/code0/identities/provider/discord.rb | 2 -- lib/code0/identities/provider/github.rb | 2 -- lib/code0/identities/provider/gitlab.rb | 3 --- lib/code0/identities/provider/google.rb | 2 -- lib/code0/identities/provider/microsoft.rb | 2 -- .../identities/identity_provider_spec.rb | 4 +-- .../code0/identities/provider/discord_spec.rb | 25 +++++++++++++++---- 9 files changed, 33 insertions(+), 22 deletions(-) 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 From 15c9c5644b55ef0a65bcc496b850086b2c8c4a5b Mon Sep 17 00:00:00 2001 From: Dario P Date: Sun, 25 Aug 2024 14:47:18 +0200 Subject: [PATCH 2/6] update readme to support functions and hashes instead of only functions --- README.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f755b56..6ed7df2 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ require "code0/identities" begin identity = Code0::Identities::Provider::Discord.new( - -> { + { redirect_uri : "http://localhost:8080/redirect", client_id : "id" client_secret : "xxxx" @@ -70,4 +70,25 @@ identity_provider.load_identity(:gitlab, params) identity_provider.load_identity(:my_custom_gitlab_provider, params) +``` + +We also support passing in a function as a configuration instead of a hash + +```ruby + +def get_identity + provider = Code0::Identities::Provider::Discord.new(fetch_configuration) + + provider.load_identity(params) +end + +def fetch_configuration + # Do some database action, to dynamicly load the configuration + # { + redirect_uri : "http://localhost:8080/redirect", + client_id : "some dynamic value" + client_secret : "xxxx" + } +end + ``` \ No newline at end of file From b9e7a6c893811d7c7046b491c60782599b58b21f Mon Sep 17 00:00:00 2001 From: Dario P Date: Sun, 25 Aug 2024 14:48:36 +0200 Subject: [PATCH 3/6] fix rubocop vulnerabilities --- lib/code0/identities/provider/base_oauth.rb | 5 ++-- .../code0/identities/provider/discord_spec.rb | 25 ++++++++++--------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/code0/identities/provider/base_oauth.rb b/lib/code0/identities/provider/base_oauth.rb index 347411a..b02eb16 100644 --- a/lib/code0/identities/provider/base_oauth.rb +++ b/lib/code0/identities/provider/base_oauth.rb @@ -68,9 +68,8 @@ def create_identity(*) end def config - if config_loader.is_a?(Proc) - return config_loader.call - end + return config_loader.call if config_loader.is_a?(Proc) + config_loader end end diff --git a/spec/code0/identities/provider/discord_spec.rb b/spec/code0/identities/provider/discord_spec.rb index 7d84452..604c890 100644 --- a/spec/code0/identities/provider/discord_spec.rb +++ b/spec/code0/identities/provider/discord_spec.rb @@ -3,10 +3,10 @@ RSpec.describe Code0::Identities::Provider::Discord do subject(:service_response) do described_class.new({ - redirect_uri: redirect_uri, - client_id: client_id, - client_secret: client_secret - }).load_identity(code: code) + redirect_uri: redirect_uri, + client_id: client_id, + client_secret: client_secret + }).load_identity(code: code) end let(:redirect_uri) { SecureRandom.hex } @@ -66,18 +66,19 @@ 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) + described_class.new(lambda { + { + 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 + context "when config is a hash" do it_behaves_like "when everything is valid" end - - end From f87548550e798daadbf90dc97c975636549701c0 Mon Sep 17 00:00:00 2001 From: Dario Pranjic <96529060+Knerio@users.noreply.github.com> Date: Sun, 25 Aug 2024 15:24:33 +0200 Subject: [PATCH 4/6] Update README.md Co-authored-by: Niklas van Schrick --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6ed7df2..2bf5fc9 100644 --- a/README.md +++ b/README.md @@ -84,10 +84,10 @@ end def fetch_configuration # Do some database action, to dynamicly load the configuration - # { - redirect_uri : "http://localhost:8080/redirect", - client_id : "some dynamic value" - client_secret : "xxxx" + { + redirect_uri: "http://localhost:8080/redirect", + client_id: "some dynamic value", + client_secret: "xxxx" } end From 2eb6cba1020b50eafbee1b2ab72055c6f75a2dd7 Mon Sep 17 00:00:00 2001 From: Dario Pranjic <96529060+Knerio@users.noreply.github.com> Date: Sun, 25 Aug 2024 15:24:41 +0200 Subject: [PATCH 5/6] Update README.md Co-authored-by: Niklas van Schrick --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2bf5fc9..531a1ff 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ We also support passing in a function as a configuration instead of a hash ```ruby def get_identity - provider = Code0::Identities::Provider::Discord.new(fetch_configuration) + provider = Code0::Identities::Provider::Discord.new(-> { fetch_configuration }) provider.load_identity(params) end From a93edf5f6b975a35909f7b6bf781667cf106c8df Mon Sep 17 00:00:00 2001 From: Dario Pranjic <96529060+Knerio@users.noreply.github.com> Date: Sun, 25 Aug 2024 15:24:48 +0200 Subject: [PATCH 6/6] Update README.md Co-authored-by: Niklas van Schrick --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 531a1ff..0b26ae4 100644 --- a/README.md +++ b/README.md @@ -32,9 +32,9 @@ begin identity = Code0::Identities::Provider::Discord.new( { - redirect_uri : "http://localhost:8080/redirect", - client_id : "id" - client_secret : "xxxx" + redirect_uri: "http://localhost:8080/redirect", + client_id: "id", + client_secret: "xxxx" }).load_identity({ code: "a_valid_code" }) rescue Code0::Error => e