authproto 0.3.0.beta4 → 0.3.0.beta5

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.
Files changed (106) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +21 -0
  3. data/README.md +474 -311
  4. data/examples/basic_webapp/Gemfile +14 -0
  5. data/examples/{confidential_client → basic_webapp}/Gemfile.lock +1 -1
  6. data/examples/basic_webapp/README.md +300 -0
  7. data/examples/basic_webapp/app.rb +351 -0
  8. data/examples/basic_webapp/config/oauth-client-metadata.localhost.json +11 -0
  9. data/examples/basic_webapp/public/styles.css +346 -0
  10. data/examples/basic_webapp/scripts/generate_keys.rb +35 -0
  11. data/examples/basic_webapp/views/authorized.erb +48 -0
  12. data/examples/basic_webapp/views/index.erb +48 -0
  13. data/examples/{confidential_client → basic_webapp}/views/layout.erb +2 -2
  14. data/examples/bookmarks/Gemfile +16 -0
  15. data/examples/bookmarks/Gemfile.lock +158 -0
  16. data/examples/bookmarks/README.md +170 -0
  17. data/examples/bookmarks/Rakefile +23 -0
  18. data/examples/bookmarks/app/account.rb +16 -0
  19. data/examples/bookmarks/app/bookmark.rb +89 -0
  20. data/examples/bookmarks/app/init.rb +26 -0
  21. data/examples/bookmarks/app/oauth.rb +43 -0
  22. data/examples/bookmarks/app/server.rb +184 -0
  23. data/examples/bookmarks/config/oauth-client-metadata.localhost.json +11 -0
  24. data/examples/bookmarks/config/oauth-client-metadata.production.json +19 -0
  25. data/examples/bookmarks/config.ru +5 -0
  26. data/examples/bookmarks/db/migrate/20261107004141_create_bookmarks.rb +27 -0
  27. data/examples/bookmarks/db/schema.rb +33 -0
  28. data/examples/bookmarks/public/styles.css +59 -0
  29. data/examples/bookmarks/scripts/generate_keys.rb +35 -0
  30. data/examples/bookmarks/views/bookmarks.erb +86 -0
  31. data/examples/bookmarks/views/index.erb +17 -0
  32. data/examples/bookmarks/views/layout.erb +20 -0
  33. data/examples/minisky_console/Gemfile.lock +2 -2
  34. data/examples/minisky_console/README.md +29 -25
  35. data/examples/minisky_console/app.rb +3 -3
  36. data/examples/minisky_console/localhost.rb +5 -7
  37. data/examples/minisky_console/production.rb +6 -8
  38. data/lib/authproto/client.rb +413 -126
  39. data/lib/authproto/configuration.rb +1 -1
  40. data/lib/authproto/dpop/client.rb +28 -25
  41. data/lib/authproto/dpop/key_set.rb +2 -0
  42. data/lib/authproto/dpop/nonce_manager.rb +23 -20
  43. data/lib/authproto/dpop/proof_generator.rb +6 -3
  44. data/lib/authproto/dpop/stored_nonce.rb +7 -5
  45. data/lib/authproto/dpop.rb +2 -0
  46. data/lib/authproto/encryption.rb +2 -0
  47. data/lib/authproto/http_client.rb +5 -5
  48. data/lib/authproto/http_helpers.rb +6 -0
  49. data/lib/authproto/metadata/authorization_server.rb +4 -3
  50. data/lib/authproto/metadata/client_metadata.rb +6 -2
  51. data/lib/authproto/metadata/resource_server.rb +4 -3
  52. data/lib/authproto/minisky.rb +120 -33
  53. data/lib/authproto/par/client.rb +4 -2
  54. data/lib/authproto/par/client_assertion.rb +4 -1
  55. data/lib/authproto/par/request.rb +2 -0
  56. data/lib/authproto/par/response.rb +2 -0
  57. data/lib/authproto/par.rb +2 -0
  58. data/lib/authproto/pkce.rb +2 -0
  59. data/lib/authproto/serialization/session.rb +2 -2
  60. data/lib/authproto/serialization/stored_nonce.rb +2 -2
  61. data/lib/authproto/state/session.rb +8 -41
  62. data/lib/authproto/state/session_info.rb +150 -0
  63. data/lib/authproto/state/session_manager.rb +18 -15
  64. data/lib/authproto/state.rb +3 -1
  65. data/lib/authproto/storage/interface.rb +10 -0
  66. data/lib/authproto/storage/key_builder.rb +3 -3
  67. data/lib/authproto/storage/memory.rb +24 -18
  68. data/lib/authproto/storage/sqlite.rb +14 -0
  69. data/lib/authproto/token/refresh.rb +2 -2
  70. data/lib/authproto/token/response.rb +2 -0
  71. data/lib/authproto/token/revocation.rb +4 -2
  72. data/lib/authproto/version.rb +1 -1
  73. data/lib/authproto.rb +6 -3
  74. data/sig/authproto/client.rbs +33 -10
  75. data/sig/authproto/dpop/client.rbs +9 -3
  76. data/sig/authproto/dpop/nonce_manager.rbs +6 -6
  77. data/sig/authproto/dpop/proof_generator.rbs +4 -4
  78. data/sig/authproto/dpop/stored_nonce.rbs +2 -2
  79. data/sig/authproto/http_client.rbs +23 -6
  80. data/sig/authproto/http_helpers.rbs +1 -0
  81. data/sig/authproto/metadata/authorization_server.rbs +3 -3
  82. data/sig/authproto/metadata/client_metadata.rbs +1 -1
  83. data/sig/authproto/metadata/resource_server.rbs +2 -2
  84. data/sig/authproto/minisky.rbs +57 -0
  85. data/sig/authproto/state/session.rbs +12 -6
  86. data/sig/authproto/state/session_info.rbs +23 -0
  87. data/sig/authproto/state/session_manager.rbs +7 -2
  88. data/sig/authproto/storage/interface.rbs +1 -0
  89. data/sig/authproto/storage/key_builder.rbs +1 -1
  90. data/sig/authproto/storage/memory.rbs +1 -1
  91. data/sig/authproto/storage/sqlite.rbs +1 -0
  92. data/sig/authproto/token/revocation.rbs +1 -0
  93. data/sig/authproto.rbs +3 -8
  94. metadata +37 -15
  95. data/examples/confidential_client/Gemfile +0 -14
  96. data/examples/confidential_client/README.md +0 -236
  97. data/examples/confidential_client/app.rb +0 -308
  98. data/examples/confidential_client/public/styles.css +0 -70
  99. data/examples/confidential_client/scripts/generate_keys.rb +0 -21
  100. data/examples/confidential_client/views/authorized.erb +0 -46
  101. data/examples/confidential_client/views/index.erb +0 -44
  102. data/sig/authproto/minisky_oauth.rbs +0 -27
  103. /data/examples/{confidential_client/config/oauth-client-metadata.example.json → basic_webapp/config/oauth-client-metadata.production.json} +0 -0
  104. /data/examples/{confidential_client → basic_webapp}/config.ru +0 -0
  105. /data/examples/{confidential_client → basic_webapp}/screenshots/screenshot-1-sign-in.png +0 -0
  106. /data/examples/{confidential_client → basic_webapp}/screenshots/screenshot-2-success.png +0 -0
@@ -63,7 +63,7 @@ module AuthProto
63
63
  end
64
64
 
65
65
  # Raised when global configuration settings are invalid.
66
- class ConfigurationError < Error; end
66
+ class ConfigurationError < AuthProto::Error; end
67
67
 
68
68
  #
69
69
  # Configuration class for global AuthProto settings
@@ -10,6 +10,8 @@ require_relative 'proof_generator'
10
10
 
11
11
  module AuthProto
12
12
 
13
+ #
14
+ # @api private
13
15
  #
14
16
  # Shared proof generation and server-origin nonces for DPoP authentication.
15
17
  #
@@ -51,18 +53,13 @@ module AuthProto
51
53
  #
52
54
  # @param session [State::Session] session whose DPoP key signs the request
53
55
  # @param method [String] HTTP method, converted to uppercase
54
- # @param uri [String] absolute HTTPS request URL; query and fragment are omitted from the signed `htu`
56
+ # @param uri [String, URI::HTTPS] absolute HTTPS request URL; query and fragment are omitted from the signed `htu`
55
57
  # @param access_token [String, nil] token to bind with an `ath` claim
56
58
  # @return [JOSE::SignedBinary] compact signed DPoP JWT
57
59
  # @raise [Error] if URI parsing, nonce lookup or signing fails
58
60
  #
59
61
  def generate_proof(session:, method:, uri:, access_token: nil)
60
- parsed_uri = URI(uri)
61
-
62
- server_url = "#{parsed_uri.scheme}://#{parsed_uri.host}"
63
- server_url += ":#{parsed_uri.port}" if parsed_uri.port != parsed_uri.default_port
64
-
65
- nonce = @nonce_manager.get(server_url: server_url, session_id: session.session_id)
62
+ nonce = @nonce_manager.get(url: uri, session_id: session.session_id)
66
63
 
67
64
  @proof_generator.generate(
68
65
  key_set: session.dpop_key,
@@ -81,17 +78,14 @@ module AuthProto
81
78
  #
82
79
  # @param session_id [String] session that made the request
83
80
  # @param response [Hash{Symbol => Object}] HTTP response with `:status`, `:headers`, and `:body`
84
- # @param server_url [String] request URL whose origin owns the nonce
81
+ # @param url [String, URI::HTTPS] URL of the request that produced the response
85
82
  # @return [void]
86
83
  # @raise [Error] if nonce validation or persistence fails
87
84
  #
88
- def process_response(response, server_url:, session_id:)
89
- raise Error, "Expected a response hash" unless response.is_a?(Hash)
85
+ def process_response(response, url:, session_id:)
86
+ validate_response_fields(response)
90
87
 
91
- response_headers = response[:headers]
92
- raise Error, "Missing or invalid response headers" unless response_headers.is_a?(Hash)
93
-
94
- if response_headers.keys.count { |name| name.casecmp?('DPoP-Nonce') } > 1
88
+ if response[:headers].keys.count { |name| name.casecmp?('DPoP-Nonce') } > 1
95
89
  raise Error, "Multiple DPoP-Nonce headers"
96
90
  end
97
91
 
@@ -102,7 +96,7 @@ module AuthProto
102
96
  end
103
97
 
104
98
  # Store new nonce for future requests
105
- @nonce_manager.update(nonce: nonce, server_url: server_url, session_id: session_id)
99
+ @nonce_manager.update(nonce: nonce, url: url, session_id: session_id)
106
100
  rescue StandardError => e
107
101
  raise Error, "Failed to process response: #{e.message}"
108
102
  end
@@ -118,12 +112,9 @@ module AuthProto
118
112
  # @raise [Error] if the response or its headers have an invalid type
119
113
  #
120
114
  def has_nonce_error?(response)
121
- raise Error, "Expected a response hash" unless response.is_a?(Hash)
122
-
123
- headers = response[:headers]
124
- raise Error, "Missing or invalid response headers" unless headers.is_a?(Hash)
115
+ validate_response_fields(response)
125
116
 
126
- case response[:status]
117
+ case response_status(response)
127
118
  when 400
128
119
  return false unless response[:body].is_a?(String)
129
120
 
@@ -146,22 +137,22 @@ module AuthProto
146
137
  # The block must build a fresh proof and send the request on each invocation;
147
138
  # the initial proof may already include a cached nonce.
148
139
  #
149
- # @param session [State::Session] session making the request
150
- # @param url [String] request URL whose origin owns the nonce
140
+ # @param session_id [String] ID of the session making the request
141
+ # @param url [String, URI::HTTPS] request URL whose origin owns the nonce
151
142
  # @yield sends one HTTP request using a fresh DPoP proof
152
143
  # @yieldreturn [Hash{Symbol => Object}] HTTP response with status, headers and body
153
144
  # @return [Hash{Symbol => Object}] final response, including any remaining error
154
145
  # @raise [Error] if a response lacks a valid nonce or nonce persistence fails
155
146
  #
156
- def with_nonce_handling(session, url)
147
+ def with_nonce_handling(session_id, url)
157
148
  response = yield
158
149
 
159
- process_response(response, server_url: url, session_id: session.session_id)
150
+ process_response(response, url: url, session_id: session_id)
160
151
 
161
152
  if has_nonce_error?(response)
162
153
  response = yield
163
154
 
164
- process_response(response, server_url: url, session_id: session.session_id)
155
+ process_response(response, url: url, session_id: session_id)
165
156
  end
166
157
 
167
158
  response
@@ -170,6 +161,18 @@ module AuthProto
170
161
 
171
162
  private
172
163
 
164
+ def validate_response_fields(response)
165
+ raise Error, "Expected a response hash" unless response.is_a?(Hash)
166
+
167
+ missing_keys = %i[status headers body].reject { |key| response.key?(key) }
168
+
169
+ unless missing_keys.empty?
170
+ raise Error, "Missing response keys: #{missing_keys.join(', ')}"
171
+ end
172
+
173
+ raise Error, "Missing or invalid response headers" unless response[:headers].is_a?(Hash)
174
+ end
175
+
173
176
  def nonce_challenge?(challenge)
174
177
  authentication_challenge(challenge, scheme: 'DPoP')&.fetch('error', nil) == DPoP::ERROR_USE_NONCE
175
178
  end
@@ -9,6 +9,8 @@ require_relative '../errors'
9
9
  module AuthProto
10
10
  module DPoP
11
11
 
12
+ #
13
+ # @api private
12
14
  #
13
15
  # Holds an ES256 signing keypair and imports/exports JWKs for DPoP proofs.
14
16
  # Keys are held in memory; the session serializer handles encrypted persistence.
@@ -12,6 +12,8 @@ require_relative 'stored_nonce'
12
12
  module AuthProto
13
13
  module DPoP
14
14
 
15
+ #
16
+ # @api private
15
17
  #
16
18
  # Manages DPoP nonces provided by servers during the OAuth flow.
17
19
  # Tracks separate nonces for each session and server using persistent storage.
@@ -40,15 +42,15 @@ module AuthProto
40
42
  #
41
43
  # @param nonce [String] server-provided nonce using RFC 9449's `1*NQCHAR` syntax
42
44
  # @param session_id [String] nonempty OAuth session identifier
43
- # @param server_url [String] HTTPS server URL
45
+ # @param url [String, URI::HTTPS] HTTPS URL whose origin owns the nonce
44
46
  # @return [void]
45
47
  # @raise [NonceError] if inputs are invalid or storage reports failure
46
48
  # @raise [Storage::StorageError] if backend validation or storage fails
47
49
  #
48
- def update(nonce:, server_url:, session_id:)
50
+ def update(nonce:, url:, session_id:)
49
51
  validate_session_id(session_id)
50
- validate_inputs(nonce, server_url)
51
- origin = normalize_server_url(server_url)
52
+ validate_inputs(nonce, url)
53
+ origin = normalize_url(url)
52
54
 
53
55
  stored_nonce = StoredNonce.new(nonce, origin)
54
56
  serialized = @serializer.serialize(stored_nonce)
@@ -62,15 +64,15 @@ module AuthProto
62
64
  # Retrieves the current nonce for a session and URL's origin.
63
65
  #
64
66
  # @param session_id [String] nonempty OAuth session identifier
65
- # @param server_url [String] HTTPS server URL
67
+ # @param url [String, URI::HTTPS] HTTPS URL whose origin owns the nonce
66
68
  # @return [String, nil] stored nonce, or `nil` when missing/expired according to storage TTL
67
69
  # @raise [NonceError] if the session ID or URL is invalid, or serialized data cannot be read
68
70
  # @raise [Storage::StorageError] if storage retrieval fails
69
71
  #
70
- def get(server_url:, session_id:)
72
+ def get(url:, session_id:)
71
73
  validate_session_id(session_id)
72
- validate_server_url(server_url)
73
- origin = normalize_server_url(server_url)
74
+ validate_url(url)
75
+ origin = normalize_url(url)
74
76
  key = Storage::KeyBuilder.nonce_key(session_id, origin)
75
77
 
76
78
  stored = AuthProto.storage.get(key)
@@ -87,19 +89,20 @@ module AuthProto
87
89
  # Removes the stored nonce for a session and URL's origin.
88
90
  #
89
91
  # @param session_id [String] nonempty OAuth session identifier
90
- # @param server_url [String] HTTPS server URL
92
+ # @param url [String, URI::HTTPS] HTTPS URL whose origin owns the nonce
91
93
  # @return [Boolean] backend deletion status
92
94
  # @raise [NonceError] if the session ID or URL is invalid
93
95
  # @raise [Storage::StorageError] if deletion fails
94
96
  #
95
- def clear(server_url:, session_id:)
97
+ def clear(url:, session_id:)
96
98
  validate_session_id(session_id)
97
- validate_server_url(server_url)
98
- origin = normalize_server_url(server_url)
99
+ validate_url(url)
100
+ origin = normalize_url(url)
99
101
  key = Storage::KeyBuilder.nonce_key(session_id, origin)
100
102
  AuthProto.storage.delete(key)
101
103
  end
102
104
 
105
+
103
106
  private
104
107
 
105
108
  def validate_session_id(session_id)
@@ -108,7 +111,7 @@ module AuthProto
108
111
  raise NonceError, "session_id must be a nonempty string"
109
112
  end
110
113
 
111
- def normalize_server_url(url)
114
+ def normalize_url(url)
112
115
  uri = URI(url)
113
116
  port = uri.port
114
117
  port = nil if uri.scheme == 'https' && port == 443
@@ -118,22 +121,22 @@ module AuthProto
118
121
  origin
119
122
  end
120
123
 
121
- def validate_inputs(nonce, server_url)
124
+ def validate_inputs(nonce, url)
122
125
  raise NonceError, "nonce must not be empty" if nonce.to_s == ''
123
126
  raise NonceError, "Invalid DPoP nonce" unless nonce.is_a?(String)
124
127
  raise NonceError, "Invalid characters in the DPoP nonce" unless nonce.ascii_only? && nonce =~ NONCE_REGEXP
125
128
 
126
- validate_server_url(server_url)
129
+ validate_url(url)
127
130
  end
128
131
 
129
- def validate_server_url(server_url)
130
- raise NonceError, "server_url is required" if server_url.nil? || server_url.empty?
132
+ def validate_url(url)
133
+ raise NonceError, "url is required" if url.nil? || url == ''
131
134
 
132
- uri = ValidatedURI.new(server_url)
135
+ uri = ValidatedURI.new(url)
133
136
 
134
- raise NonceError, "server_url must be HTTPS" unless uri.scheme == 'https'
137
+ raise NonceError, "url must be HTTPS" unless uri.scheme == 'https'
135
138
  rescue URI::InvalidURIError => e
136
- raise NonceError, "invalid server_url: #{e.message}"
139
+ raise NonceError, "invalid url: #{e.message}"
137
140
  end
138
141
  end
139
142
  end
@@ -13,6 +13,8 @@ require_relative 'key_set'
13
13
  module AuthProto
14
14
  module DPoP
15
15
 
16
+ #
17
+ # @api private
16
18
  #
17
19
  # Creates and manages DPoP proof JWTs according to RFC 9449.
18
20
  # DPoP proofs are used to prove possession of a key when making
@@ -32,7 +34,7 @@ module AuthProto
32
34
  #
33
35
  # @param key_set [KeySet] key belonging to the session making this request
34
36
  # @param method [String] nonempty HTTP method, converted to uppercase
35
- # @param uri [String] absolute HTTPS request URL
37
+ # @param uri [String, URI::HTTPS] absolute HTTPS request URL
36
38
  # @param nonce [String, nil] server-provided nonce to include
37
39
  # @param access_token [String, nil] access token to bind to the proof
38
40
  # @return [JOSE::SignedBinary] compact signed DPoP JWT
@@ -56,11 +58,12 @@ module AuthProto
56
58
  raise ProofError, "Failed to generate proof: #{e.message}"
57
59
  end
58
60
 
61
+
59
62
  private
60
63
 
61
64
  def validate_inputs(method, uri)
62
65
  raise ProofError, "method is required" if method.nil? || method.empty?
63
- raise ProofError, "uri is required" if uri.nil? || uri.empty?
66
+ raise ProofError, "uri is required" if uri.nil? || uri == ''
64
67
 
65
68
  parsed_uri = ValidatedURI.new(uri)
66
69
  raise ProofError, "uri must use HTTPS" unless parsed_uri.scheme == 'https'
@@ -94,7 +97,7 @@ module AuthProto
94
97
  end
95
98
 
96
99
  def normalize_uri(uri)
97
- uri = URI(uri)
100
+ uri = URI(uri).dup
98
101
 
99
102
  # Remove default ports
100
103
  uri.port = nil if uri.scheme == 'https' && uri.port == 443
@@ -4,7 +4,9 @@ module AuthProto
4
4
  module DPoP
5
5
 
6
6
  #
7
- # Represents a stored nonce with its server URL
7
+ # @api private
8
+ #
9
+ # Represents a stored nonce with its server origin.
8
10
  #
9
11
 
10
12
  class StoredNonce
@@ -15,7 +17,7 @@ module AuthProto
15
17
 
16
18
  # Server origin associated with the nonce.
17
19
  # @return [String]
18
- attr_reader :server_url
20
+ attr_reader :origin
19
21
 
20
22
  # Creation timestamp; defaults to integer Unix seconds.
21
23
  # @return [Integer, Time]
@@ -24,12 +26,12 @@ module AuthProto
24
26
  # Creates a nonce record without validation.
25
27
  #
26
28
  # @param value [String] server-provided nonce
27
- # @param server_url [String] server origin URL
29
+ # @param origin [String] server origin URL
28
30
  # @param timestamp [Integer, Time, nil] timestamp to retain, or `nil` for current Unix seconds
29
31
  #
30
- def initialize(value, server_url, timestamp: nil)
32
+ def initialize(value, origin, timestamp: nil)
31
33
  @value = value
32
- @server_url = server_url
34
+ @origin = origin
33
35
  @timestamp = timestamp || Time.now.to_i
34
36
  end
35
37
  end
@@ -2,6 +2,8 @@
2
2
 
3
3
  module AuthProto
4
4
 
5
+ #
6
+ # @api private
5
7
  #
6
8
  # Demonstrating Proof of Possession (DPoP) signing and request authentication.
7
9
  #
@@ -8,6 +8,8 @@ require_relative 'errors'
8
8
 
9
9
  module AuthProto
10
10
 
11
+ #
12
+ # @api private
11
13
  #
12
14
  # AES-GCM envelopes and context-specific key derivation for stored OAuth secrets.
13
15
  #
@@ -54,13 +54,13 @@ module AuthProto
54
54
  MAX_RESPONSE_SIZE = 10 * 1024 * 1024 # 10MB
55
55
 
56
56
  # Error raised when a request is blocked due to SSRF protection
57
- class SSRFError < Error; end
57
+ class SSRFError < AuthProto::Error; end
58
58
 
59
59
  #
60
60
  # Error raised when an HTTP request fails
61
61
  #
62
62
 
63
- class HTTPError < Error
63
+ class HTTPError < AuthProto::Error
64
64
 
65
65
  # Response associated with the failure, when retained by the transport.
66
66
  # @return [Net::HTTPResponse, nil]
@@ -106,7 +106,7 @@ module AuthProto
106
106
  # Non-2xx status codes are returned for the caller to interpret; they do not
107
107
  # themselves raise an exception.
108
108
  #
109
- # @param url [String] absolute HTTPS request URL without a fragment
109
+ # @param url [String, URI::HTTPS] absolute HTTPS request URL without a fragment
110
110
  # @param headers [Hash{String => String}] additional request headers; can override the default `Cache-Control` value
111
111
  # @param follow_redirects [Boolean] follow redirects; defaults to false
112
112
  # @return [Hash{Symbol => Integer, Hash{String => String}, String, nil}] `:status` (Integer), lowercase `:headers` (Hash), and `:body` (String or nil)
@@ -136,7 +136,7 @@ module AuthProto
136
136
  # Non-2xx status codes are returned for the caller to interpret; they do not
137
137
  # themselves raise an exception.
138
138
  #
139
- # @param url [String] absolute HTTPS request URL without a fragment
139
+ # @param url [String, URI::HTTPS] absolute HTTPS request URL without a fragment
140
140
  # @param body [String, Hash, nil] raw bytes, form fields encoded with `URI.encode_www_form`, or no body
141
141
  # @param headers [Hash{String => String}] additional request headers; can override the default `Cache-Control` value
142
142
  # @return [Hash{Symbol => Integer, Hash{String => String}, String, nil}] `:status` (Integer), lowercase `:headers` (Hash), and `:body` (String or nil)
@@ -283,7 +283,7 @@ module AuthProto
283
283
  location = URI(response["location"])
284
284
  location = kwargs[:original_uri] + location if location.relative?
285
285
 
286
- validate_uri(location.to_s)
286
+ validate_uri(location)
287
287
  validate_ip(location)
288
288
 
289
289
  # Increment redirect count for the next request
@@ -12,6 +12,11 @@ module AuthProto
12
12
 
13
13
  private
14
14
 
15
+ def response_status(response)
16
+ status = response[:status]
17
+ status.is_a?(String) && /\A[0-9]+\z/.match?(status) ? status.to_i : status
18
+ end
19
+
15
20
  # Looks up a response header without regard to the case of its name.
16
21
  #
17
22
  # @param response [Hash{Symbol => Object}] HTTP response with an optional `:headers` hash
@@ -21,6 +26,7 @@ module AuthProto
21
26
  #
22
27
  def response_header(response, name, strip_parameters: false)
23
28
  value = (response[:headers] || {}).find { |header, value| header.casecmp?(name) }&.last
29
+ value = value.join(', ') if value.is_a?(Array)
24
30
  return value unless value && strip_parameters
25
31
 
26
32
  value.split(';', 2).first.to_s.strip
@@ -121,7 +121,7 @@ module AuthProto
121
121
  #
122
122
  # The document's issuer must exactly match the requested url.
123
123
  #
124
- # @param url [String] authorization server issuer origin
124
+ # @param url [String, URI::HTTPS] authorization server issuer origin
125
125
  # @return [AuthorizationServer] validated discovery metadata
126
126
  # @raise [InvalidAuthorizationServer] if HTTP transport fails, JSON is invalid, or metadata/issuer validation fails
127
127
  # @raise [HTTPClient::SSRFError] if the transport rejects the initial destination
@@ -129,6 +129,7 @@ module AuthProto
129
129
  #
130
130
  def self.from_url(url)
131
131
  validate_source_url(url)
132
+ url = url.to_s
132
133
 
133
134
  response = fetch_metadata(url)
134
135
  validate_response(response)
@@ -335,13 +336,13 @@ module AuthProto
335
336
  def fetch_metadata(url)
336
337
  metadata_url = URI.join(url, "/.well-known/oauth-authorization-server")
337
338
 
338
- AuthProto.configuration.http_client.get(metadata_url.to_s)
339
+ AuthProto.configuration.http_client.get(metadata_url)
339
340
  rescue HTTPClient::HTTPError => e
340
341
  raise InvalidAuthorizationServer, "Failed to fetch authorization server metadata: #{e.message}"
341
342
  end
342
343
 
343
344
  def validate_response(response)
344
- unless response[:status] == 200
345
+ unless response_status(response) == 200
345
346
  raise InvalidAuthorizationServer, "Authorization server metadata must return HTTP 200"
346
347
  end
347
348
 
@@ -145,7 +145,7 @@ module AuthProto
145
145
  # The document's `client_id` must equal the requested URL.
146
146
  # Localhost HTTP client IDs generate virtual metadata without a network request.
147
147
  #
148
- # @param url [String] HTTPS metadata URL, or localhost URL for development
148
+ # @param url [String, URI::HTTPS] HTTPS metadata URL, or localhost URL for development
149
149
  # @return [ClientMetadata] validated metadata
150
150
  # @raise [InvalidClientMetadata] if metadata is missing, invalid/mismatched, not JSON,
151
151
  # or transport isn't configured
@@ -154,6 +154,7 @@ module AuthProto
154
154
  #
155
155
  def self.from_url(url)
156
156
  uri = ValidatedURI.new(url)
157
+ url = url.to_s
157
158
 
158
159
  unless uri.scheme == 'https' || (uri.scheme == 'http' && uri.host == 'localhost')
159
160
  raise InvalidClientMetadata, "client_id must use HTTPS except for localhost"
@@ -165,7 +166,7 @@ module AuthProto
165
166
 
166
167
  response = AuthProto.configuration.http_client.get(url)
167
168
 
168
- unless response[:status] == 200
169
+ unless response_status(response) == 200
169
170
  raise InvalidClientMetadata,
170
171
  "expected HTTP 200 response for client metadata request, got #{response[:status]}"
171
172
  end
@@ -303,6 +304,7 @@ module AuthProto
303
304
  raise InvalidClientMetadata, "grant_types must be an array" unless grant_types.is_a?(Array)
304
305
 
305
306
  unless grant_types.uniq.length == grant_types.length
307
+ # follows the reference implementation
306
308
  raise InvalidClientMetadata, "grant_types must not contain duplicates"
307
309
  end
308
310
 
@@ -364,6 +366,7 @@ module AuthProto
364
366
  raise InvalidClientMetadata, "redirect URI must not contain a fragment" if uri.fragment
365
367
 
366
368
  if ['localhost', '127.0.0.1', '[::1]'].include?(uri.host.downcase)
369
+ # follows the reference implementation
367
370
  raise InvalidClientMetadata, "HTTPS redirect URI must not use a loopback host"
368
371
  end
369
372
 
@@ -435,6 +438,7 @@ module AuthProto
435
438
  raise InvalidClientMetadata, "atproto scope is required" unless scope_values.include?('atproto')
436
439
 
437
440
  unless scope_values.uniq.length == scope_values.length
441
+ # follows the reference implementation
438
442
  raise InvalidClientMetadata, "scope must not contain duplicate tokens"
439
443
  end
440
444
 
@@ -44,7 +44,7 @@ module AuthProto
44
44
  # Requires HTTPS even when the configured transport permits HTTP.
45
45
  # The PDS URL must have no userinfo, path prefix, query or fragment.
46
46
  #
47
- # @param url [String] resource server/PDS base URL
47
+ # @param url [String, URI::HTTPS] resource server/PDS base URL
48
48
  # @return [ResourceServer] validated resource server metadata
49
49
  # @raise [InvalidAuthorizationServer] if HTTP transport, JSON parsing or metadata validation fails
50
50
  # @raise [HTTPClient::SSRFError] if the transport rejects the initial destination
@@ -52,6 +52,7 @@ module AuthProto
52
52
  #
53
53
  def self.from_url(url)
54
54
  validate_source_url(url)
55
+ url = url.to_s
55
56
 
56
57
  response = fetch_metadata(url)
57
58
  validate_response(response)
@@ -99,13 +100,13 @@ module AuthProto
99
100
  def fetch_metadata(url)
100
101
  metadata_url = URI.join(url, "/.well-known/oauth-protected-resource")
101
102
 
102
- AuthProto.configuration.http_client.get(metadata_url.to_s)
103
+ AuthProto.configuration.http_client.get(metadata_url)
103
104
  rescue HTTPClient::HTTPError => e
104
105
  raise InvalidAuthorizationServer, "Failed to fetch resource server metadata: #{e.message}"
105
106
  end
106
107
 
107
108
  def validate_response(response)
108
- unless response[:status] == 200
109
+ unless response_status(response) == 200
109
110
  raise InvalidAuthorizationServer, "Resource server metadata must return HTTP 200"
110
111
  end
111
112