Skip to content

Commit 3ac0ca7

Browse files
Merge pull request #34 from brettchaldecott/parameter_override_thread_safe
Support for multi business setup
2 parents b516ddb + 8f48822 commit 3ac0ca7

File tree

2 files changed

+64
-17
lines changed

2 files changed

+64
-17
lines changed

lib/kinde_sdk.rb

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,59 @@ def configure
2727
# receive url for authorization in Kinde itself
2828
#
2929
# @return [Hash]
30-
def auth_url(redirect_uri: @config.callback_url, **kwargs)
30+
def auth_url(
31+
client_id: @config.client_id,
32+
client_secret: @config.client_secret,
33+
domain: @config.domain,
34+
redirect_uri: @config.callback_url,
35+
**kwargs)
3136
params = {
3237
redirect_uri: redirect_uri,
3338
state: SecureRandom.hex,
3439
scope: @config.scope
3540
}.merge(**kwargs)
36-
return { url: @config.oauth_client.auth_code.authorize_url(params) } unless @config.pkce_enabled
41+
return { url: @config.oauth_client(
42+
client_id: client_id,
43+
client_secret: client_secret,
44+
domain: domain,
45+
authorize_url: "#{domain}/oauth2/auth",
46+
token_url: "#{domain}/oauth2/token").auth_code.authorize_url(params) } unless @config.pkce_enabled
3747

3848
pkce_challenge = PkceChallenge.challenge(char_length: 128)
3949
params.merge!(code_challenge_method: 'S256', code_challenge: pkce_challenge.code_challenge)
4050
{
41-
url: @config.oauth_client.auth_code.authorize_url(params),
51+
url: @config.oauth_client(
52+
client_id: client_id,
53+
client_secret: client_secret,
54+
domain: domain,
55+
authorize_url: "#{domain}/oauth2/auth",
56+
token_url: "#{domain}/oauth2/token").auth_code.authorize_url(params),
4257
code_verifier: pkce_challenge.code_verifier
4358
}
4459
end
4560

4661
# when callback processor receives code, it needs to be used for fetching bearer token
4762
#
4863
# @return [Hash]
49-
def fetch_tokens(params_or_code, code_verifier: nil, redirect_uri: @config.callback_url)
50-
code = params_or_code.kind_of?(Hash) ? params.fetch("code") : params_or_code
64+
def fetch_tokens(
65+
params_or_code,
66+
client_id: @config.client_id,
67+
client_secret: @config.client_secret,
68+
domain: @config.domain,
69+
code_verifier: nil,
70+
redirect_uri: @config.callback_url)
71+
code = params_or_code.kind_of?(Hash) ? params_or_code.fetch("code") : params_or_code
5172
params = {
5273
redirect_uri: redirect_uri,
5374
headers: { 'User-Agent' => "Kinde-SDK: Ruby/#{KindeSdk::VERSION}" }
5475
}
5576
params[:code_verifier] = code_verifier if code_verifier
56-
@config.oauth_client.auth_code.get_token(code.to_s, params).to_hash
77+
@config.oauth_client(
78+
client_id: client_id,
79+
client_secret: client_secret,
80+
domain: domain,
81+
authorize_url: "#{domain}/oauth2/auth",
82+
token_url: "#{domain}/oauth2/token").auth_code.get_token(code.to_s, params).to_hash
5783
end
5884

5985
# tokens_hash #=>
@@ -70,35 +96,56 @@ def client(tokens_hash)
7096
KindeSdk::Client.new(sdk_api_client, tokens_hash, @config.auto_refresh_tokens)
7197
end
7298

73-
def logout_url
74-
query = @config.logout_url ? URI.encode_www_form(redirect: @config.logout_url) : nil
75-
host = URI::parse(@config.domain).host
99+
def logout_url(logout_url: @config.logout_url, domain: @config.domain)
100+
query = logout_url ? URI.encode_www_form(redirect: logout_url) : nil
101+
host = URI::parse(domain).host
76102
URI::HTTP.build(host: host, path: '/logout', query: query).to_s
77103
end
78104

79105
def client_credentials_access(
80106
client_id: @config.client_id,
81107
client_secret: @config.client_secret,
82-
audience: "#{@config.domain}/api"
108+
audience: "#{@config.domain}/api",
109+
domain: @config.domain
83110
)
84-
Faraday.new(url: @config.domain) do |faraday|
111+
Faraday.new(url: domain) do |faraday|
85112
faraday.response :json
86113
faraday.use Faraday::FollowRedirects::Middleware
87114
end
88-
.post(@config.token_url) do |req|
115+
.post("#{domain}/oauth2/token") do |req|
89116
req.headers[:content_type] = 'application/x-www-form-urlencoded'
90117
req.body =
91118
"grant_type=client_credentials&client_id=#{client_id}&client_secret=#{client_secret}&audience=#{audience}"
92119
end.body
93120
end
94121

95-
def token_expired?(hash)
96-
OAuth2::AccessToken.from_hash(@config.oauth_client, hash).expired?
122+
def token_expired?(hash,
123+
client_id: @config.client_id,
124+
client_secret: @config.client_secret,
125+
audience: "#{@config.domain}/api",
126+
domain: @config.domain
127+
)
128+
OAuth2::AccessToken.from_hash(@config.oauth_client(
129+
client_id: client_id,
130+
client_secret: client_secret,
131+
domain: domain,
132+
authorize_url: "#{domain}/oauth2/auth",
133+
token_url: "#{domain}/oauth2/token"), hash).expired?
97134
end
98135

99136
# @return [Hash]
100-
def refresh_token(hash)
101-
OAuth2::AccessToken.from_hash(@config.oauth_client, hash).refresh.to_hash
137+
def refresh_token(hash,
138+
client_id: @config.client_id,
139+
client_secret: @config.client_secret,
140+
audience: "#{@config.domain}/api",
141+
domain: @config.domain
142+
)
143+
OAuth2::AccessToken.from_hash(@config.oauth_client(
144+
client_id: client_id,
145+
client_secret: client_secret,
146+
domain: domain,
147+
authorize_url: "#{domain}/oauth2/auth",
148+
token_url: "#{domain}/oauth2/token"), hash).refresh.to_hash
102149
end
103150

104151
# init sdk api client by bearer token

lib/kinde_sdk/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def configure
3636
yield(self) if block_given?
3737
end
3838

39-
def oauth_client
39+
def oauth_client(client_id: @client_id, client_secret: @client_secret, domain: @domain, authorize_url: @authorize_url, token_url: @token_url )
4040
::OAuth2::Client.new(
4141
client_id,
4242
client_secret,

0 commit comments

Comments
 (0)