Skip to content

Commit 37dc109

Browse files
authored
Merge pull request #4 from code0-tech/3-support-objects-too-instead-of-only-lamda-functions
add a check to also allow hashes instead of only lambda blocks
2 parents 4e5374f + a93edf5 commit 37dc109

File tree

10 files changed

+61
-29
lines changed

10 files changed

+61
-29
lines changed

README.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ require "code0/identities"
3131
begin
3232

3333
identity = Code0::Identities::Provider::Discord.new(
34-
-> {
35-
redirect_uri : "http://localhost:8080/redirect",
36-
client_id : "id"
37-
client_secret : "xxxx"
34+
{
35+
redirect_uri: "http://localhost:8080/redirect",
36+
client_id: "id",
37+
client_secret: "xxxx"
3838
}).load_identity({ code: "a_valid_code" })
3939

4040
rescue Code0::Error => e
@@ -70,4 +70,25 @@ identity_provider.load_identity(:gitlab, params)
7070

7171
identity_provider.load_identity(:my_custom_gitlab_provider, params)
7272

73+
```
74+
75+
We also support passing in a function as a configuration instead of a hash
76+
77+
```ruby
78+
79+
def get_identity
80+
provider = Code0::Identities::Provider::Discord.new(-> { fetch_configuration })
81+
82+
provider.load_identity(params)
83+
end
84+
85+
def fetch_configuration
86+
# Do some database action, to dynamicly load the configuration
87+
{
88+
redirect_uri: "http://localhost:8080/redirect",
89+
client_id: "some dynamic value",
90+
client_secret: "xxxx"
91+
}
92+
end
93+
7394
```

lib/code0/identities/identity_provider.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ def initialize
99
@providers = {}
1010
end
1111

12-
def add_provider(provider_type, config_loader)
13-
add_named_provider provider_type, provider_type, config_loader
12+
def add_provider(provider_type, config)
13+
add_named_provider provider_type, provider_type, config
1414
end
1515

16-
def add_named_provider(provider_id, provider_type, config_loader)
17-
provider = Identities::Provider.const_get(provider_type.capitalize).new(config_loader)
16+
def add_named_provider(provider_id, provider_type, config)
17+
provider = Identities::Provider.const_get(provider_type.capitalize).new(config)
1818
providers[provider_id] = provider
1919
end
2020

lib/code0/identities/provider/base_oauth.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ def check_response(response)
6666
def create_identity(*)
6767
raise NotImplementedError
6868
end
69+
70+
def config
71+
return config_loader.call if config_loader.is_a?(Proc)
72+
73+
config_loader
74+
end
6975
end
7076
end
7177
end

lib/code0/identities/provider/discord.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ def token_url
99
end
1010

1111
def token_payload(code)
12-
config = config_loader.call
1312
{ code: code,
1413
grant_type: "authorization_code",
1514
redirect_uri: config[:redirect_uri],
@@ -22,7 +21,6 @@ def user_details_url
2221
end
2322

2423
def authorization_url
25-
config = config_loader.call
2624
"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"
2725
end
2826

lib/code0/identities/provider/github.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ def token_url
99
end
1010

1111
def token_payload(code)
12-
config = config_loader.call
1312
{ code: code,
1413
redirect_uri: config[:redirect_uri],
1514
client_id: config[:client_id],
@@ -21,7 +20,6 @@ def user_details_url
2120
end
2221

2322
def authorization_url
24-
config = config_loader.call
2523
"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"
2624
end
2725

lib/code0/identities/provider/gitlab.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ module Identities
55
module Provider
66
class Gitlab < BaseOauth
77
def base_url
8-
config = config_loader.call
98
config[:base_url]
109
end
1110

@@ -14,7 +13,6 @@ def token_url
1413
end
1514

1615
def token_payload(code)
17-
config = config_loader.call
1816
{ code: code,
1917
grant_type: "authorization_code",
2018
redirect_uri: config[:redirect_uri],
@@ -27,7 +25,6 @@ def user_details_url
2725
end
2826

2927
def authorization_url
30-
config = config_loader.call
3128
# rubocop:disable Layout/LineLength
3229
base_url + "/oauth/authorize?client_id=#{config[:client_id]}&response_type=code&redirect_uri=#{URI.encode_uri_component(config[:redirect_uri])}&scope=read_user"
3330
# rubocop:enable Layout/LineLength

lib/code0/identities/provider/google.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ def token_url
1313
end
1414

1515
def token_payload(code)
16-
config = config_loader.call
1716
{
1817
code: code,
1918
grant_type: "authorization_code",
@@ -28,7 +27,6 @@ def user_details_url
2827
end
2928

3029
def authorization_url
31-
config = config_loader.call
3230
# rubocop:disable Layout/LineLength
3331
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"
3432
# rubocop:enable Layout/LineLength

lib/code0/identities/provider/microsoft.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ def token_url
1313
end
1414

1515
def token_payload(code)
16-
config = config_loader.call
1716
{ code: code,
1817
grant_type: "authorization_code",
1918
redirect_uri: config[:redirect_uri],
@@ -26,7 +25,6 @@ def user_details_url
2625
end
2726

2827
def authorization_url
29-
config = config_loader.call
3028
"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"
3129
end
3230

spec/code0/identities/identity_provider_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

88
describe "#add_provider" do
99
it "adds the correct class" do
10-
instance.add_provider :google, -> {}
10+
instance.add_provider :google, {}
1111
expect(instance.providers).to match(google: an_instance_of(Code0::Identities::Provider::Google))
1212
end
1313
end
1414

1515
describe "#load_identity" do
1616
it "calls the right provider" do
17-
provider = Code0::Identities::Provider::Google.new(-> {})
17+
provider = Code0::Identities::Provider::Google.new({})
1818
allow(provider).to receive(:load_identity)
1919
instance.providers[:google] = provider
2020
instance.load_identity(:google, { test: 1 })

spec/code0/identities/provider/discord_spec.rb

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
RSpec.describe Code0::Identities::Provider::Discord do
44
subject(:service_response) do
5-
described_class.new(lambda {
6-
{
7-
redirect_uri: redirect_uri,
8-
client_id: client_id,
9-
client_secret: client_secret
10-
}
11-
}).load_identity(code: code)
5+
described_class.new({
6+
redirect_uri: redirect_uri,
7+
client_id: client_id,
8+
client_secret: client_secret
9+
}).load_identity(code: code)
1210
end
1311

1412
let(:redirect_uri) { SecureRandom.hex }
@@ -34,7 +32,7 @@
3432
end
3533
end
3634

37-
context "when everything is valid" do
35+
shared_examples "when everything is valid" do
3836
let(:access_token) { SecureRandom.hex }
3937
let(:response_body) { { id: 1, username: "name", email: "example@code0.tech" }.to_json }
4038

@@ -65,4 +63,22 @@
6563
expect(service_response.email).to eq("example@code0.tech")
6664
end
6765
end
66+
67+
context "when config is Proc" do
68+
subject(:service_response) do
69+
described_class.new(lambda {
70+
{
71+
redirect_uri: redirect_uri,
72+
client_id: client_id,
73+
client_secret: client_secret
74+
}
75+
}).load_identity(code: code)
76+
end
77+
78+
it_behaves_like "when everything is valid"
79+
end
80+
81+
context "when config is a hash" do
82+
it_behaves_like "when everything is valid"
83+
end
6884
end

0 commit comments

Comments
 (0)