authproto 0.3.0.beta3 → 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.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +18 -1
  3. data/examples/confidential_client/Gemfile.lock +1 -1
  4. data/examples/confidential_client/README.md +1 -1
  5. data/examples/confidential_client/app.rb +27 -10
  6. data/examples/minisky_console/Gemfile.lock +2 -2
  7. data/lib/authproto/client.rb +110 -43
  8. data/lib/authproto/dpop/client.rb +1 -1
  9. data/lib/authproto/dpop/nonce_manager.rb +5 -9
  10. data/lib/authproto/dpop/proof_generator.rb +4 -3
  11. data/lib/authproto/encryption.rb +29 -12
  12. data/lib/authproto/http_client.rb +8 -7
  13. data/lib/authproto/metadata/authorization_server.rb +11 -0
  14. data/lib/authproto/metadata/client_metadata.rb +28 -1
  15. data/lib/authproto/metadata/resource_server.rb +3 -3
  16. data/lib/authproto/serialization/session.rb +2 -0
  17. data/lib/authproto/serialization/token_set.rb +5 -2
  18. data/lib/authproto/state/session.rb +37 -1
  19. data/lib/authproto/state/session_manager.rb +30 -6
  20. data/lib/authproto/state/token_set.rb +6 -3
  21. data/lib/authproto/state.rb +1 -1
  22. data/lib/authproto/token/identity.rb +54 -0
  23. data/lib/authproto/token/refresh.rb +42 -52
  24. data/lib/authproto/token/response.rb +73 -0
  25. data/lib/authproto/token/revocation.rb +92 -0
  26. data/lib/authproto/version.rb +1 -1
  27. data/sig/authproto/client.rbs +5 -3
  28. data/sig/authproto/encryption.rbs +4 -2
  29. data/sig/authproto/http_client.rbs +0 -1
  30. data/sig/authproto/metadata/client_metadata.rbs +1 -0
  31. data/sig/authproto/state/session.rbs +3 -0
  32. data/sig/authproto/state/session_manager.rbs +3 -1
  33. data/sig/authproto/token/refresh.rbs +2 -1
  34. data/sig/authproto/token/response.rbs +12 -0
  35. data/sig/authproto/token/revocation.rbs +14 -0
  36. data/sig/authproto.rbs +1 -1
  37. metadata +17 -5
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ require_relative '../errors'
6
+ require_relative '../http_helpers'
7
+ require_relative '../state/token_set'
8
+
9
+ module AuthProto
10
+ module Token
11
+
12
+ #
13
+ # Parses successful token responses shared by code exchange and refresh.
14
+ #
15
+
16
+ class Response
17
+ extend HTTPHelpers
18
+
19
+ # Error raised when a successful token response is malformed.
20
+ class Error < AuthProto::Error; end
21
+
22
+ # Validates the response representation and constructs its token set.
23
+ # Identity checks remain the responsibility of the calling authorization flow.
24
+ # Rejects OpenID Connect `id_token` fields, matching the ATProto reference client.
25
+ #
26
+ # @param response [Hash] successful HTTP response from the token endpoint
27
+ # @param current_tokens [State::TokenSet, nil] current tokens whose refresh token is retained if omitted
28
+ # @return [State::TokenSet] validated tokens
29
+ # @raise [Error] if the response has an invalid content type, body or token fields
30
+ #
31
+ def self.parse(response, current_tokens: nil)
32
+ content_type = response_header(response, 'Content-Type', strip_parameters: true)
33
+
34
+ unless content_type&.casecmp?('application/json')
35
+ raise Error, "Token response Content-Type must be application/json"
36
+ end
37
+
38
+ raise Error, "Missing token response body" unless response[:body].is_a?(String)
39
+
40
+ data = JSON.parse(response[:body])
41
+ raise Error, "Token response must be a JSON object" unless data.is_a?(Hash)
42
+ raise Error, "Token response must not include id_token" if data.key?('id_token')
43
+
44
+ ['access_token', 'token_type', 'expires_in', 'scope', 'sub'].each do |field|
45
+ raise Error, "Missing #{field} in token response" if data[field].nil?
46
+ end
47
+
48
+ unless data['expires_in'].is_a?(Integer) && data['expires_in'].positive?
49
+ raise Error, "Invalid expires_in in token response: must be a positive integer"
50
+ end
51
+
52
+ if data.key?('refresh_token') && data['refresh_token'].nil?
53
+ raise Error, "Invalid refresh_token in token response"
54
+ end
55
+
56
+ tokens = State::TokenSet.new(
57
+ access_token: data['access_token'],
58
+ refresh_token: data.fetch('refresh_token', current_tokens&.refresh_token),
59
+ token_type: data['token_type'],
60
+ expires_in: data['expires_in'],
61
+ scope: data['scope'],
62
+ sub: data['sub']
63
+ )
64
+
65
+ raise Error, "Invalid scope in token response: missing atproto" unless tokens.scope.split.include?('atproto')
66
+
67
+ tokens
68
+ rescue JSON::ParserError, ArgumentError => e
69
+ raise Error, "Invalid token response: #{e.message}"
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../configuration'
4
+ require_relative '../errors'
5
+ require_relative '../par'
6
+
7
+ module AuthProto
8
+ module Token
9
+
10
+ #
11
+ # Revokes tokens using the issuing server and the session's authentication keys.
12
+ #
13
+
14
+ class Revocation
15
+
16
+ # Error raised when revocation is unavailable or rejected by the server.
17
+ class Error < AuthProto::Error; end
18
+
19
+ # Creates a revoker using the original session's server and authentication keys.
20
+ #
21
+ # @param session [State::Session] session bound to the issuing authorization server
22
+ # @param dpop_client [DPoP::Client] proof signer and nonce tracker
23
+ # @param client_metadata [Metadata::ClientMetadata] client authentication configuration
24
+ # @param client_assertion [PAR::ClientAssertion, nil] confidential-client assertion signer
25
+ #
26
+ def initialize(session:, dpop_client:, client_metadata:, client_assertion: nil)
27
+ @session = session
28
+ @dpop_client = dpop_client
29
+ @client_metadata = client_metadata
30
+ @client_assertion = client_assertion
31
+ end
32
+
33
+ # Revokes an access or refresh token and requires a successful response.
34
+ # The response body is ignored, including for tokens already revoked.
35
+ #
36
+ # @param token [String] nonempty token to revoke
37
+ # @param token_type [String, Symbol] `access_token` or `refresh_token`
38
+ # @return [void]
39
+ # @raise [ArgumentError] if the token or hint is invalid
40
+ # @raise [Error] if the endpoint is absent or the server rejects revocation
41
+ # @raise [DPoP::Client::Error, HTTPClient::HTTPError] if proof or transport handling fails
42
+ #
43
+ def revoke_token(token, token_type:)
44
+ raise ArgumentError, "Token must be a nonempty string" unless token.is_a?(String) && !token.empty?
45
+
46
+ token_type = token_type.to_s if token_type.is_a?(Symbol)
47
+
48
+ unless %w[access_token refresh_token].include?(token_type)
49
+ raise ArgumentError, "Invalid token_type"
50
+ end
51
+
52
+ endpoint = @session.auth_server&.revocation_endpoint
53
+ raise Error, "Authorization server does not advertise a revocation endpoint" unless endpoint
54
+
55
+ response = @dpop_client.with_nonce_handling(@session, endpoint) do
56
+ make_request(token, endpoint, token_type)
57
+ end
58
+
59
+ raise Error, "Token revocation failed: HTTP #{response[:status]}" unless response[:status] == 200
60
+ end
61
+
62
+
63
+ private
64
+
65
+ def make_request(token, endpoint, token_type)
66
+ body = {
67
+ token: token,
68
+ token_type_hint: token_type,
69
+ client_id: @client_metadata.client_id
70
+ }
71
+
72
+ if @client_metadata.confidential?
73
+ body.merge!(
74
+ client_assertion_type: PAR::CLIENT_ASSERTION_TYPE,
75
+ client_assertion: @client_assertion.generate_jwt(audience: @session.auth_server.issuer)
76
+ )
77
+ end
78
+
79
+ proof = @dpop_client.generate_proof(session: @session, method: 'POST', uri: endpoint)
80
+
81
+ AuthProto.configuration.http_client.post(
82
+ endpoint,
83
+ body: body,
84
+ headers: {
85
+ 'Content-Type' => 'application/x-www-form-urlencoded',
86
+ 'DPoP' => proof
87
+ }
88
+ )
89
+ end
90
+ end
91
+ end
92
+ end
@@ -4,5 +4,5 @@ module AuthProto
4
4
 
5
5
  # Current gem version.
6
6
  # @return [String]
7
- VERSION = "0.3.0.beta3"
7
+ VERSION = "0.3.0.beta4"
8
8
  end
@@ -22,19 +22,21 @@ module AuthProto
22
22
  def refresh_token: (String session_id) -> refreshed_tokens
23
23
  def authorized?: (String session_id) -> bool
24
24
  def auth_headers: (session_id: String, method: String, url: String) -> Hash[String, String]
25
- def delete_session: (String session_id) -> void
25
+ def delete_session: (String session_id, ?revoke: bool) -> void
26
26
  def cleanup_expired_sessions: () -> void
27
27
 
28
28
  private
29
29
  def load_client_metadata: (json_object? metadata) -> Metadata::ClientMetadata
30
30
  def validate_redirect_uri: () -> void
31
+ def loopback_redirect_components: (String url) -> Array[String?]?
31
32
  def initialize_client_assertion: ((json_object | JOSE::JWK)? key) -> String?
32
33
  def metadata_key_matches_public_key?: (json_object candidate, json_object public_key) -> bool
33
34
  def validate_auth_params: (String? handle, String? pds, String scope) -> void
34
35
  def resolve_auth_server_info: (String pds) -> Metadata::AuthorizationServer
35
36
  def submit_authorization_par: (Metadata::AuthorizationServer authorization_server, State::Session session, ?login_hint: String?) -> String
36
- def exchange_code: (code: String, session: State::Session) -> json_object
37
+ def exchange_code: (code: String, session: State::Session) -> State::TokenSet
37
38
  def make_token_request: (String code, State::Session session) -> http_response
38
- def validate_token_response: (json_object response, State::Session session) -> void
39
+ def validate_token_identity: (State::TokenSet tokens, State::Session session) -> void
40
+ def revoke_rejected_refresh_token: (String token, State::Session session) -> void
39
41
  end
40
42
  end
@@ -11,13 +11,15 @@ module AuthProto
11
11
  class DecryptionError < Error
12
12
  end
13
13
 
14
+ CIPHER: String
15
+ VERSION: Integer
16
+ MASTER_KEY_BYTES: Integer
17
+
14
18
  module HKDF
15
19
  def self.derive: (_ToS secret, salt: String, info: String, length: Integer) -> String
16
20
  end
17
21
 
18
22
  class Service
19
- CIPHER: String
20
- VERSION: Integer
21
23
  def initialize: () -> void
22
24
  def encrypt: (_ToS data, context: String) -> envelope
23
25
  def decrypt: (envelope encrypted, context: String) -> String
@@ -39,7 +39,6 @@ module AuthProto
39
39
  def add_security_headers: (Net::HTTPRequest request, Hash[String, String] headers) -> void
40
40
  def handle_redirect: (original_uri: URI::HTTP, response: Net::HTTPResponse, headers: Hash[String, String], redirect_count: Integer) -> Net::HTTPResponse
41
41
  def validate_response: (Net::HTTPResponse response) -> void
42
- def check_success_status: (Net::HTTPResponse response) -> void
43
42
  def check_content_length: (Net::HTTPResponse response) -> void
44
43
  end
45
44
  end
@@ -67,6 +67,7 @@ module AuthProto
67
67
  def validate_client_id: (String client_id) -> String
68
68
 
69
69
  def valid_https_client_id?: (String client_id) -> bool
70
+ def valid_client_id_hostname?: (String hostname) -> bool
70
71
 
71
72
  def valid_localhost_client_id?: (String client_id) -> bool
72
73
 
@@ -22,8 +22,11 @@ module AuthProto
22
22
  def authorization_server=: (Metadata::AuthorizationServer server) -> Metadata::AuthorizationServer
23
23
  def did=: (String did) -> String
24
24
  def tokens=: (TokenSet tokens) -> String
25
+ def granted_scope: () -> String?
26
+ def has_limited_scope?: () -> bool
25
27
  def session_data: () -> AuthProto::session_data?
26
28
  def pending?: () -> bool
29
+ def can_be_revoked?: () -> bool
27
30
  def tokens_valid?: () -> bool
28
31
  alias authorized? tokens_valid?
29
32
  def renewable?: () -> bool
@@ -2,9 +2,10 @@ module AuthProto
2
2
  module State
3
3
  class SessionManager
4
4
  @serializer: Serialization::Session
5
+ @client_id: String
5
6
  @supported_signing_keys: Hash[String, String]
6
7
 
7
- def initialize: (?supported_signing_keys: Hash[String, String]) -> void
8
+ def initialize: (client_id: String, ?supported_signing_keys: Hash[String, String]) -> void
8
9
  def create_session: (client_id: String, scope: String, ?auth_server: Metadata::AuthorizationServer?, ?did: String?, ?signing_key_id: String?) -> Session
9
10
  def update_session: (Session session) -> Session
10
11
  def complete_authorization: (Session session) -> Session
@@ -14,6 +15,7 @@ module AuthProto
14
15
  def cleanup_expired: () -> void
15
16
 
16
17
  private
18
+ def validate_client_id: (String client_id) -> void
17
19
  def session_ttl: (Session session) -> Integer?
18
20
  end
19
21
  end
@@ -31,6 +31,7 @@ module AuthProto
31
31
  def perform_refresh: () -> State::TokenSet
32
32
 
33
33
  private
34
+ def revoke_rejected_refresh_token: (String token) -> void
34
35
  def with_retries: [T] () { () -> T } -> T
35
36
  def calculate_delay: () -> Float
36
37
  def request_token_refresh: () -> State::TokenSet
@@ -40,7 +41,7 @@ module AuthProto
40
41
  def process_successful_response: (http_response response) -> State::TokenSet
41
42
  def handle_error_response: (http_response response) -> bot
42
43
  def handle_rate_limit_response: (http_response response) -> bot
43
- def validate_refresh_response: (json_object data) -> void
44
+ def validate_refresh_identity: (State::TokenSet tokens) -> void
44
45
  end
45
46
  end
46
47
  end
@@ -0,0 +1,12 @@
1
+ module AuthProto
2
+ module Token
3
+ class Response
4
+ extend HTTPHelpers
5
+
6
+ class Error < AuthProto::Error
7
+ end
8
+
9
+ def self.parse: (http_response response, ?current_tokens: State::TokenSet?) -> State::TokenSet
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ module AuthProto
2
+ module Token
3
+ class Revocation
4
+ class Error < AuthProto::Error
5
+ end
6
+
7
+ def initialize: (session: State::Session, dpop_client: DPoP::Client, client_metadata: Metadata::ClientMetadata, ?client_assertion: PAR::ClientAssertion?) -> void
8
+ def revoke_token: (String token, token_type: String | Symbol) -> void
9
+
10
+ private
11
+ def make_request: (String token, String endpoint, String token_type) -> http_response
12
+ end
13
+ end
14
+ end
data/sig/authproto.rbs CHANGED
@@ -8,7 +8,7 @@ module AuthProto
8
8
  type http_body = String | Hash[untyped, untyped] | nil
9
9
  type http_response = { status: Integer, headers: Hash[String, String], body: String? }
10
10
  type session_data = { access_token: String, token_type: String, expires_in: Integer,
11
- refresh_token: String?, scope: String, session_id: String, did: String }
11
+ refresh_token: String?, scope: String, granted_scope: String, session_id: String, did: String }
12
12
  type token_info = session_data
13
13
  type refreshed_tokens = session_data
14
14
  type callback_result = session_data
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: authproto
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0.beta3
4
+ version: 0.3.0.beta4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Huckabee
@@ -52,9 +52,16 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: 0.5.0
55
- description: A Ruby library for implementing AT Protocol OAuth flows, including DPoP,
56
- PAR, and dynamic client registration. Supports both client and server-side implementations
57
- with comprehensive security features.
55
+ description: "\n A Ruby gem implementing an OAuth 2.1 client specifically targeted
56
+ for use in AT Protocol context. Supports both\n public and confidential clients,
57
+ handles DPoP, nonce management, client and server metadata JSON, PAR, PKCE,\n client
58
+ assertions for confidential clients, storage of session state, and at rest encryption
59
+ of stored tokens.\n Includes SQLite, Redis and in-memory storage backends, and
60
+ an adapter for using the Minisky gem with OAuth\n login support.\n\n This
61
+ gem deliberately does not support any features, profiles and configurations of OAuth
62
+ that are not currently\n used in ATProto, and does not support turning off any
63
+ additional requirements and restrictions that ATProto\n introduces on top of
64
+ standard OAuth specifications.\n "
58
65
  email:
59
66
  - jakub.suder@gmail.com
60
67
  executables: []
@@ -125,7 +132,10 @@ files:
125
132
  - lib/authproto/storage/memory.rb
126
133
  - lib/authproto/storage/redis.rb
127
134
  - lib/authproto/storage/sqlite.rb
135
+ - lib/authproto/token/identity.rb
128
136
  - lib/authproto/token/refresh.rb
137
+ - lib/authproto/token/response.rb
138
+ - lib/authproto/token/revocation.rb
129
139
  - lib/authproto/validated_uri.rb
130
140
  - lib/authproto/version.rb
131
141
  - sig/authproto.rbs
@@ -167,6 +177,8 @@ files:
167
177
  - sig/authproto/storage/redis_client.rbs
168
178
  - sig/authproto/storage/sqlite.rbs
169
179
  - sig/authproto/token/refresh.rbs
180
+ - sig/authproto/token/response.rbs
181
+ - sig/authproto/token/revocation.rbs
170
182
  - sig/authproto/validated_uri.rbs
171
183
  - sig/authproto/version.rbs
172
184
  homepage: https://tangled.org/mackuba.eu/authproto
@@ -193,5 +205,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
193
205
  requirements: []
194
206
  rubygems_version: 4.0.23
195
207
  specification_version: 4
196
- summary: Ruby implementation of the AT Protocol OAuth specification
208
+ summary: Ruby implementation of an AT Protocol specific OAuth client
197
209
  test_files: []