Class: X::OAuth2Authenticator
- Inherits:
-
Authenticator
- Object
- Authenticator
- X::OAuth2Authenticator
- Defined in:
- lib/x/oauth2_authenticator.rb
Overview
Handles OAuth 2.0 authentication with token refresh capability
Constant Summary collapse
- TOKEN_PATH =
Path for the OAuth 2.0 token endpoint
"/2/oauth2/token".freeze
- TOKEN_HOST =
Host for token refresh requests
"api.x.com".freeze
- REFRESH_GRANT_TYPE =
Grant type for token refresh
"refresh_token".freeze
- EXPIRATION_BUFFER =
Buffer time in seconds to account for clock skew and network latency
30
Constants inherited from Authenticator
Authenticator::AUTHENTICATION_HEADER
Instance Attribute Summary collapse
-
#access_token ⇒ String
The OAuth 2.0 access token.
-
#client_id ⇒ String
The OAuth 2.0 client ID.
-
#client_secret ⇒ String
The OAuth 2.0 client secret.
-
#connection ⇒ Connection
The connection for making token requests.
-
#expires_at ⇒ Time?
The expiration time of the access token.
-
#refresh_token ⇒ String
The OAuth 2.0 refresh token.
Instance Method Summary collapse
-
#header(_request) ⇒ Hash{String => String}
Generate the authentication header.
-
#initialize(client_id:, client_secret:, access_token:, refresh_token:, expires_at: nil, connection: Connection.new) ⇒ OAuth2Authenticator
constructor
Initialize a new OAuth 2.0 authenticator.
-
#refresh_token! ⇒ Hash{String => Object}
Refresh the access token using the refresh token.
-
#token_expired? ⇒ Boolean
Check if the access token has expired or will expire soon.
Constructor Details
#initialize(client_id:, client_secret:, access_token:, refresh_token:, expires_at: nil, connection: Connection.new) ⇒ OAuth2Authenticator
Initialize a new OAuth 2.0 authenticator
76 77 78 79 80 81 82 83 84 |
# File 'lib/x/oauth2_authenticator.rb', line 76 def initialize(client_id:, client_secret:, access_token:, refresh_token:, expires_at: nil, connection: Connection.new) @client_id = client_id @client_secret = client_secret @access_token = access_token @refresh_token = refresh_token @expires_at = expires_at @connection = connection end |
Instance Attribute Details
#access_token ⇒ String
The OAuth 2.0 access token
38 39 40 |
# File 'lib/x/oauth2_authenticator.rb', line 38 def access_token @access_token end |
#client_id ⇒ String
The OAuth 2.0 client ID
26 27 28 |
# File 'lib/x/oauth2_authenticator.rb', line 26 def client_id @client_id end |
#client_secret ⇒ String
The OAuth 2.0 client secret
32 33 34 |
# File 'lib/x/oauth2_authenticator.rb', line 32 def client_secret @client_secret end |
#connection ⇒ Connection
The connection for making token requests
57 58 59 |
# File 'lib/x/oauth2_authenticator.rb', line 57 def connection @connection end |
#expires_at ⇒ Time?
The expiration time of the access token
50 51 52 |
# File 'lib/x/oauth2_authenticator.rb', line 50 def expires_at @expires_at end |
#refresh_token ⇒ String
The OAuth 2.0 refresh token
44 45 46 |
# File 'lib/x/oauth2_authenticator.rb', line 44 def refresh_token @refresh_token end |
Instance Method Details
#header(_request) ⇒ Hash{String => String}
Generate the authentication header
93 94 95 |
# File 'lib/x/oauth2_authenticator.rb', line 93 def header(_request) {AUTHENTICATION_HEADER => "Bearer #{access_token}"} end |
#refresh_token! ⇒ Hash{String => Object}
Refresh the access token using the refresh token
116 117 118 119 |
# File 'lib/x/oauth2_authenticator.rb', line 116 def refresh_token! response = send_token_request handle_token_response(response) end |
#token_expired? ⇒ Boolean
Check if the access token has expired or will expire soon
103 104 105 106 107 |
# File 'lib/x/oauth2_authenticator.rb', line 103 def token_expired? return false if expires_at.nil? Time.now >= expires_at - EXPIRATION_BUFFER end |