Module: Keycardai::OAuth::TokenRequests
- Included in:
- ClientCredentialsClient, TokenExchangeClient
- Defined in:
- lib/keycardai/oauth/token_requests.rb
Overview
Shared internals for clients that POST to the token endpoint: lazy endpoint discovery with caching, shared-secret (HTTP Basic) client authentication, and RFC 6749 §5.2 error parsing. Not public API.
Class Method Summary collapse
-
.error_for(response) ⇒ OAuthError, HTTPError
RFC 6749 §5.2: an error response carries error / error_description / error_uri.
-
.parse_response(response) ⇒ TokenResponse
Parse a token-endpoint response: a TokenResponse on 2xx, a raised typed error otherwise.
Class Method Details
.error_for(response) ⇒ OAuthError, HTTPError
RFC 6749 §5.2: an error response carries error / error_description / error_uri. Anything else non-2xx is a plain HTTP error.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/keycardai/oauth/token_requests.rb', line 37 def self.error_for(response) payload = begin JSON.parse(response.body) rescue JSON::ParserError nil end unless payload.is_a?(Hash) && payload["error"].is_a?(String) return HTTPError.new("token endpoint returned HTTP #{response.status}", status: response.status, body: response.body) end OAuthError.new("token endpoint returned #{payload["error"]}", error: payload["error"], error_description: payload["error_description"], error_uri: payload["error_uri"], status: response.status, body: response.body) end |
.parse_response(response) ⇒ TokenResponse
Parse a token-endpoint response: a TokenResponse on 2xx, a raised typed error otherwise.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/keycardai/oauth/token_requests.rb', line 16 def self.parse_response(response) raise error_for(response) unless response.success? document = begin JSON.parse(response.body) rescue JSON::ParserError raise ProtocolError.new("token response is not valid JSON", code: "invalid_response") end unless document.is_a?(Hash) raise ProtocolError.new("token response is not a JSON object", code: "invalid_response") end TokenResponse.from_wire(document) end |