Class: A2A::Client::Auth::OAuth2
- Inherits:
-
Object
- Object
- A2A::Client::Auth::OAuth2
- Defined in:
- lib/a2a/client/auth/oauth2.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#client_id ⇒ Object
readonly
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
readonly
Returns the value of attribute client_secret.
-
#expires_at ⇒ Object
readonly
Returns the value of attribute expires_at.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#token_url ⇒ Object
readonly
Returns the value of attribute token_url.
Instance Method Summary collapse
-
#apply_to_request(request) ⇒ Object
Apply authentication to a Faraday request.
-
#authorization_header ⇒ String
Get authorization header value.
-
#clear_token! ⇒ Object
Clear the current token (force re-authentication).
-
#initialize(client_id:, client_secret:, token_url:, scope: nil) ⇒ OAuth2
constructor
Initialize OAuth2 authentication.
-
#refresh_token ⇒ Object
private
Refresh the access token using client credentials flow.
-
#refresh_token! ⇒ String
Force refresh the access token.
-
#token ⇒ String
Get a valid access token, refreshing if necessary.
-
#token_expired? ⇒ Boolean
private
Check if the token is expired.
-
#token_valid? ⇒ Boolean
Check if the current token is valid.
Constructor Details
#initialize(client_id:, client_secret:, token_url:, scope: nil) ⇒ OAuth2
Initialize OAuth2 authentication
26 27 28 29 30 31 32 33 34 |
# File 'lib/a2a/client/auth/oauth2.rb', line 26 def initialize(client_id:, client_secret:, token_url:, scope: nil) @client_id = client_id @client_secret = client_secret @token_url = token_url @scope = scope @access_token = nil @expires_at = nil @token_mutex = Mutex.new end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
17 18 19 |
# File 'lib/a2a/client/auth/oauth2.rb', line 17 def access_token @access_token end |
#client_id ⇒ Object (readonly)
Returns the value of attribute client_id.
17 18 19 |
# File 'lib/a2a/client/auth/oauth2.rb', line 17 def client_id @client_id end |
#client_secret ⇒ Object (readonly)
Returns the value of attribute client_secret.
17 18 19 |
# File 'lib/a2a/client/auth/oauth2.rb', line 17 def client_secret @client_secret end |
#expires_at ⇒ Object (readonly)
Returns the value of attribute expires_at.
17 18 19 |
# File 'lib/a2a/client/auth/oauth2.rb', line 17 def expires_at @expires_at end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
17 18 19 |
# File 'lib/a2a/client/auth/oauth2.rb', line 17 def scope @scope end |
#token_url ⇒ Object (readonly)
Returns the value of attribute token_url.
17 18 19 |
# File 'lib/a2a/client/auth/oauth2.rb', line 17 def token_url @token_url end |
Instance Method Details
#apply_to_request(request) ⇒ Object
Apply authentication to a Faraday request
59 60 61 |
# File 'lib/a2a/client/auth/oauth2.rb', line 59 def apply_to_request(request) request.headers["Authorization"] = end |
#authorization_header ⇒ String
Get authorization header value
51 52 53 |
# File 'lib/a2a/client/auth/oauth2.rb', line 51 def "Bearer #{token}" end |
#clear_token! ⇒ Object
Clear the current token (force re-authentication)
83 84 85 86 87 88 |
# File 'lib/a2a/client/auth/oauth2.rb', line 83 def clear_token! @token_mutex.synchronize do @access_token = nil @expires_at = nil end end |
#refresh_token ⇒ Object (private)
Refresh the access token using client credentials flow
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/a2a/client/auth/oauth2.rb', line 105 def refresh_token connection = Faraday.new do |conn| conn.request :url_encoded conn.response :json conn.adapter Faraday.default_adapter end # Prepare request parameters params = { grant_type: "client_credentials", client_id: @client_id, client_secret: @client_secret } params[:scope] = @scope if @scope # Make token request response = connection.post(@token_url, params) unless response.success? raise A2A::Errors::AuthenticationError, "OAuth2 token request failed: #{response.status} - #{response.body}" end token_data = response.body unless token_data["access_token"] raise A2A::Errors::AuthenticationError, "OAuth2 response missing access_token: #{token_data}" end @access_token = token_data["access_token"] # Calculate expiration time expires_in = token_data["expires_in"]&.to_i || 3600 # Default to 1 hour @expires_at = Time.now + expires_in @access_token rescue Faraday::Error => e raise A2A::Errors::AuthenticationError, "OAuth2 token request failed: #{e.message}" end |
#refresh_token! ⇒ String
Force refresh the access token
75 76 77 78 79 |
# File 'lib/a2a/client/auth/oauth2.rb', line 75 def refresh_token! @token_mutex.synchronize do refresh_token end end |
#token ⇒ String
Get a valid access token, refreshing if necessary
40 41 42 43 44 45 |
# File 'lib/a2a/client/auth/oauth2.rb', line 40 def token @token_mutex.synchronize do refresh_token if token_expired? @access_token end end |
#token_expired? ⇒ Boolean (private)
Check if the token is expired
96 97 98 99 100 101 |
# File 'lib/a2a/client/auth/oauth2.rb', line 96 def token_expired? return true unless @expires_at # Consider token expired if it expires within 30 seconds Time.now >= (@expires_at - 30) end |
#token_valid? ⇒ Boolean
Check if the current token is valid
67 68 69 |
# File 'lib/a2a/client/auth/oauth2.rb', line 67 def token_valid? @access_token && !token_expired? end |