authproto 0.3.0.beta1 → 0.3.0.beta3
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 +67 -7
- data/README.md +128 -5
- data/examples/confidential_client/Gemfile.lock +5 -1
- data/examples/confidential_client/README.md +12 -2
- data/examples/confidential_client/app.rb +23 -22
- data/examples/confidential_client/config/{client-metadata.example.json → oauth-client-metadata.example.json} +0 -1
- data/examples/confidential_client/scripts/generate_keys.rb +15 -9
- data/examples/minisky_console/Gemfile.lock +2 -2
- data/examples/minisky_console/README.md +20 -1
- data/examples/minisky_console/localhost.rb +13 -28
- data/examples/minisky_console/production.rb +10 -4
- data/lib/authproto/client.rb +185 -253
- data/lib/authproto/configuration.rb +37 -16
- data/lib/authproto/dpop/client.rb +96 -79
- data/lib/authproto/dpop/key_set.rb +157 -0
- data/lib/authproto/dpop/nonce_manager.rb +38 -39
- data/lib/authproto/dpop/proof_generator.rb +39 -50
- data/lib/authproto/dpop.rb +19 -0
- data/lib/authproto/errors.rb +1 -1
- data/lib/authproto/http_client.rb +17 -31
- data/lib/authproto/http_helpers.rb +85 -0
- data/lib/authproto/localhost_server.rb +15 -6
- data/lib/authproto/metadata/authorization_server.rb +364 -0
- data/lib/authproto/metadata/client_metadata.rb +587 -0
- data/lib/authproto/metadata/resource_server.rb +139 -0
- data/lib/authproto/{server_metadata.rb → metadata.rb} +7 -7
- data/lib/authproto/minisky.rb +41 -41
- data/lib/authproto/par/client.rb +60 -115
- data/lib/authproto/par/client_assertion.rb +95 -21
- data/lib/authproto/par/request.rb +60 -146
- data/lib/authproto/par/response.rb +9 -10
- data/lib/authproto/par.rb +5 -6
- data/lib/authproto/pkce.rb +29 -106
- data/lib/authproto/serialization/session.rb +36 -18
- data/lib/authproto/serialization/token_set.rb +6 -7
- data/lib/authproto/state/session.rb +93 -37
- data/lib/authproto/state/session_manager.rb +97 -23
- data/lib/authproto/state/token_set.rb +19 -19
- data/lib/authproto/state.rb +4 -4
- data/lib/authproto/storage/key_builder.rb +3 -11
- data/lib/authproto/storage/memory.rb +8 -2
- data/lib/authproto/token/refresh.rb +50 -91
- data/lib/authproto/validated_uri.rb +80 -0
- data/lib/authproto/version.rb +1 -1
- data/lib/authproto.rb +1 -1
- data/sig/authproto/client.rbs +11 -14
- data/sig/authproto/dependencies.rbs +2 -0
- data/sig/authproto/dpop/client.rbs +9 -9
- data/sig/authproto/dpop/{key_manager.rbs → key_set.rbs} +9 -11
- data/sig/authproto/dpop/nonce_manager.rbs +4 -5
- data/sig/authproto/dpop/proof_generator.rbs +10 -15
- data/sig/authproto/dpop.rbs +6 -0
- data/sig/authproto/http_client.rbs +2 -2
- data/sig/authproto/http_helpers.rbs +7 -0
- data/sig/authproto/localhost_server.rbs +1 -1
- data/sig/authproto/{server_metadata → metadata}/authorization_server.rbs +19 -12
- data/sig/authproto/metadata/client_metadata.rbs +119 -0
- data/sig/authproto/metadata/resource_server.rbs +28 -0
- data/sig/authproto/minisky_oauth.rbs +9 -4
- data/sig/authproto/par/client.rbs +8 -14
- data/sig/authproto/par/client_assertion.rbs +6 -1
- data/sig/authproto/par/request.rbs +5 -28
- data/sig/authproto/par/response.rbs +1 -5
- data/sig/authproto/pkce.rbs +6 -16
- data/sig/authproto/serialization/session.rbs +2 -2
- data/sig/authproto/state/session.rbs +13 -9
- data/sig/authproto/state/session_manager.rbs +9 -4
- data/sig/authproto/state/token_set.rbs +1 -2
- data/sig/authproto/storage/key_builder.rbs +1 -2
- data/sig/authproto/token/refresh.rbs +6 -7
- data/sig/authproto/validated_uri.rbs +36 -0
- data/sig/authproto.rbs +10 -5
- metadata +24 -27
- data/.rspec +0 -1
- data/.rubocop.yml +0 -31
- data/.tm_properties +0 -1
- data/.yardopts +0 -4
- data/Rakefile +0 -8
- data/examples/confidential_client/.gitignore +0 -5
- data/examples/minisky_console/.gitignore +0 -2
- data/lib/authproto/client_metadata.rb +0 -478
- data/lib/authproto/dpop/key_manager.rb +0 -223
- data/lib/authproto/serialization/dpop_key.rb +0 -38
- data/lib/authproto/server_metadata/authorization_server.rb +0 -267
- data/lib/authproto/server_metadata/origin_url.rb +0 -71
- data/lib/authproto/server_metadata/resource_server.rb +0 -98
- data/sig/authproto/client_metadata.rbs +0 -113
- data/sig/authproto/serialization/dpop_key.rbs +0 -14
- data/sig/authproto/server_metadata/origin_url.rbs +0 -21
- data/sig/authproto/server_metadata/resource_server.rbs +0 -27
|
@@ -6,6 +6,7 @@ require_relative '../configuration'
|
|
|
6
6
|
require_relative '../errors'
|
|
7
7
|
require_relative '../serialization/stored_nonce'
|
|
8
8
|
require_relative '../storage/key_builder'
|
|
9
|
+
require_relative '../validated_uri'
|
|
9
10
|
require_relative 'stored_nonce'
|
|
10
11
|
|
|
11
12
|
module AuthProto
|
|
@@ -13,61 +14,64 @@ module AuthProto
|
|
|
13
14
|
|
|
14
15
|
#
|
|
15
16
|
# Manages DPoP nonces provided by servers during the OAuth flow.
|
|
16
|
-
# Tracks separate nonces for each server using persistent storage.
|
|
17
|
+
# Tracks separate nonces for each session and server using persistent storage.
|
|
17
18
|
# Thread-safe to handle concurrent requests.
|
|
18
19
|
#
|
|
19
20
|
|
|
20
21
|
class NonceManager
|
|
21
22
|
|
|
23
|
+
# Regexp for checking the nonce value for invalid characters
|
|
24
|
+
NONCE_REGEXP = /\A[\x21\x23-\x5B\x5D-\x7E]+\z/
|
|
25
|
+
|
|
22
26
|
# Error for nonce-related issues
|
|
23
27
|
class NonceError < AuthProto::Error; end
|
|
24
28
|
|
|
25
|
-
# Default stored nonce lifetime in seconds.
|
|
26
|
-
# @return [Integer]
|
|
27
|
-
DEFAULT_TTL = 300 # 5 minutes
|
|
28
|
-
|
|
29
29
|
# Creates a nonce manager using the global storage backend.
|
|
30
30
|
#
|
|
31
|
-
# @param ttl [Integer, nil] nonce lifetime in seconds; `nil` uses {
|
|
31
|
+
# @param ttl [Integer, nil] nonce lifetime in seconds; `nil` uses {Configuration#dpop_nonce_lifetime}
|
|
32
32
|
# @raise [ArgumentError] if the master encryption key is not valid Base64
|
|
33
33
|
#
|
|
34
34
|
def initialize(ttl: nil)
|
|
35
|
-
@ttl = ttl ||
|
|
35
|
+
@ttl = ttl || AuthProto.configuration.dpop_nonce_lifetime
|
|
36
36
|
@serializer = Serialization::StoredNonce.new
|
|
37
37
|
end
|
|
38
38
|
|
|
39
|
-
# Stores a nonce for the URL's origin, replacing any previous value.
|
|
39
|
+
# Stores a nonce for the session and URL's origin, replacing any previous value.
|
|
40
40
|
#
|
|
41
|
-
# @param nonce [String]
|
|
41
|
+
# @param nonce [String] server-provided nonce using RFC 9449's `1*NQCHAR` syntax
|
|
42
|
+
# @param session_id [String] nonempty OAuth session identifier
|
|
42
43
|
# @param server_url [String] HTTPS URL, or HTTP URL with host `localhost`
|
|
43
44
|
# @return [void]
|
|
44
45
|
# @raise [NonceError] if inputs are invalid or storage reports failure
|
|
45
46
|
# @raise [Storage::StorageError] if backend validation or storage fails
|
|
46
47
|
#
|
|
47
|
-
def update(nonce:, server_url:)
|
|
48
|
+
def update(nonce:, server_url:, session_id:)
|
|
49
|
+
validate_session_id(session_id)
|
|
48
50
|
validate_inputs(nonce, server_url)
|
|
49
51
|
origin = normalize_server_url(server_url)
|
|
50
52
|
|
|
51
53
|
stored_nonce = StoredNonce.new(nonce, origin)
|
|
52
54
|
serialized = @serializer.serialize(stored_nonce)
|
|
53
55
|
|
|
54
|
-
key = Storage::KeyBuilder.nonce_key(origin)
|
|
56
|
+
key = Storage::KeyBuilder.nonce_key(session_id, origin)
|
|
55
57
|
return if AuthProto.storage.set(key, serialized, ttl: @ttl)
|
|
56
58
|
|
|
57
59
|
raise NonceError, "Failed to store nonce"
|
|
58
60
|
end
|
|
59
61
|
|
|
60
|
-
# Retrieves the current nonce for a URL's origin.
|
|
62
|
+
# Retrieves the current nonce for a session and URL's origin.
|
|
61
63
|
#
|
|
64
|
+
# @param session_id [String] nonempty OAuth session identifier
|
|
62
65
|
# @param server_url [String] HTTPS URL, or HTTP URL with host `localhost`
|
|
63
66
|
# @return [String, nil] stored nonce, or `nil` when missing/expired according to storage TTL
|
|
64
|
-
# @raise [NonceError] if the URL is invalid or serialized data cannot be read
|
|
67
|
+
# @raise [NonceError] if the session ID or URL is invalid, or serialized data cannot be read
|
|
65
68
|
# @raise [Storage::StorageError] if storage retrieval fails
|
|
66
69
|
#
|
|
67
|
-
def get(server_url)
|
|
70
|
+
def get(server_url:, session_id:)
|
|
71
|
+
validate_session_id(session_id)
|
|
68
72
|
validate_server_url(server_url)
|
|
69
73
|
origin = normalize_server_url(server_url)
|
|
70
|
-
key = Storage::KeyBuilder.nonce_key(origin)
|
|
74
|
+
key = Storage::KeyBuilder.nonce_key(session_id, origin)
|
|
71
75
|
|
|
72
76
|
stored = AuthProto.storage.get(key)
|
|
73
77
|
return nil unless stored
|
|
@@ -80,41 +84,35 @@ module AuthProto
|
|
|
80
84
|
end
|
|
81
85
|
end
|
|
82
86
|
|
|
83
|
-
# Removes the stored nonce for a URL's origin.
|
|
87
|
+
# Removes the stored nonce for a session and URL's origin.
|
|
84
88
|
#
|
|
89
|
+
# @param session_id [String] nonempty OAuth session identifier
|
|
85
90
|
# @param server_url [String] HTTPS URL, or HTTP URL with host `localhost`
|
|
86
91
|
# @return [Boolean] backend deletion status
|
|
87
|
-
# @raise [NonceError] if the URL is invalid
|
|
92
|
+
# @raise [NonceError] if the session ID or URL is invalid
|
|
88
93
|
# @raise [Storage::StorageError] if deletion fails
|
|
89
94
|
#
|
|
90
|
-
def clear(server_url)
|
|
95
|
+
def clear(server_url:, session_id:)
|
|
96
|
+
validate_session_id(session_id)
|
|
91
97
|
validate_server_url(server_url)
|
|
92
98
|
origin = normalize_server_url(server_url)
|
|
93
|
-
key = Storage::KeyBuilder.nonce_key(origin)
|
|
99
|
+
key = Storage::KeyBuilder.nonce_key(session_id, origin)
|
|
94
100
|
AuthProto.storage.delete(key)
|
|
95
101
|
end
|
|
96
102
|
|
|
97
|
-
# Checks for an unexpired nonce record in storage for a URL's origin.
|
|
98
|
-
#
|
|
99
|
-
# @param server_url [String] HTTPS URL, or HTTP URL with host `localhost`
|
|
100
|
-
# @return [Boolean] whether the backend reports a nonce record present
|
|
101
|
-
# @raise [NonceError] if the URL is invalid
|
|
102
|
-
# @raise [Storage::StorageError] if the existence check fails
|
|
103
|
-
#
|
|
104
|
-
def valid_nonce?(server_url)
|
|
105
|
-
validate_server_url(server_url)
|
|
106
|
-
origin = normalize_server_url(server_url)
|
|
107
|
-
key = Storage::KeyBuilder.nonce_key(origin)
|
|
108
|
-
AuthProto.storage.exists?(key)
|
|
109
|
-
end
|
|
110
|
-
|
|
111
103
|
private
|
|
112
104
|
|
|
105
|
+
def validate_session_id(session_id)
|
|
106
|
+
return if session_id.is_a?(String) && !session_id.empty?
|
|
107
|
+
|
|
108
|
+
raise NonceError, "session_id must be a nonempty string"
|
|
109
|
+
end
|
|
110
|
+
|
|
113
111
|
def normalize_server_url(url)
|
|
114
112
|
uri = URI(url)
|
|
115
113
|
port = uri.port
|
|
116
|
-
port = nil if (uri.scheme ==
|
|
117
|
-
(uri.scheme ==
|
|
114
|
+
port = nil if (uri.scheme == 'https' && port == 443) ||
|
|
115
|
+
(uri.scheme == 'http' && port == 80)
|
|
118
116
|
|
|
119
117
|
origin = "#{uri.scheme}://#{uri.host}"
|
|
120
118
|
origin = "#{origin}:#{port}" if port
|
|
@@ -122,7 +120,9 @@ module AuthProto
|
|
|
122
120
|
end
|
|
123
121
|
|
|
124
122
|
def validate_inputs(nonce, server_url)
|
|
125
|
-
raise NonceError, "nonce
|
|
123
|
+
raise NonceError, "nonce must not be empty" if nonce.to_s == ''
|
|
124
|
+
raise NonceError, "Invalid DPoP nonce" unless nonce.is_a?(String)
|
|
125
|
+
raise NonceError, "Invalid characters in the DPoP nonce" unless nonce.ascii_only? && nonce =~ NONCE_REGEXP
|
|
126
126
|
|
|
127
127
|
validate_server_url(server_url)
|
|
128
128
|
end
|
|
@@ -130,11 +130,10 @@ module AuthProto
|
|
|
130
130
|
def validate_server_url(server_url)
|
|
131
131
|
raise NonceError, "server_url is required" if server_url.nil? || server_url.empty?
|
|
132
132
|
|
|
133
|
-
uri =
|
|
134
|
-
raise NonceError, "server_url must be HTTP(S)" unless uri.is_a?(URI::HTTP)
|
|
133
|
+
uri = ValidatedURI.new(server_url)
|
|
135
134
|
|
|
136
135
|
# Allow HTTP for localhost only
|
|
137
|
-
if uri.host !=
|
|
136
|
+
if uri.host != 'localhost' && uri.scheme != 'https'
|
|
138
137
|
raise NonceError, "server_url must be HTTPS (except for localhost)"
|
|
139
138
|
end
|
|
140
139
|
rescue URI::InvalidURIError => e
|
|
@@ -5,8 +5,10 @@ require 'openssl'
|
|
|
5
5
|
require 'securerandom'
|
|
6
6
|
require 'uri'
|
|
7
7
|
|
|
8
|
+
require_relative '../dpop'
|
|
8
9
|
require_relative '../errors'
|
|
9
|
-
require_relative '
|
|
10
|
+
require_relative '../validated_uri'
|
|
11
|
+
require_relative 'key_set'
|
|
10
12
|
|
|
11
13
|
module AuthProto
|
|
12
14
|
module DPoP
|
|
@@ -23,95 +25,82 @@ module AuthProto
|
|
|
23
25
|
# Error raised for proof generation/validation issues
|
|
24
26
|
class ProofError < AuthProto::Error; end
|
|
25
27
|
|
|
26
|
-
# Key manager used to sign proofs.
|
|
27
|
-
# @return [KeyManager]
|
|
28
|
-
attr_reader :key_manager
|
|
29
|
-
|
|
30
|
-
# Creates a new ProofGenerator instance
|
|
31
|
-
#
|
|
32
|
-
# @param key_manager [KeyManager] Key manager to use for signing proofs
|
|
33
|
-
# @raise [ProofError] if key_manager is invalid
|
|
34
|
-
#
|
|
35
|
-
def initialize(key_manager)
|
|
36
|
-
raise ProofError, "key_manager is required" unless key_manager
|
|
37
|
-
raise ProofError, "invalid key_manager type" unless key_manager.is_a?(KeyManager)
|
|
38
|
-
|
|
39
|
-
@key_manager = key_manager
|
|
40
|
-
end
|
|
41
|
-
|
|
42
28
|
# Builds and signs a fresh DPoP JWT with `jti`, `htm`, `htu` and `iat` claims.
|
|
43
29
|
#
|
|
44
|
-
#
|
|
45
|
-
#
|
|
30
|
+
# Includes a nonce when provided and an access-token hash whenever an access token
|
|
31
|
+
# is supplied. The URI normalizer removes query strings, fragments and default ports.
|
|
46
32
|
#
|
|
47
|
-
# @param
|
|
48
|
-
# @param
|
|
33
|
+
# @param key_set [KeySet] key belonging to the session making this request
|
|
34
|
+
# @param method [String] nonempty HTTP method, converted to uppercase
|
|
35
|
+
# @param uri [String] absolute HTTP(S) request URL
|
|
49
36
|
# @param nonce [String, nil] server-provided nonce to include
|
|
50
37
|
# @param access_token [String, nil] access token to bind to the proof
|
|
51
|
-
# @param ath [Boolean, nil] include an access-token hash; `nil` enables it when a token is supplied
|
|
52
38
|
# @return [JOSE::SignedBinary] compact signed DPoP JWT
|
|
53
39
|
# @raise [ProofError] if input validation or proof generation fails
|
|
54
40
|
#
|
|
55
|
-
def generate(
|
|
56
|
-
|
|
57
|
-
|
|
41
|
+
def generate(key_set:, method:, uri:, nonce: nil, access_token: nil)
|
|
42
|
+
raise ProofError, "invalid key_set type" unless key_set.is_a?(KeySet)
|
|
43
|
+
|
|
44
|
+
validate_inputs(method, uri)
|
|
58
45
|
|
|
59
|
-
header = build_header
|
|
46
|
+
header = build_header(key_set)
|
|
60
47
|
payload = build_payload(
|
|
61
|
-
|
|
62
|
-
|
|
48
|
+
method: method,
|
|
49
|
+
uri: uri,
|
|
63
50
|
nonce: nonce,
|
|
64
|
-
access_token: access_token
|
|
65
|
-
include_ath: ath
|
|
51
|
+
access_token: access_token
|
|
66
52
|
)
|
|
67
53
|
|
|
68
|
-
|
|
54
|
+
key_set.sign_segments(header, payload)
|
|
69
55
|
rescue StandardError => e
|
|
70
56
|
raise ProofError, "Failed to generate proof: #{e.message}"
|
|
71
57
|
end
|
|
72
58
|
|
|
73
59
|
private
|
|
74
60
|
|
|
75
|
-
def validate_inputs(
|
|
76
|
-
raise ProofError, "
|
|
77
|
-
raise ProofError, "
|
|
61
|
+
def validate_inputs(method, uri)
|
|
62
|
+
raise ProofError, "method is required" if method.nil? || method.empty?
|
|
63
|
+
raise ProofError, "uri is required" if uri.nil? || uri.empty?
|
|
78
64
|
|
|
79
|
-
uri
|
|
80
|
-
raise ProofError, "invalid http_uri" unless uri.is_a?(URI::HTTP)
|
|
65
|
+
ValidatedURI.new(uri)
|
|
81
66
|
rescue URI::InvalidURIError => e
|
|
82
|
-
raise ProofError, "invalid
|
|
67
|
+
raise ProofError, "invalid uri: #{e.message}"
|
|
83
68
|
end
|
|
84
69
|
|
|
85
|
-
def build_header
|
|
70
|
+
def build_header(key_set)
|
|
86
71
|
{
|
|
87
|
-
typ:
|
|
88
|
-
alg:
|
|
89
|
-
jwk:
|
|
72
|
+
typ: 'dpop+jwt',
|
|
73
|
+
alg: ALGORITHM,
|
|
74
|
+
jwk: key_set.public_jwk
|
|
90
75
|
}
|
|
91
76
|
end
|
|
92
77
|
|
|
93
|
-
def build_payload(
|
|
78
|
+
def build_payload(method:, uri:, nonce: nil, access_token: nil)
|
|
94
79
|
payload = {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
80
|
+
'jti' => SecureRandom.uuid,
|
|
81
|
+
'htm' => method.upcase,
|
|
82
|
+
'htu' => normalize_uri(uri),
|
|
83
|
+
'iat' => Time.now.to_i
|
|
99
84
|
}
|
|
100
85
|
|
|
101
86
|
# Add the nonce if provided
|
|
102
|
-
payload[
|
|
87
|
+
payload['nonce'] = nonce if nonce
|
|
103
88
|
|
|
104
|
-
#
|
|
105
|
-
payload[
|
|
89
|
+
# Bind the proof to the access token whenever one is supplied
|
|
90
|
+
payload['ath'] = generate_access_token_hash(access_token) unless access_token.nil?
|
|
106
91
|
|
|
107
92
|
payload
|
|
108
93
|
end
|
|
109
94
|
|
|
110
95
|
def normalize_uri(uri)
|
|
111
96
|
uri = URI(uri)
|
|
97
|
+
|
|
112
98
|
# Remove default ports
|
|
113
|
-
uri.port = nil if (uri.scheme ==
|
|
99
|
+
uri.port = nil if (uri.scheme == 'https' && uri.port == 443) || (uri.scheme == 'http' && uri.port == 80)
|
|
100
|
+
|
|
101
|
+
uri.query = nil
|
|
114
102
|
uri.fragment = nil
|
|
103
|
+
|
|
115
104
|
uri.to_s
|
|
116
105
|
end
|
|
117
106
|
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AuthProto
|
|
4
|
+
|
|
5
|
+
#
|
|
6
|
+
# Demonstrating Proof of Possession (DPoP) signing and request authentication.
|
|
7
|
+
#
|
|
8
|
+
|
|
9
|
+
module DPoP
|
|
10
|
+
|
|
11
|
+
# JWS signing algorithm used for DPoP proofs.
|
|
12
|
+
# @return [String]
|
|
13
|
+
ALGORITHM = "ES256"
|
|
14
|
+
|
|
15
|
+
# OAuth error value requesting a retry with a server-provided DPoP nonce.
|
|
16
|
+
# @return [String]
|
|
17
|
+
ERROR_USE_NONCE = "use_dpop_nonce"
|
|
18
|
+
end
|
|
19
|
+
end
|
data/lib/authproto/errors.rb
CHANGED
|
@@ -58,7 +58,7 @@ module AuthProto
|
|
|
58
58
|
#
|
|
59
59
|
# @example Handling authorization server errors
|
|
60
60
|
# begin
|
|
61
|
-
# server = AuthProto::
|
|
61
|
+
# server = AuthProto::Metadata::AuthorizationServer.from_url("https://auth.example.com")
|
|
62
62
|
# rescue AuthProto::InvalidAuthorizationServer => e
|
|
63
63
|
# puts "Failed to validate authorization server: #{e.message}"
|
|
64
64
|
# end
|
|
@@ -7,6 +7,7 @@ require 'resolv'
|
|
|
7
7
|
require 'uri'
|
|
8
8
|
|
|
9
9
|
require_relative 'errors'
|
|
10
|
+
require_relative 'validated_uri'
|
|
10
11
|
|
|
11
12
|
module AuthProto
|
|
12
13
|
|
|
@@ -96,20 +97,20 @@ module AuthProto
|
|
|
96
97
|
# Sends an HTTP GET request with destination validation and redirect handling.
|
|
97
98
|
#
|
|
98
99
|
# Uses HTTPS and rejects private/loopback destinations by default. Subclasses may
|
|
99
|
-
# relax these rules.
|
|
100
|
+
# relax these rules. When enabled, follows at most {MAX_REDIRECTS} redirects and checks size
|
|
100
101
|
# against {MAX_RESPONSE_SIZE} using `Content-Length`, or body bytes when absent.
|
|
101
102
|
# Non-2xx status codes are returned for the caller to interpret; they do not
|
|
102
103
|
# themselves raise an exception.
|
|
103
104
|
#
|
|
104
105
|
# @param url [String] absolute HTTPS request URL without a fragment
|
|
105
106
|
# @param headers [Hash{String => String}] additional request headers; can override the default `Cache-Control` value
|
|
106
|
-
# @param follow_redirects [Boolean] follow redirects
|
|
107
|
+
# @param follow_redirects [Boolean] follow redirects; defaults to false
|
|
107
108
|
# @return [Hash{Symbol => Integer, Hash{String => String}, String, nil}] `:status` (Integer), lowercase `:headers` (Hash), and `:body` (String or nil)
|
|
108
109
|
# @raise [SSRFError] if initial scheme, host, fragment or resolved address is rejected
|
|
109
110
|
# @raise [HTTPError] if transport, TLS, redirect validation or response size checks fail
|
|
110
111
|
# @raise [URI::InvalidURIError] if the initial URL cannot be parsed
|
|
111
112
|
#
|
|
112
|
-
def get(url, headers = {}, follow_redirects:
|
|
113
|
+
def get(url, headers = {}, follow_redirects: false)
|
|
113
114
|
uri = validate_uri(url)
|
|
114
115
|
validate_ip(uri)
|
|
115
116
|
|
|
@@ -123,11 +124,11 @@ module AuthProto
|
|
|
123
124
|
}
|
|
124
125
|
end
|
|
125
126
|
|
|
126
|
-
# Sends an HTTP POST request with destination validation
|
|
127
|
+
# Sends an HTTP POST request with destination validation, without following redirects.
|
|
127
128
|
#
|
|
128
129
|
# Uses HTTPS and rejects private/loopback destinations by default. Subclasses may
|
|
129
|
-
# relax these rules.
|
|
130
|
-
#
|
|
130
|
+
# relax these rules. Checks size against {MAX_RESPONSE_SIZE} using `Content-Length`,
|
|
131
|
+
# or body bytes when absent.
|
|
131
132
|
# Non-2xx status codes are returned for the caller to interpret; they do not
|
|
132
133
|
# themselves raise an exception.
|
|
133
134
|
#
|
|
@@ -157,17 +158,16 @@ module AuthProto
|
|
|
157
158
|
private
|
|
158
159
|
|
|
159
160
|
def validate_uri(url)
|
|
160
|
-
uri =
|
|
161
|
+
uri = ValidatedURI.new(url)
|
|
161
162
|
unless self.class::ALLOWED_SCHEMES.include?(uri.scheme)
|
|
162
163
|
raise SSRFError, "URL scheme must be one of: #{self.class::ALLOWED_SCHEMES.join(", ")}"
|
|
163
164
|
end
|
|
164
165
|
|
|
165
|
-
# Extract and validate host before any network activity
|
|
166
|
-
host = uri.host.to_s.strip
|
|
167
|
-
raise SSRFError, "URL must include host" if host.empty?
|
|
168
166
|
raise SSRFError, "URL must not include fragment" if uri.fragment
|
|
169
167
|
|
|
170
|
-
uri
|
|
168
|
+
uri.to_uri
|
|
169
|
+
rescue URI::InvalidURIError => e
|
|
170
|
+
raise SSRFError, e.message
|
|
171
171
|
end
|
|
172
172
|
|
|
173
173
|
def validate_ip(uri)
|
|
@@ -207,9 +207,9 @@ module AuthProto
|
|
|
207
207
|
FORBIDDEN_IP_RANGES.any? { |range| range.include?(ip) }
|
|
208
208
|
end
|
|
209
209
|
|
|
210
|
-
def make_request(uri, headers = {}, redirect_count = 0, follow_redirects:
|
|
210
|
+
def make_request(uri, headers = {}, redirect_count = 0, follow_redirects: false)
|
|
211
211
|
http = Net::HTTP.new(uri.hostname, uri.port)
|
|
212
|
-
configure_http_client(http, ssl: uri.scheme ==
|
|
212
|
+
configure_http_client(http, ssl: uri.scheme == 'https')
|
|
213
213
|
|
|
214
214
|
request = Net::HTTP::Get.new(uri.request_uri)
|
|
215
215
|
add_security_headers(request, headers)
|
|
@@ -228,23 +228,15 @@ module AuthProto
|
|
|
228
228
|
raise HTTPError.new("Request failed: #{e.message}", nil)
|
|
229
229
|
end
|
|
230
230
|
|
|
231
|
-
def make_post_request(uri, body, headers = {}
|
|
231
|
+
def make_post_request(uri, body, headers = {})
|
|
232
232
|
http = Net::HTTP.new(uri.hostname, uri.port)
|
|
233
|
-
configure_http_client(http, ssl: uri.scheme ==
|
|
233
|
+
configure_http_client(http, ssl: uri.scheme == 'https')
|
|
234
234
|
|
|
235
235
|
request = Net::HTTP::Post.new(uri.request_uri)
|
|
236
236
|
add_security_headers(request, headers)
|
|
237
237
|
request.body = body.is_a?(Hash) ? URI.encode_www_form(body) : body if body
|
|
238
238
|
|
|
239
|
-
|
|
240
|
-
handle_redirect(
|
|
241
|
-
original_uri: uri,
|
|
242
|
-
body: body,
|
|
243
|
-
method: :post,
|
|
244
|
-
response: response,
|
|
245
|
-
headers: headers,
|
|
246
|
-
redirect_count: redirect_count
|
|
247
|
-
)
|
|
239
|
+
http.request(request)
|
|
248
240
|
rescue Net::OpenTimeout, Net::ReadTimeout => e
|
|
249
241
|
raise HTTPError.new("Request timeout: #{e.message}", nil)
|
|
250
242
|
rescue StandardError => e
|
|
@@ -269,11 +261,9 @@ module AuthProto
|
|
|
269
261
|
# Handle HTTP redirects
|
|
270
262
|
# kwargs can include:
|
|
271
263
|
# - original_uri: URI of the original request
|
|
272
|
-
# - method: HTTP method of the original request (:get or :post)
|
|
273
264
|
# - response: Net::HTTPResponse object
|
|
274
265
|
# - headers: Hash of headers from the original request
|
|
275
266
|
# - redirect_count: Number of redirects so far
|
|
276
|
-
# - body: Request body for POST requests
|
|
277
267
|
#
|
|
278
268
|
def handle_redirect(**kwargs)
|
|
279
269
|
response = kwargs[:response]
|
|
@@ -292,11 +282,7 @@ module AuthProto
|
|
|
292
282
|
redirect_count += 1
|
|
293
283
|
|
|
294
284
|
# Recursive call to handle the next redirect
|
|
295
|
-
|
|
296
|
-
make_post_request(location, kwargs[:body], kwargs[:headers], redirect_count)
|
|
297
|
-
else
|
|
298
|
-
make_request(location, kwargs[:headers], redirect_count)
|
|
299
|
-
end
|
|
285
|
+
make_request(location, kwargs[:headers], redirect_count, follow_redirects: true)
|
|
300
286
|
end
|
|
301
287
|
|
|
302
288
|
def validate_response(response)
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'strscan'
|
|
4
|
+
|
|
5
|
+
module AuthProto
|
|
6
|
+
|
|
7
|
+
#
|
|
8
|
+
# Shared helpers for HTTP response hashes returned by the configured transport.
|
|
9
|
+
#
|
|
10
|
+
|
|
11
|
+
module HTTPHelpers
|
|
12
|
+
|
|
13
|
+
private
|
|
14
|
+
|
|
15
|
+
# Looks up a response header without regard to the case of its name.
|
|
16
|
+
#
|
|
17
|
+
# @param response [Hash{Symbol => Object}] HTTP response with an optional `:headers` hash
|
|
18
|
+
# @param name [String] header name, in any case
|
|
19
|
+
# @param strip_parameters [Boolean] return only the trimmed value before the first semicolon
|
|
20
|
+
# @return [String, nil] header value, or `nil` if the header or headers hash is absent
|
|
21
|
+
#
|
|
22
|
+
def response_header(response, name, strip_parameters: false)
|
|
23
|
+
value = (response[:headers] || {}).find { |header, value| header.casecmp?(name) }&.last
|
|
24
|
+
return value unless value && strip_parameters
|
|
25
|
+
|
|
26
|
+
value.split(';', 2).first.to_s.strip
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Finds an authentication scheme in a WWW-Authenticate challenge list.
|
|
30
|
+
# Quoted commas and escapes are preserved while parsing. Scheme and parameter
|
|
31
|
+
# names are case-insensitive; parameter values retain their original case.
|
|
32
|
+
#
|
|
33
|
+
# @param header [String, nil] combined WWW-Authenticate header value
|
|
34
|
+
# @param scheme [String] authentication scheme to select
|
|
35
|
+
# @return [Hash{String => String}, nil] parameters, or `nil` for absent, malformed or multiple matching challenges
|
|
36
|
+
#
|
|
37
|
+
def authentication_challenge(header, scheme:)
|
|
38
|
+
return nil unless header.is_a?(String)
|
|
39
|
+
|
|
40
|
+
scanner = StringScanner.new(header)
|
|
41
|
+
matches = []
|
|
42
|
+
scanner.skip(/[ \t]*/)
|
|
43
|
+
|
|
44
|
+
loop do
|
|
45
|
+
name = scanner.scan(/[!#$%&'*+\-.^_`|~0-9A-Za-z]+/)
|
|
46
|
+
return nil unless name
|
|
47
|
+
|
|
48
|
+
whitespace = scanner.skip(/[ \t]*/)
|
|
49
|
+
params = {}
|
|
50
|
+
|
|
51
|
+
unless scanner.eos? || scanner.check(/,/)
|
|
52
|
+
return nil if whitespace.zero?
|
|
53
|
+
|
|
54
|
+
# Other schemes can carry token68 credentials rather than parameters.
|
|
55
|
+
unless scanner.scan(/[A-Za-z0-9\-._~+\/]+=*(?=[ \t]*(?:,|\z))/)
|
|
56
|
+
loop do
|
|
57
|
+
parameter = scanner.scan(/[!#$%&'*+\-.^_`|~0-9A-Za-z]+/)&.downcase
|
|
58
|
+
return nil unless parameter && !params.key?(parameter)
|
|
59
|
+
|
|
60
|
+
scanner.skip(/[ \t]*/)
|
|
61
|
+
return nil unless scanner.scan(/=/)
|
|
62
|
+
|
|
63
|
+
scanner.skip(/[ \t]*/)
|
|
64
|
+
value = scanner.scan(/"(?:\\.|[^"\\])*"/) || scanner.scan(/[!#$%&'*+\-.^_`|~0-9A-Za-z]+/)
|
|
65
|
+
return nil unless value
|
|
66
|
+
|
|
67
|
+
params[parameter] = value.start_with?('"') ? value[1...-1].gsub(/\\(.)/, '\\1') : value
|
|
68
|
+
scanner.skip(/[ \t]*/)
|
|
69
|
+
break unless scanner.check(/,[ \t]*[!#$%&'*+\-.^_`|~0-9A-Za-z]+[ \t]*=/)
|
|
70
|
+
|
|
71
|
+
scanner.scan(/,[ \t]*/)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
matches << params if name.casecmp?(scheme)
|
|
77
|
+
scanner.skip(/[ \t]*/)
|
|
78
|
+
break if scanner.eos?
|
|
79
|
+
return nil unless scanner.scan(/,[ \t]*/)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
matches.one? ? matches.first : nil
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
@@ -5,6 +5,9 @@ require 'socket'
|
|
|
5
5
|
require 'timeout'
|
|
6
6
|
require 'uri'
|
|
7
7
|
|
|
8
|
+
require_relative 'metadata/client_metadata'
|
|
9
|
+
require_relative 'validated_uri'
|
|
10
|
+
|
|
8
11
|
module AuthProto
|
|
9
12
|
|
|
10
13
|
#
|
|
@@ -89,25 +92,31 @@ module AuthProto
|
|
|
89
92
|
# Accepts `localhost`, IPv4 loopback, or IPv6 loopback. The URI must have a valid
|
|
90
93
|
# port and no user information or fragment. Its path is used as the callback path.
|
|
91
94
|
#
|
|
92
|
-
# @param metadata [Hash{String, Symbol => Object}]
|
|
95
|
+
# @param metadata [Hash{String, Symbol => Object}, Metadata::ClientMetadata]
|
|
96
|
+
# metadata containing a nonempty `redirect_uris` array of strings
|
|
93
97
|
# @raise [ArgumentError] if the first redirect URI is missing, malformed or not a supported HTTP loopback URL
|
|
94
98
|
#
|
|
95
99
|
def initialize(metadata)
|
|
96
|
-
redirects =
|
|
100
|
+
redirects = if metadata.is_a?(Metadata::ClientMetadata)
|
|
101
|
+
metadata.redirect_uris
|
|
102
|
+
else
|
|
103
|
+
metadata['redirect_uris'] || metadata[:redirect_uris]
|
|
104
|
+
end
|
|
97
105
|
|
|
98
106
|
unless redirects.is_a?(Array) && redirects.first.is_a?(String)
|
|
99
107
|
raise ArgumentError, "metadata must contain a non-empty redirect_uris array"
|
|
100
108
|
end
|
|
101
109
|
|
|
102
|
-
|
|
110
|
+
uri = ValidatedURI.new(redirects.first)
|
|
111
|
+
@uri = uri.to_uri
|
|
103
112
|
|
|
104
|
-
unless
|
|
113
|
+
unless uri.scheme == 'http' && loopback_host?(uri.hostname)
|
|
105
114
|
raise ArgumentError, "The first redirect URI must be an HTTP loopback URL"
|
|
106
115
|
end
|
|
107
116
|
|
|
108
117
|
raise ArgumentError, "The redirect URI should have a valid port" unless @uri.port.between?(1, 65_535)
|
|
109
|
-
raise ArgumentError, "The redirect URI should not include user info"
|
|
110
|
-
raise ArgumentError, "The redirect URI should not include a URL fragment"
|
|
118
|
+
raise ArgumentError, "The redirect URI should not include user info" if uri.userinfo
|
|
119
|
+
raise ArgumentError, "The redirect URI should not include a URL fragment" if uri.fragment
|
|
111
120
|
|
|
112
121
|
@path = (@uri.path.empty?) ? "/" : @uri.path
|
|
113
122
|
rescue URI::InvalidURIError
|