omniauth-vitsme 0.4.0 → 0.6.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/AGENTS.md +18 -14
- data/CHANGELOG.md +36 -0
- data/README.md +83 -38
- data/data/claims.json +24 -6
- data/lib/omniauth/strategies/vitsme.rb +4 -14
- data/lib/omniauth/strategies/vitsme_partner.rb +129 -0
- data/lib/omniauth/vitsme/auth_shape.rb +8 -10
- data/lib/omniauth/vitsme/catalog.rb +3 -4
- data/lib/omniauth/vitsme/events.rb +21 -22
- data/lib/omniauth/vitsme/http.rb +7 -0
- data/lib/omniauth/vitsme/image.rb +68 -0
- data/lib/omniauth/vitsme/partner.rb +138 -0
- data/lib/omniauth/vitsme/partner_directory.rb +74 -0
- data/lib/omniauth/vitsme/test.rb +20 -5
- data/lib/omniauth/vitsme/version.rb +1 -1
- data/lib/omniauth/vitsme.rb +3 -1
- metadata +5 -2
- data/lib/omniauth/vitsme/knot.rb +0 -92
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 110b41d2f27a5dc18c65283b4fab14a657c95c3dd5794f9da5b5520d60e6a3d7
|
|
4
|
+
data.tar.gz: c1815b99a5c966917bcb06db93b73fcfda710c9ca29b7f3699fc3be5d9e13f19
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c3fea5371b786b385adf82fa06380cc0b1e290cfb6034c57488d4a9fb645e1eafdc69a7e95c4791fb7b56f6664bddf8397f9e4b3358042db83071f44b441ea10
|
|
7
|
+
data.tar.gz: 0abf8f83269d66acfd2980bdc28714aa4d411edd3af7e8dd3fc109fcb9d81e2955beb0757188e174f1c2e5dc29b0e98c26184daae4b9da91575153f12c4e82dc
|
data/AGENTS.md
CHANGED
|
@@ -10,7 +10,9 @@ person's verified details to another.
|
|
|
10
10
|
Read it instead of guessing; an unknown scope raises at boot.
|
|
11
11
|
- Ask for the least that answers the question: `age__over_18` rather than `birthdate`, a gate
|
|
12
12
|
rather than `nationality` when "is this person Israeli?" is all you need.
|
|
13
|
-
- vits.me has no email, phone
|
|
13
|
+
- vits.me has no email, phone or username claim, and no picture URL. Ask the person for anything else.
|
|
14
|
+
- `portrait` is the person's photo, sent whole as an `OmniAuth::Vitsme::Image`. Attach it to your own
|
|
15
|
+
storage in the callback (`photo.io`); never link to vits.me for it, and never log it.
|
|
14
16
|
|
|
15
17
|
## Rules
|
|
16
18
|
|
|
@@ -30,8 +32,8 @@ person's verified details to another.
|
|
|
30
32
|
6. **`fluid_` means unverified.** Without the `fluid` scope you never see one. With it, give such
|
|
31
33
|
people a limited, tentative account and merge it when `extra.fluid_uids` shows up on a verified
|
|
32
34
|
sign-in.
|
|
33
|
-
7. **Read everything in the callback.** Tokens live 5 seconds and there is no refresh token.
|
|
34
|
-
|
|
35
|
+
7. **Read everything in the callback.** Tokens live 5 seconds and there is no refresh token. Keep
|
|
36
|
+
`auth.credentials.id_token`: it is the hint a partner needs (rule 12).
|
|
35
37
|
8. **Find returning people by `extra.lookup_uids`** (plus `fluid_uids` with FLUID), current first, and
|
|
36
38
|
move a record found under an older uid to `auth.uid`.
|
|
37
39
|
9. **User-facing copy says "verified"**, not "vits.me-verified". There is no need to name vits.me to
|
|
@@ -40,17 +42,19 @@ person's verified details to another.
|
|
|
40
42
|
`not_signed_in`, `identity_mismatch`, `unverified_identity`, `nationality_gate_refused`,
|
|
41
43
|
`site_suspended`, `access_denied`.
|
|
42
44
|
11. **Ask only for scopes the site registered.** Its page at vits.me/developer sets which it may ask
|
|
43
|
-
for, and vits.me refuses a request for any other. `il_government_id_number`
|
|
44
|
-
on request, per site.
|
|
45
|
-
12. **
|
|
46
|
-
|
|
47
|
-
`
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
`
|
|
53
|
-
|
|
45
|
+
for, and vits.me refuses a request for any other. `il_government_id_number` and `portrait` are
|
|
46
|
+
enabled by vits.me on request, per site.
|
|
47
|
+
12. **Partners (KNOTs) are asked directly, not through vits.me.** Use `provider :vitsme_partner` with the
|
|
48
|
+
partner's issuer as vits.me lists it (`OmniAuth::Vitsme::Partner.listing`), its own scope names from its
|
|
49
|
+
documentation, and `id_token:` returning the vits.me ID token you kept for the signed-in person. Never ask
|
|
50
|
+
a partner vits.me does not list as active, never send the ID token unencrypted (the strategy encrypts
|
|
51
|
+
it), and never treat the partner's `uid` as the vits.me one: it is the partner's own identifier for the
|
|
52
|
+
person at your site. Ask from a button, when you need it. A scope the person did not give comes back as
|
|
53
|
+
an empty list. Refresh with `OmniAuth::Vitsme::Partner.fresh_claims` and keep the next refresh token;
|
|
54
|
+
`Refused` means the consent ended: let go of what you had.
|
|
55
|
+
13. **Verify every partner event** with `OmniAuth::Vitsme::Events.verify!(token, partner:, client_id:)`
|
|
56
|
+
before acting on it, answer `202`, and treat it as idempotent. It never carries values: refetch on
|
|
57
|
+
`:claims_changed`, forget on `:consent_withdrawn`.
|
|
54
58
|
|
|
55
59
|
## Doing the work
|
|
56
60
|
|
data/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,42 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.6.0] - 2026-11-21
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- The `full_name__local` and `full_name__intl` scopes: a full name in the person's own language and
|
|
15
|
+
script, and in its international variant (English, capitalized, Latin letters with dashes and apostrophes,
|
|
16
|
+
no accents). The person may leave out given names or show them as initials, keeping one written out.
|
|
17
|
+
- The `portrait` scope: the person's photo, the first picture claim. `extra.claims["portrait"]` is an
|
|
18
|
+
`OmniAuth::Vitsme::Image` (`content_type`, `width`, `height`, `data`, `io`), decoded from the strict-base64
|
|
19
|
+
JPEG vits.me sends whole in UserInfo; anything else (another type, loose base64, over 200 KB) is dropped.
|
|
20
|
+
It is kept out of `info` and `extra.raw_info`, and its bytes out of `inspect`. Enabled by vits.me per site,
|
|
21
|
+
on request. `OmniAuth::Vitsme::Test` takes one as a claim.
|
|
22
|
+
|
|
23
|
+
## [0.5.0] - 2026-11-13
|
|
24
|
+
|
|
25
|
+
### Changed (breaking)
|
|
26
|
+
|
|
27
|
+
- Partners of vits.me (KNOTs) are asked directly, as their own OAuth servers, not through vits.me. The new
|
|
28
|
+
`vitsme_partner` strategy pushes the request (RFC 9126) with PKCE and the person's hint, the vits.me ID
|
|
29
|
+
token you kept for them encrypted to vits.me's `use: enc` key, and sends the person to the partner. The
|
|
30
|
+
callback redeems the code and fetches the claims, verified against the partner's keys, issuer and your
|
|
31
|
+
client id: `uid` is the partner's identifier for the person at your site, `extra.claims` the answer, and
|
|
32
|
+
`credentials.refresh_token` is there when the partner keeps a standing consent.
|
|
33
|
+
- `OmniAuth::Vitsme::Partner`: `listing` and `trusted!` (vits.me's list of partners, and only active
|
|
34
|
+
ones are asked or believed), `metadata`, `software_statement`, `register` (RFC 7591), `fresh_claims`.
|
|
35
|
+
- `OmniAuth::Vitsme::Events.verify!` now verifies a partner's event: `partner:` and `client_id:`, with
|
|
36
|
+
the person named by the partner's own identifier (`sub_id` format `opaque`).
|
|
37
|
+
- `OmniAuth::Vitsme::Test.mock_partner` and `.partner_auth_hash`.
|
|
38
|
+
|
|
39
|
+
### Removed
|
|
40
|
+
|
|
41
|
+
- KNOT scopes on the vits.me sign-in (`knot:<knot>:<name>`), `extra.knot_claims`, `extra.knot_errors`,
|
|
42
|
+
`OmniAuth::Vitsme::Knot` (`scopes`, `resolve`, `fresh_claims`) and `Test.mock_auth(knot_claims:)`:
|
|
43
|
+
vits.me no longer introduces sites to partners or relays their events.
|
|
44
|
+
|
|
45
|
+
|
|
10
46
|
## [0.4.0] - 2026-11-10
|
|
11
47
|
|
|
12
48
|
### Added
|
data/README.md
CHANGED
|
@@ -26,7 +26,7 @@ rules built in.
|
|
|
26
26
|
|
|
27
27
|
```ruby
|
|
28
28
|
# Gemfile
|
|
29
|
-
gem "omniauth-vitsme", "~> 0.
|
|
29
|
+
gem "omniauth-vitsme", "~> 0.6"
|
|
30
30
|
gem "omniauth-rails_csrf_protection" # OmniAuth 2 starts sign-in by POST only
|
|
31
31
|
```
|
|
32
32
|
|
|
@@ -79,9 +79,12 @@ formats, is [data/claims.json](data/claims.json).
|
|
|
79
79
|
|---|---|---|
|
|
80
80
|
| `name` | String: one name at the detail the person chose | `"Jane"`, `"J. D."`, `"Jane Doe"` |
|
|
81
81
|
| `name__full` | String | `"Jane Doe"` |
|
|
82
|
+
| `full_name__local` | String: a full name in the person's own language and script; given names may be left out or initialled, one kept whole | `"יובל טוביאס"`, `"José Núñez"` |
|
|
83
|
+
| `full_name__intl` | String: the same in its international variant (English, capitalized, Latin letters, dashes and apostrophes, no accents) | `"Jose Nunez"`, `"Jane M. Doe"` |
|
|
82
84
|
| `nationality` | String, ISO 3166-1 alpha-2 | `"IL"` |
|
|
83
85
|
| `nationality__in_eu`, `__in_eea`, `__in_esm`, `__gdpr_compliant` | Boolean | `true` |
|
|
84
86
|
| `il_government_id_number` | String | (sensitive: ask only with a legal need) |
|
|
87
|
+
| `portrait` | `OmniAuth::Vitsme::Image`: the person's photo, a JPEG, with `content_type`, `width`, `height`, `data` and `io` | (enabled by vits.me on request; see below) |
|
|
85
88
|
| `age` | String: a threshold, generation, range or years | `"over_18"`, `"gen_x"`, `"25_to_34"`, `"37"` |
|
|
86
89
|
| `age__years` | Integer | `37` |
|
|
87
90
|
| `age__range` | String | `"25_to_34"` |
|
|
@@ -105,7 +108,23 @@ else ask_them_to_share_it
|
|
|
105
108
|
end
|
|
106
109
|
```
|
|
107
110
|
|
|
108
|
-
There is no email, phone, picture
|
|
111
|
+
There is no email, phone or username, and no picture URL. If you need a public handle, ask the person for one.
|
|
112
|
+
|
|
113
|
+
**The photo.** `portrait` is the face the person's passport chip carries, the one they chose to share. It
|
|
114
|
+
arrives whole in the callback, as a plain JPEG at the passport's own resolution and framing, never cropped:
|
|
115
|
+
**keep your own copy**, because there is no URL to fetch it from later and the tokens live five seconds.
|
|
116
|
+
|
|
117
|
+
```ruby
|
|
118
|
+
if (photo = auth.extra.claims["portrait"])
|
|
119
|
+
user.photo.attach(io: photo.io, filename: "portrait.jpg", content_type: photo.content_type)
|
|
120
|
+
end
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
It is in `extra.claims` only: not in `info`, and left out of `extra.raw_info` so an auth hash kept in a cookie
|
|
124
|
+
session does not carry the picture. Anything that is not the JPEG vits.me promises (another type, loose
|
|
125
|
+
base64, over 200 KB) is dropped. A face links a person's accounts across sites whatever their identifiers, so
|
|
126
|
+
vits.me enables the scope per site, on request, like `il_government_id_number`. In tests, pass one:
|
|
127
|
+
`OmniAuth::Vitsme::Test.mock_auth(claims: { portrait: OmniAuth::Vitsme::Image.new(content_type: "image/jpeg", width: 3, height: 4, data: jpeg_bytes) })`.
|
|
109
128
|
|
|
110
129
|
## Verifying a signed-in person
|
|
111
130
|
|
|
@@ -158,59 +177,89 @@ is still pending can sign in too:
|
|
|
158
177
|
`extra.fluid_uids`. Merge the tentative account into the verified one, and drop tentative accounts
|
|
159
178
|
nobody claimed after a week.
|
|
160
179
|
|
|
161
|
-
## Partner services (
|
|
180
|
+
## Partner services (KNOTs)
|
|
162
181
|
|
|
163
|
-
Some facts come from partner services rather than vits.me
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
182
|
+
Some facts come from partner services rather than vits.me: the organizations a person acts for, the groups
|
|
183
|
+
they belong to, a verified email address. Each partner is an OAuth server of its own. Your site asks it
|
|
184
|
+
directly, as its client, and gets the answer straight from it. vits.me is not on the way: it signs the
|
|
185
|
+
person in at the partner, with their pincode, and makes sure it is the person your site means. Partners
|
|
186
|
+
come and go, so this gem knows none of them. vits.me lists the active ones, and each partner documents its
|
|
187
|
+
own scopes:
|
|
167
188
|
|
|
168
189
|
```ruby
|
|
169
|
-
OmniAuth::Vitsme::
|
|
170
|
-
|
|
190
|
+
OmniAuth::Vitsme::Partner.listing(OmniAuth::Vitsme.issuer(:production))
|
|
191
|
+
# [{ "name" => "…", "issuer" => "https://…", "status" => "active", "scopes" => [{ "name" => "…", "title" => {…}, … }] }]
|
|
171
192
|
```
|
|
172
193
|
|
|
173
|
-
|
|
194
|
+
**Keep the ID token.** When your site signs a person in, keep `auth.credentials.id_token` with them. It is
|
|
195
|
+
the hint you send a partner: encrypted to vits.me, so the partner cannot read it, and vits.me then lets
|
|
196
|
+
nobody but that person answer. Without one there is nobody to ask about.
|
|
197
|
+
|
|
198
|
+
**Register your site's callback for the partner** among its redirect addresses at vits.me/developer:
|
|
199
|
+
partners send codes only to an address vits.me lists for your site. A partner that takes registrations
|
|
200
|
+
(its metadata has a `registration_endpoint`) wants your site registered there once, with a software
|
|
201
|
+
statement vits.me signs for it:
|
|
174
202
|
|
|
175
203
|
```ruby
|
|
176
|
-
|
|
177
|
-
|
|
204
|
+
statement = OmniAuth::Vitsme::Partner.software_statement(vitsme_issuer: OmniAuth::Vitsme.issuer(:production),
|
|
205
|
+
client_id: ENV["VITSME_APP_ID"],
|
|
206
|
+
client_secret: ENV["VITSME_APP_SECRET"],
|
|
207
|
+
audience: "https://partner.example")
|
|
208
|
+
OmniAuth::Vitsme::Partner.register(partner: "https://partner.example", software_statement: statement,
|
|
209
|
+
events_uri: "https://your.site/partner/events")
|
|
210
|
+
# => { "client_id" => …, "client_secret" => "…" } keep the secret: it is shown once
|
|
178
211
|
```
|
|
179
212
|
|
|
180
|
-
|
|
181
|
-
|
|
213
|
+
A partner that takes none (`software_statement_required` in its metadata) gets a fresh statement with each
|
|
214
|
+
request, which the strategy fetches for you.
|
|
215
|
+
|
|
216
|
+
**Ask from a button of your own**, when you need what the partner holds, with your own explanation:
|
|
217
|
+
|
|
218
|
+
```ruby
|
|
219
|
+
provider :vitsme_partner, name: :contacts,
|
|
220
|
+
partner: "https://partner.example", # its issuer, as vits.me lists it
|
|
221
|
+
scope: %w[email], # its own scope names
|
|
222
|
+
client_id: ENV["VITSME_APP_ID"], client_secret: ENV["VITSME_APP_SECRET"],
|
|
223
|
+
partner_secret: ENV["PARTNER_SECRET"], # only if you registered there
|
|
224
|
+
id_token: ->(env) { Person.find_by(id: env["rack.session"]["person_id"])&.vitsme_id_token }
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
The person signs in at the partner with their pincode, sees what your site asks for, turns on what to give
|
|
228
|
+
and selects which. The callback hands you the answer, verified:
|
|
182
229
|
|
|
183
230
|
```ruby
|
|
184
|
-
auth.
|
|
185
|
-
auth.extra.
|
|
231
|
+
auth.uid # the partner's own identifier for the person at your site (never their vits.me uid)
|
|
232
|
+
auth.extra.claims # { "<scope>" => <the partner's answer> }, an empty list for one they did not give
|
|
233
|
+
auth.credentials.refresh_token # when the partner keeps a standing consent; nil otherwise
|
|
186
234
|
```
|
|
187
235
|
|
|
188
|
-
|
|
189
|
-
|
|
236
|
+
Failures are `:not_signed_in` (no ID token), `:partner_untrusted` (vits.me does not list it as active),
|
|
237
|
+
`:partner_unavailable`, `:partner_refused`, `:partner_mixup` (an answer from another issuer) and
|
|
238
|
+
`:access_denied`.
|
|
190
239
|
|
|
191
|
-
**Fresh claims, without the person.** While
|
|
192
|
-
ask again:
|
|
240
|
+
**Fresh claims, without the person.** While their standing consent at the partner holds:
|
|
193
241
|
|
|
194
242
|
```ruby
|
|
195
|
-
result = OmniAuth::Vitsme::
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
#
|
|
243
|
+
result = OmniAuth::Vitsme::Partner.fresh_claims(partner: "https://partner.example", client_id: ENV["VITSME_APP_ID"],
|
|
244
|
+
partner_secret: ENV["PARTNER_SECRET"], refresh_token: person.partner_refresh_token,
|
|
245
|
+
vitsme_issuer: OmniAuth::Vitsme.issuer(:production))
|
|
246
|
+
result.claims # as above
|
|
247
|
+
result.refresh_token # keep it: the one you used is spent
|
|
248
|
+
# raises OmniAuth::Vitsme::Partner::Refused once the consent no longer holds: let go of what you had
|
|
199
249
|
```
|
|
200
250
|
|
|
201
|
-
**Change events.**
|
|
202
|
-
|
|
203
|
-
Verify each one:
|
|
251
|
+
**Change events.** A partner that keeps standing consents POSTs to the events address you registered
|
|
252
|
+
with it when something changed for someone, or they stopped it. Verify each one:
|
|
204
253
|
|
|
205
254
|
```ruby
|
|
206
|
-
class
|
|
255
|
+
class PartnerEventsController < ActionController::API
|
|
207
256
|
def create
|
|
208
|
-
event = OmniAuth::Vitsme::Events.verify!(request.raw_post,
|
|
209
|
-
environment: Rails.env)
|
|
210
|
-
|
|
257
|
+
event = OmniAuth::Vitsme::Events.verify!(request.raw_post, partner: "https://partner.example",
|
|
258
|
+
client_id: ENV["VITSME_APP_ID"], environment: Rails.env)
|
|
259
|
+
link = PartnerLink.find_by(subject: event.uid) # the uid the partner's answer gave you
|
|
211
260
|
case event.type
|
|
212
|
-
when :claims_changed then RefreshPartnerClaimsJob.perform_later(
|
|
213
|
-
when :consent_withdrawn then
|
|
261
|
+
when :claims_changed then RefreshPartnerClaimsJob.perform_later(link.id) if link
|
|
262
|
+
when :consent_withdrawn then link&.destroy
|
|
214
263
|
end
|
|
215
264
|
head :accepted
|
|
216
265
|
rescue OmniAuth::Vitsme::Events::Invalid
|
|
@@ -219,8 +268,6 @@ class VitsmeEventsController < ActionController::API
|
|
|
219
268
|
end
|
|
220
269
|
```
|
|
221
270
|
|
|
222
|
-
Events never carry values, may arrive twice, and are retried until you answer `202`.
|
|
223
|
-
|
|
224
271
|
## Finding a returning person
|
|
225
272
|
|
|
226
273
|
Look a person up by `extra.lookup_uids`, current uid first, plus `extra.fluid_uids` if you use FLUID.
|
|
@@ -263,9 +310,7 @@ OmniAuth sends failures to `OmniAuth.config.on_failure` (in Rails, `/auth/failur
|
|
|
263
310
|
lookup_uids: ["solid_…", "solid_<earlier>"],
|
|
264
311
|
fluid_uids: [],
|
|
265
312
|
claims: { "name" => "Jane D.", "age__over_18" => true }, # typed, shared claims only
|
|
266
|
-
|
|
267
|
-
knot_errors: {}, # partners that could not be asked
|
|
268
|
-
raw_info: { "sub" => "solid_…", … } # the UserInfo response, without KNOT sources
|
|
313
|
+
raw_info: { "sub" => "solid_…", … } # the UserInfo response
|
|
269
314
|
}
|
|
270
315
|
}
|
|
271
316
|
```
|
data/data/claims.json
CHANGED
|
@@ -57,6 +57,19 @@
|
|
|
57
57
|
"examples": ["Jane Doe", "Jane M. Doe"],
|
|
58
58
|
"description": "A full name, chosen from the person's full-name forms."
|
|
59
59
|
},
|
|
60
|
+
"full_name__local": {
|
|
61
|
+
"scope": "full_name__local",
|
|
62
|
+
"type": "string",
|
|
63
|
+
"examples": ["יובל טוביאס", "יובל ש. טוביאס", "José Núñez"],
|
|
64
|
+
"description": "A full name in the person's own language and script, as their documents print it: Hebrew for an Israeli. The person may leave out given names or show them as initials, keeping at least one written out; the family name is whole. Not a legal name."
|
|
65
|
+
},
|
|
66
|
+
"full_name__intl": {
|
|
67
|
+
"scope": "full_name__intl",
|
|
68
|
+
"type": "string",
|
|
69
|
+
"format": "Latin letters A-Z, words joined by a space, a dash or an apostrophe, and a period only after an initial; capitalized, never all caps; no accents",
|
|
70
|
+
"examples": ["Jane Doe", "Jane M. Doe", "Jean-Luc O'Brien", "Jose Nunez"],
|
|
71
|
+
"description": "A full name in its international variant: the English version, capitalized, in Latin letters without accents. The person may leave out given names or show them as initials, keeping at least one written out; the family name is whole. Not a legal name."
|
|
72
|
+
},
|
|
60
73
|
"nationality": {
|
|
61
74
|
"scope": "nationality",
|
|
62
75
|
"type": "string",
|
|
@@ -89,6 +102,13 @@
|
|
|
89
102
|
"type": "string",
|
|
90
103
|
"description": "Israeli government ID number (teudat zehut). Highly sensitive: request only with a legal need."
|
|
91
104
|
},
|
|
105
|
+
"portrait": {
|
|
106
|
+
"scope": "portrait",
|
|
107
|
+
"type": "image",
|
|
108
|
+
"format": "An object: content_type \"image/jpeg\", width and height in pixels, and content, the JPEG in standard base64 (RFC 4648 §4, no line breaks). Always a plain sRGB JPEG with no metadata, at most 200 KB, at the passport's own resolution and framing (about 7:9), never cropped or upscaled.",
|
|
109
|
+
"examples": [{ "content_type": "image/jpeg", "width": 480, "height": 640, "content": "/9j/4AAQSkZJRgABAQ…" }],
|
|
110
|
+
"description": "The person's photo: the face their passport chip carries, the one they chose to share. Sent whole: keep your own copy, there is no URL to fetch it from. Enabled by vits.me on request, per site, since a face links a person's accounts across sites whatever their identifiers."
|
|
111
|
+
},
|
|
92
112
|
"age": {
|
|
93
113
|
"scope": "age",
|
|
94
114
|
"type": "string",
|
|
@@ -150,12 +170,10 @@
|
|
|
150
170
|
}
|
|
151
171
|
},
|
|
152
172
|
"knots": {
|
|
153
|
-
"
|
|
154
|
-
"
|
|
155
|
-
"
|
|
156
|
-
"
|
|
157
|
-
"fresh_claims": "While the consent stands: OmniAuth::Vitsme::Knot.fresh_claims(uid:, scopes:, client_id:, client_secret:). Raises ConsentRequired once it no longer does.",
|
|
158
|
-
"events": "Set a change notifications URL on your registration; verify each POST with OmniAuth::Vitsme::Events.verify! (types claims_changed, consent_withdrawn with reason withdrawn or retired) and answer 202."
|
|
173
|
+
"description": "Claims from a partner service (a KNOT) that verifies what vits.me does not. Partners are OAuth servers of their own, asked directly with the vitsme_partner strategy, never through vits.me: none is listed here. GET <issuer>/knot/partners lists them with their status; only an active one is asked or believed (OmniAuth::Vitsme::Partner.listing). Each documents its own scopes.",
|
|
174
|
+
"auth_hash": "uid is the partner's own identifier for the person at your site; extra.claims holds its answer, an empty list for a scope not given; credentials.refresh_token when it keeps a standing consent.",
|
|
175
|
+
"fresh_claims": "While the standing consent holds: OmniAuth::Vitsme::Partner.fresh_claims(partner:, client_id:, partner_secret:, refresh_token:, vitsme_issuer:). Raises Refused once it does not.",
|
|
176
|
+
"events": "Register an events address with the partner; verify each POST with OmniAuth::Vitsme::Events.verify!(token, partner:, client_id:) (claims_changed, consent_withdrawn) and answer 202."
|
|
159
177
|
},
|
|
160
178
|
"refusals": {
|
|
161
179
|
"nationality_gate": {
|
|
@@ -17,8 +17,7 @@ module OmniAuth
|
|
|
17
17
|
# expected_uid: ->(env) { ... }, # "details about a signed-in person" only
|
|
18
18
|
# require_verified: true # refuse pending (fluid_) identities
|
|
19
19
|
#
|
|
20
|
-
# A
|
|
21
|
-
# extra.knot_claims (OmniAuth::Vitsme::Knot).
|
|
20
|
+
# A partner of vits.me (a KNOT) is asked with the vitsme_partner strategy, not here.
|
|
22
21
|
#
|
|
23
22
|
# See README.md for the auth hash, the failure types and the rules (the uid is private).
|
|
24
23
|
class Vitsme < OpenIDConnect
|
|
@@ -83,21 +82,12 @@ module OmniAuth
|
|
|
83
82
|
end
|
|
84
83
|
|
|
85
84
|
def extra
|
|
86
|
-
OmniAuth::Vitsme::AuthShape.extra(uid, raw_info
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
# The KNOT sources in UserInfo, fetched and verified now: their introductions live 30 seconds.
|
|
90
|
-
def knot
|
|
91
|
-
return @knot if defined?(@knot)
|
|
92
|
-
|
|
93
|
-
@knot = (if raw_info['_claim_sources']
|
|
94
|
-
OmniAuth::Vitsme::Knot.resolve(raw_info,
|
|
95
|
-
client_id: client_options.identifier)
|
|
96
|
-
end)
|
|
85
|
+
OmniAuth::Vitsme::AuthShape.extra(uid, raw_info)
|
|
97
86
|
end
|
|
98
87
|
|
|
99
88
|
# vits.me issues no refresh token and its access token lapses five seconds after issue: everything
|
|
100
|
-
# it can tell you is already in `info` and `extra`.
|
|
89
|
+
# it can tell you is already in `info` and `extra`. Keep the ID token: it is what you send a partner
|
|
90
|
+
# as the hint (the vitsme_partner strategy's `id_token`).
|
|
101
91
|
def credentials
|
|
102
92
|
{ id_token: access_token.id_token, scope: access_token.scope }
|
|
103
93
|
end
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'omniauth'
|
|
4
|
+
require 'securerandom'
|
|
5
|
+
require 'omniauth/vitsme/issuers'
|
|
6
|
+
require 'omniauth/vitsme/partner'
|
|
7
|
+
|
|
8
|
+
module OmniAuth
|
|
9
|
+
module Strategies
|
|
10
|
+
# Ask a partner of vits.me (a KNOT) about the person signed in to your site.
|
|
11
|
+
#
|
|
12
|
+
# provider :vitsme_partner, name: :contacts,
|
|
13
|
+
# partner: 'https://contacts.mattyr.com', # its issuer, as vits.me lists it
|
|
14
|
+
# scope: %w[email], # its own scope names
|
|
15
|
+
# client_id: ..., client_secret: ..., # your site at vits.me
|
|
16
|
+
# partner_secret: ..., # yours at the partner, if it takes registrations
|
|
17
|
+
# id_token: ->(env) { ...the vits.me ID token you keep for the signed-in person... }
|
|
18
|
+
#
|
|
19
|
+
# The person is sent to the partner, signs in there with their pincode (vits.me lets only the person
|
|
20
|
+
# you mean), and answers on the partner's own page. The callback has `uid`, the partner's identifier
|
|
21
|
+
# for them at your site, `extra.claims`, what they agreed to (an empty list for what they did not),
|
|
22
|
+
# and `credentials.refresh_token` when the partner keeps a standing consent (ask again with
|
|
23
|
+
# OmniAuth::Vitsme::Partner.fresh_claims). Ask from a button of your own, when you need it.
|
|
24
|
+
class VitsmePartner
|
|
25
|
+
include OmniAuth::Strategy
|
|
26
|
+
|
|
27
|
+
SESSION_KEY = 'omniauth.vitsme_partner'
|
|
28
|
+
|
|
29
|
+
option :name, 'vitsme_partner'
|
|
30
|
+
option :partner, nil
|
|
31
|
+
option :scope, []
|
|
32
|
+
option :client_id, nil
|
|
33
|
+
option :client_secret, nil
|
|
34
|
+
option :partner_secret, nil
|
|
35
|
+
option :id_token, nil
|
|
36
|
+
option :ui_locales, nil
|
|
37
|
+
option :environment, :production
|
|
38
|
+
option :issuer, nil
|
|
39
|
+
|
|
40
|
+
FAILURES = %i[not_signed_in partner_untrusted partner_unavailable partner_refused partner_mixup
|
|
41
|
+
access_denied].freeze
|
|
42
|
+
|
|
43
|
+
def request_phase
|
|
44
|
+
id_token = options.id_token&.call(env).to_s
|
|
45
|
+
return fail!(:not_signed_in) if id_token.empty?
|
|
46
|
+
|
|
47
|
+
OmniAuth::Vitsme::Partner.trusted!(options.partner, vitsme_issuer)
|
|
48
|
+
redirect OmniAuth::Vitsme::Partner.push(options.partner, push_params(id_token), basic: partner_basic)
|
|
49
|
+
rescue OmniAuth::Vitsme::Partner::Untrusted => e
|
|
50
|
+
fail!(:partner_untrusted, e)
|
|
51
|
+
rescue OmniAuth::Vitsme::Partner::Unavailable, OmniAuth::Vitsme::Keys::Unavailable => e
|
|
52
|
+
fail!(:partner_unavailable, e)
|
|
53
|
+
rescue OmniAuth::Vitsme::Partner::Refused => e
|
|
54
|
+
fail!(:partner_refused, e)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def callback_phase
|
|
58
|
+
stored = session.delete("#{SESSION_KEY}.#{name}") || {}
|
|
59
|
+
refusal = callback_refusal(stored)
|
|
60
|
+
return fail!(refusal) if refusal
|
|
61
|
+
|
|
62
|
+
@result = redeem(stored)
|
|
63
|
+
super
|
|
64
|
+
rescue OmniAuth::Vitsme::Partner::Refused => e
|
|
65
|
+
fail!(:partner_refused, e)
|
|
66
|
+
rescue OmniAuth::Vitsme::Partner::Unavailable, OmniAuth::Vitsme::JWS::Invalid,
|
|
67
|
+
OmniAuth::Vitsme::Keys::Unavailable => e
|
|
68
|
+
fail!(:partner_unavailable, e)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
uid { @result.subject }
|
|
72
|
+
|
|
73
|
+
extra { { 'partner' => options.partner, 'claims' => @result.claims, 'scopes' => @result.scopes } }
|
|
74
|
+
|
|
75
|
+
credentials { { 'refresh_token' => @result.refresh_token }.compact }
|
|
76
|
+
|
|
77
|
+
# Without the query OmniAuth would add: the partner sends codes only to the exact address vits.me
|
|
78
|
+
# lists for your site.
|
|
79
|
+
def callback_url = full_host + callback_path
|
|
80
|
+
|
|
81
|
+
private
|
|
82
|
+
|
|
83
|
+
def callback_refusal(stored)
|
|
84
|
+
return :csrf_detected unless stored['state'] && secure_compare(request.params['state'].to_s,
|
|
85
|
+
stored['state'])
|
|
86
|
+
return :access_denied if request.params['error']
|
|
87
|
+
|
|
88
|
+
:partner_mixup if request.params['iss'] && request.params['iss'] != options.partner
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def redeem(stored)
|
|
92
|
+
OmniAuth::Vitsme::Partner.finish(partner: options.partner, client_id: options.client_id,
|
|
93
|
+
code: request.params['code'], redirect_uri: callback_url,
|
|
94
|
+
code_verifier: stored['verifier'], basic: partner_basic)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def push_params(id_token)
|
|
98
|
+
verifier, challenge = OmniAuth::Vitsme::Partner.pkce
|
|
99
|
+
state = SecureRandom.urlsafe_base64(24)
|
|
100
|
+
session["#{SESSION_KEY}.#{name}"] = { 'state' => state, 'verifier' => verifier }
|
|
101
|
+
params = { response_type: 'code', client_id: options.client_id, redirect_uri: callback_url,
|
|
102
|
+
scope: Array(options.scope).join(' '), state: state, code_challenge: challenge,
|
|
103
|
+
code_challenge_method: 'S256', ui_locales: ui_locales,
|
|
104
|
+
id_token_hint: OmniAuth::Vitsme::Partner.hint(id_token, vitsme_issuer) }.compact
|
|
105
|
+
if OmniAuth::Vitsme::Partner.metadata(options.partner)['software_statement_required']
|
|
106
|
+
statement = software_statement
|
|
107
|
+
end
|
|
108
|
+
statement ? params.merge(software_statement: statement) : params
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def software_statement
|
|
112
|
+
OmniAuth::Vitsme::Partner.software_statement(vitsme_issuer: vitsme_issuer, client_id: options.client_id,
|
|
113
|
+
client_secret: options.client_secret, audience: options.partner)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def ui_locales
|
|
117
|
+
options.ui_locales.respond_to?(:call) ? options.ui_locales.call(env) : options.ui_locales
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def partner_basic = options.partner_secret ? [options.client_id, options.partner_secret] : nil
|
|
121
|
+
|
|
122
|
+
def vitsme_issuer = options.issuer || OmniAuth::Vitsme.issuer(options.environment)
|
|
123
|
+
|
|
124
|
+
def secure_compare(given, expected)
|
|
125
|
+
given.bytesize == expected.bytesize && OpenSSL.fixed_length_secure_compare(given, expected)
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
end
|
|
@@ -7,8 +7,6 @@ module OmniAuth
|
|
|
7
7
|
module AuthShape
|
|
8
8
|
SOLID_PREFIX = 'solid_'
|
|
9
9
|
FLUID_PREFIX = 'fluid_'
|
|
10
|
-
# Spent as soon as the strategy fetched them, and none of your app's business afterwards.
|
|
11
|
-
KNOT_SOURCE_KEYS = %w[_claim_names _claim_sources knot_txn].freeze
|
|
12
10
|
|
|
13
11
|
module_function
|
|
14
12
|
|
|
@@ -26,9 +24,7 @@ module OmniAuth
|
|
|
26
24
|
}.compact
|
|
27
25
|
end
|
|
28
26
|
|
|
29
|
-
|
|
30
|
-
def extra(uid, raw_info, knot: nil)
|
|
31
|
-
knot ||= Knot::Result.empty
|
|
27
|
+
def extra(uid, raw_info)
|
|
32
28
|
{
|
|
33
29
|
identity: identity_kind(uid),
|
|
34
30
|
# Look a returning person up by every one of these, current first: a record keyed by a defunct
|
|
@@ -37,14 +33,16 @@ module OmniAuth
|
|
|
37
33
|
# Tentative accounts (keyed by a pending signup's FLUID) to merge into this person.
|
|
38
34
|
fluid_uids: Array(raw_info['fluid']),
|
|
39
35
|
claims: claims(raw_info),
|
|
40
|
-
#
|
|
41
|
-
# and
|
|
42
|
-
|
|
43
|
-
knot_errors: knot.errors,
|
|
44
|
-
raw_info: raw_info.except(*KNOT_SOURCE_KEYS)
|
|
36
|
+
# Without the pictures: kept whole there, they would follow the auth hash into a cookie session
|
|
37
|
+
# and overflow it. The decoded picture is in `claims`.
|
|
38
|
+
raw_info: raw_info.except(*image_claims)
|
|
45
39
|
}
|
|
46
40
|
end
|
|
47
41
|
|
|
42
|
+
def image_claims
|
|
43
|
+
@image_claims ||= Catalog.claims.select { |_name, spec| spec['type'] == 'image' }.keys.freeze
|
|
44
|
+
end
|
|
45
|
+
|
|
48
46
|
# The disclosure claims vits.me returned, typed by the catalog. Identity claims (sub, solid,
|
|
49
47
|
# defunct_solids, fluid) are in `uid`, `lookup_uids` and `fluid_uids` instead; anything the
|
|
50
48
|
# catalog does not know stays in raw_info only.
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'json'
|
|
4
|
-
require 'omniauth/vitsme/knot'
|
|
5
4
|
|
|
6
5
|
module OmniAuth
|
|
7
6
|
module Vitsme
|
|
@@ -32,10 +31,9 @@ module OmniAuth
|
|
|
32
31
|
GATE_PATTERN.match?(scope.to_s)
|
|
33
32
|
end
|
|
34
33
|
|
|
35
|
-
#
|
|
36
|
-
# one is accepted here, and vits.me refuses one your site was not granted.
|
|
34
|
+
# A partner's scopes are not asked of vits.me: they go to the partner (the vitsme_partner strategy).
|
|
37
35
|
def known_scope?(scope)
|
|
38
|
-
scopes.include?(scope.to_s) || gate_scope?(scope)
|
|
36
|
+
scopes.include?(scope.to_s) || gate_scope?(scope)
|
|
39
37
|
end
|
|
40
38
|
|
|
41
39
|
# The OmniAuth failure type for a `vitsme_error` refusal reason, or nil for an unknown one.
|
|
@@ -52,6 +50,7 @@ module OmniAuth
|
|
|
52
50
|
when 'boolean' then boolean(value)
|
|
53
51
|
when 'integer' then integer(value)
|
|
54
52
|
when 'string_array' then Array(value).map(&:to_s)
|
|
53
|
+
when 'image' then Image.from_claim(value)
|
|
55
54
|
else value.to_s
|
|
56
55
|
end
|
|
57
56
|
end
|
|
@@ -2,22 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
require 'omniauth/vitsme/jws'
|
|
4
4
|
require 'omniauth/vitsme/issuers'
|
|
5
|
+
require 'omniauth/vitsme/partner'
|
|
5
6
|
|
|
6
7
|
module OmniAuth
|
|
7
8
|
module Vitsme
|
|
8
|
-
#
|
|
9
|
-
# RFC 8417, pushed per RFC 8935
|
|
10
|
-
#
|
|
9
|
+
# Change events a partner of vits.me (a KNOT) pushes to the events address you registered with it
|
|
10
|
+
# (Security Event Tokens, RFC 8417, pushed per RFC 8935): only partners that keep a standing consent
|
|
11
|
+
# send them, and only about people who gave your site one.
|
|
11
12
|
#
|
|
12
|
-
# :claims_changed what
|
|
13
|
-
#
|
|
14
|
-
#
|
|
13
|
+
# :claims_changed what the partner would tell you about them changed: ask again
|
|
14
|
+
# (Partner.fresh_claims with the refresh token you hold)
|
|
15
|
+
# :consent_withdrawn they stopped it, or their account ended: forget what you had, and the refresh token
|
|
15
16
|
#
|
|
16
|
-
# An event
|
|
17
|
-
# arrive twice.
|
|
18
|
-
#
|
|
19
|
-
# event = OmniAuth::Vitsme::Events.verify!(request.raw_post, client_id: ENV['VITSME_APP_ID'])
|
|
20
|
-
# person = Person.find_by(vitsme_uid: event.uid) # the uid is private, as everywhere
|
|
17
|
+
# An event names the person by the partner's identifier for them at your site (the `uid` its answers
|
|
18
|
+
# gave you), never says what changed, and may arrive twice. Answer 202.
|
|
21
19
|
module Events
|
|
22
20
|
TYPES = {
|
|
23
21
|
'https://vits.me/secevent/knot/claims-changed' => :claims_changed,
|
|
@@ -28,24 +26,26 @@ module OmniAuth
|
|
|
28
26
|
|
|
29
27
|
class Invalid < StandardError; end
|
|
30
28
|
|
|
31
|
-
Event = Struct.new(:type, :uid, :scopes, :
|
|
29
|
+
Event = Struct.new(:type, :uid, :scopes, :id, :issued_at, keyword_init: true)
|
|
32
30
|
|
|
33
31
|
module_function
|
|
34
32
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
33
|
+
# Verifies an event from `partner` (its issuer) addressed to your site, and that vits.me still lists
|
|
34
|
+
# the partner as active.
|
|
35
|
+
def verify!(token, partner:, client_id:, environment: :production, issuer: nil, now: Time.now)
|
|
36
|
+
Partner.trusted!(partner, issuer || Vitsme.issuer(environment))
|
|
37
|
+
claims = JWS.verify!(token, jwks_uri: Partner.metadata(partner).fetch('jwks_uri'), iss: partner, aud: client_id,
|
|
38
38
|
typ: 'secevent+jwt', expiring: false, now: now)
|
|
39
39
|
raise Invalid, 'stale or future event' unless (claims['iat'].to_i - now.to_i).abs <= MAX_AGE
|
|
40
40
|
|
|
41
|
-
event(claims
|
|
42
|
-
rescue JWS::Invalid, Keys::Unavailable => e
|
|
41
|
+
event(claims)
|
|
42
|
+
rescue JWS::Invalid, Keys::Unavailable, Partner::Unavailable, Partner::Untrusted, KeyError => e
|
|
43
43
|
raise Invalid, e.message
|
|
44
44
|
end
|
|
45
45
|
|
|
46
|
-
def event(claims
|
|
46
|
+
def event(claims)
|
|
47
47
|
sub_id = claims['sub_id']
|
|
48
|
-
unless sub_id.is_a?(Hash) && sub_id['format'] == '
|
|
48
|
+
unless sub_id.is_a?(Hash) && sub_id['format'] == 'opaque' && sub_id['id'].to_s != ''
|
|
49
49
|
raise Invalid,
|
|
50
50
|
'unsupported subject'
|
|
51
51
|
end
|
|
@@ -53,9 +53,8 @@ module OmniAuth
|
|
|
53
53
|
type_uri, body = Hash(claims['events']).find { |uri, _| TYPES.key?(uri) }
|
|
54
54
|
raise Invalid, 'unsupported event' unless type_uri
|
|
55
55
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
id: claims['jti'], issued_at: Time.at(claims['iat'].to_i))
|
|
56
|
+
Event.new(type: TYPES[type_uri], uid: sub_id['id'], scopes: Hash(body)['scope'].to_s.split, id: claims['jti'],
|
|
57
|
+
issued_at: Time.at(claims['iat'].to_i))
|
|
59
58
|
end
|
|
60
59
|
end
|
|
61
60
|
end
|
data/lib/omniauth/vitsme/http.rb
CHANGED
|
@@ -31,6 +31,13 @@ module OmniAuth
|
|
|
31
31
|
request(req)
|
|
32
32
|
end
|
|
33
33
|
|
|
34
|
+
def post_json(url, body, headers: {})
|
|
35
|
+
req = Net::HTTP::Post.new(URI(url),
|
|
36
|
+
headers.merge('Content-Type' => 'application/json', 'Accept' => 'application/json'))
|
|
37
|
+
req.body = JSON.generate(body)
|
|
38
|
+
request(req)
|
|
39
|
+
end
|
|
40
|
+
|
|
34
41
|
def request(req)
|
|
35
42
|
uri = req.uri
|
|
36
43
|
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https',
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'base64'
|
|
4
|
+
require 'stringio'
|
|
5
|
+
|
|
6
|
+
module OmniAuth
|
|
7
|
+
module Vitsme
|
|
8
|
+
# A picture claim (`portrait`), decoded: the JPEG vits.me sent, with its size. vits.me sends it whole, in
|
|
9
|
+
# UserInfo, so your app keeps its own copy:
|
|
10
|
+
#
|
|
11
|
+
# if (photo = auth.extra.claims['portrait'])
|
|
12
|
+
# user.photo.attach(io: photo.io, filename: 'portrait.jpg', content_type: photo.content_type)
|
|
13
|
+
# end
|
|
14
|
+
#
|
|
15
|
+
# Read only from what vits.me promises: a JPEG (by its bytes, not only its label) in strict base64, no
|
|
16
|
+
# larger than MAX_BYTES. Anything else is dropped (nil), never guessed at.
|
|
17
|
+
Image = Struct.new(:content_type, :width, :height, :data, keyword_init: true) do
|
|
18
|
+
def io = StringIO.new(data)
|
|
19
|
+
def bytesize = data.bytesize
|
|
20
|
+
|
|
21
|
+
# The claim as vits.me sends it, for OmniAuth::Vitsme::Test.
|
|
22
|
+
def to_claim
|
|
23
|
+
{ 'content_type' => content_type, 'width' => width, 'height' => height,
|
|
24
|
+
'content' => Base64.strict_encode64(data) }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# The bytes are a person's face: never in an inspect, a log line or an exception message.
|
|
28
|
+
def inspect = "#<OmniAuth::Vitsme::Image #{content_type} #{width}x#{height} #{bytesize} bytes>"
|
|
29
|
+
alias_method :to_s, :inspect
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
class << Image
|
|
33
|
+
MAX_BYTES = 200 * 1024
|
|
34
|
+
JPEG_MAGIC = "\xFF\xD8\xFF".b
|
|
35
|
+
|
|
36
|
+
def max_bytes = MAX_BYTES
|
|
37
|
+
|
|
38
|
+
# An Image from the claim's JSON value, or nil.
|
|
39
|
+
def from_claim(value)
|
|
40
|
+
return value if value.is_a?(Image)
|
|
41
|
+
return nil unless value.is_a?(Hash)
|
|
42
|
+
|
|
43
|
+
value = value.transform_keys(&:to_s)
|
|
44
|
+
data = jpeg_bytes(value)
|
|
45
|
+
return nil unless data
|
|
46
|
+
|
|
47
|
+
new(content_type: 'image/jpeg', width: dimension(value['width']), height: dimension(value['height']),
|
|
48
|
+
data: data)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
private
|
|
52
|
+
|
|
53
|
+
# The decoded content, if it is what vits.me promises: a JPEG label, strict base64, JPEG bytes, in size.
|
|
54
|
+
def jpeg_bytes(value)
|
|
55
|
+
content = value['content']
|
|
56
|
+
return nil unless value['content_type'] == 'image/jpeg' && content.is_a?(String)
|
|
57
|
+
return nil if content.bytesize > (MAX_BYTES * 4 / 3) + 4 # refused before it is decoded
|
|
58
|
+
|
|
59
|
+
data = Base64.strict_decode64(content)
|
|
60
|
+
data if data.bytesize <= MAX_BYTES && data.start_with?(JPEG_MAGIC)
|
|
61
|
+
rescue ArgumentError
|
|
62
|
+
nil
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def dimension(value) = value.is_a?(Integer) && value.positive? ? value : nil
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'base64'
|
|
4
|
+
require 'digest'
|
|
5
|
+
require 'json/jwt'
|
|
6
|
+
require 'securerandom'
|
|
7
|
+
require 'omniauth/vitsme/http'
|
|
8
|
+
require 'omniauth/vitsme/jws'
|
|
9
|
+
require 'omniauth/vitsme/keys'
|
|
10
|
+
require 'omniauth/vitsme/partner_directory'
|
|
11
|
+
|
|
12
|
+
module OmniAuth
|
|
13
|
+
module Vitsme
|
|
14
|
+
# Asking a partner of vits.me (a KNOT) about a person signed in to your site. Partners are OAuth servers
|
|
15
|
+
# of their own: your site is their client, asks them directly, and gets their answer straight from them.
|
|
16
|
+
# vits.me only signs the person in there and makes sure it is the person you mean, from the hint you
|
|
17
|
+
# send: the ID token vits.me gave you for them, encrypted to vits.me, so the partner cannot read it.
|
|
18
|
+
#
|
|
19
|
+
# The browser part is the `vitsme_partner` strategy (OmniAuth::Strategies::VitsmePartner). This module
|
|
20
|
+
# is the rest: whom to trust, the partner's endpoints, and asking again with a refresh token.
|
|
21
|
+
module Partner
|
|
22
|
+
STATEMENT_TYPE = 'software-statement+jwt'
|
|
23
|
+
|
|
24
|
+
# The partner refused: an OAuth error, with its description.
|
|
25
|
+
class Refused < StandardError; end
|
|
26
|
+
|
|
27
|
+
Result = Struct.new(:claims, :subject, :refresh_token, :scopes, keyword_init: true)
|
|
28
|
+
|
|
29
|
+
class << self
|
|
30
|
+
# Whom to trust and where its endpoints are (PartnerDirectory).
|
|
31
|
+
def listing(vitsme_issuer) = PartnerDirectory.listing(vitsme_issuer)
|
|
32
|
+
def trusted!(partner, vitsme_issuer) = PartnerDirectory.trusted!(partner, vitsme_issuer)
|
|
33
|
+
def metadata(partner) = PartnerDirectory.metadata(partner)
|
|
34
|
+
def clear! = PartnerDirectory.clear!
|
|
35
|
+
|
|
36
|
+
# The ID token you hold for the person, encrypted to vits.me's own key (use: enc in its JWKS), anew
|
|
37
|
+
# each time, so the partner can neither read it nor match one person's requests.
|
|
38
|
+
def hint(id_token, vitsme_issuer)
|
|
39
|
+
key = Keys.for("#{vitsme_issuer}/oauth/discovery/keys", kid: nil).find { |jwk| jwk[:use] == 'enc' }
|
|
40
|
+
raise Unavailable, 'vits.me publishes no encryption key' unless key
|
|
41
|
+
|
|
42
|
+
jwe = JSON::JWE.new(id_token.to_s)
|
|
43
|
+
jwe.alg = :'RSA-OAEP'
|
|
44
|
+
jwe.enc = :A256GCM
|
|
45
|
+
jwe.header[:cty] = 'JWT'
|
|
46
|
+
jwe.kid = key[:kid]
|
|
47
|
+
jwe.encrypt!(key.to_key)
|
|
48
|
+
jwe.to_s
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# What vits.me says about your site, made out to one partner, for a partner that takes no
|
|
52
|
+
# registrations (it asks for one with each request) or for registering at one that does.
|
|
53
|
+
def software_statement(vitsme_issuer:, client_id:, client_secret:, audience:)
|
|
54
|
+
response = HTTP.post_form("#{vitsme_issuer}/oauth/software_statement", { audience: audience },
|
|
55
|
+
basic: [client_id, client_secret], headers: { 'Accept' => 'application/json' })
|
|
56
|
+
raise Refused, "vits.me: HTTP #{response.status}" unless response.ok?
|
|
57
|
+
|
|
58
|
+
response.json.fetch('software_statement')
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Registers your site at a partner that takes registrations (RFC 7591). Returns its answer: keep
|
|
62
|
+
# `client_secret`, which it shows once.
|
|
63
|
+
def register(partner:, software_statement:, events_uri: nil, app_roles: nil)
|
|
64
|
+
endpoint = metadata(partner)['registration_endpoint'] or raise Refused, "#{partner} takes no registrations"
|
|
65
|
+
body = { software_statement: software_statement, events_uri: events_uri, app_roles: app_roles }.compact
|
|
66
|
+
response = HTTP.post_json(endpoint, body)
|
|
67
|
+
raise Refused, refusal(response) unless response.status == 201
|
|
68
|
+
|
|
69
|
+
response.json
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def pkce
|
|
73
|
+
verifier = SecureRandom.urlsafe_base64(48)
|
|
74
|
+
[verifier, Base64.urlsafe_encode64(Digest::SHA256.digest(verifier), padding: false)]
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Pushes the request (RFC 9126) and returns where to send the person.
|
|
78
|
+
def push(partner, params, basic: nil)
|
|
79
|
+
meta = metadata(partner)
|
|
80
|
+
response = HTTP.post_form(meta.fetch('pushed_authorization_request_endpoint'), params,
|
|
81
|
+
basic: basic, headers: { 'Accept' => 'application/json' })
|
|
82
|
+
raise Refused, refusal(response) unless response.status == 201
|
|
83
|
+
|
|
84
|
+
uri = URI(meta.fetch('authorization_endpoint'))
|
|
85
|
+
uri.query = URI.encode_www_form(client_id: params[:client_id],
|
|
86
|
+
request_uri: response.json.fetch('request_uri'))
|
|
87
|
+
uri.to_s
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Redeems the code the partner sent back, and fetches what the person agreed to.
|
|
91
|
+
def finish(partner:, client_id:, code:, redirect_uri:, code_verifier:, basic: nil)
|
|
92
|
+
tokens = token(partner, { grant_type: 'authorization_code', code: code, redirect_uri: redirect_uri,
|
|
93
|
+
code_verifier: code_verifier, client_id: client_id }, basic: basic)
|
|
94
|
+
result(partner, client_id, tokens)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Asks again without the person, while their standing consent at the partner holds. Returns a
|
|
98
|
+
# Result with the next refresh token: the one you used is spent. Raises Refused when it does not
|
|
99
|
+
# hold any more: let go of what you had.
|
|
100
|
+
def fresh_claims(partner:, client_id:, partner_secret:, refresh_token:, vitsme_issuer:)
|
|
101
|
+
trusted!(partner, vitsme_issuer)
|
|
102
|
+
tokens = token(partner, { grant_type: 'refresh_token', refresh_token: refresh_token },
|
|
103
|
+
basic: [client_id, partner_secret])
|
|
104
|
+
result(partner, client_id, tokens)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
private
|
|
108
|
+
|
|
109
|
+
def token(partner, form, basic:)
|
|
110
|
+
response = HTTP.post_form(metadata(partner).fetch('token_endpoint'), form,
|
|
111
|
+
basic: basic, headers: { 'Accept' => 'application/json' })
|
|
112
|
+
raise Refused, refusal(response) unless response.ok?
|
|
113
|
+
|
|
114
|
+
response.json
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def result(partner, client_id, tokens)
|
|
118
|
+
meta = metadata(partner)
|
|
119
|
+
bearer = "Bearer #{tokens.fetch('access_token')}"
|
|
120
|
+
response = HTTP.get(meta.fetch('claims_endpoint'), 'Authorization' => bearer, 'Accept' => 'application/jwt')
|
|
121
|
+
raise Unavailable, "#{partner}: HTTP #{response.status}" unless response.ok?
|
|
122
|
+
|
|
123
|
+
claims = JWS.verify!(response.body, jwks_uri: meta.fetch('jwks_uri'), iss: partner, aud: client_id)
|
|
124
|
+
told = claims.except('iss', 'aud', 'sub', 'iat', 'exp')
|
|
125
|
+
Result.new(claims: told, subject: claims.fetch('sub'), refresh_token: tokens['refresh_token'],
|
|
126
|
+
scopes: tokens['scope'].to_s.split)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def refusal(response)
|
|
130
|
+
body = response.json
|
|
131
|
+
[body['error'], body['error_description']].compact.join(': ')
|
|
132
|
+
rescue JSON::ParserError
|
|
133
|
+
"HTTP #{response.status}"
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'omniauth/vitsme/http'
|
|
4
|
+
|
|
5
|
+
module OmniAuth
|
|
6
|
+
module Vitsme
|
|
7
|
+
module Partner
|
|
8
|
+
class Unavailable < StandardError; end
|
|
9
|
+
# vits.me does not list the partner as active: ask it nothing, and trust nothing it says.
|
|
10
|
+
class Untrusted < StandardError; end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Whom to trust, and where each partner's endpoints are: vits.me's listing of partners, and each
|
|
14
|
+
# partner's own authorization server metadata (RFC 8414), both cached. Partner delegates to it.
|
|
15
|
+
module PartnerDirectory
|
|
16
|
+
LISTING_TTL = 300
|
|
17
|
+
METADATA_TTL = 3600
|
|
18
|
+
|
|
19
|
+
@cache = {}
|
|
20
|
+
@mutex = Mutex.new
|
|
21
|
+
|
|
22
|
+
class << self
|
|
23
|
+
# vits.me's listing of partners (GET /knot/partners), cached for minutes.
|
|
24
|
+
def listing(vitsme_issuer)
|
|
25
|
+
cached("listing #{vitsme_issuer}", LISTING_TTL) do
|
|
26
|
+
response = HTTP.get("#{vitsme_issuer}/knot/partners", 'Accept' => 'application/json')
|
|
27
|
+
raise Partner::Unavailable, "vits.me: HTTP #{response.status}" unless response.ok?
|
|
28
|
+
|
|
29
|
+
response.json.fetch('partners')
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def trusted!(partner, vitsme_issuer)
|
|
34
|
+
entry = listing(vitsme_issuer).find { |listed| listed['issuer'] == partner }
|
|
35
|
+
unless entry && entry['status'] == 'active'
|
|
36
|
+
raise Partner::Untrusted, "#{partner} is not an active partner of vits.me"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
entry
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# The partner's metadata, cached for an hour, and only if it is about the partner itself.
|
|
43
|
+
def metadata(partner)
|
|
44
|
+
cached("metadata #{partner}", METADATA_TTL) do
|
|
45
|
+
response = HTTP.get("#{partner}/.well-known/oauth-authorization-server", 'Accept' => 'application/json')
|
|
46
|
+
raise Partner::Unavailable, "#{partner}: HTTP #{response.status}" unless response.ok?
|
|
47
|
+
|
|
48
|
+
document = response.json
|
|
49
|
+
raise Partner::Unavailable, "#{partner}: metadata for another issuer" unless document['issuer'] == partner
|
|
50
|
+
|
|
51
|
+
document
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def clear!
|
|
56
|
+
@mutex.synchronize { @cache.clear }
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
private
|
|
60
|
+
|
|
61
|
+
def cached(key, ttl)
|
|
62
|
+
entry = @mutex.synchronize { @cache[key] }
|
|
63
|
+
return entry[:value] if entry && entry[:at] > Time.now - ttl
|
|
64
|
+
|
|
65
|
+
value = yield
|
|
66
|
+
@mutex.synchronize { @cache[key] = { value: value, at: Time.now } }
|
|
67
|
+
value
|
|
68
|
+
rescue HTTP::Unavailable, JSON::ParserError, KeyError => e
|
|
69
|
+
raise Partner::Unavailable, e.message
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
data/lib/omniauth/vitsme/test.rb
CHANGED
|
@@ -13,7 +13,7 @@ module OmniAuth
|
|
|
13
13
|
# OmniAuth::Vitsme::Test.mock_auth(claims: { age__over_18: true }) # provider :vitsme
|
|
14
14
|
# OmniAuth::Vitsme::Test.mock_auth(:verify, uid: person.vitsme_uid, claims: { name: 'Jane D.' })
|
|
15
15
|
# OmniAuth::Vitsme::Test.mock_failure(:vitsme, :nationality_gate_refused)
|
|
16
|
-
# OmniAuth::Vitsme::Test.
|
|
16
|
+
# OmniAuth::Vitsme::Test.mock_partner(:contacts, partner: 'https://contacts.example', claims: { email: [...] })
|
|
17
17
|
#
|
|
18
18
|
# The auth hash is built by the same code as the real strategy's (AuthShape), so `info`, `extra`
|
|
19
19
|
# and the typed claims look exactly as they will in production. Test mode skips the trip to vits.me
|
|
@@ -30,7 +30,9 @@ module OmniAuth
|
|
|
30
30
|
|
|
31
31
|
# The UserInfo response vits.me would send: identity claims plus the disclosure claims given.
|
|
32
32
|
def raw_info(uid:, claims: {}, defunct_uids: [], fluid_uids: [])
|
|
33
|
+
# A picture is given as an OmniAuth::Vitsme::Image and sent as vits.me sends it.
|
|
33
34
|
claims = claims.transform_keys(&:to_s)
|
|
35
|
+
.transform_values { |value| value.respond_to?(:to_claim) ? value.to_claim : value }
|
|
34
36
|
unknown = claims.keys.reject { |name| Catalog.claim(name) }
|
|
35
37
|
raise ArgumentError, "omniauth-vitsme: unknown claim(s) #{unknown.join(', ')}" unless unknown.empty?
|
|
36
38
|
|
|
@@ -41,17 +43,15 @@ module OmniAuth
|
|
|
41
43
|
info.merge(claims)
|
|
42
44
|
end
|
|
43
45
|
|
|
44
|
-
def auth_hash(provider = :vitsme, identity: :solid, uid: nil, claims: {}, defunct_uids: [], fluid_uids: []
|
|
45
|
-
knot_claims: {}, knot_errors: {})
|
|
46
|
+
def auth_hash(provider = :vitsme, identity: :solid, uid: nil, claims: {}, defunct_uids: [], fluid_uids: [])
|
|
46
47
|
uid ||= self.uid(identity)
|
|
47
48
|
raw = raw_info(uid: uid, claims: claims, defunct_uids: defunct_uids, fluid_uids: fluid_uids)
|
|
48
|
-
knot = Knot::Result.new(knot_claims.transform_keys(&:to_s), knot_errors.transform_keys(&:to_s))
|
|
49
49
|
OmniAuth::AuthHash.new(
|
|
50
50
|
provider: provider.to_s,
|
|
51
51
|
uid: uid,
|
|
52
52
|
info: AuthShape.info(uid, raw),
|
|
53
53
|
credentials: { id_token: 'test.id.token', scope: 'openid' },
|
|
54
|
-
extra: AuthShape.extra(uid, raw
|
|
54
|
+
extra: AuthShape.extra(uid, raw)
|
|
55
55
|
)
|
|
56
56
|
end
|
|
57
57
|
|
|
@@ -60,6 +60,21 @@ module OmniAuth
|
|
|
60
60
|
OmniAuth.config.mock_auth[provider.to_sym] = auth_hash(provider, **)
|
|
61
61
|
end
|
|
62
62
|
|
|
63
|
+
# A partner's answer (the vitsme_partner strategy), as your callback gets it. `claims` is whatever the
|
|
64
|
+
# partner says, passed through as it is; `subject` stands for the partner's identifier for the person.
|
|
65
|
+
def partner_auth_hash(provider, partner:, claims: {}, subject: nil, refresh_token: nil)
|
|
66
|
+
claims = claims.transform_keys(&:to_s)
|
|
67
|
+
subject ||= "ks_#{SecureRandom.urlsafe_base64(32)}"
|
|
68
|
+
OmniAuth::AuthHash.new(provider: provider.to_s, uid: subject, info: {},
|
|
69
|
+
credentials: { refresh_token: refresh_token }.compact,
|
|
70
|
+
extra: { partner: partner, claims: claims, scopes: claims.keys })
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Makes the next callback for a vitsme_partner `provider` succeed with this answer.
|
|
74
|
+
def mock_partner(provider, **)
|
|
75
|
+
OmniAuth.config.mock_auth[provider.to_sym] = partner_auth_hash(provider, **)
|
|
76
|
+
end
|
|
77
|
+
|
|
63
78
|
# Makes the next callback for `provider` fail with `type` (e.g. :identity_mismatch, :not_signed_in,
|
|
64
79
|
# :unverified_identity, :nationality_gate_refused, :site_suspended, :access_denied).
|
|
65
80
|
def mock_failure(provider = :vitsme, type = :access_denied)
|
data/lib/omniauth/vitsme.rb
CHANGED
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
require 'omniauth/vitsme/version'
|
|
4
4
|
require 'omniauth/vitsme/issuers'
|
|
5
|
+
require 'omniauth/vitsme/image'
|
|
5
6
|
require 'omniauth/vitsme/catalog'
|
|
6
7
|
require 'omniauth/vitsme/auth_shape'
|
|
7
|
-
require 'omniauth/vitsme/
|
|
8
|
+
require 'omniauth/vitsme/partner'
|
|
8
9
|
require 'omniauth/vitsme/events'
|
|
9
10
|
require 'omniauth/strategies/vitsme'
|
|
11
|
+
require 'omniauth/strategies/vitsme_partner'
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: omniauth-vitsme
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.6.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Yuval Tobias
|
|
@@ -46,15 +46,18 @@ files:
|
|
|
46
46
|
- lib/generators/omniauth_vitsme/install/templates/vitsme_callbacks_controller.rb.tt
|
|
47
47
|
- lib/omniauth-vitsme.rb
|
|
48
48
|
- lib/omniauth/strategies/vitsme.rb
|
|
49
|
+
- lib/omniauth/strategies/vitsme_partner.rb
|
|
49
50
|
- lib/omniauth/vitsme.rb
|
|
50
51
|
- lib/omniauth/vitsme/auth_shape.rb
|
|
51
52
|
- lib/omniauth/vitsme/catalog.rb
|
|
52
53
|
- lib/omniauth/vitsme/events.rb
|
|
53
54
|
- lib/omniauth/vitsme/http.rb
|
|
55
|
+
- lib/omniauth/vitsme/image.rb
|
|
54
56
|
- lib/omniauth/vitsme/issuers.rb
|
|
55
57
|
- lib/omniauth/vitsme/jws.rb
|
|
56
58
|
- lib/omniauth/vitsme/keys.rb
|
|
57
|
-
- lib/omniauth/vitsme/
|
|
59
|
+
- lib/omniauth/vitsme/partner.rb
|
|
60
|
+
- lib/omniauth/vitsme/partner_directory.rb
|
|
58
61
|
- lib/omniauth/vitsme/test.rb
|
|
59
62
|
- lib/omniauth/vitsme/version.rb
|
|
60
63
|
- llms.txt
|
data/lib/omniauth/vitsme/knot.rb
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require 'omniauth/vitsme/http'
|
|
4
|
-
require 'omniauth/vitsme/jws'
|
|
5
|
-
require 'omniauth/vitsme/issuers'
|
|
6
|
-
|
|
7
|
-
module OmniAuth
|
|
8
|
-
module Vitsme
|
|
9
|
-
# Claims from KNOTs: partner services that verify what vits.me does not (organization memberships, for
|
|
10
|
-
# one) and serve it to sites directly, with vits.me brokering identity and consent.
|
|
11
|
-
#
|
|
12
|
-
# vits.me's UserInfo names each KNOT source with an `endpoint`, an `access_token` (an introduction,
|
|
13
|
-
# encrypted to the KNOT and opaque to you), and the KNOT's `iss` and `jwks_uri`. The KNOT answers with a
|
|
14
|
-
# JWS, `aud` your client_id and `sub` the response's `knot_txn`. Introductions live 30 seconds and work
|
|
15
|
-
# once, so the strategy fetches them while it builds the auth hash.
|
|
16
|
-
module Knot
|
|
17
|
-
# Partners come and go, so the gem knows none of them: any well-formed KNOT scope is accepted, and
|
|
18
|
-
# vits.me refuses one your site was not granted.
|
|
19
|
-
SCOPE_PATTERN = /\Aknot:[^:\s]+:\S+\z/
|
|
20
|
-
|
|
21
|
-
class ConsentRequired < StandardError; end
|
|
22
|
-
class InvalidClient < StandardError; end
|
|
23
|
-
class Unavailable < StandardError; end
|
|
24
|
-
|
|
25
|
-
# `claims` by claim name ({ "<claim>" => <answer> }); `errors` by source, for KNOTs that could not
|
|
26
|
-
# be asked or whose answer did not verify. A claim missing from `claims` is unknown, never false.
|
|
27
|
-
Result = Struct.new(:claims, :errors) do
|
|
28
|
-
def self.empty = new({}, {})
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
module_function
|
|
32
|
-
|
|
33
|
-
def scope?(scope) = SCOPE_PATTERN.match?(scope.to_s)
|
|
34
|
-
|
|
35
|
-
# The active KNOT scopes, as vits.me describes them now (GET <issuer>/knot/scopes): each one's
|
|
36
|
-
# `scope`, `knot`, `claim`, `title`, `description` and `documentation_url`. For developers and their
|
|
37
|
-
# tools; signing in never depends on it. Raises Unavailable.
|
|
38
|
-
def scopes(environment: :production, issuer: nil)
|
|
39
|
-
response = HTTP.get("#{issuer || Vitsme.issuer(environment)}/knot/scopes", 'Accept' => 'application/json')
|
|
40
|
-
raise Unavailable, "vits.me: HTTP #{response.status}" unless response.ok?
|
|
41
|
-
|
|
42
|
-
response.json.fetch('knot_scopes')
|
|
43
|
-
rescue HTTP::Unavailable, JSON::ParserError, KeyError => e
|
|
44
|
-
raise Unavailable, e.message
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
# Fetches and verifies every KNOT source in a UserInfo (or POST /knot/introductions) response.
|
|
48
|
-
def resolve(response, client_id:)
|
|
49
|
-
names = response['_claim_names'] || {}
|
|
50
|
-
sources = response['_claim_sources'] || {}
|
|
51
|
-
txn = response['knot_txn']
|
|
52
|
-
sources.each_with_object(Result.empty) do |(source, spec), result|
|
|
53
|
-
answer = answer(spec, client_id: client_id, txn: txn)
|
|
54
|
-
names.each { |claim, from| result.claims[claim] = answer[claim] if from == source && answer.key?(claim) }
|
|
55
|
-
rescue JWS::Invalid, Keys::Unavailable, HTTP::Unavailable, Unavailable => e
|
|
56
|
-
result.errors[source] = e.message
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
# Fresh KNOT claims about someone, without them present, while they keep a standing consent with your
|
|
61
|
-
# site: vits.me introduces your site to each KNOT again. `uid` is the person's uid at your site.
|
|
62
|
-
# Raises ConsentRequired when nothing asked for is still agreed, InvalidClient for wrong credentials,
|
|
63
|
-
# and Unavailable when vits.me cannot be reached.
|
|
64
|
-
def fresh_claims(uid:, scopes:, client_id:, client_secret:, environment: :production, issuer: nil)
|
|
65
|
-
issuer ||= Vitsme.issuer(environment)
|
|
66
|
-
response = HTTP.post_form("#{issuer}/knot/introductions", { sub: uid, scope: Array(scopes).join(' ') },
|
|
67
|
-
basic: [client_id, client_secret], headers: { 'Accept' => 'application/json' })
|
|
68
|
-
case response.status
|
|
69
|
-
when 200 then resolve(response.json, client_id: client_id)
|
|
70
|
-
when 403 then raise ConsentRequired, 'the person does not let your site be told this'
|
|
71
|
-
when 401 then raise InvalidClient, 'vits.me did not accept the client credentials'
|
|
72
|
-
else raise Unavailable, "vits.me: HTTP #{response.status}"
|
|
73
|
-
end
|
|
74
|
-
rescue HTTP::Unavailable, JSON::ParserError => e
|
|
75
|
-
raise Unavailable, e.message
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
def answer(spec, client_id:, txn:)
|
|
79
|
-
response = HTTP.get(spec.fetch('endpoint'), 'Authorization' => "Bearer #{spec.fetch('access_token')}",
|
|
80
|
-
'Accept' => 'application/jwt')
|
|
81
|
-
raise Unavailable, "HTTP #{response.status}" unless response.ok?
|
|
82
|
-
|
|
83
|
-
claims = JWS.verify!(response.body, jwks_uri: spec.fetch('jwks_uri'), iss: spec.fetch('iss'), aud: client_id)
|
|
84
|
-
raise JWS::Invalid, 'answer for another transaction' unless txn && claims['sub'] == txn
|
|
85
|
-
|
|
86
|
-
claims
|
|
87
|
-
rescue KeyError
|
|
88
|
-
raise Unavailable, 'incomplete source'
|
|
89
|
-
end
|
|
90
|
-
end
|
|
91
|
-
end
|
|
92
|
-
end
|