standard_id-void_which_binds 0.1.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 +7 -0
- data/CHANGELOG.md +79 -0
- data/LICENSE +22 -0
- data/README.md +128 -0
- data/Rakefile +12 -0
- data/app/models/standard_id/void_which_binds/login.rb +14 -0
- data/app/models/standard_id/void_which_binds/received_event.rb +26 -0
- data/app/models/standard_id/void_which_binds/subject.rb +48 -0
- data/config/routes.rb +9 -0
- data/db/migrate/20261005000000_create_standard_id_void_which_binds_tables.rb +64 -0
- data/lib/generators/standard_id/void_which_binds/install/install_generator.rb +81 -0
- data/lib/generators/standard_id/void_which_binds/install/templates/initializer.rb.erb +28 -0
- data/lib/standard_id/void_which_binds/configuration.rb +275 -0
- data/lib/standard_id/void_which_binds/current.rb +18 -0
- data/lib/standard_id/void_which_binds/engine.rb +44 -0
- data/lib/standard_id/void_which_binds/errors.rb +48 -0
- data/lib/standard_id/void_which_binds/events_endpoint.rb +92 -0
- data/lib/standard_id/void_which_binds/id_token.rb +178 -0
- data/lib/standard_id/void_which_binds/jose.rb +258 -0
- data/lib/standard_id/void_which_binds/logins.rb +54 -0
- data/lib/standard_id/void_which_binds/providers/void_which_binds.rb +243 -0
- data/lib/standard_id/void_which_binds/receiver.rb +192 -0
- data/lib/standard_id/void_which_binds/security_event.rb +270 -0
- data/lib/standard_id/void_which_binds/staff_policy.rb +72 -0
- data/lib/standard_id/void_which_binds/testing.rb +65 -0
- data/lib/standard_id/void_which_binds/validators.rb +128 -0
- data/lib/standard_id/void_which_binds/version.rb +7 -0
- data/lib/standard_id/void_which_binds.rb +25 -0
- metadata +131 -0
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
require "json"
|
|
5
|
+
require "openssl"
|
|
6
|
+
|
|
7
|
+
module StandardId
|
|
8
|
+
module VoidWhichBinds
|
|
9
|
+
# The compact-JWS layer ADR-0023's two verifiers share: strict parsing of a
|
|
10
|
+
# three-part compact JWS, RFC 8037 OKP JWKs and their RFC 7638 thumbprints,
|
|
11
|
+
# and the relying party's pinned key set.
|
|
12
|
+
#
|
|
13
|
+
# A port of void-which-binds-go's internal/jose, rule for rule, so that
|
|
14
|
+
# every golden vector reaches the same verdict here as there. It is
|
|
15
|
+
# deliberately narrow: the only algorithm is Ed25519 (`alg` "EdDSA", with
|
|
16
|
+
# RFC 9864's "Ed25519" also accepted, ADR-0023 owner decision 3), the only
|
|
17
|
+
# key type an OKP key on Ed25519, and the header is exactly
|
|
18
|
+
# {"alg","kid","typ"}: any other header member (crit, jku, jwk, x5u, ...) is
|
|
19
|
+
# refused rather than interpreted. JSON is parsed strictly: duplicate member
|
|
20
|
+
# names at any depth, trailing data and invalid UTF-8 are malformed, and
|
|
21
|
+
# members are read by their exact names.
|
|
22
|
+
#
|
|
23
|
+
# Ed25519 runs on Ruby's OpenSSL binding (OpenSSL 3 raw keys), not the
|
|
24
|
+
# `ed25519` gem: OpenSSL is already loaded by every Rails app, it is the
|
|
25
|
+
# binding the Ruby core team maintains, and it adds no native dependency to
|
|
26
|
+
# the consumers.
|
|
27
|
+
module Jose
|
|
28
|
+
# Bounds a token before anything is decoded (ADR-0023's tokens are a few
|
|
29
|
+
# hundred bytes).
|
|
30
|
+
MAX_TOKEN_LEN = 16 * 1024
|
|
31
|
+
# Bounds JSON nesting (a SET's sub_id and events are depth 2).
|
|
32
|
+
MAX_DEPTH = 8
|
|
33
|
+
|
|
34
|
+
ALG_EDDSA = "EdDSA"
|
|
35
|
+
ALG_ED25519 = "Ed25519"
|
|
36
|
+
HEADER_MEMBERS = %w[alg kid typ].freeze
|
|
37
|
+
MAX_INT64 = (2**63) - 1
|
|
38
|
+
|
|
39
|
+
# Unpadded base64url.
|
|
40
|
+
def self.encode(bytes)
|
|
41
|
+
[bytes].pack("m0").tr("+/", "-_").delete("=")
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Strict unpadded base64url: padding, line breaks, non-zero trailing bits
|
|
45
|
+
# and any character outside the alphabet are refused (nil).
|
|
46
|
+
def self.decode(string)
|
|
47
|
+
return nil unless string.is_a?(String) && string.match?(/\A[A-Za-z0-9_-]*\z/)
|
|
48
|
+
return nil if string.length % 4 == 1
|
|
49
|
+
|
|
50
|
+
padded = string.tr("-_", "+/")
|
|
51
|
+
padded += "=" * ((4 - (padded.length % 4)) % 4)
|
|
52
|
+
bytes = padded.unpack1("m0")
|
|
53
|
+
# A non-canonical encoding (non-zero trailing bits) does not round-trip.
|
|
54
|
+
encode(bytes) == string ? bytes : nil
|
|
55
|
+
rescue ArgumentError
|
|
56
|
+
nil
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Compact JSON exactly as every port renders it (Go's encoding/json with
|
|
60
|
+
# HTML escaping off): member order as given, no whitespace, and U+2028 /
|
|
61
|
+
# U+2029 escaped, which Go always does.
|
|
62
|
+
def self.marshal(value)
|
|
63
|
+
JSON.generate(value).gsub("\u2028", "\\u2028").gsub("\u2029", "\\u2029")
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Parses `data` as exactly one JSON object of valid UTF-8, refusing
|
|
67
|
+
# duplicate member names at any depth, nesting deeper than MAX_DEPTH and
|
|
68
|
+
# trailing data. Returns the object (a Hash), or raises Refusal
|
|
69
|
+
# "malformed".
|
|
70
|
+
def self.strict_object(data, what = "json")
|
|
71
|
+
text = data.to_s.dup.force_encoding(Encoding::UTF_8)
|
|
72
|
+
raise Refusal.new("malformed", "#{what}: not valid UTF-8") unless text.valid_encoding?
|
|
73
|
+
|
|
74
|
+
value = JSON.parse(text, allow_duplicate_key: false, allow_nan: false, max_nesting: false,
|
|
75
|
+
create_additions: false)
|
|
76
|
+
raise Refusal.new("malformed", "#{what}: not a JSON object") unless value.is_a?(Hash)
|
|
77
|
+
raise Refusal.new("malformed", "#{what}: nested too deeply") unless depth_ok?(value, 0)
|
|
78
|
+
|
|
79
|
+
value
|
|
80
|
+
rescue JSON::ParserError, EncodingError => e
|
|
81
|
+
raise Refusal.new("malformed", "#{what}: #{e.class}")
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Go's walk: every value sits at a depth (the top object 0, its members
|
|
85
|
+
# 1, ...), and any value deeper than MAX_DEPTH is refused.
|
|
86
|
+
def self.depth_ok?(value, depth)
|
|
87
|
+
return false if depth > MAX_DEPTH
|
|
88
|
+
|
|
89
|
+
case value
|
|
90
|
+
when Hash then value.each_value.all? { |v| depth_ok?(v, depth + 1) }
|
|
91
|
+
when Array then value.all? { |v| depth_ok?(v, depth + 1) }
|
|
92
|
+
else true
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# A NumericDate written as a non-negative JSON integer within int64.
|
|
97
|
+
def self.int(value)
|
|
98
|
+
value.is_a?(Integer) && value >= 0 && value <= MAX_INT64 ? value : nil
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def self.string(value)
|
|
102
|
+
value.is_a?(String) ? value : nil
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# RFC 7638 SHA-256 thumbprint of an OKP key (RFC 8037 §2):
|
|
106
|
+
# base64url(SHA-256({"crv","kty","x"})), the required members in
|
|
107
|
+
# lexicographic order with no whitespace. Reads only those three members.
|
|
108
|
+
def self.thumbprint(jwk)
|
|
109
|
+
jwk = jwk.transform_keys(&:to_s)
|
|
110
|
+
encode(Digest::SHA256.digest(marshal({ "crv" => jwk["crv"].to_s, "kty" => jwk["kty"].to_s, "x" => jwk["x"].to_s })))
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# This profile's JWK for a raw 32-byte Ed25519 public key, in its
|
|
114
|
+
# rendering order {"kty","crv","x","kid","use","alg"}.
|
|
115
|
+
def self.public_jwk(raw_public_key)
|
|
116
|
+
jwk = { "kty" => "OKP", "crv" => "Ed25519", "x" => encode(raw_public_key) }
|
|
117
|
+
{ "kty" => "OKP", "crv" => "Ed25519", "x" => jwk["x"], "kid" => thumbprint(jwk), "use" => "sig", "alg" => ALG_EDDSA }
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Parses a key set strictly: one JSON object with a "keys" array of
|
|
121
|
+
# objects, and each of kty, crv, x, kid, use and alg, where present, a
|
|
122
|
+
# string. Other members are ignored. Accepts the JSON text or an
|
|
123
|
+
# already-parsed Hash.
|
|
124
|
+
def self.parse_jwks(raw)
|
|
125
|
+
top = raw.is_a?(Hash) ? raw.transform_keys(&:to_s) : strict_object(raw, "jwks")
|
|
126
|
+
keys = top["keys"]
|
|
127
|
+
raise Refusal.new("malformed", "jwks: keys is missing or not an array") unless keys.is_a?(Array)
|
|
128
|
+
|
|
129
|
+
keys.each_with_index.map do |entry, i|
|
|
130
|
+
raise Refusal.new("malformed", "jwks: key #{i} is not an object") unless entry.is_a?(Hash)
|
|
131
|
+
|
|
132
|
+
entry = entry.transform_keys(&:to_s)
|
|
133
|
+
%w[kty crv x kid use alg].each_with_object({}) do |name, jwk|
|
|
134
|
+
next unless entry.key?(name)
|
|
135
|
+
raise Refusal.new("malformed", "jwks: key #{i} member #{name} is not a string") unless entry[name].is_a?(String)
|
|
136
|
+
|
|
137
|
+
jwk[name] = entry[name]
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# An RP's trust root (ADR-0023, "Pinning"): a non-empty set of pinned
|
|
143
|
+
# thumbprints, and the key set (fetched from the JWKS, or carried inline)
|
|
144
|
+
# the pinned keys are looked up in. The JWKS distributes keys but is never
|
|
145
|
+
# trusted on its own: a token verifies only under a key whose RECOMPUTED
|
|
146
|
+
# thumbprint is pinned and equals the token's kid. A published key that is
|
|
147
|
+
# not pinned is ignored.
|
|
148
|
+
class Pins
|
|
149
|
+
attr_reader :thumbprints, :keys
|
|
150
|
+
|
|
151
|
+
# @param keys [Array<Hash>, Hash, String] parsed JWKs, a JWKS Hash, or JWKS JSON
|
|
152
|
+
# @param thumbprints [Array<String>] each 43 characters of base64url (a SHA-256)
|
|
153
|
+
def initialize(keys, thumbprints)
|
|
154
|
+
thumbprints = Array(thumbprints)
|
|
155
|
+
raise ConfigurationError, "the pin set is empty" if thumbprints.empty?
|
|
156
|
+
|
|
157
|
+
thumbprints.each do |t|
|
|
158
|
+
bytes = Jose.decode(t.to_s)
|
|
159
|
+
raise ConfigurationError, "pin #{t.inspect} is not a base64url SHA-256 thumbprint" unless bytes&.bytesize == 32
|
|
160
|
+
end
|
|
161
|
+
@thumbprints = thumbprints.map(&:to_s).uniq.freeze
|
|
162
|
+
@keys = (keys.is_a?(Array) ? keys.map { |k| k.transform_keys(&:to_s) } : Jose.parse_jwks(keys)).freeze
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def pinned?(kid)
|
|
166
|
+
@thumbprints.include?(kid)
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# The OpenSSL Ed25519 public key for a header kid, refusing exactly as
|
|
170
|
+
# void-which-binds-go's Pins.key does.
|
|
171
|
+
def key(kid)
|
|
172
|
+
raise Refusal, "kid_not_pinned" unless pinned?(kid)
|
|
173
|
+
|
|
174
|
+
named = false
|
|
175
|
+
@keys.each do |jwk|
|
|
176
|
+
if Jose.thumbprint(jwk) != kid
|
|
177
|
+
named ||= jwk["kid"] == kid
|
|
178
|
+
next
|
|
179
|
+
end
|
|
180
|
+
raise Refusal.new("kid_thumbprint_mismatch", "the key with thumbprint #{kid} states another kid") if jwk["kid"] != kid
|
|
181
|
+
raise Refusal.new("key_not_ed25519", "kty #{jwk["kty"].inspect} crv #{jwk["crv"].inspect}") if jwk["kty"] != "OKP" || jwk["crv"] != "Ed25519"
|
|
182
|
+
raise Refusal.new("key_not_ed25519", "use #{jwk["use"].inspect}") if !jwk["use"].to_s.empty? && jwk["use"] != "sig"
|
|
183
|
+
if !jwk["alg"].to_s.empty? && ![ALG_EDDSA, ALG_ED25519].include?(jwk["alg"])
|
|
184
|
+
raise Refusal.new("key_not_ed25519", "alg #{jwk["alg"].inspect}")
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
x = Jose.decode(jwk["x"].to_s)
|
|
188
|
+
raise Refusal.new("key_not_ed25519", "x is not 32 bytes of base64url") unless x&.bytesize == 32
|
|
189
|
+
|
|
190
|
+
return OpenSSL::PKey.new_raw_public_key("ED25519", x)
|
|
191
|
+
end
|
|
192
|
+
raise Refusal.new("kid_thumbprint_mismatch", "a key states kid #{kid} but its thumbprint differs") if named
|
|
193
|
+
|
|
194
|
+
raise Refusal, "key_not_published"
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
# A verified compact JWS: its payload's top-level members and the pinned
|
|
199
|
+
# thumbprint it verified under.
|
|
200
|
+
Token = Struct.new(:claims, :kid)
|
|
201
|
+
|
|
202
|
+
# Parses `token` and checks, in this order: its shape (malformed), the
|
|
203
|
+
# header's `typ` equals `typ` (wrong_type), `alg` (alg), `kid` is pinned
|
|
204
|
+
# (kid_not_pinned), the key set holds that key (key_not_published,
|
|
205
|
+
# kid_thumbprint_mismatch, key_not_ed25519), and the signature
|
|
206
|
+
# (bad_signature). Only then is the payload parsed (malformed).
|
|
207
|
+
def self.verify(token, typ, pins)
|
|
208
|
+
raise Refusal.new("kid_not_pinned", "no pinned thumbprints") if pins.nil? || pins.thumbprints.empty?
|
|
209
|
+
raise Refusal.new("malformed", "not a string") unless token.is_a?(String)
|
|
210
|
+
raise Refusal.new("malformed", "#{token.bytesize} bytes, over #{MAX_TOKEN_LEN}") if token.bytesize > MAX_TOKEN_LEN
|
|
211
|
+
|
|
212
|
+
parts = token.split(".", -1)
|
|
213
|
+
raise Refusal.new("malformed", "#{parts.size} parts, a compact JWS has 3") unless parts.size == 3
|
|
214
|
+
|
|
215
|
+
header_bytes, payload_bytes, signature = parts.map { |part| Jose.decode(part) }
|
|
216
|
+
raise Refusal.new("malformed", "header is not base64url") if header_bytes.nil?
|
|
217
|
+
raise Refusal.new("malformed", "payload is not base64url") if payload_bytes.nil?
|
|
218
|
+
raise Refusal.new("malformed", "signature is not base64url") if signature.nil?
|
|
219
|
+
|
|
220
|
+
header = strict_object(header_bytes, "header")
|
|
221
|
+
extra = header.keys - HEADER_MEMBERS
|
|
222
|
+
raise Refusal.new("malformed", "header member #{extra.first.inspect} (the header is exactly alg, kid, typ)") if extra.any?
|
|
223
|
+
|
|
224
|
+
got_typ = string(header["typ"])
|
|
225
|
+
raise Refusal.new("wrong_type", "header typ is missing or not a string") if got_typ.nil?
|
|
226
|
+
raise Refusal.new("wrong_type", "typ #{got_typ.inspect}, want #{typ.inspect}") if got_typ != typ
|
|
227
|
+
|
|
228
|
+
alg = string(header["alg"])
|
|
229
|
+
raise Refusal.new("alg", "alg #{header["alg"].inspect}") unless [ALG_EDDSA, ALG_ED25519].include?(alg)
|
|
230
|
+
|
|
231
|
+
kid = string(header["kid"])
|
|
232
|
+
raise Refusal.new("kid_not_pinned", "header kid is missing or not a string") if kid.nil?
|
|
233
|
+
|
|
234
|
+
public_key = pins.key(kid)
|
|
235
|
+
raise Refusal, "bad_signature" unless signature.bytesize == 64 && ed25519_verify(public_key, signature, "#{parts[0]}.#{parts[1]}")
|
|
236
|
+
|
|
237
|
+
Token.new(strict_object(payload_bytes, "payload"), kid)
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
def self.ed25519_verify(public_key, signature, input)
|
|
241
|
+
public_key.verify(nil, signature, input)
|
|
242
|
+
rescue OpenSSL::PKey::PKeyError
|
|
243
|
+
false
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
# The duplicate-member refusal rests on JSON.parse's
|
|
247
|
+
# `allow_duplicate_key: false` (json >= 2.13). An older json would ignore
|
|
248
|
+
# the option and silently take the last duplicate, so check once that it
|
|
249
|
+
# is honoured rather than fail open.
|
|
250
|
+
def self.assert_strict_json!
|
|
251
|
+
JSON.parse('{"a":1,"a":2}', allow_duplicate_key: false)
|
|
252
|
+
raise ConfigurationError, "json #{JSON::VERSION} accepts duplicate members; standard_id-void_which_binds needs json >= 2.13"
|
|
253
|
+
rescue JSON::ParserError
|
|
254
|
+
true
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
end
|
|
258
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module StandardId
|
|
4
|
+
module VoidWhichBinds
|
|
5
|
+
# Session creation under the revocation watermark (ADR-0023, the RP's
|
|
6
|
+
# contract, step 3, amended by #135).
|
|
7
|
+
#
|
|
8
|
+
# The provider calls check! as soon as the ID token verifies, so a stale
|
|
9
|
+
# token is refused before any account is touched. standard_id then creates
|
|
10
|
+
# the browser session; its SESSION_CREATED event runs record!, which, in
|
|
11
|
+
# one transaction holding the subject's row lock, checks again (a SET may
|
|
12
|
+
# have landed in between) and records the session's login_iat. A SET
|
|
13
|
+
# applied before that lock is taken is seen by the second check; one
|
|
14
|
+
# applied after it finds the login row and revokes the session. Either
|
|
15
|
+
# way no session outlives a revocation moneta has been told is applied.
|
|
16
|
+
module Logins
|
|
17
|
+
module_function
|
|
18
|
+
|
|
19
|
+
# Raises Refusal ("session_revoked" or "link_disabled") when a session
|
|
20
|
+
# may not be created from an ID token issued at login_iat for (iss, sub).
|
|
21
|
+
def check!(iss:, sub:, login_iat:)
|
|
22
|
+
refusal = Subject.find_by(iss: iss, sub: sub)&.refusal_for(login_iat)
|
|
23
|
+
raise Refusal, refusal if refusal
|
|
24
|
+
|
|
25
|
+
true
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# The SESSION_CREATED subscriber. Records the pending login (the ID
|
|
29
|
+
# token this request verified) against the session just created from
|
|
30
|
+
# it, or refuses the session by raising StandardId::InvalidGrantError,
|
|
31
|
+
# which standard_id's callback turns into a revoked session and a
|
|
32
|
+
# redirect back to the login page.
|
|
33
|
+
def record!(session)
|
|
34
|
+
pending = Current.pending_login
|
|
35
|
+
return if pending.nil? || session.nil? || session.id.nil?
|
|
36
|
+
|
|
37
|
+
Current.pending_login = nil
|
|
38
|
+
ActiveRecord::Base.transaction do
|
|
39
|
+
subject = Subject.lock_for!(pending[:iss], pending[:sub])
|
|
40
|
+
refusal = subject.refusal_for(pending[:iat])
|
|
41
|
+
if refusal
|
|
42
|
+
raise StandardId::InvalidGrantError, "Void-Which-Binds sign-in refused (#{refusal})"
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
Login.create!(session_id: session.id, iss: pending[:iss], sub: pending[:sub], login_iat: pending[:iat])
|
|
46
|
+
attributes = {}
|
|
47
|
+
attributes[:last_login_iat] = pending[:iat] if subject.last_login_iat.nil? || pending[:iat] > subject.last_login_iat
|
|
48
|
+
attributes[:disabled_toe] = nil if subject.disabled_toe
|
|
49
|
+
subject.update!(attributes) if attributes.any?
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
module StandardId
|
|
8
|
+
module Providers
|
|
9
|
+
# Sign in with the organisation's Void-Which-Binds broker (moneta), per
|
|
10
|
+
# ADR-0023: OpenID Connect authorization code + PKCE S256 + nonce, and an
|
|
11
|
+
# Ed25519 (EdDSA) ID token verified only under PINNED key thumbprints.
|
|
12
|
+
#
|
|
13
|
+
# Web flow only. standard_id (0.46+) generates the state, the nonce and,
|
|
14
|
+
# because supports_pkce? is true, a fresh PKCE verifier for every sign-in,
|
|
15
|
+
# and keeps all three in the browser's encrypted pending-requests cookie;
|
|
16
|
+
# only the S256 challenge leaves the server. At the web callback core
|
|
17
|
+
# passes the stored verifier (`code_verifier:`) and the callback's RFC 9207
|
|
18
|
+
# `iss` (`callback_iss:`). The native/API callback never passes a verifier
|
|
19
|
+
# (it has no server-held flow state), so it is refused, and core's API
|
|
20
|
+
# social login grant refuses a PKCE provider before it starts.
|
|
21
|
+
class VoidWhichBinds < Base
|
|
22
|
+
PROVIDER_NAME = "void_which_binds"
|
|
23
|
+
DEFAULT_SCOPE = "openid email"
|
|
24
|
+
|
|
25
|
+
class << self
|
|
26
|
+
def provider_name
|
|
27
|
+
PROVIDER_NAME
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# :nonce makes standard_id's login controller generate a nonce for
|
|
31
|
+
# every flow and hand it back to get_user_info at the callback.
|
|
32
|
+
def supported_authorization_params
|
|
33
|
+
[:nonce]
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def default_scope
|
|
37
|
+
DEFAULT_SCOPE
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Core-managed PKCE (standard_id 0.46): core generates and stores the
|
|
41
|
+
# verifier, sends its S256 challenge to authorization_url, and hands
|
|
42
|
+
# it back to get_user_info on the web callback only.
|
|
43
|
+
def supports_pkce?
|
|
44
|
+
true
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# moneta verifies the address before it sets email_verified (a link
|
|
48
|
+
# it mailed to that address, or an org-administered domain; ADR-0023
|
|
49
|
+
# D7), so a verified moneta email may link to an existing verified
|
|
50
|
+
# account created another way. Only an org's OWN identity provider may
|
|
51
|
+
# return true here; see the README.
|
|
52
|
+
def trusted_for_linking?
|
|
53
|
+
true
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def config_schema
|
|
57
|
+
{
|
|
58
|
+
void_which_binds_client_id: { type: :string, default: nil },
|
|
59
|
+
void_which_binds_client_secret: { type: :string, default: nil, required: true },
|
|
60
|
+
void_which_binds_issuer: { type: :string, default: nil, required: true },
|
|
61
|
+
void_which_binds_org: { type: :string, default: nil, required: true },
|
|
62
|
+
# Array of RFC 7638 thumbprints, or one comma/space-separated
|
|
63
|
+
# String (the ENV form).
|
|
64
|
+
void_which_binds_jwks_pins: { type: :any, default: nil, required: true },
|
|
65
|
+
# The JWKS inline (JSON or a Hash); nil fetches
|
|
66
|
+
# issuer + /.well-known/jwks.json. Either way only pinned keys count.
|
|
67
|
+
void_which_binds_jwks: { type: :any, default: nil },
|
|
68
|
+
# Override discovery (issuer + /.well-known/openid-configuration).
|
|
69
|
+
void_which_binds_authorization_endpoint: { type: :string, default: nil },
|
|
70
|
+
void_which_binds_token_endpoint: { type: :string, default: nil },
|
|
71
|
+
# Enforced by StandardId::VoidWhichBinds.staff_policy, and gates
|
|
72
|
+
# the staff lock on a roster removal.
|
|
73
|
+
void_which_binds_require_for_staff: { type: :boolean, default: true },
|
|
74
|
+
# ->(account) { truthy for staff }; a callable, so never from ENV.
|
|
75
|
+
void_which_binds_staff_predicate: { type: :any, default: nil, env: false }
|
|
76
|
+
}
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def authorization_url(state:, redirect_uri:, nonce: nil, code_challenge: nil, code_challenge_method: nil, **_options)
|
|
80
|
+
raise StandardId::InvalidRequestError, "Void-Which-Binds sign-in needs a server-generated nonce" if nonce.blank?
|
|
81
|
+
if code_challenge.blank? || code_challenge_method != StandardId::VoidWhichBinds::Pkce::METHOD
|
|
82
|
+
raise StandardId::InvalidRequestError, "Void-Which-Binds sign-in needs a server-generated PKCE S256 challenge"
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
authorization_endpoint, = StandardId::VoidWhichBinds::Broker.endpoints
|
|
86
|
+
query = {
|
|
87
|
+
client_id: credentials[:client_id],
|
|
88
|
+
redirect_uri: redirect_uri,
|
|
89
|
+
response_type: "code",
|
|
90
|
+
state: state,
|
|
91
|
+
scope: DEFAULT_SCOPE,
|
|
92
|
+
nonce: nonce,
|
|
93
|
+
code_challenge: code_challenge,
|
|
94
|
+
code_challenge_method: code_challenge_method
|
|
95
|
+
}
|
|
96
|
+
"#{authorization_endpoint}?#{URI.encode_www_form(query)}"
|
|
97
|
+
rescue StandardId::VoidWhichBinds::Error => e
|
|
98
|
+
raise StandardId::InvalidRequestError, "Void-Which-Binds is misconfigured: #{e.message}"
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Exchanges the code (client_secret_basic, PKCE verifier), verifies the
|
|
102
|
+
# ID token under the pinned keys exactly as void-which-binds-go's
|
|
103
|
+
# oidc.VerifyIDToken does, refuses a token issued at or before the
|
|
104
|
+
# subject's revocation watermark, and returns its claims.
|
|
105
|
+
#
|
|
106
|
+
# `code_verifier` comes only from core's web callback (the verifier it
|
|
107
|
+
# stored with this flow's state); its absence means the native/API
|
|
108
|
+
# callback, which is refused. `callback_iss` is the callback's RFC 9207
|
|
109
|
+
# `iss`, checked before the code is exchanged.
|
|
110
|
+
def get_user_info(code: nil, id_token: nil, access_token: nil, redirect_uri: nil, nonce: nil,
|
|
111
|
+
callback_iss: nil, code_verifier: nil, **_options)
|
|
112
|
+
if id_token.present? || access_token.present?
|
|
113
|
+
raise StandardId::InvalidRequestError, "Void-Which-Binds sign-in accepts only the authorization code flow"
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
rescue_to_oauth_error do
|
|
117
|
+
raise StandardId::InvalidRequestError, "Void-Which-Binds authorization code is missing" if code.blank?
|
|
118
|
+
# Only core's web callback passes the server-held verifier.
|
|
119
|
+
if code_verifier.blank?
|
|
120
|
+
raise StandardId::InvalidRequestError, "Void-Which-Binds sign-in is only available through the web callback"
|
|
121
|
+
end
|
|
122
|
+
raise StandardId::InvalidRequestError, "Void-Which-Binds sign-in needs a server-generated nonce" if nonce.blank?
|
|
123
|
+
raise StandardId::InvalidRequestError, "Void-Which-Binds redirect_uri is missing" if redirect_uri.blank?
|
|
124
|
+
|
|
125
|
+
with_refusals_as_oauth_errors do
|
|
126
|
+
check_callback_issuer!(callback_iss)
|
|
127
|
+
token = exchange_code(code: code, redirect_uri: redirect_uri, code_verifier: code_verifier)
|
|
128
|
+
claims = verify_id_token(token, nonce: nonce)
|
|
129
|
+
StandardId::VoidWhichBinds::Logins.check!(iss: claims.iss, sub: claims.sub, login_iat: claims.iat)
|
|
130
|
+
StandardId::VoidWhichBinds::Current.pending_login = { iss: claims.iss, sub: claims.sub, iat: claims.iat }
|
|
131
|
+
|
|
132
|
+
build_response(user_info(claims), tokens: { id_token: token })
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Verifies an ID token for this app's configuration and the session's
|
|
138
|
+
# nonce; raises StandardId::VoidWhichBinds::Refusal.
|
|
139
|
+
def verify_id_token(token, nonce:, now: Time.now.to_i)
|
|
140
|
+
StandardId::VoidWhichBinds::Broker.with_pins do |pins|
|
|
141
|
+
StandardId::VoidWhichBinds::IdToken.verify(token, StandardId::VoidWhichBinds::IdToken::Expect.new(
|
|
142
|
+
issuer: StandardId::VoidWhichBinds::Configuration.issuer,
|
|
143
|
+
client_id: credentials[:client_id],
|
|
144
|
+
org: StandardId::VoidWhichBinds::Configuration.org,
|
|
145
|
+
nonce: nonce.to_s,
|
|
146
|
+
pins: pins,
|
|
147
|
+
now: now,
|
|
148
|
+
leeway: StandardId::VoidWhichBinds::IdToken::MAX_LEEWAY
|
|
149
|
+
))
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
private
|
|
154
|
+
|
|
155
|
+
# A verifier's refusal names only its verdict (never a token value);
|
|
156
|
+
# misconfiguration names the field.
|
|
157
|
+
def with_refusals_as_oauth_errors
|
|
158
|
+
yield
|
|
159
|
+
rescue StandardId::VoidWhichBinds::Refusal => e
|
|
160
|
+
raise StandardId::InvalidRequestError, "Void-Which-Binds sign-in refused (#{e.verdict})"
|
|
161
|
+
rescue StandardId::VoidWhichBinds::BrokerUnavailable => e
|
|
162
|
+
raise StandardId::InvalidRequestError, "Void-Which-Binds is unavailable: #{e.message}"
|
|
163
|
+
rescue StandardId::VoidWhichBinds::Error => e
|
|
164
|
+
raise StandardId::InvalidRequestError, "Void-Which-Binds is misconfigured: #{e.message}"
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def credentials
|
|
168
|
+
client_id = StandardId::VoidWhichBinds::Configuration.client_id
|
|
169
|
+
raise StandardId::InvalidRequestError, "Void-Which-Binds is not configured" if client_id.nil?
|
|
170
|
+
if (error = StandardId::VoidWhichBinds::Validators.client_id_error(client_id))
|
|
171
|
+
raise StandardId::VoidWhichBinds::ConfigurationError, "void_which_binds_client_id: #{error}"
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
{ client_id: client_id, client_secret: StandardId::VoidWhichBinds::Configuration.client_secret }
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# RFC 9207: the redirect carries moneta's `iss`, checked before the
|
|
178
|
+
# code is exchanged (mix-up defence). A missing one is refused: moneta
|
|
179
|
+
# always sends it.
|
|
180
|
+
def check_callback_issuer!(callback_iss)
|
|
181
|
+
return if callback_iss.is_a?(String) && callback_iss == StandardId::VoidWhichBinds::Configuration.issuer
|
|
182
|
+
|
|
183
|
+
raise StandardId::InvalidRequestError, "Void-Which-Binds sign-in refused (callback iss)"
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def exchange_code(code:, redirect_uri:, code_verifier:)
|
|
187
|
+
creds = credentials
|
|
188
|
+
raise StandardId::InvalidRequestError, "Void-Which-Binds client secret is not set" if creds[:client_secret].blank?
|
|
189
|
+
|
|
190
|
+
_, token_endpoint = StandardId::VoidWhichBinds::Broker.endpoints
|
|
191
|
+
response = StandardId::VoidWhichBinds::Http.post_form(
|
|
192
|
+
token_endpoint,
|
|
193
|
+
{
|
|
194
|
+
grant_type: "authorization_code",
|
|
195
|
+
code: code,
|
|
196
|
+
redirect_uri: redirect_uri,
|
|
197
|
+
code_verifier: code_verifier
|
|
198
|
+
},
|
|
199
|
+
basic_auth: basic_auth(creds[:client_id], creds[:client_secret])
|
|
200
|
+
)
|
|
201
|
+
unless response.is_a?(Net::HTTPSuccess)
|
|
202
|
+
raise StandardId::InvalidRequestError, "Failed to exchange Void-Which-Binds authorization code: #{error_reason(response)}"
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
parsed = JSON.parse(response.body.to_s)
|
|
206
|
+
token = parsed.is_a?(Hash) ? parsed["id_token"] : nil
|
|
207
|
+
raise StandardId::InvalidRequestError, "Void-Which-Binds token response is missing id_token" unless token.is_a?(String) && token.present?
|
|
208
|
+
|
|
209
|
+
token
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
# client_secret_basic (RFC 6749 §2.3.1): both parts form-urlencoded,
|
|
213
|
+
# then base64.
|
|
214
|
+
def basic_auth(client_id, client_secret)
|
|
215
|
+
pair = "#{URI.encode_www_form_component(client_id)}:#{URI.encode_www_form_component(client_secret)}"
|
|
216
|
+
"Basic #{[pair].pack("m0")}"
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def user_info(claims)
|
|
220
|
+
{
|
|
221
|
+
"sub" => claims.sub,
|
|
222
|
+
"email" => claims.email,
|
|
223
|
+
"email_verified" => claims.email_verified,
|
|
224
|
+
"org" => claims.org,
|
|
225
|
+
"role" => claims.role,
|
|
226
|
+
"amr" => claims.amr,
|
|
227
|
+
"iss" => claims.iss,
|
|
228
|
+
"iat" => claims.iat,
|
|
229
|
+
"auth_time" => claims.auth_time
|
|
230
|
+
}.compact
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
def error_reason(response)
|
|
234
|
+
body = JSON.parse(response.body.to_s)
|
|
235
|
+
reason = body["error"] if body.is_a?(Hash)
|
|
236
|
+
reason.is_a?(String) && reason.present? ? reason : "HTTP #{response.code}"
|
|
237
|
+
rescue JSON::ParserError
|
|
238
|
+
"HTTP #{response.code}"
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
end
|
|
243
|
+
end
|