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 +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +25 -11
- data/lib/any_auth/strategies/base.rb +0 -4
- data/lib/any_auth/strategies/hubspot.rb +5 -0
- data/lib/any_auth/strategies/oauth2.rb +10 -13
- data/lib/any_auth/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c0bcfb06c8504e1753a62da0f7e9ba347d0ef8ddc5021190a0ac4dce0f2b1dc5
|
|
4
|
+
data.tar.gz: 02244110ee2e09c11fe7a7cf7866cde762a7ff9aff9740ac1a11aac42380dbee
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
|
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
|
-
|
|
269
|
-
|
|
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
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
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
|
|
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 `
|
|
496
|
+
`access_token` where OmniAuth has `token`, and no `expires` flag.
|
|
483
497
|
|
|
484
498
|
## Testing
|
|
485
499
|
|
|
@@ -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' =>
|
|
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
|
|
data/lib/any_auth/version.rb
CHANGED