mistri 0.7.0 → 0.8.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 +321 -2
- data/CONTRIBUTING.md +17 -3
- data/NOTICE +11 -0
- data/README.md +145 -284
- data/UPGRADING.md +810 -0
- data/assets/vendors/README.md +12 -0
- data/assets/vendors/claude-mark.svg +3 -0
- data/assets/vendors/gemini-mark.svg +10 -0
- data/assets/vendors/openai-mark-dark.svg +1 -0
- data/assets/vendors/openai-mark.svg +1 -0
- data/docs/README.md +2 -1
- data/docs/context-and-workspaces.md +84 -13
- data/docs/logging.md +90 -0
- data/docs/mcp.md +762 -21
- data/docs/reliability.md +210 -24
- data/docs/sessions.md +415 -36
- data/docs/sub-agents.md +85 -7
- data/docs/tool-contracts.md +528 -13
- data/examples/approval.rb +7 -7
- data/examples/browser.rb +1 -1
- data/examples/page_editor.rb +2 -2
- data/examples/quickstart.rb +1 -1
- data/lib/generators/mistri/install/templates/migration.rb.tt +4 -2
- data/lib/generators/mistri/mcp/mcp_generator.rb +2 -2
- data/lib/generators/mistri/mcp/templates/model.rb.tt +21 -8
- data/lib/mistri/agent.rb +815 -180
- data/lib/mistri/call_order.rb +137 -0
- data/lib/mistri/child.rb +69 -13
- data/lib/mistri/compaction.rb +200 -35
- data/lib/mistri/compactor/text.rb +91 -0
- data/lib/mistri/compactor.rb +474 -117
- data/lib/mistri/console.rb +44 -11
- data/lib/mistri/content.rb +70 -3
- data/lib/mistri/definition.rb +2 -2
- data/lib/mistri/edit.rb +13 -7
- data/lib/mistri/errors.rb +37 -0
- data/lib/mistri/event.rb +20 -9
- data/lib/mistri/locks/rails_cache.rb +16 -2
- data/lib/mistri/locks.rb +12 -3
- data/lib/mistri/mcp/client.rb +247 -61
- data/lib/mistri/mcp/conversation.rb +161 -0
- data/lib/mistri/mcp/egress.rb +5 -4
- data/lib/mistri/mcp/mirror.rb +90 -0
- data/lib/mistri/mcp/negotiation.rb +222 -0
- data/lib/mistri/mcp/oauth/client_identity.rb +53 -0
- data/lib/mistri/mcp/oauth/tolerance.rb +61 -0
- data/lib/mistri/mcp/oauth.rb +303 -82
- data/lib/mistri/mcp/results.rb +97 -0
- data/lib/mistri/mcp/sdk_client.rb +47 -0
- data/lib/mistri/mcp/wires.rb +325 -52
- data/lib/mistri/mcp.rb +304 -55
- data/lib/mistri/memory.rb +1 -1
- data/lib/mistri/message.rb +45 -8
- data/lib/mistri/models.rb +146 -24
- data/lib/mistri/partial_json.rb +5 -5
- data/lib/mistri/providers/anthropic/assembler.rb +40 -3
- data/lib/mistri/providers/anthropic/serializer.rb +154 -22
- data/lib/mistri/providers/anthropic.rb +77 -12
- data/lib/mistri/providers/fake.rb +4 -1
- data/lib/mistri/providers/gemini/assembler.rb +63 -3
- data/lib/mistri/providers/gemini/serializer.rb +27 -16
- data/lib/mistri/providers/gemini.rb +56 -11
- data/lib/mistri/providers/openai/assembler.rb +36 -5
- data/lib/mistri/providers/openai/serializer.rb +76 -22
- data/lib/mistri/providers/openai.rb +91 -14
- data/lib/mistri/reminder.rb +35 -3
- data/lib/mistri/result.rb +6 -4
- data/lib/mistri/result_limit.rb +164 -0
- data/lib/mistri/sandbox/quickjs.rb +345 -0
- data/lib/mistri/sandbox.rb +18 -0
- data/lib/mistri/schema.rb +26 -2
- data/lib/mistri/scratchpad.rb +107 -0
- data/lib/mistri/session/log.rb +137 -0
- data/lib/mistri/session.rb +405 -75
- data/lib/mistri/sinks/logger.rb +17 -11
- data/lib/mistri/skills.rb +10 -4
- data/lib/mistri/spawner.rb +43 -12
- data/lib/mistri/sse.rb +3 -2
- data/lib/mistri/stores/active_record.rb +30 -6
- data/lib/mistri/stores/jsonl.rb +26 -7
- data/lib/mistri/stores/memory.rb +6 -2
- data/lib/mistri/sub_agent/execution.rb +10 -2
- data/lib/mistri/sub_agent/runtime.rb +11 -1
- data/lib/mistri/sub_agent.rb +13 -4
- data/lib/mistri/tool.rb +71 -7
- data/lib/mistri/tool_arguments.rb +10 -11
- data/lib/mistri/tool_context.rb +15 -3
- data/lib/mistri/tool_executor.rb +49 -26
- data/lib/mistri/tool_result.rb +8 -2
- data/lib/mistri/tool_search/index.rb +193 -0
- data/lib/mistri/tool_search.rb +200 -0
- data/lib/mistri/tools/delete_file.rb +22 -0
- data/lib/mistri/tools/edit_file.rb +24 -14
- data/lib/mistri/tools/find_in_file.rb +1 -0
- data/lib/mistri/tools/grep.rb +212 -0
- data/lib/mistri/tools/list_files.rb +38 -5
- data/lib/mistri/tools/read_file.rb +83 -21
- data/lib/mistri/tools/read_memory.rb +2 -1
- data/lib/mistri/tools/run_code.rb +167 -0
- data/lib/mistri/tools/write_file.rb +66 -9
- data/lib/mistri/tools.rb +72 -1
- data/lib/mistri/transport/streamable_http.rb +140 -0
- data/lib/mistri/transport.rb +53 -63
- data/lib/mistri/usage.rb +33 -4
- data/lib/mistri/version.rb +1 -1
- data/lib/mistri/workspace/active_record.rb +22 -10
- data/lib/mistri/workspace/directory.rb +5 -1
- data/lib/mistri/workspace/memory.rb +4 -0
- data/lib/mistri/workspace/path_space.rb +56 -0
- data/lib/mistri/workspace/single.rb +6 -1
- data/lib/mistri.rb +10 -4
- metadata +29 -2
data/lib/mistri/mcp/oauth.rb
CHANGED
|
@@ -19,7 +19,7 @@ module Mistri
|
|
|
19
19
|
# redirect_uri: mcp_callback_url)
|
|
20
20
|
# # persist flow, redirect the user to flow["authorize_url"]
|
|
21
21
|
#
|
|
22
|
-
# tokens = Mistri::MCP::OAuth.complete(code: params[:code], **persisted)
|
|
22
|
+
# tokens = Mistri::MCP::OAuth.complete(code: params[:code], iss: params[:iss], **persisted)
|
|
23
23
|
# tokens = Mistri::MCP::OAuth.refresh(**persisted)
|
|
24
24
|
# # The host verifies callback state before complete.
|
|
25
25
|
#
|
|
@@ -29,6 +29,26 @@ module Mistri
|
|
|
29
29
|
# directly and skip it.
|
|
30
30
|
module OAuth # rubocop:disable Metrics/ModuleLength -- one flow shares one validated network chain
|
|
31
31
|
class ConnectionFailure < Error; end
|
|
32
|
+
|
|
33
|
+
# The token endpoint refused a request. reason is the code it gave when
|
|
34
|
+
# that is one of the standard ones, and status its HTTP status.
|
|
35
|
+
class TokenError < Error
|
|
36
|
+
attr_reader :status, :reason
|
|
37
|
+
|
|
38
|
+
def initialize(message = nil, status: nil, reason: nil)
|
|
39
|
+
@status = status
|
|
40
|
+
@reason = reason
|
|
41
|
+
super(message)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# The grant is spent or revoked, so the person signs in again.
|
|
46
|
+
class InvalidGrantError < TokenError; end
|
|
47
|
+
|
|
48
|
+
# The server does not name the issuer a client is registered with: its
|
|
49
|
+
# authorization server changed, or the issuer was written down wrong.
|
|
50
|
+
class IssuerChanged < Error; end
|
|
51
|
+
|
|
32
52
|
Response = Data.define(:code, :headers, :body) do
|
|
33
53
|
def [](name) = headers[name.to_s.downcase]
|
|
34
54
|
end
|
|
@@ -41,9 +61,11 @@ module Mistri
|
|
|
41
61
|
unsupported_grant_type invalid_scope invalid_target
|
|
42
62
|
].freeze
|
|
43
63
|
TOKEN_AUTH_METHODS = %w[none client_secret_basic client_secret_post].freeze
|
|
64
|
+
# Stands for a callback whose iss the host did not hand over.
|
|
65
|
+
UNCHECKED = Object.new.freeze
|
|
44
66
|
private_constant :ConnectionFailure, :Response, :AUTHORIZE_PARAMETERS,
|
|
45
67
|
:MAX_RESPONSE_BYTES, :WWW_AUTH_PARAM, :TOKEN_ERROR_CODES,
|
|
46
|
-
:TOKEN_AUTH_METHODS
|
|
68
|
+
:TOKEN_AUTH_METHODS, :UNCHECKED
|
|
47
69
|
|
|
48
70
|
module_function
|
|
49
71
|
|
|
@@ -51,49 +73,70 @@ module Mistri
|
|
|
51
73
|
# and build the authorize URL. Returns everything the callback and
|
|
52
74
|
# refresh need: authorize_url, state, code_verifier, client_id,
|
|
53
75
|
# client_secret, token_auth_method, issuer, resource, and redirect_uri.
|
|
76
|
+
# scope is the scope asked for, which is what was granted when the
|
|
77
|
+
# token response names none.
|
|
54
78
|
# token_endpoint remains informational for compatibility; later
|
|
55
79
|
# operations rediscover it from issuer.
|
|
56
80
|
#
|
|
57
81
|
# With no scope given, the challenge or protected resource's advertised
|
|
58
|
-
# scopes are requested. Extra privileges are always host policy
|
|
82
|
+
# scopes are requested. Extra privileges are always host policy, and
|
|
83
|
+
# offline_access: true is one: the scope is added when the authorization
|
|
84
|
+
# server lists it.
|
|
85
|
+
#
|
|
86
|
+
# client_metadata_url: is the address of the application's client ID
|
|
87
|
+
# metadata document. A server that takes one gets it as the client ID,
|
|
88
|
+
# and nothing is registered. tolerant: true accepts what exact discovery
|
|
89
|
+
# refuses; see docs/mcp.md.
|
|
59
90
|
def start(url:, client_name:, redirect_uri:, scope: nil,
|
|
60
91
|
client_id: nil, client_secret: nil, token_auth_method: nil,
|
|
61
|
-
issuer: nil, allow_non_public: nil
|
|
92
|
+
issuer: nil, allow_non_public: nil, client_metadata_url: nil,
|
|
93
|
+
offline_access: false, tolerant: false)
|
|
62
94
|
validate_registration_input(client_id:, client_secret:, token_auth_method:, issuer:)
|
|
63
|
-
|
|
95
|
+
validate_client_metadata_url(client_metadata_url) if client_metadata_url
|
|
64
96
|
redirect_uri = Egress.redirect_uri(redirect_uri)
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
)
|
|
68
|
-
authority = authorization_server(resource_metadata, issuer: issuer)
|
|
69
|
-
metadata = server_metadata(authority, allow_non_public: allow_non_public)
|
|
70
|
-
validate_endpoints(metadata, allow_non_public: allow_non_public)
|
|
71
|
-
validate_pkce(metadata)
|
|
72
|
-
resolved_scope = resolve_scope(scope, challenge_scope, resource_metadata)
|
|
97
|
+
found = discovery(url, issuer:, scope:, allow_non_public:, tolerant:)
|
|
98
|
+
metadata = found["server_metadata"]
|
|
73
99
|
registration = register(metadata, client_name:, redirect_uri:, client_id:, client_secret:,
|
|
74
|
-
token_auth_method:, allow_non_public
|
|
100
|
+
token_auth_method:, allow_non_public:,
|
|
101
|
+
client_metadata_url:)
|
|
75
102
|
verifier = SecureRandom.urlsafe_base64(48)
|
|
76
103
|
state = SecureRandom.urlsafe_base64(32)
|
|
104
|
+
requested = offline_access ? with_offline_access(found["scope"], metadata) : found["scope"]
|
|
77
105
|
grant = { client_id: registration["client_id"], redirect_uri: redirect_uri,
|
|
78
|
-
verifier: verifier, state: state, resource: resource,
|
|
79
|
-
scope:
|
|
106
|
+
verifier: verifier, state: state, resource: found["resource"],
|
|
107
|
+
scope: requested }
|
|
80
108
|
{
|
|
81
109
|
"authorize_url" => authorize_url(metadata, grant),
|
|
82
110
|
"state" => state, "code_verifier" => verifier,
|
|
83
111
|
"client_id" => registration["client_id"],
|
|
84
112
|
"client_secret" => registration["client_secret"],
|
|
85
113
|
"token_auth_method" => registration["token_endpoint_auth_method"],
|
|
86
|
-
"issuer" =>
|
|
114
|
+
"issuer" => found["issuer"],
|
|
87
115
|
"token_endpoint" => metadata.fetch("token_endpoint"),
|
|
88
|
-
"resource" => resource, "redirect_uri" => redirect_uri
|
|
116
|
+
"resource" => found["resource"], "redirect_uri" => redirect_uri,
|
|
117
|
+
"scope" => requested
|
|
89
118
|
}
|
|
90
119
|
end
|
|
91
120
|
|
|
92
|
-
#
|
|
121
|
+
# How a server signs people in, read and nothing registered: the
|
|
122
|
+
# resource tokens are asked for, the issuer, the scope a sign-in would
|
|
123
|
+
# ask for, and the two metadata documents as the servers publish them.
|
|
124
|
+
def discover(url:, issuer: nil, allow_non_public: nil, tolerant: false)
|
|
125
|
+
validate_issuer_argument(issuer) if issuer
|
|
126
|
+
discovery(url, issuer:, scope: nil, allow_non_public:, tolerant:)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# Exchange the callback's code for tokens. iss: is the iss parameter of
|
|
130
|
+
# the callback, nil when it had none. A callback that names another
|
|
131
|
+
# issuer, or names none where the authorization server says it sends
|
|
132
|
+
# one, is refused before the code is sent anywhere (RFC 9207). A host
|
|
133
|
+
# that leaves the keyword out gets no such check.
|
|
93
134
|
def complete(code:, code_verifier:, client_id:, resource:, redirect_uri:, issuer: nil,
|
|
94
135
|
client_secret: nil, token_auth_method: nil,
|
|
95
|
-
allow_non_public: nil, **)
|
|
96
|
-
|
|
136
|
+
allow_non_public: nil, iss: UNCHECKED, tolerant: false, **)
|
|
137
|
+
metadata = bound_metadata(issuer, allow_non_public:, tolerant:, resource:)
|
|
138
|
+
verify_response_issuer(iss, metadata)
|
|
139
|
+
endpoint = checked_token_endpoint(metadata, allow_non_public:)
|
|
97
140
|
form = { "grant_type" => "authorization_code", "code" => code,
|
|
98
141
|
"code_verifier" => code_verifier, "client_id" => client_id,
|
|
99
142
|
"redirect_uri" => Egress.redirect_uri(redirect_uri),
|
|
@@ -105,46 +148,121 @@ module Mistri
|
|
|
105
148
|
# Trade a refresh token for a fresh set; OAuth 2.1 rotates refresh
|
|
106
149
|
# tokens, so persist the returned one.
|
|
107
150
|
def refresh(refresh_token:, client_id:, resource:, issuer: nil, client_secret: nil,
|
|
108
|
-
token_auth_method: nil, allow_non_public: nil, **)
|
|
109
|
-
endpoint = discovered_token_endpoint(issuer, allow_non_public:
|
|
151
|
+
token_auth_method: nil, allow_non_public: nil, tolerant: false, **)
|
|
152
|
+
endpoint = discovered_token_endpoint(issuer, allow_non_public:, tolerant:, resource:)
|
|
110
153
|
form = { "grant_type" => "refresh_token", "refresh_token" => refresh_token,
|
|
111
154
|
"client_id" => client_id, "resource" => canonical(resource) }
|
|
112
155
|
token_request(endpoint, form, client_secret, token_auth_method,
|
|
113
156
|
allow_non_public: allow_non_public)
|
|
114
157
|
end
|
|
115
158
|
|
|
159
|
+
# Ends a token at its authorization server (RFC 7009). Returns false
|
|
160
|
+
# for a server that names no revocation endpoint.
|
|
161
|
+
def revoke(token:, client_id:, issuer: nil, client_secret: nil, token_auth_method: nil,
|
|
162
|
+
allow_non_public: nil, tolerant: false, resource: nil, **)
|
|
163
|
+
metadata = bound_metadata(issuer, allow_non_public:, tolerant:, resource:)
|
|
164
|
+
endpoint = metadata["revocation_endpoint"]
|
|
165
|
+
return false unless endpoint
|
|
166
|
+
|
|
167
|
+
method = stored_auth_method(client_secret, token_auth_method)
|
|
168
|
+
form, credentials = token_authentication({ "token" => token, "client_id" => client_id },
|
|
169
|
+
client_secret, method)
|
|
170
|
+
response = post(endpoint, form, basic_auth: credentials, allow_non_public:,
|
|
171
|
+
label: "revocation_endpoint")
|
|
172
|
+
return true if response.code.to_i == 200
|
|
173
|
+
|
|
174
|
+
raise TokenError.new("token revocation failed (#{response.code})",
|
|
175
|
+
status: response.code.to_i)
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# The Bearer challenge of a refusal: "error", "scope", "resource_metadata"
|
|
179
|
+
# and whatever else the server sent. Empty when it sent none.
|
|
180
|
+
def challenge(error)
|
|
181
|
+
parse_www_authenticate(error.respond_to?(:challenge) ? error.challenge : nil)
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
# The scope to sign in again with after a 403 that asks for more: what
|
|
185
|
+
# was granted and what the challenge asks for, each once. nil when the
|
|
186
|
+
# challenge names none, so the sign-in asks for its default.
|
|
187
|
+
def step_up_scope(granted, challenged)
|
|
188
|
+
return nil if challenged.to_s.strip.empty?
|
|
189
|
+
|
|
190
|
+
[granted, challenged].flat_map { |scope| scope.to_s.split }.uniq.join(" ")
|
|
191
|
+
end
|
|
192
|
+
|
|
116
193
|
# -- discovery ---------------------------------------------------------
|
|
117
194
|
|
|
195
|
+
# The steps before a registration. Without a resource document, which
|
|
196
|
+
# only tolerant discovery goes on from, the server's origin stands for
|
|
197
|
+
# the authorization server, as the 2025-03-26 revision had it.
|
|
198
|
+
def discovery(url, issuer:, scope:, allow_non_public:, tolerant:)
|
|
199
|
+
resource = canonical(url)
|
|
200
|
+
document, challenge_scope = resource_metadata_for(resource, allow_non_public:, tolerant:)
|
|
201
|
+
authority = if document
|
|
202
|
+
authorization_server(document, issuer:, tolerant:)
|
|
203
|
+
else
|
|
204
|
+
origin_as_issuer(resource, issuer)
|
|
205
|
+
end
|
|
206
|
+
metadata = server_metadata(authority, allow_non_public:, tolerant:, defaults: document.nil?)
|
|
207
|
+
validate_endpoints(metadata, allow_non_public: allow_non_public)
|
|
208
|
+
validate_pkce(metadata)
|
|
209
|
+
resource = canonical(document["resource"]) if tolerant && document
|
|
210
|
+
{ "resource" => resource, "issuer" => metadata["issuer"],
|
|
211
|
+
"scope" => resolve_scope(scope, challenge_scope, document || {}),
|
|
212
|
+
"resource_metadata" => document, "server_metadata" => metadata }
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# A client that was registered with another issuer is not handed to the
|
|
216
|
+
# origin of a server that names no authorization server.
|
|
217
|
+
def origin_as_issuer(resource, issuer)
|
|
218
|
+
origin = Egress.origin(URI(resource))
|
|
219
|
+
return origin if issuer.nil? || same_issuer?(origin, issuer)
|
|
220
|
+
|
|
221
|
+
raise IssuerChanged, "the server's origin stands for its authorization server, " \
|
|
222
|
+
"and it is not the configured issuer"
|
|
223
|
+
end
|
|
224
|
+
|
|
118
225
|
# RFC 9728: a 401's WWW-Authenticate names the resource metadata URL;
|
|
119
226
|
# servers that skip the header serve the well-known path.
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
227
|
+
#
|
|
228
|
+
# Tolerant discovery also looks at the well-known paths when the address
|
|
229
|
+
# a challenge names holds nothing, and goes on with no document only for
|
|
230
|
+
# a server that did refuse the request: one that takes it has no sign-in.
|
|
231
|
+
def resource_metadata_for(url, allow_non_public: nil, tolerant: false)
|
|
232
|
+
refusal = unauthorized(url, allow_non_public:)
|
|
233
|
+
challenge = refusal ? parse_www_authenticate(refusal["WWW-Authenticate"]) : {}
|
|
234
|
+
named = challenge["resource_metadata"]
|
|
235
|
+
candidates = named ? [[named, url]] : resource_metadata_candidates(url)
|
|
236
|
+
candidates += resource_metadata_candidates(url) if named && tolerant
|
|
237
|
+
missed = tolerant ? [] : nil
|
|
127
238
|
candidates.each do |metadata_url, expected_resource|
|
|
128
|
-
document = get_json(metadata_url, allow_non_public:
|
|
239
|
+
document = get_json(metadata_url, allow_non_public:, optional: true, missed:)
|
|
129
240
|
next unless document
|
|
130
241
|
|
|
131
|
-
validate_resource(document["resource"], expected_resource)
|
|
242
|
+
validate_resource(document["resource"], expected_resource, url, tolerant:)
|
|
132
243
|
authorization_servers(document)
|
|
133
244
|
return [document, challenge["scope"]]
|
|
134
245
|
end
|
|
246
|
+
return [nil, challenge["scope"]] if tolerant && refusal && unpublished?(missed)
|
|
247
|
+
|
|
135
248
|
raise Error, "no protected resource metadata for #{Egress.display(URI(url))}"
|
|
136
249
|
end
|
|
137
250
|
|
|
138
251
|
def challenge_parameters(url, allow_non_public: nil)
|
|
252
|
+
refusal = unauthorized(url, allow_non_public:)
|
|
253
|
+
refusal ? parse_www_authenticate(refusal["WWW-Authenticate"]) : {}
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# The server's answer to a request that carries no token, when it is a
|
|
257
|
+
# 401. nil for a server that takes the request.
|
|
258
|
+
def unauthorized(url, allow_non_public: nil)
|
|
139
259
|
targets = Egress.targets(url, allow_non_public:, label: "MCP URL")
|
|
140
260
|
request = Net::HTTP::Post.new(targets.first.uri)
|
|
141
261
|
request["Accept"] = "application/json, text/event-stream"
|
|
142
262
|
request["Content-Type"] = "application/json"
|
|
143
263
|
request.body = JSON.generate({ jsonrpc: "2.0", id: 0, method: "ping" })
|
|
144
264
|
response = http(targets, request)
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
parse_www_authenticate(response["WWW-Authenticate"])
|
|
265
|
+
response if response.code.to_i == 401
|
|
148
266
|
end
|
|
149
267
|
|
|
150
268
|
def well_known_resource_urls(url)
|
|
@@ -162,28 +280,48 @@ module Mistri
|
|
|
162
280
|
|
|
163
281
|
# RFC 8414 metadata, with the OpenID Connect path as a fallback since
|
|
164
282
|
# large providers often serve only that document.
|
|
165
|
-
|
|
283
|
+
# defaults: is for the server of the 2025-03-26 revision, which is its
|
|
284
|
+
# own authorization server and may publish no metadata at all.
|
|
285
|
+
def server_metadata(authority, allow_non_public: nil, tolerant: false, defaults: false)
|
|
166
286
|
candidates = authorization_metadata_urls(authority)
|
|
287
|
+
missed = tolerant ? [] : nil
|
|
167
288
|
candidates.each do |candidate|
|
|
168
|
-
document = get_json(candidate, allow_non_public:
|
|
289
|
+
document = get_json(candidate, allow_non_public:, optional: true, missed:)
|
|
169
290
|
next unless document
|
|
170
291
|
|
|
171
|
-
|
|
172
|
-
|
|
292
|
+
named = document["issuer"]
|
|
293
|
+
alike = same_issuer?(named, authority)
|
|
294
|
+
unless tolerant ? alike : named == authority
|
|
295
|
+
raise Error, "authorization server metadata issuer does not match #{authority}" \
|
|
296
|
+
"#{tolerable("it differs by one trailing slash") if alike}"
|
|
173
297
|
end
|
|
174
298
|
|
|
175
299
|
return document
|
|
176
300
|
end
|
|
301
|
+
return default_metadata(authority) if tolerant && defaults && unpublished?(missed)
|
|
302
|
+
|
|
177
303
|
raise Error, "no authorization server metadata at #{authority}"
|
|
178
304
|
end
|
|
179
305
|
|
|
180
|
-
def discovered_token_endpoint(issuer, allow_non_public: nil)
|
|
306
|
+
def discovered_token_endpoint(issuer, allow_non_public: nil, tolerant: false, resource: nil)
|
|
307
|
+
checked_token_endpoint(bound_metadata(issuer, allow_non_public:, tolerant:, resource:),
|
|
308
|
+
allow_non_public:)
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
# The metadata of the issuer a flow was started with. Every later
|
|
312
|
+
# request of the flow goes to an endpoint it names, and to no other. An
|
|
313
|
+
# issuer that is the origin of resource: may be a server of 2025-03-26.
|
|
314
|
+
def bound_metadata(issuer, allow_non_public: nil, tolerant: false, resource: nil)
|
|
181
315
|
unless issuer.is_a?(String) && !issuer.empty?
|
|
182
316
|
raise ConfigurationError,
|
|
183
317
|
"issuer: is required; persist flow[\"issuer\"] from OAuth.start"
|
|
184
318
|
end
|
|
185
319
|
|
|
186
|
-
|
|
320
|
+
own = tolerant && resource && same_issuer?(issuer, Egress.origin(URI(canonical(resource))))
|
|
321
|
+
server_metadata(issuer, allow_non_public:, tolerant:, defaults: own)
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
def checked_token_endpoint(metadata, allow_non_public: nil)
|
|
187
325
|
endpoint = metadata["token_endpoint"]
|
|
188
326
|
raise Error, "authorization server metadata has no token_endpoint" unless endpoint
|
|
189
327
|
|
|
@@ -191,6 +329,20 @@ module Mistri
|
|
|
191
329
|
endpoint
|
|
192
330
|
end
|
|
193
331
|
|
|
332
|
+
# RFC 9207: a code goes only to the server its response names.
|
|
333
|
+
def verify_response_issuer(iss, metadata)
|
|
334
|
+
return if iss.equal?(UNCHECKED)
|
|
335
|
+
|
|
336
|
+
if iss.nil? || iss == ""
|
|
337
|
+
return unless metadata["authorization_response_iss_parameter_supported"] == true
|
|
338
|
+
|
|
339
|
+
raise Error, "the authorization response has no iss, which its server says it sends"
|
|
340
|
+
end
|
|
341
|
+
return if iss == metadata["issuer"]
|
|
342
|
+
|
|
343
|
+
raise Error, "the authorization response's iss is not the issuer of this sign-in"
|
|
344
|
+
end
|
|
345
|
+
|
|
194
346
|
def authorization_metadata_urls(authority)
|
|
195
347
|
uri = Egress.normalize(authority, "authorization server")
|
|
196
348
|
raise Error, "authorization server must not contain a query" if uri.query
|
|
@@ -220,12 +372,16 @@ module Mistri
|
|
|
220
372
|
# Authorization-server selection is host policy whenever discovery
|
|
221
373
|
# offers a choice. Pre-registered client identities are always bound to
|
|
222
374
|
# the exact issuer supplied when they were provisioned.
|
|
223
|
-
def authorization_server(document, issuer: nil)
|
|
375
|
+
def authorization_server(document, issuer: nil, tolerant: false)
|
|
224
376
|
servers = authorization_servers(document)
|
|
225
377
|
if issuer
|
|
226
|
-
|
|
378
|
+
alike = servers.select { |server| same_issuer?(server, issuer) }
|
|
379
|
+
named = tolerant ? alike.first : alike.find { |server| server == issuer }
|
|
380
|
+
return named if named
|
|
227
381
|
|
|
228
|
-
|
|
382
|
+
slash = tolerable("one it names differs by one trailing slash") unless alike.empty?
|
|
383
|
+
raise IssuerChanged,
|
|
384
|
+
"protected resource metadata does not name the configured issuer#{slash}"
|
|
229
385
|
end
|
|
230
386
|
return servers.first if servers.one?
|
|
231
387
|
|
|
@@ -237,8 +393,12 @@ module Mistri
|
|
|
237
393
|
# returned hash keeps the token endpoint auth method the server
|
|
238
394
|
# granted, so token requests authenticate the way it expects.
|
|
239
395
|
def register(metadata, client_name:, redirect_uri:, client_id:, client_secret:,
|
|
240
|
-
token_auth_method:, allow_non_public:)
|
|
396
|
+
token_auth_method:, allow_non_public:, client_metadata_url: nil)
|
|
241
397
|
return pre_registered_client(client_id, client_secret, token_auth_method) if client_id
|
|
398
|
+
if client_metadata_url && metadata["client_id_metadata_document_supported"] == true
|
|
399
|
+
return { "client_id" => client_metadata_url, "client_secret" => nil,
|
|
400
|
+
"token_endpoint_auth_method" => "none" }
|
|
401
|
+
end
|
|
242
402
|
|
|
243
403
|
endpoint = metadata["registration_endpoint"]
|
|
244
404
|
unless endpoint
|
|
@@ -252,16 +412,10 @@ module Mistri
|
|
|
252
412
|
"redirect_uris" => [redirect_uri],
|
|
253
413
|
"grant_types" => %w[authorization_code refresh_token],
|
|
254
414
|
"response_types" => ["code"],
|
|
255
|
-
"token_endpoint_auth_method" => requested_method
|
|
415
|
+
"token_endpoint_auth_method" => requested_method,
|
|
416
|
+
"application_type" => application_type(redirect_uri)
|
|
256
417
|
}, allow_non_public: allow_non_public)
|
|
257
|
-
result =
|
|
258
|
-
"client_id" => presence(registration["client_id"]) ||
|
|
259
|
-
raise(Error, "registration returned no client_id"),
|
|
260
|
-
"client_secret" => presence(registration["client_secret"]),
|
|
261
|
-
"token_endpoint_auth_method" => presence(registration["token_endpoint_auth_method"])
|
|
262
|
-
}
|
|
263
|
-
default_method = result["client_secret"] ? requested_method : "none"
|
|
264
|
-
result["token_endpoint_auth_method"] ||= default_method
|
|
418
|
+
result = registered(registration, requested_method)
|
|
265
419
|
validate_token_auth_method(result["token_endpoint_auth_method"])
|
|
266
420
|
if result["token_endpoint_auth_method"] != "none" && !result["client_secret"]
|
|
267
421
|
raise Error, "registration selected secret authentication without a client_secret"
|
|
@@ -273,6 +427,21 @@ module Mistri
|
|
|
273
427
|
result
|
|
274
428
|
end
|
|
275
429
|
|
|
430
|
+
# A server may hand out a secret and name no method. The method is then
|
|
431
|
+
# the one asked for, and with none asked for the secret is not kept,
|
|
432
|
+
# because the client would never present it.
|
|
433
|
+
def registered(registration, requested_method)
|
|
434
|
+
named = presence(registration["token_endpoint_auth_method"])
|
|
435
|
+
secret = presence(registration["client_secret"])
|
|
436
|
+
method = named || (secret ? requested_method : "none")
|
|
437
|
+
secret = nil if named.nil? && method == "none"
|
|
438
|
+
client_id = presence(registration["client_id"])
|
|
439
|
+
raise Error, "registration returned no client_id" unless client_id
|
|
440
|
+
|
|
441
|
+
{ "client_id" => client_id, "client_secret" => secret,
|
|
442
|
+
"token_endpoint_auth_method" => method }
|
|
443
|
+
end
|
|
444
|
+
|
|
276
445
|
def pre_registered_client(client_id, client_secret, token_auth_method)
|
|
277
446
|
token_auth_method ||= client_secret ? "client_secret_basic" : "none"
|
|
278
447
|
validate_token_auth_method(token_auth_method)
|
|
@@ -374,6 +543,19 @@ module Mistri
|
|
|
374
543
|
supported.empty? ? nil : supported.uniq.join(" ")
|
|
375
544
|
end
|
|
376
545
|
|
|
546
|
+
# offline_access is what makes most servers return a refresh token. It
|
|
547
|
+
# joins a scope that is being asked for, and is never the only one: a
|
|
548
|
+
# request with no scope gets the server's default, which one scope
|
|
549
|
+
# written out would replace.
|
|
550
|
+
def with_offline_access(scope, metadata)
|
|
551
|
+
listed = metadata["scopes_supported"]
|
|
552
|
+
asked = scope.to_s.split
|
|
553
|
+
return scope if asked.empty? || asked.include?("offline_access")
|
|
554
|
+
return scope unless listed.is_a?(Array) && listed.include?("offline_access")
|
|
555
|
+
|
|
556
|
+
[*asked, "offline_access"].join(" ")
|
|
557
|
+
end
|
|
558
|
+
|
|
377
559
|
def validate_endpoints(metadata, allow_non_public: nil)
|
|
378
560
|
%w[authorization_endpoint token_endpoint].each do |required|
|
|
379
561
|
raise Error, "authorization server metadata has no #{required}" unless metadata[required]
|
|
@@ -424,11 +606,8 @@ module Mistri
|
|
|
424
606
|
end
|
|
425
607
|
|
|
426
608
|
def token_request(endpoint, form, client_secret, auth_method = nil, allow_non_public: nil)
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
auth_method ||= client_secret ? "client_secret_post" : "none"
|
|
430
|
-
validate_token_auth_method(auth_method)
|
|
431
|
-
form, credentials = token_authentication(form, client_secret, auth_method)
|
|
609
|
+
form, credentials = token_authentication(form, client_secret,
|
|
610
|
+
stored_auth_method(client_secret, auth_method))
|
|
432
611
|
payload = post_form(endpoint, form, basic_auth: credentials,
|
|
433
612
|
allow_non_public: allow_non_public)
|
|
434
613
|
validate_token_response(payload)
|
|
@@ -442,6 +621,14 @@ module Mistri
|
|
|
442
621
|
}
|
|
443
622
|
end
|
|
444
623
|
|
|
624
|
+
# 0.5.0 persisted no method and sent the secret in the form. New flows
|
|
625
|
+
# always persist an explicit method; keep old connected rows usable.
|
|
626
|
+
def stored_auth_method(client_secret, auth_method)
|
|
627
|
+
auth_method ||= client_secret ? "client_secret_post" : "none"
|
|
628
|
+
validate_token_auth_method(auth_method)
|
|
629
|
+
auth_method
|
|
630
|
+
end
|
|
631
|
+
|
|
445
632
|
def token_authentication(form, client_secret, auth_method)
|
|
446
633
|
if auth_method == "none" && client_secret
|
|
447
634
|
raise Error, "token_endpoint_auth_method none cannot use a client_secret"
|
|
@@ -455,6 +642,7 @@ module Mistri
|
|
|
455
642
|
credentials = [form["client_id"], client_secret].map do |part|
|
|
456
643
|
URI.encode_www_form_component(part)
|
|
457
644
|
end
|
|
645
|
+
form = form.except("client_id")
|
|
458
646
|
when "client_secret_post"
|
|
459
647
|
raise Error, "client_secret_post requires a client_secret" unless client_secret
|
|
460
648
|
|
|
@@ -476,25 +664,34 @@ module Mistri
|
|
|
476
664
|
raise Error, "token endpoint returned no access_token"
|
|
477
665
|
end
|
|
478
666
|
|
|
479
|
-
|
|
667
|
+
# missed: collects why an optional document was not read: the status,
|
|
668
|
+
# :unreadable for a body that is no JSON object, nil for no answer. A
|
|
669
|
+
# caller that passes it is told, and gets nil where a body is no JSON.
|
|
670
|
+
def get_json(url, allow_non_public: nil, optional: false, missed: nil)
|
|
480
671
|
targets = Egress.targets(url, allow_non_public:, label: "metadata URL")
|
|
481
672
|
location = Egress.display(targets.first.uri)
|
|
482
673
|
request = Net::HTTP::Get.new(targets.first.uri)
|
|
483
674
|
request["Accept"] = "application/json"
|
|
484
675
|
response = http(targets, request)
|
|
485
|
-
return
|
|
486
|
-
raise Error, "GET #{location} answered #{response.code}" unless response.code.to_i == 200
|
|
487
|
-
|
|
488
|
-
object = JSON.parse(response.body)
|
|
489
|
-
raise Error, "GET #{location} did not return a JSON object" unless object.is_a?(Hash)
|
|
676
|
+
return json_object(response.body, location, missed) if response.code.to_i == 200
|
|
490
677
|
|
|
491
|
-
|
|
678
|
+
missed&.push(response.code.to_i)
|
|
679
|
+
raise Error, "GET #{location} answered #{response.code}" unless optional
|
|
492
680
|
rescue ConnectionFailure
|
|
493
|
-
|
|
681
|
+
missed&.push(nil)
|
|
682
|
+
raise unless optional
|
|
683
|
+
end
|
|
684
|
+
|
|
685
|
+
def json_object(body, location, missed)
|
|
686
|
+
object = JSON.parse(body)
|
|
687
|
+
return object if object.is_a?(Hash)
|
|
688
|
+
raise Error, "GET #{location} did not return a JSON object" unless missed
|
|
494
689
|
|
|
495
|
-
|
|
690
|
+
missed.push(:unreadable) && nil
|
|
496
691
|
rescue JSON::ParserError
|
|
497
|
-
raise Error, "GET #{location} returned invalid JSON"
|
|
692
|
+
raise Error, "GET #{location} returned invalid JSON" unless missed
|
|
693
|
+
|
|
694
|
+
missed.push(:unreadable) && nil
|
|
498
695
|
end
|
|
499
696
|
|
|
500
697
|
def post_json(url, body, allow_non_public: nil)
|
|
@@ -518,13 +715,7 @@ module Mistri
|
|
|
518
715
|
end
|
|
519
716
|
|
|
520
717
|
def post_form(url, form, basic_auth: nil, allow_non_public: nil)
|
|
521
|
-
|
|
522
|
-
request = Net::HTTP::Post.new(targets.first.uri)
|
|
523
|
-
request["Content-Type"] = "application/x-www-form-urlencoded"
|
|
524
|
-
request["Accept"] = "application/json"
|
|
525
|
-
request.basic_auth(*basic_auth) if basic_auth
|
|
526
|
-
request.body = URI.encode_www_form(form)
|
|
527
|
-
response = http(targets, request)
|
|
718
|
+
response = post(url, form, basic_auth:, allow_non_public:, label: "token_endpoint")
|
|
528
719
|
payload = begin
|
|
529
720
|
JSON.parse(response.body)
|
|
530
721
|
rescue JSON::ParserError
|
|
@@ -538,11 +729,23 @@ module Mistri
|
|
|
538
729
|
reason = payload["error"]
|
|
539
730
|
reason = nil unless TOKEN_ERROR_CODES.include?(reason)
|
|
540
731
|
detail = reason ? ": #{reason}" : ""
|
|
541
|
-
|
|
732
|
+
kind = reason == "invalid_grant" ? InvalidGrantError : TokenError
|
|
733
|
+
raise kind.new("token request failed (#{response.code})#{detail}",
|
|
734
|
+
status: response.code.to_i, reason: reason)
|
|
542
735
|
end
|
|
543
736
|
payload
|
|
544
737
|
end
|
|
545
738
|
|
|
739
|
+
def post(url, form, basic_auth:, allow_non_public:, label:)
|
|
740
|
+
targets = Egress.targets(url, allow_non_public:, label:)
|
|
741
|
+
request = Net::HTTP::Post.new(targets.first.uri)
|
|
742
|
+
request["Content-Type"] = "application/x-www-form-urlencoded"
|
|
743
|
+
request["Accept"] = "application/json"
|
|
744
|
+
request.basic_auth(*basic_auth) if basic_auth
|
|
745
|
+
request.body = URI.encode_www_form(form)
|
|
746
|
+
http(targets, request)
|
|
747
|
+
end
|
|
748
|
+
|
|
546
749
|
def http(targets, request)
|
|
547
750
|
request["Accept-Encoding"] = "identity"
|
|
548
751
|
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + 15
|
|
@@ -603,15 +806,25 @@ module Mistri
|
|
|
603
806
|
nil
|
|
604
807
|
end
|
|
605
808
|
|
|
606
|
-
|
|
809
|
+
# Exact discovery takes a document that names the address it was read
|
|
810
|
+
# for. Tolerant discovery takes one that names the server, its origin
|
|
811
|
+
# or a path above it. A refusal does not show a query, because a query
|
|
812
|
+
# can hold a secret.
|
|
813
|
+
def validate_resource(candidate, expected, url, tolerant: false)
|
|
607
814
|
raise Error, "protected resource metadata has no resource" unless candidate.is_a?(String)
|
|
608
815
|
|
|
609
816
|
resource = canonical(candidate)
|
|
610
|
-
|
|
817
|
+
covered = covers?(resource, canonical(url))
|
|
818
|
+
return if tolerant ? covered : resource == canonical(expected)
|
|
819
|
+
|
|
820
|
+
refused = Egress.display(URI(tolerant ? url : expected))
|
|
821
|
+
message = "protected resource metadata resource does not match #{refused}"
|
|
822
|
+
raise Error, message unless covered
|
|
823
|
+
|
|
824
|
+
named = Egress.display(URI(resource))
|
|
825
|
+
raise Error, message + tolerable("it differs only in the query") if named == refused
|
|
611
826
|
|
|
612
|
-
raise Error,
|
|
613
|
-
"protected resource metadata resource does not match " \
|
|
614
|
-
"#{Egress.display(URI(expected))}"
|
|
827
|
+
raise Error, message + tolerable("it names #{named}", "a resource")
|
|
615
828
|
end
|
|
616
829
|
|
|
617
830
|
def parse_www_authenticate(header)
|
|
@@ -631,5 +844,13 @@ module Mistri
|
|
|
631
844
|
params
|
|
632
845
|
end
|
|
633
846
|
end # rubocop:enable Metrics/ModuleLength
|
|
847
|
+
|
|
848
|
+
# The failures that only a new sign-in ends: a token the server refused,
|
|
849
|
+
# and a grant that is spent.
|
|
850
|
+
SIGN_IN_ERRORS = [AuthenticationError, OAuth::InvalidGrantError].freeze
|
|
851
|
+
private_constant :SIGN_IN_ERRORS
|
|
634
852
|
end
|
|
635
853
|
end
|
|
854
|
+
|
|
855
|
+
require_relative "oauth/client_identity"
|
|
856
|
+
require_relative "oauth/tolerance"
|