authproto 0.3.0.beta2 → 0.3.0.beta4
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 +36 -0
- data/README.md +108 -5
- data/examples/confidential_client/Gemfile.lock +1 -1
- data/examples/confidential_client/README.md +11 -1
- data/examples/confidential_client/app.rb +35 -17
- data/examples/confidential_client/scripts/generate_keys.rb +1 -1
- data/examples/minisky_console/Gemfile.lock +2 -2
- data/examples/minisky_console/README.md +20 -1
- data/examples/minisky_console/localhost.rb +5 -3
- data/examples/minisky_console/production.rb +9 -3
- data/lib/authproto/client.rb +173 -123
- data/lib/authproto/configuration.rb +37 -16
- data/lib/authproto/dpop/client.rb +105 -40
- data/lib/authproto/dpop/{key_manager.rb → key_set.rb} +54 -120
- data/lib/authproto/dpop/nonce_manager.rb +38 -29
- data/lib/authproto/dpop/proof_generator.rb +21 -35
- data/lib/authproto/dpop.rb +19 -0
- data/lib/authproto/encryption.rb +29 -12
- data/lib/authproto/http_client.rb +8 -7
- data/lib/authproto/http_helpers.rb +85 -0
- data/lib/authproto/metadata/authorization_server.rb +23 -6
- data/lib/authproto/metadata/client_metadata.rb +46 -6
- data/lib/authproto/metadata/resource_server.rb +7 -5
- data/lib/authproto/minisky.rb +36 -34
- data/lib/authproto/par/client.rb +22 -73
- data/lib/authproto/par/client_assertion.rb +47 -16
- data/lib/authproto/par/request.rb +4 -8
- data/lib/authproto/par.rb +2 -1
- data/lib/authproto/serialization/session.rb +33 -13
- data/lib/authproto/serialization/token_set.rb +6 -4
- data/lib/authproto/state/session.rb +102 -6
- data/lib/authproto/state/session_manager.rb +120 -22
- data/lib/authproto/state/token_set.rb +22 -19
- data/lib/authproto/state.rb +2 -2
- data/lib/authproto/storage/key_builder.rb +3 -11
- data/lib/authproto/storage/memory.rb +8 -2
- data/lib/authproto/token/identity.rb +54 -0
- data/lib/authproto/token/refresh.rb +66 -109
- data/lib/authproto/token/response.rb +73 -0
- data/lib/authproto/token/revocation.rb +92 -0
- data/lib/authproto/validated_uri.rb +23 -0
- data/lib/authproto/version.rb +1 -1
- data/lib/authproto.rb +0 -1
- data/sig/authproto/client.rbs +10 -10
- data/sig/authproto/dependencies.rbs +2 -0
- data/sig/authproto/dpop/client.rbs +11 -4
- data/sig/authproto/dpop/{key_manager.rbs → key_set.rbs} +9 -11
- data/sig/authproto/dpop/nonce_manager.rbs +4 -4
- data/sig/authproto/dpop/proof_generator.rbs +5 -10
- data/sig/authproto/dpop.rbs +6 -0
- data/sig/authproto/encryption.rbs +4 -2
- data/sig/authproto/http_client.rbs +0 -1
- data/sig/authproto/http_helpers.rbs +7 -0
- data/sig/authproto/localhost_server.rbs +1 -1
- data/sig/authproto/metadata/authorization_server.rbs +1 -0
- data/sig/authproto/metadata/client_metadata.rbs +3 -1
- data/sig/authproto/metadata/resource_server.rbs +1 -0
- data/sig/authproto/minisky_oauth.rbs +5 -2
- data/sig/authproto/par/client.rbs +4 -8
- data/sig/authproto/par/client_assertion.rbs +4 -0
- data/sig/authproto/par/request.rbs +0 -9
- data/sig/authproto/state/session.rbs +13 -3
- data/sig/authproto/state/session_manager.rbs +10 -3
- data/sig/authproto/state/token_set.rbs +1 -2
- data/sig/authproto/storage/key_builder.rbs +1 -2
- data/sig/authproto/token/refresh.rbs +5 -5
- data/sig/authproto/token/response.rbs +12 -0
- data/sig/authproto/token/revocation.rbs +14 -0
- data/sig/authproto.rbs +6 -3
- metadata +30 -23
- data/.rspec +0 -1
- data/.rubocop.yml +0 -31
- data/.tm_properties +0 -1
- data/.yardopts +0 -5
- data/AGENTS.md +0 -80
- data/Rakefile +0 -8
- data/examples/confidential_client/.gitignore +0 -6
- data/examples/minisky_console/.gitignore +0 -2
- data/lib/authproto/serialization/dpop_key.rb +0 -38
- data/misc/yard_attributes.rb +0 -34
- data/sig/authproto/serialization/dpop_key.rbs +0 -14
|
@@ -5,9 +5,10 @@ require 'openssl'
|
|
|
5
5
|
require 'securerandom'
|
|
6
6
|
require 'uri'
|
|
7
7
|
|
|
8
|
+
require_relative '../dpop'
|
|
8
9
|
require_relative '../errors'
|
|
9
10
|
require_relative '../validated_uri'
|
|
10
|
-
require_relative '
|
|
11
|
+
require_relative 'key_set'
|
|
11
12
|
|
|
12
13
|
module AuthProto
|
|
13
14
|
module DPoP
|
|
@@ -24,49 +25,33 @@ module AuthProto
|
|
|
24
25
|
# Error raised for proof generation/validation issues
|
|
25
26
|
class ProofError < AuthProto::Error; end
|
|
26
27
|
|
|
27
|
-
# Key manager used to sign proofs.
|
|
28
|
-
# @return [KeyManager]
|
|
29
|
-
attr_reader :key_manager
|
|
30
|
-
|
|
31
|
-
# Creates a new ProofGenerator instance
|
|
32
|
-
#
|
|
33
|
-
# @param key_manager [KeyManager] Key manager to use for signing proofs
|
|
34
|
-
# @raise [ProofError] if key_manager is invalid
|
|
35
|
-
#
|
|
36
|
-
def initialize(key_manager)
|
|
37
|
-
raise ProofError, "key_manager is required" unless key_manager
|
|
38
|
-
raise ProofError, "invalid key_manager type" unless key_manager.is_a?(KeyManager)
|
|
39
|
-
|
|
40
|
-
@key_manager = key_manager
|
|
41
|
-
end
|
|
42
|
-
|
|
43
28
|
# Builds and signs a fresh DPoP JWT with `jti`, `htm`, `htu` and `iat` claims.
|
|
44
29
|
#
|
|
45
|
-
#
|
|
46
|
-
# query strings, fragments and default ports.
|
|
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.
|
|
47
32
|
#
|
|
33
|
+
# @param key_set [KeySet] key belonging to the session making this request
|
|
48
34
|
# @param method [String] nonempty HTTP method, converted to uppercase
|
|
49
|
-
# @param uri [String] absolute
|
|
35
|
+
# @param uri [String] absolute HTTPS request URL
|
|
50
36
|
# @param nonce [String, nil] server-provided nonce to include
|
|
51
37
|
# @param access_token [String, nil] access token to bind to the proof
|
|
52
|
-
# @param ath [Boolean, nil] include an access-token hash; `nil` enables it when a token is supplied
|
|
53
38
|
# @return [JOSE::SignedBinary] compact signed DPoP JWT
|
|
54
39
|
# @raise [ProofError] if input validation or proof generation fails
|
|
55
40
|
#
|
|
56
|
-
def generate(method:, uri:, nonce: nil, access_token: nil
|
|
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
|
+
|
|
57
44
|
validate_inputs(method, uri)
|
|
58
|
-
ath = !access_token.nil? if ath.nil?
|
|
59
45
|
|
|
60
|
-
header = build_header
|
|
46
|
+
header = build_header(key_set)
|
|
61
47
|
payload = build_payload(
|
|
62
48
|
method: method,
|
|
63
49
|
uri: uri,
|
|
64
50
|
nonce: nonce,
|
|
65
|
-
access_token: access_token
|
|
66
|
-
include_ath: ath
|
|
51
|
+
access_token: access_token
|
|
67
52
|
)
|
|
68
53
|
|
|
69
|
-
|
|
54
|
+
key_set.sign_segments(header, payload)
|
|
70
55
|
rescue StandardError => e
|
|
71
56
|
raise ProofError, "Failed to generate proof: #{e.message}"
|
|
72
57
|
end
|
|
@@ -77,20 +62,21 @@ module AuthProto
|
|
|
77
62
|
raise ProofError, "method is required" if method.nil? || method.empty?
|
|
78
63
|
raise ProofError, "uri is required" if uri.nil? || uri.empty?
|
|
79
64
|
|
|
80
|
-
ValidatedURI.new(uri)
|
|
65
|
+
parsed_uri = ValidatedURI.new(uri)
|
|
66
|
+
raise ProofError, "uri must use HTTPS" unless parsed_uri.scheme == 'https'
|
|
81
67
|
rescue URI::InvalidURIError => e
|
|
82
68
|
raise ProofError, "invalid uri: #{e.message}"
|
|
83
69
|
end
|
|
84
70
|
|
|
85
|
-
def build_header
|
|
71
|
+
def build_header(key_set)
|
|
86
72
|
{
|
|
87
73
|
typ: 'dpop+jwt',
|
|
88
|
-
alg:
|
|
89
|
-
jwk:
|
|
74
|
+
alg: ALGORITHM,
|
|
75
|
+
jwk: key_set.public_jwk
|
|
90
76
|
}
|
|
91
77
|
end
|
|
92
78
|
|
|
93
|
-
def build_payload(method:, uri:, nonce: nil, access_token: nil
|
|
79
|
+
def build_payload(method:, uri:, nonce: nil, access_token: nil)
|
|
94
80
|
payload = {
|
|
95
81
|
'jti' => SecureRandom.uuid,
|
|
96
82
|
'htm' => method.upcase,
|
|
@@ -101,8 +87,8 @@ module AuthProto
|
|
|
101
87
|
# Add the nonce if provided
|
|
102
88
|
payload['nonce'] = nonce if nonce
|
|
103
89
|
|
|
104
|
-
#
|
|
105
|
-
payload['ath'] = generate_access_token_hash(access_token)
|
|
90
|
+
# Bind the proof to the access token whenever one is supplied
|
|
91
|
+
payload['ath'] = generate_access_token_hash(access_token) unless access_token.nil?
|
|
106
92
|
|
|
107
93
|
payload
|
|
108
94
|
end
|
|
@@ -111,7 +97,7 @@ module AuthProto
|
|
|
111
97
|
uri = URI(uri)
|
|
112
98
|
|
|
113
99
|
# Remove default ports
|
|
114
|
-
uri.port = nil if
|
|
100
|
+
uri.port = nil if uri.scheme == 'https' && uri.port == 443
|
|
115
101
|
|
|
116
102
|
uri.query = nil
|
|
117
103
|
uri.fragment = nil
|
|
@@ -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/encryption.rb
CHANGED
|
@@ -17,7 +17,7 @@ module AuthProto
|
|
|
17
17
|
# Base class for encryption and key-configuration failures.
|
|
18
18
|
class Error < AuthProto::Error; end
|
|
19
19
|
|
|
20
|
-
# Raised when a required key derivation context is missing.
|
|
20
|
+
# Raised when the master key length is invalid or a required key derivation context is missing.
|
|
21
21
|
class ConfigurationError < Error; end
|
|
22
22
|
|
|
23
23
|
# Raised when plaintext inputs are invalid or encryption fails.
|
|
@@ -26,6 +26,18 @@ module AuthProto
|
|
|
26
26
|
# Raised when an encrypted envelope cannot be decoded, authenticated or decrypted.
|
|
27
27
|
class DecryptionError < Error; end
|
|
28
28
|
|
|
29
|
+
# OpenSSL cipher name used for authenticated encryption.
|
|
30
|
+
# @return [String]
|
|
31
|
+
CIPHER = "aes-256-gcm"
|
|
32
|
+
|
|
33
|
+
# Version stored in encrypted field envelopes.
|
|
34
|
+
# @return [Integer]
|
|
35
|
+
VERSION = 1
|
|
36
|
+
|
|
37
|
+
# Required length of the decoded master key in bytes.
|
|
38
|
+
# @return [Integer]
|
|
39
|
+
MASTER_KEY_BYTES = 32
|
|
40
|
+
|
|
29
41
|
#
|
|
30
42
|
# HKDF implementation based on RFC 5869
|
|
31
43
|
#
|
|
@@ -70,16 +82,9 @@ module AuthProto
|
|
|
70
82
|
|
|
71
83
|
class Service
|
|
72
84
|
|
|
73
|
-
# OpenSSL cipher name used for authenticated encryption.
|
|
74
|
-
# @return [String]
|
|
75
|
-
CIPHER = "aes-256-gcm"
|
|
76
|
-
|
|
77
|
-
# Version stored in encrypted field envelopes.
|
|
78
|
-
# @return [Integer]
|
|
79
|
-
VERSION = 1
|
|
80
|
-
|
|
81
85
|
# Creates an encryption service backed by {KeyProvider}.
|
|
82
86
|
# @raise [ArgumentError] if the configured master key is not valid Base64
|
|
87
|
+
# @raise [ConfigurationError] if the configured master key does not decode to exactly 32 bytes
|
|
83
88
|
#
|
|
84
89
|
def initialize
|
|
85
90
|
@key_provider = KeyProvider.new
|
|
@@ -133,6 +138,8 @@ module AuthProto
|
|
|
133
138
|
data = Base64.strict_decode64(encrypted[:data])
|
|
134
139
|
auth_tag = Base64.strict_decode64(encrypted[:tag])
|
|
135
140
|
|
|
141
|
+
raise DecryptionError, "Authentication tag must be 16 bytes" unless auth_tag.bytesize == 16
|
|
142
|
+
|
|
136
143
|
cipher = OpenSSL::Cipher.new(CIPHER)
|
|
137
144
|
cipher.decrypt
|
|
138
145
|
cipher.key = @key_provider.key_for_context(context)
|
|
@@ -195,18 +202,19 @@ module AuthProto
|
|
|
195
202
|
#
|
|
196
203
|
def self.ephemeral_master_key
|
|
197
204
|
EPHEMERAL_KEY_MUTEX.synchronize do
|
|
198
|
-
@ephemeral_master_key ||= SecureRandom.random_bytes(
|
|
205
|
+
@ephemeral_master_key ||= SecureRandom.random_bytes(MASTER_KEY_BYTES)
|
|
199
206
|
end
|
|
200
207
|
end
|
|
201
208
|
|
|
202
209
|
# Loads the master key from `ATPROTO_MASTER_KEY` or uses the process fallback.
|
|
203
210
|
#
|
|
204
|
-
# Set the environment variable to a Base64-encoded master secret
|
|
211
|
+
# Set the environment variable to a Base64-encoded master secret containing exactly
|
|
205
212
|
# 32 random bytes. Context-specific AES-256 keys are derived from that secret.
|
|
206
213
|
# Without it, a warning is emitted and {KeyProvider.ephemeral_master_key} is used;
|
|
207
214
|
# data encrypted with that fallback cannot be recovered after a restart.
|
|
208
215
|
#
|
|
209
216
|
# @raise [ArgumentError] if `ATPROTO_MASTER_KEY` is not valid Base64
|
|
217
|
+
# @raise [ConfigurationError] if `ATPROTO_MASTER_KEY` does not decode to exactly 32 bytes
|
|
210
218
|
#
|
|
211
219
|
def initialize
|
|
212
220
|
@master_key = load_master_key
|
|
@@ -235,7 +243,16 @@ module AuthProto
|
|
|
235
243
|
def load_master_key
|
|
236
244
|
# Try environment variable first
|
|
237
245
|
key = ENV.fetch("ATPROTO_MASTER_KEY", nil)
|
|
238
|
-
|
|
246
|
+
|
|
247
|
+
if key
|
|
248
|
+
decoded = Base64.strict_decode64(key)
|
|
249
|
+
|
|
250
|
+
unless decoded.bytesize == MASTER_KEY_BYTES
|
|
251
|
+
raise ConfigurationError, "ATPROTO_MASTER_KEY must decode to exactly #{MASTER_KEY_BYTES} bytes"
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
return decoded
|
|
255
|
+
end
|
|
239
256
|
|
|
240
257
|
# Generate and store a random key if not configured
|
|
241
258
|
warn "WARNING: Using randomly generated encryption key - tokens will not persist across restarts"
|
|
@@ -23,10 +23,14 @@ module AuthProto
|
|
|
23
23
|
FORBIDDEN_IP_RANGES = [
|
|
24
24
|
IPAddr.new("0.0.0.0/8"), # Current network
|
|
25
25
|
IPAddr.new("10.0.0.0/8"), # Private network
|
|
26
|
+
IPAddr.new('100.64.0.0/10'), # Shared address space (CGNAT)
|
|
26
27
|
IPAddr.new("127.0.0.0/8"), # Loopback
|
|
27
28
|
IPAddr.new("169.254.0.0/16"), # Link-local
|
|
28
29
|
IPAddr.new("172.16.0.0/12"), # Private network
|
|
29
30
|
IPAddr.new("192.168.0.0/16"), # Private network
|
|
31
|
+
IPAddr.new('198.18.0.0/15'), # Benchmarking
|
|
32
|
+
IPAddr.new('224.0.0.0/4'), # Multicast
|
|
33
|
+
IPAddr.new('240.0.0.0/4'), # Reserved, including limited broadcast
|
|
30
34
|
IPAddr.new("::/128"), # Unspecified IPv6 address
|
|
31
35
|
IPAddr.new("::1/128"), # IPv6 loopback
|
|
32
36
|
IPAddr.new("fc00::/7"), # Unique local address
|
|
@@ -224,6 +228,8 @@ module AuthProto
|
|
|
224
228
|
)
|
|
225
229
|
rescue Net::OpenTimeout, Net::ReadTimeout => e
|
|
226
230
|
raise HTTPError.new("Request timeout: #{e.message}", nil)
|
|
231
|
+
rescue AuthProto::Error
|
|
232
|
+
raise
|
|
227
233
|
rescue StandardError => e
|
|
228
234
|
raise HTTPError.new("Request failed: #{e.message}", nil)
|
|
229
235
|
end
|
|
@@ -239,6 +245,8 @@ module AuthProto
|
|
|
239
245
|
http.request(request)
|
|
240
246
|
rescue Net::OpenTimeout, Net::ReadTimeout => e
|
|
241
247
|
raise HTTPError.new("Request timeout: #{e.message}", nil)
|
|
248
|
+
rescue AuthProto::Error
|
|
249
|
+
raise
|
|
242
250
|
rescue StandardError => e
|
|
243
251
|
raise HTTPError.new("Request failed: #{e.message}", nil)
|
|
244
252
|
end
|
|
@@ -286,16 +294,9 @@ module AuthProto
|
|
|
286
294
|
end
|
|
287
295
|
|
|
288
296
|
def validate_response(response)
|
|
289
|
-
# check_success_status(response)
|
|
290
297
|
check_content_length(response)
|
|
291
298
|
end
|
|
292
299
|
|
|
293
|
-
def check_success_status(response)
|
|
294
|
-
return if response.is_a?(Net::HTTPSuccess)
|
|
295
|
-
|
|
296
|
-
raise HTTPError.new("HTTP request failed: #{response.code} #{response.message}", response)
|
|
297
|
-
end
|
|
298
|
-
|
|
299
300
|
def check_content_length(response)
|
|
300
301
|
content_length = response["content-length"]&.to_i || response.body&.bytesize || 0
|
|
301
302
|
return unless content_length > MAX_RESPONSE_SIZE
|
|
@@ -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
|
|
@@ -4,9 +4,12 @@ require 'json'
|
|
|
4
4
|
require 'uri'
|
|
5
5
|
|
|
6
6
|
require_relative '../configuration'
|
|
7
|
+
require_relative '../dpop'
|
|
7
8
|
require_relative '../errors'
|
|
8
9
|
require_relative '../http_client'
|
|
10
|
+
require_relative '../http_helpers'
|
|
9
11
|
require_relative '../metadata'
|
|
12
|
+
require_relative '../par/client_assertion'
|
|
10
13
|
require_relative '../validated_uri'
|
|
11
14
|
|
|
12
15
|
module AuthProto
|
|
@@ -34,6 +37,7 @@ module AuthProto
|
|
|
34
37
|
#
|
|
35
38
|
|
|
36
39
|
class AuthorizationServer
|
|
40
|
+
extend HTTPHelpers
|
|
37
41
|
|
|
38
42
|
# Metadata keys that must be present before capability validation.
|
|
39
43
|
# @return [Array<String>]
|
|
@@ -66,6 +70,10 @@ module AuthProto
|
|
|
66
70
|
# @return [String]
|
|
67
71
|
attr_reader :token_endpoint
|
|
68
72
|
|
|
73
|
+
# Optional HTTPS endpoint for token revocation.
|
|
74
|
+
# @return [String, nil]
|
|
75
|
+
attr_reader :revocation_endpoint
|
|
76
|
+
|
|
69
77
|
# HTTPS PAR endpoint.
|
|
70
78
|
# @return [String]
|
|
71
79
|
attr_reader :pushed_authorization_request_endpoint
|
|
@@ -144,6 +152,7 @@ module AuthProto
|
|
|
144
152
|
issuer: issuer,
|
|
145
153
|
authorization_endpoint: authorization_endpoint,
|
|
146
154
|
token_endpoint: token_endpoint,
|
|
155
|
+
revocation_endpoint: revocation_endpoint,
|
|
147
156
|
pushed_authorization_request_endpoint: pushed_authorization_request_endpoint,
|
|
148
157
|
response_types_supported: response_types_supported,
|
|
149
158
|
grant_types_supported: grant_types_supported,
|
|
@@ -171,6 +180,7 @@ module AuthProto
|
|
|
171
180
|
# Endpoint strings
|
|
172
181
|
@authorization_endpoint = validate_https_url(data['authorization_endpoint'])
|
|
173
182
|
@token_endpoint = validate_https_url(data['token_endpoint'])
|
|
183
|
+
@revocation_endpoint = validate_https_url(data['revocation_endpoint']) if data['revocation_endpoint']
|
|
174
184
|
@pushed_authorization_request_endpoint = validate_https_url(data['pushed_authorization_request_endpoint'])
|
|
175
185
|
|
|
176
186
|
# Capability lists
|
|
@@ -216,8 +226,13 @@ module AuthProto
|
|
|
216
226
|
def validate_https_url(url)
|
|
217
227
|
uri = ValidatedURI.new(url)
|
|
218
228
|
raise InvalidAuthorizationServer, "URL must use HTTPS" unless uri.scheme == 'https'
|
|
229
|
+
raise InvalidAuthorizationServer, "Endpoint URL must not contain userinfo" if uri.userinfo
|
|
219
230
|
raise InvalidAuthorizationServer, "Endpoint URL must not contain a fragment" if uri.fragment
|
|
220
231
|
|
|
232
|
+
unless uri.effective_port.between?(1, 65_535)
|
|
233
|
+
raise InvalidAuthorizationServer, "Endpoint URL must have a valid port"
|
|
234
|
+
end
|
|
235
|
+
|
|
221
236
|
url
|
|
222
237
|
end
|
|
223
238
|
|
|
@@ -258,8 +273,10 @@ module AuthProto
|
|
|
258
273
|
@token_endpoint_auth_signing_alg_values_supported, 'token_endpoint_auth_signing_alg_values_supported'
|
|
259
274
|
)
|
|
260
275
|
|
|
261
|
-
|
|
262
|
-
|
|
276
|
+
algorithm = PAR::ClientAssertion::ALGORITHM
|
|
277
|
+
|
|
278
|
+
unless @token_endpoint_auth_signing_alg_values_supported.include?(algorithm)
|
|
279
|
+
raise InvalidAuthorizationServer, "token_endpoint_auth_signing_alg_values_supported must include #{algorithm}"
|
|
263
280
|
end
|
|
264
281
|
|
|
265
282
|
if @token_endpoint_auth_signing_alg_values_supported.include?('none')
|
|
@@ -270,8 +287,8 @@ module AuthProto
|
|
|
270
287
|
def validate_dpop_signing_algs
|
|
271
288
|
validate_string_array(@dpop_signing_alg_values_supported, 'dpop_signing_alg_values_supported')
|
|
272
289
|
|
|
273
|
-
unless @dpop_signing_alg_values_supported.include?(
|
|
274
|
-
raise InvalidAuthorizationServer, "dpop_signing_alg_values_supported must include
|
|
290
|
+
unless @dpop_signing_alg_values_supported.include?(DPoP::ALGORITHM)
|
|
291
|
+
raise InvalidAuthorizationServer, "dpop_signing_alg_values_supported must include #{DPoP::ALGORITHM}"
|
|
275
292
|
end
|
|
276
293
|
end
|
|
277
294
|
|
|
@@ -328,9 +345,9 @@ module AuthProto
|
|
|
328
345
|
raise InvalidAuthorizationServer, "Authorization server metadata must return HTTP 200"
|
|
329
346
|
end
|
|
330
347
|
|
|
331
|
-
content_type = response
|
|
348
|
+
content_type = response_header(response, 'Content-Type', strip_parameters: true)
|
|
332
349
|
|
|
333
|
-
unless content_type.casecmp?('application/json')
|
|
350
|
+
unless content_type.to_s.casecmp?('application/json')
|
|
334
351
|
raise InvalidAuthorizationServer, "Authorization server metadata must use application/json"
|
|
335
352
|
end
|
|
336
353
|
end
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require '
|
|
3
|
+
require 'ipaddr'
|
|
4
4
|
require 'jose'
|
|
5
|
+
require 'json'
|
|
5
6
|
require 'uri'
|
|
6
7
|
|
|
7
8
|
require_relative '../configuration'
|
|
8
9
|
require_relative '../errors'
|
|
10
|
+
require_relative '../http_helpers'
|
|
9
11
|
require_relative '../metadata'
|
|
12
|
+
require_relative '../par/client_assertion'
|
|
10
13
|
require_relative '../validated_uri'
|
|
11
14
|
|
|
12
15
|
module AuthProto
|
|
@@ -45,6 +48,7 @@ module AuthProto
|
|
|
45
48
|
#
|
|
46
49
|
|
|
47
50
|
class ClientMetadata
|
|
51
|
+
extend HTTPHelpers
|
|
48
52
|
|
|
49
53
|
# Client authentication method for confidential clients
|
|
50
54
|
AUTH_METHOD_PRIVATE_KEY_JWT = "private_key_jwt"
|
|
@@ -166,9 +170,9 @@ module AuthProto
|
|
|
166
170
|
"expected HTTP 200 response for client metadata request, got #{response[:status]}"
|
|
167
171
|
end
|
|
168
172
|
|
|
169
|
-
content_type = (response
|
|
173
|
+
content_type = response_header(response, 'Content-Type', strip_parameters: true)
|
|
170
174
|
|
|
171
|
-
unless content_type.to_s.
|
|
175
|
+
unless content_type.to_s.downcase == 'application/json'
|
|
172
176
|
raise InvalidClientMetadata, "client metadata Content-Type must be application/json"
|
|
173
177
|
end
|
|
174
178
|
|
|
@@ -269,11 +273,23 @@ module AuthProto
|
|
|
269
273
|
|
|
270
274
|
def valid_https_client_id?(client_id)
|
|
271
275
|
uri = ValidatedURI.new(client_id)
|
|
272
|
-
uri.scheme == 'https' && !uri.port && !uri.userinfo && !uri.fragment
|
|
276
|
+
uri.scheme == 'https' && !uri.port && !uri.userinfo && !uri.fragment &&
|
|
277
|
+
valid_client_id_hostname?(uri.hostname) && !uri.path.empty? && !uri.path.end_with?('/')
|
|
273
278
|
rescue URI::InvalidURIError
|
|
274
279
|
false
|
|
275
280
|
end
|
|
276
281
|
|
|
282
|
+
def valid_client_id_hostname?(hostname)
|
|
283
|
+
labels = hostname.downcase.split('.')
|
|
284
|
+
return false if labels.length < 2 || %w[local localhost test invalid example].include?(labels.last)
|
|
285
|
+
return false if hostname.match?(/\A[0-9.]+\z/)
|
|
286
|
+
|
|
287
|
+
IPAddr.new(hostname)
|
|
288
|
+
false
|
|
289
|
+
rescue IPAddr::InvalidAddressError
|
|
290
|
+
true
|
|
291
|
+
end
|
|
292
|
+
|
|
277
293
|
def valid_localhost_client_id?(client_id)
|
|
278
294
|
uri = ValidatedURI.new(client_id)
|
|
279
295
|
uri.scheme == 'http' && uri.host == 'localhost' && !uri.port && !uri.userinfo &&
|
|
@@ -286,6 +302,10 @@ module AuthProto
|
|
|
286
302
|
raise InvalidClientMetadata, "grant_types is required" unless grant_types
|
|
287
303
|
raise InvalidClientMetadata, "grant_types must be an array" unless grant_types.is_a?(Array)
|
|
288
304
|
|
|
305
|
+
unless grant_types.uniq.length == grant_types.length
|
|
306
|
+
raise InvalidClientMetadata, "grant_types must not contain duplicates"
|
|
307
|
+
end
|
|
308
|
+
|
|
289
309
|
unless grant_types.include?('authorization_code')
|
|
290
310
|
raise InvalidClientMetadata, "grant_types must include authorization_code"
|
|
291
311
|
end
|
|
@@ -343,6 +363,10 @@ module AuthProto
|
|
|
343
363
|
raise InvalidClientMetadata, "redirect URI must not contain credentials" if uri.userinfo
|
|
344
364
|
raise InvalidClientMetadata, "redirect URI must not contain a fragment" if uri.fragment
|
|
345
365
|
|
|
366
|
+
if ['localhost', '127.0.0.1', '[::1]'].include?(uri.host.downcase)
|
|
367
|
+
raise InvalidClientMetadata, "HTTPS redirect URI must not use a loopback host"
|
|
368
|
+
end
|
|
369
|
+
|
|
346
370
|
if uri.specified_port && uri.effective_port == 443
|
|
347
371
|
raise InvalidClientMetadata, "HTTPS redirect URI must not specify the default port"
|
|
348
372
|
end
|
|
@@ -410,6 +434,10 @@ module AuthProto
|
|
|
410
434
|
scope_values = scope.split(' ')
|
|
411
435
|
raise InvalidClientMetadata, "atproto scope is required" unless scope_values.include?('atproto')
|
|
412
436
|
|
|
437
|
+
unless scope_values.uniq.length == scope_values.length
|
|
438
|
+
raise InvalidClientMetadata, "scope must not contain duplicate tokens"
|
|
439
|
+
end
|
|
440
|
+
|
|
413
441
|
scope
|
|
414
442
|
end
|
|
415
443
|
|
|
@@ -472,6 +500,8 @@ module AuthProto
|
|
|
472
500
|
raise InvalidClientMetadata, "jwks must have a 'keys' array" unless jwks['keys'].is_a?(Array)
|
|
473
501
|
raise InvalidClientMetadata, "jwks must contain at least one public key" if jwks['keys'].empty?
|
|
474
502
|
|
|
503
|
+
key_ids = Set.new
|
|
504
|
+
|
|
475
505
|
jwks['keys'].each_with_index do |key, index|
|
|
476
506
|
path = "jwks.keys.#{index}"
|
|
477
507
|
|
|
@@ -481,6 +511,12 @@ module AuthProto
|
|
|
481
511
|
raise InvalidClientMetadata, "#{path} must have a nonempty string kid"
|
|
482
512
|
end
|
|
483
513
|
|
|
514
|
+
if key_ids.include?(key['kid'])
|
|
515
|
+
raise InvalidClientMetadata, "#{path} has a duplicate kid: #{key['kid']}"
|
|
516
|
+
end
|
|
517
|
+
|
|
518
|
+
key_ids << key['kid']
|
|
519
|
+
|
|
484
520
|
if %w[d p q dp dq qi oth k].any? { |field| key.key?(field) }
|
|
485
521
|
raise InvalidClientMetadata, "#{path} must contain only public key material"
|
|
486
522
|
end
|
|
@@ -532,6 +568,8 @@ module AuthProto
|
|
|
532
568
|
@token_endpoint_auth_method = metadata['token_endpoint_auth_method']
|
|
533
569
|
@token_endpoint_auth_signing_alg = metadata['token_endpoint_auth_signing_alg']
|
|
534
570
|
|
|
571
|
+
# Not technically required, but if the field is not set, the reference PDS sets it
|
|
572
|
+
# to (unsupported) `client_secret_basic`.
|
|
535
573
|
raise InvalidClientMetadata, "token_endpoint_auth_method is required" if @token_endpoint_auth_method.nil?
|
|
536
574
|
|
|
537
575
|
@jwks = metadata['jwks']
|
|
@@ -561,8 +599,10 @@ module AuthProto
|
|
|
561
599
|
end
|
|
562
600
|
|
|
563
601
|
def validate_auth_confidential(metadata)
|
|
564
|
-
|
|
565
|
-
|
|
602
|
+
algorithm = PAR::ClientAssertion::ALGORITHM
|
|
603
|
+
|
|
604
|
+
unless @token_endpoint_auth_signing_alg == algorithm
|
|
605
|
+
raise InvalidClientMetadata, "token_endpoint_auth_signing_alg must be #{algorithm}"
|
|
566
606
|
end
|
|
567
607
|
|
|
568
608
|
if @jwks.nil? && @jwks_uri.nil?
|
|
@@ -6,6 +6,7 @@ require 'uri'
|
|
|
6
6
|
require_relative '../configuration'
|
|
7
7
|
require_relative '../errors'
|
|
8
8
|
require_relative '../http_client'
|
|
9
|
+
require_relative '../http_helpers'
|
|
9
10
|
require_relative '../metadata'
|
|
10
11
|
require_relative '../validated_uri'
|
|
11
12
|
|
|
@@ -21,6 +22,7 @@ module AuthProto
|
|
|
21
22
|
#
|
|
22
23
|
|
|
23
24
|
class ResourceServer
|
|
25
|
+
extend HTTPHelpers
|
|
24
26
|
|
|
25
27
|
# The single authorization server origin advertised by the resource server.
|
|
26
28
|
# @return [String]
|
|
@@ -39,7 +41,7 @@ module AuthProto
|
|
|
39
41
|
# Fetches `/.well-known/oauth-protected-resource` using the configured transport.
|
|
40
42
|
# Requires HTTP 200 without redirects, a JSON object served as application/json,
|
|
41
43
|
# and a resource identifier that exactly matches the requested URL.
|
|
42
|
-
#
|
|
44
|
+
# Requires HTTPS even when the configured transport permits HTTP.
|
|
43
45
|
# The PDS URL must have no userinfo, path prefix, query or fragment.
|
|
44
46
|
#
|
|
45
47
|
# @param url [String] resource server/PDS base URL
|
|
@@ -86,9 +88,9 @@ module AuthProto
|
|
|
86
88
|
def validate_source_url(url)
|
|
87
89
|
uri = ValidatedURI.new(url)
|
|
88
90
|
|
|
89
|
-
unless uri.path.empty? && !uri.userinfo && !uri.query && !uri.fragment
|
|
91
|
+
unless uri.scheme == 'https' && uri.path.empty? && !uri.userinfo && !uri.query && !uri.fragment
|
|
90
92
|
raise InvalidAuthorizationServer,
|
|
91
|
-
"Resource server URL must be an
|
|
93
|
+
"Resource server URL must be an HTTPS origin without userinfo, a path prefix, query or fragment"
|
|
92
94
|
end
|
|
93
95
|
rescue URI::InvalidURIError => e
|
|
94
96
|
raise InvalidAuthorizationServer, "Invalid resource server URL: #{e.message}"
|
|
@@ -107,9 +109,9 @@ module AuthProto
|
|
|
107
109
|
raise InvalidAuthorizationServer, "Resource server metadata must return HTTP 200"
|
|
108
110
|
end
|
|
109
111
|
|
|
110
|
-
content_type = response
|
|
112
|
+
content_type = response_header(response, 'Content-Type', strip_parameters: true)
|
|
111
113
|
|
|
112
|
-
unless content_type.casecmp?("application/json")
|
|
114
|
+
unless content_type.to_s.casecmp?("application/json")
|
|
113
115
|
raise InvalidAuthorizationServer, "Resource server metadata must use application/json"
|
|
114
116
|
end
|
|
115
117
|
end
|