any_auth 0.1.0 → 0.2.0

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: 64e8f1d4f67339c3ac209d8b86f0a184381b42a5eb672ba1b31919c791247c12
4
- data.tar.gz: 3497cbd74e6a5b41c506a76a2bdad2da90b11a852067b3b5309a26170ffa4097
3
+ metadata.gz: c0bcfb06c8504e1753a62da0f7e9ba347d0ef8ddc5021190a0ac4dce0f2b1dc5
4
+ data.tar.gz: 02244110ee2e09c11fe7a7cf7866cde762a7ff9aff9740ac1a11aac42380dbee
5
5
  SHA512:
6
- metadata.gz: 659eed9f561175a3f05151b7a27bfc00f436fbfd6420a8fb81de5be16c5181dfa9f9e1f21e19ea9b08c684d3416a5c393b1ead0ff0b9e059530ecafc480fbf34
7
- data.tar.gz: 7b149adb51811d7c2303954db76db827ea8d303b8d9c7cdff5899a34755e8b73de3c678346a3d058ea1ce1d4a9d20af2fbab49e996e52bab5a7de22897a5fe77
6
+ metadata.gz: 0e6ce3faa7a19071c99d358bc6e3af491b9a783c7172cbfba7292e412890f00d7280e311c02d6c3c3fa6cd52ae96b43868f27560c8f61b30316c32ee62401235
7
+ data.tar.gz: ee01e16001b35daf46c69c4a93afebe4ecc8c5b5a0ffbd135bd0bc19416d40241cc86be176eb0c6433096a1081cb7e063d555b6d69e9b65721a7fbf975c0cb3e
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.0
4
+
5
+ - `credentials` adds `token_type`, `scope` and `refresh_token_expires_at`.
6
+ - Expiry times are epoch seconds, not ISO 8601 text. Credentials saved by
7
+ 0.1.0 keep a text `expires_at` until their next refresh.
8
+ - `refresh` always asks the vendor, so check `expires_at` before calling it.
9
+ `expired?` and `refresh!` are gone.
10
+
3
11
  ## 0.1.0
4
12
 
5
13
  The first release: strategies for Google, Microsoft, Apple, SAML, Signicat,
data/README.md CHANGED
@@ -257,7 +257,7 @@ else. `Economic` and `Adversus` have fields and no sign-in at all: check
257
257
 
258
258
  ### Store and refresh the credentials
259
259
 
260
- Store `auth.credentials` (encrypted), and renew it before each use:
260
+ Store `auth.credentials` (encrypted), and renew it when it is about to run out:
261
261
 
262
262
  ```ruby
263
263
  class Shop < ApplicationRecord
@@ -265,23 +265,37 @@ class Shop < ApplicationRecord
265
265
  encrypts :credentials
266
266
 
267
267
  def access_token
268
- renewed = strategy.refresh(credentials)
269
- update!(credentials: renewed) if renewed != credentials
270
- renewed["access_token"]
268
+ renew if expiring?
269
+ credentials["access_token"]
271
270
  end
272
271
 
273
272
  private
273
+ def expiring?
274
+ expires_at = credentials["expires_at"]
275
+ expires_at.present? && expires_at < 2.minutes.from_now.to_i
276
+ end
277
+
278
+ def renew
279
+ update!(credentials: strategy.refresh(credentials))
280
+ end
281
+
274
282
  def strategy
275
283
  AnyAuth.strategy(:shopify, shop: credentials["shop"])
276
284
  end
277
285
  end
278
286
  ```
279
287
 
280
- `refresh` asks the vendor only when the token runs out within two minutes, and
281
- keeps a refresh token the vendor did not replace. A vendor that rotates its
282
- refresh token, as Shopify does, must not be refreshed twice at once. How a
283
- vendor wants the token sent on an API call is on its
284
- [strategy page](docs/strategies).
288
+ `refresh` always asks the vendor, and keeps a refresh token the vendor did not
289
+ replace; a strategy with nothing to refresh, such as OAuth 1, answers the
290
+ credentials as they are. When to refresh is yours to decide. Renew a little
291
+ before `expires_at`, since a token used to its last second can run out
292
+ mid-request. `refresh_token_expires_at` says when the refresh token itself runs
293
+ out, for a keep-alive that renews idle credentials in time, but most vendors
294
+ don't say, so its absence does not mean it never runs out. Both are epoch
295
+ seconds; credentials stored by 0.1.0 hold `expires_at` as ISO 8601 text until
296
+ their next refresh. A vendor that rotates its refresh token, as Shopify does,
297
+ must not be refreshed twice at once. How a vendor wants the token sent on an
298
+ API call is on its [strategy page](docs/strategies).
285
299
 
286
300
  ### Configuring each sign-in (OmniAuth's setup phase)
287
301
 
@@ -355,7 +369,7 @@ next person at the browser cannot finish it.
355
369
  | `auth.provider` | the name the provider is configured under |
356
370
  | `auth.uid` | who the vendor's token is for, as the vendor identifies them |
357
371
  | `auth.info` | `name`, `email`, `image`, nil where the vendor does not say; `email_verified`, false unless it vouches |
358
- | `auth.credentials` | `access_token`, and `refresh_token` and `expires_at` (ISO 8601) when sent |
372
+ | `auth.credentials` | `access_token`, and when sent `token_type`, `refresh_token`, `expires_at`, `refresh_token_expires_at` (epoch seconds) and `scope` |
359
373
  | `auth.raw` | what the vendor said about who signed in: the ID token's claims, or its answer without the tokens |
360
374
  | `auth.params` | what you passed as `params:` |
361
375
 
@@ -479,7 +493,7 @@ checked along the way.
479
493
  - Keep your provider names: `config.provider :google_oauth2, strategy: :google`.
480
494
  - Compare `uid`s before switching; each strategy page says what it is.
481
495
  - `auth` is an object: `auth.uid`, not `auth["uid"]`. `credentials` has
482
- `access_token` where OmniAuth has `token`, and `expires_at` as ISO 8601 text.
496
+ `access_token` where OmniAuth has `token`, and no `expires` flag.
483
497
 
484
498
  ## Testing
485
499
 
@@ -20,10 +20,6 @@ module AnyAuth
20
20
  @provider || self.class.provider
21
21
  end
22
22
 
23
- def expired?(_credentials)
24
- false
25
- end
26
-
27
23
  def refresh(credentials)
28
24
  credentials
29
25
  end
@@ -13,6 +13,11 @@ module AnyAuth
13
13
 
14
14
  private
15
15
 
16
+ # HubSpot sends the granted scopes as an array named `scopes`.
17
+ def credentials_from(access_token)
18
+ super.merge('scope' => access_token.params['scopes']&.join(' ')).compact
19
+ end
20
+
16
21
  def identity(access_token, _session)
17
22
  token_info = introspect(access_token.token)
18
23
  raise AccessDenied, "#{provider} returned no user" unless named_user?(token_info)
@@ -40,19 +40,7 @@ module AnyAuth
40
40
  Auth.new(provider:, credentials:, **identity(access_token, session))
41
41
  end
42
42
 
43
- def expired?(credentials)
44
- expires_at = credentials['expires_at']&.to_time
45
-
46
- expires_at.present? && expires_at.before?(2.minutes.from_now)
47
- end
48
-
49
43
  def refresh(credentials)
50
- return credentials unless expired?(credentials)
51
-
52
- refresh!(credentials)
53
- end
54
-
55
- def refresh!(credentials)
56
44
  access_token = with_error_handling do
57
45
  ::OAuth2::AccessToken.from_hash(client, credentials).refresh
58
46
  end
@@ -93,10 +81,19 @@ module AnyAuth
93
81
  def credentials_from(access_token)
94
82
  raise AccessDenied, "#{provider} returned no access token" if access_token.token.blank?
95
83
 
84
+ params = access_token.params
85
+ # The refresh token's lifetime: the IETF draft calls it refresh_token_timeout; GitHub,
86
+ # Google and Shopify say refresh_token_expires_in.
87
+ lifetime = params['refresh_token_timeout'] || params['refresh_token_expires_in']
88
+ refresh_token_expires_at = Time.now.to_i + lifetime.to_i if lifetime
89
+
96
90
  {
97
91
  'access_token' => access_token.token,
92
+ 'token_type' => params['token_type'],
98
93
  'refresh_token' => access_token.refresh_token,
99
- 'expires_at' => (Time.at(access_token.expires_at).utc.iso8601 if access_token.expires_at),
94
+ 'expires_at' => access_token.expires_at,
95
+ 'refresh_token_expires_at' => refresh_token_expires_at,
96
+ 'scope' => params['scope'],
100
97
  }.compact
101
98
  end
102
99
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AnyAuth
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: any_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Displayful