Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a check to also allow hashes instead of only lambda blocks #4

Merged
merged 6 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Knerio marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -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)
Knerio marked this conversation as resolved.
Show resolved Hide resolved

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"
}
Knerio marked this conversation as resolved.
Show resolved Hide resolved
end

```
8 changes: 4 additions & 4 deletions lib/code0/identities/identity_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions lib/code0/identities/provider/base_oauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ def check_response(response)
def create_identity(*)
raise NotImplementedError
end

def config
return config_loader.call if config_loader.is_a?(Proc)

config_loader
end
end
end
end
Expand Down
2 changes: 0 additions & 2 deletions lib/code0/identities/provider/discord.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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

Expand Down
2 changes: 0 additions & 2 deletions lib/code0/identities/provider/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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

Expand Down
3 changes: 0 additions & 3 deletions lib/code0/identities/provider/gitlab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module Identities
module Provider
class Gitlab < BaseOauth
def base_url
config = config_loader.call
config[:base_url]
end

Expand All @@ -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],
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions lib/code0/identities/provider/google.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def token_url
end

def token_payload(code)
config = config_loader.call
{
code: code,
grant_type: "authorization_code",
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions lib/code0/identities/provider/microsoft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions spec/code0/identities/identity_provider_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
32 changes: 24 additions & 8 deletions spec/code0/identities/provider/discord_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

RSpec.describe Code0::Identities::Provider::Discord do
subject(:service_response) do
described_class.new(lambda {
{
redirect_uri: redirect_uri,
client_id: client_id,
client_secret: client_secret
}
}).load_identity(code: code)
described_class.new({
redirect_uri: redirect_uri,
client_id: client_id,
client_secret: client_secret
}).load_identity(code: code)
end

let(:redirect_uri) { SecureRandom.hex }
Expand All @@ -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: "[email protected]" }.to_json }

Expand Down Expand Up @@ -65,4 +63,22 @@
expect(service_response.email).to eq("[email protected]")
end
end

context "when config is Proc" do
subject(:service_response) do
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
it_behaves_like "when everything is valid"
end
end