Class: AtProto::DpopHandler
- Inherits:
-
Object
- Object
- AtProto::DpopHandler
- Defined in:
- lib/atproto_client/dpop_handler.rb
Overview
Handler for DPoP (Demonstrating Proof-of-Possession) protocol implementation
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#private_key ⇒ Object
Returns the value of attribute private_key.
Instance Method Summary collapse
-
#generate_token(http_method, url, nonce = @current_nonce) ⇒ String
Generates a DPoP token for a request.
-
#initialize(private_key = nil, access_token = nil) ⇒ DpopHandler
constructor
Initialize a new DPoP handler.
-
#make_request(uri, method, headers: {}, body: nil) ⇒ Net::HTTPResponse
Makes an HTTP request with DPoP handling, when no nonce is used for the first try, takes it from the response and retry.
-
#update_nonce(response) ⇒ Object
Updates the current nonce from response headers.
Constructor Details
#initialize(private_key = nil, access_token = nil) ⇒ DpopHandler
Initialize a new DPoP handler
9 10 11 12 13 14 15 |
# File 'lib/atproto_client/dpop_handler.rb', line 9 def initialize(private_key = nil, access_token = nil) @private_key = private_key || generate_private_key @current_nonce = nil @nonce_mutex = Mutex.new @token_mutex = Mutex.new @access_token = access_token end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
4 5 6 |
# File 'lib/atproto_client/dpop_handler.rb', line 4 def access_token @access_token end |
#private_key ⇒ Object
Returns the value of attribute private_key.
4 5 6 |
# File 'lib/atproto_client/dpop_handler.rb', line 4 def private_key @private_key end |
Instance Method Details
#generate_token(http_method, url, nonce = @current_nonce) ⇒ String
Generates a DPoP token for a request
22 23 24 25 26 |
# File 'lib/atproto_client/dpop_handler.rb', line 22 def generate_token(http_method, url, nonce = @current_nonce) @token_mutex.synchronize do create_dpop_token(http_method, url, nonce) end end |
#make_request(uri, method, headers: {}, body: nil) ⇒ Net::HTTPResponse
Makes an HTTP request with DPoP handling, when no nonce is used for the first try, takes it from the response and retry
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/atproto_client/dpop_handler.rb', line 45 def make_request(uri, method, headers: {}, body: nil) retried = false begin dpop_token = generate_token(method.to_s.upcase, uri.to_s) request = Request.new(method, uri, headers.merge('DPoP' => dpop_token)) request.body = body.is_a?(Hash) ? body.to_json : body if body request.run rescue Net::HTTPClientException => e unless retried update_nonce(e.response) retried = true retry end raise APIError, "Request failed: #{e.response.code} - #{e.response.body}" end end |
#update_nonce(response) ⇒ Object
Updates the current nonce from response headers
30 31 32 33 34 35 |
# File 'lib/atproto_client/dpop_handler.rb', line 30 def update_nonce(response) new_nonce = response.to_hash.dig('dpop-nonce', 0) @nonce_mutex.synchronize do @current_nonce = new_nonce if new_nonce end end |