kinde_sdk 1.2.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2edea62c5bda0b80177bcb0444a278e5deb6b6a99317f29f2e5d51033dd6a488
4
- data.tar.gz: 360a4ef5a4de05384ad6186960f22c3955839d63edcc7df6cb3f76f66428fdb0
3
+ metadata.gz: 4813caa0071e90cbe5821db9c0f8c553d2affbd62a1d64071e241957591bb7cb
4
+ data.tar.gz: 1802d0a2c465d51d358dcbdb497288eeedb2181ffe9ebf8e22877b332830b57d
5
5
  SHA512:
6
- metadata.gz: 6b69c73af4c5ca4c291e54cba3c9284d74d2aaab8024c27c6d09b7f40ac859cf3004dabf362dd5839acf46d2bfab25f9fcd4a67377ea52afb948e619b6827eec
7
- data.tar.gz: b9b249b70f9983335153b5d6ba6ab8187588c4a9da240e97fb6e1f17076b37d600d16a7ea61c9693adf419d80cdbdfa671f867b8a8d94dab9331c6925ef564e0
6
+ metadata.gz: fc3e8f2567082887624aabc0b407622f6296ac2495afe43befbc4608de1387ce050c67befe82044b9c8f802e8368a744613d324712b10906e6f99e1072592a29
7
+ data.tar.gz: f6e9e510340d215d26877d20ac1e25ac2d34496738a7dbd711882b64c91ad57c90f0afb952abcb7112253fa9b34b9a4f67fbc32e931ca42a46940f374008faab
data/README.md CHANGED
@@ -65,6 +65,7 @@ KindeSdk.configure do |c|
65
65
  # c.authorize_url = '/oauth2/auth' # default value
66
66
  # c.token_url = '/oauth2/token' # default value
67
67
  # c.debugging = false # default value
68
+ # c.auto_refresh_tokens = true # default value
68
69
  c.logger = Rails.logger
69
70
  end
70
71
  ```
@@ -84,6 +85,8 @@ defined in allowed logout urls of your kinde organization's application config
84
85
  - `Debugging` set to true start writing verbose request logs. Might be useful while developing your application.
85
86
  - `Logger` might be set to any kind of loggers you are using. By default it is set to `Rails.logger` if gem is used in
86
87
  rails application or `Logger.new(STDOUT)` if it is not a rails app.
88
+ - `auto_refresh_tokens` defines default behaviour on api instance method calls. If the config set to false, there will not be any auto refreshes during method calling,
89
+ otherwise each time client will try to refresh expired tokens if `expires_at` are present (see [token expiration and refreshing](#token-expiration-and-refreshing) section).
87
90
 
88
91
  These variables can be handled with any system you want: .env files, settings.yml or any type of config files.
89
92
  For example, .env file (you can name variables by yourself):
@@ -181,6 +184,13 @@ client.refresh_token # => {"access_token" => "qwe...", ...., "expires_at"=>16854
181
184
  If you are calling `#refresh_token` on a client instance, the instance token data will be automatically updated.
182
185
  If you are calling `KindeSdk#refresh_token`, you'll need to store new token data in your configured storage (redis/session/etc).
183
186
 
187
+ **Warning!**
188
+ Each instance_api method checking tokens for expiration if expires_at present in a hash.
189
+ So, if in your backend code you are using some storage, be sure you are saving `client.tokens_hash` after each instance
190
+ method calling, otherwise you will keep in your storage (session/redis/etc.) old data and unable to fetch new tokens.
191
+
192
+ If you don't want auto refreshing behavior, set `auto_refresh_tokens` config to false.
193
+
184
194
  #### Audience
185
195
  An `audience` is the intended recipient of an access token - for example the API for your application.
186
196
  The audience argument can be passed to the Kinde `#auth_url` method to request an audience be added to the provided token:
@@ -5,10 +5,11 @@ module KindeSdk
5
5
  include FeatureFlags
6
6
  include Permissions
7
7
 
8
- attr_accessor :kinde_api_client, :bearer_token, :tokens_hash, :expires_at
8
+ attr_accessor :kinde_api_client, :auto_refresh_tokens, :bearer_token, :tokens_hash, :expires_at
9
9
 
10
- def initialize(sdk_api_client, tokens_hash)
10
+ def initialize(sdk_api_client, tokens_hash, auto_refresh_tokens)
11
11
  @kinde_api_client = sdk_api_client
12
+ @auto_refresh_tokens = auto_refresh_tokens
12
13
  set_hash_related_data(tokens_hash)
13
14
  end
14
15
 
@@ -49,12 +50,23 @@ module KindeSdk
49
50
 
50
51
  def set_hash_related_data(tokens_hash)
51
52
  @tokens_hash = tokens_hash.transform_keys(&:to_sym)
52
- @bearer_token = tokens_hash[:access_token]
53
- @expires_at = tokens_hash[:expires_at]
53
+ @bearer_token = @tokens_hash[:access_token]
54
+ @expires_at = @tokens_hash[:expires_at]
54
55
  end
55
56
 
57
+ # going from another side: prepending each api_client's public method to check token for expiration
56
58
  def init_instance_api(api_klass)
57
- api_klass.new(kinde_api_client)
59
+ instance = api_klass.new(kinde_api_client)
60
+ main_client = self
61
+ methods_to_prepend = instance.public_methods(false).reject { |m| m.to_s.start_with?("api_client") }
62
+ methods_to_prepend.each do |method_name|
63
+ original = instance.method(method_name)
64
+ instance.define_singleton_method(method_name) do |*args, &block|
65
+ main_client.refresh_token if main_client.auto_refresh_tokens && main_client.token_expired?
66
+ original.call(*args, &block)
67
+ end
68
+ end
69
+ instance
58
70
  end
59
71
  end
60
72
  end
@@ -14,6 +14,7 @@ module KindeSdk
14
14
  attr_accessor :debugging
15
15
  attr_accessor :oauth_client
16
16
  attr_accessor :pkce_enabled
17
+ attr_accessor :auto_refresh_tokens
17
18
 
18
19
  def initialize
19
20
  @authorize_url = '/oauth2/auth'
@@ -22,6 +23,7 @@ module KindeSdk
22
23
  @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
23
24
  @scope = 'openid offline email profile'
24
25
  @pkce_enabled = true
26
+ @auto_refresh_tokens = true
25
27
 
26
28
  yield(self) if block_given?
27
29
  end
@@ -1,3 +1,3 @@
1
1
  module KindeSdk
2
- VERSION = "1.2.0"
2
+ VERSION = "1.2.1"
3
3
  end
data/lib/kinde_sdk.rb CHANGED
@@ -67,7 +67,7 @@ module KindeSdk
67
67
  # @return [KindeSdk::Client]
68
68
  def client(tokens_hash)
69
69
  sdk_api_client = api_client(tokens_hash["access_token"])
70
- KindeSdk::Client.new(sdk_api_client, tokens_hash)
70
+ KindeSdk::Client.new(sdk_api_client, tokens_hash, @config.auto_refresh_tokens)
71
71
  end
72
72
 
73
73
  def logout_url
@@ -6,6 +6,7 @@ describe KindeSdk do
6
6
  let(:client_secret) { "client_secret" }
7
7
  let(:callback_url) { "http://localhost:3000/callback" }
8
8
  let(:logout_url) { "http://localhost/logout-callback" }
9
+ let(:auto_refresh_tokens) { true }
9
10
 
10
11
  before do
11
12
  KindeSdk.configure do |c|
@@ -14,6 +15,7 @@ describe KindeSdk do
14
15
  c.client_secret = client_secret
15
16
  c.callback_url = callback_url
16
17
  c.logout_url = logout_url
18
+ c.auto_refresh_tokens = auto_refresh_tokens
17
19
  end
18
20
  end
19
21
 
@@ -227,6 +229,11 @@ describe KindeSdk do
227
229
  end
228
230
 
229
231
  describe "api instances" do
232
+ before do
233
+ stub_request(:get, "#{domain}/oauth2/user_profile")
234
+ # allow(client.oauth).to receive(:get_user_with_http_info).and_return(["data", 200, {}])
235
+ end
236
+
230
237
  it 'initializes client by passing the tokens_hash' do
231
238
  expect(client).to be_instance_of(KindeSdk::Client)
232
239
  end
@@ -242,6 +249,29 @@ describe KindeSdk do
242
249
  it "initializes feature flags instance api" do
243
250
  expect(client.feature_flags).to be_instance_of(KindeApi::FeatureFlagsApi)
244
251
  end
252
+
253
+ it "does not call for refresh tokens" do
254
+ expect(client).not_to receive(:refresh_token)
255
+ client.oauth.get_user({})
256
+ end
257
+
258
+ context "when token expired" do
259
+ let(:expires_at) { Time.now.to_i - 1 }
260
+
261
+ it "calls refresh_tokens before method if token expired" do
262
+ expect(client).to receive(:refresh_token).at_least(:once)
263
+ client.oauth.get_user({})
264
+ end
265
+
266
+ context "when auto_refresh_tokens disabled" do
267
+ let(:auto_refresh_tokens) { false }
268
+
269
+ it "does not call for refresh tokens" do
270
+ expect(client).not_to receive(:refresh_token)
271
+ client.oauth.get_user({})
272
+ end
273
+ end
274
+ end
245
275
  end
246
276
  end
247
277
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kinde_sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kinde Australia Pty Ltd
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-07 00:00:00.000000000 Z
11
+ date: 2023-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typhoeus