Class: X::OAuth2Authenticator

Inherits:
Authenticator show all
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

Instance Method Summary collapse

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

Examples:

Create an authenticator

authenticator = X::OAuth2Authenticator.new(
  client_id: "id",
  client_secret: "secret",
  access_token: "token",
  refresh_token: "refresh"
)

Parameters:

  • client_id (String)

    the OAuth 2.0 client ID

  • client_secret (String)

    the OAuth 2.0 client secret

  • access_token (String)

    the OAuth 2.0 access token

  • refresh_token (String)

    the OAuth 2.0 refresh token

  • expires_at (Time, nil) (defaults to: nil)

    the expiration time of the access token

  • connection (Connection) (defaults to: Connection.new)

    the connection for making token requests



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_tokenString

The OAuth 2.0 access token

Examples:

Get the access token

authenticator.access_token

Returns:

  • (String)

    the access token



38
39
40
# File 'lib/x/oauth2_authenticator.rb', line 38

def access_token
  @access_token
end

#client_idString

The OAuth 2.0 client ID

Examples:

Get the client ID

authenticator.client_id

Returns:

  • (String)

    the client ID



26
27
28
# File 'lib/x/oauth2_authenticator.rb', line 26

def client_id
  @client_id
end

#client_secretString

The OAuth 2.0 client secret

Examples:

Get the client secret

authenticator.client_secret

Returns:

  • (String)

    the client secret



32
33
34
# File 'lib/x/oauth2_authenticator.rb', line 32

def client_secret
  @client_secret
end

#connectionConnection

The connection for making token requests

Examples:

Get the connection

authenticator.connection

Returns:



57
58
59
# File 'lib/x/oauth2_authenticator.rb', line 57

def connection
  @connection
end

#expires_atTime?

The expiration time of the access token

Examples:

Get the expiration time

authenticator.expires_at

Returns:

  • (Time, nil)

    the expiration time



50
51
52
# File 'lib/x/oauth2_authenticator.rb', line 50

def expires_at
  @expires_at
end

#refresh_tokenString

The OAuth 2.0 refresh token

Examples:

Get the refresh token

authenticator.refresh_token

Returns:

  • (String)

    the 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

Examples:

Get the header

authenticator.header(request)

Parameters:

  • _request (Net::HTTPRequest, nil)

    the HTTP request (unused)

Returns:

  • (Hash{String => String})

    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

Examples:

Refresh the token

authenticator.refresh_token!

Returns:

  • (Hash{String => Object})

    the token response

Raises:

  • (Error)

    if token refresh fails



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

Examples:

Check expiration

authenticator.token_expired?

Returns:

  • (Boolean)

    true if the token has expired or will expire within the buffer period



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