Class: X::OAuthAuthenticator

Inherits:
Authenticator show all
Defined in:
lib/x/oauth_authenticator.rb

Overview

Authenticator for OAuth 1.0a authentication

Constant Summary collapse

OAUTH_VERSION =

OAuth version

"1.0".freeze
OAUTH_SIGNATURE_METHOD =

OAuth signature method

"HMAC-SHA1".freeze
OAUTH_SIGNATURE_ALGORITHM =

OAuth signature algorithm

"sha1".freeze

Constants inherited from Authenticator

Authenticator::AUTHENTICATION_HEADER

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, api_key_secret:, access_token:, access_token_secret:) ⇒ OAuthAuthenticator

Initialize a new OAuthAuthenticator

Examples:

Create an OAuth authenticator

authenticator = X::OAuthAuthenticator.new(
  api_key: "key",
  api_key_secret: "secret",
  access_token: "token",
  access_token_secret: "token_secret"
)

Parameters:

  • api_key (String)

    the API key (consumer key)

  • api_key_secret (String)

    the API key secret (consumer secret)

  • access_token (String)

    the access token

  • access_token_secret (String)

    the access token secret



63
64
65
66
67
68
# File 'lib/x/oauth_authenticator.rb', line 63

def initialize(api_key:, api_key_secret:, access_token:, access_token_secret:)
  @api_key = api_key
  @api_key_secret = api_key_secret
  @access_token = access_token
  @access_token_secret = access_token_secret
end

Instance Attribute Details

#access_tokenString

The access token

Examples:

Get or set the access token

authenticator.access_token = "token"

Returns:

  • (String)

    the access token



39
40
41
# File 'lib/x/oauth_authenticator.rb', line 39

def access_token
  @access_token
end

#access_token_secretString

The access token secret

Examples:

Get or set the access token secret

authenticator.access_token_secret = "token_secret"

Returns:

  • (String)

    the access token secret



46
47
48
# File 'lib/x/oauth_authenticator.rb', line 46

def access_token_secret
  @access_token_secret
end

#api_keyString

The API key (consumer key)

Examples:

Get or set the API key

authenticator.api_key = "key"

Returns:

  • (String)

    the API key (consumer key)



25
26
27
# File 'lib/x/oauth_authenticator.rb', line 25

def api_key
  @api_key
end

#api_key_secretString

The API key secret (consumer secret)

Examples:

Get or set the API key secret

authenticator.api_key_secret = "secret"

Returns:

  • (String)

    the API key secret (consumer secret)



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

def api_key_secret
  @api_key_secret
end

Instance Method Details

#header(request) ⇒ Hash{String => String}

Generate the OAuth authentication header for a request

Examples:

Generate an OAuth authentication header

authenticator.header(request)

Parameters:

  • request (Net::HTTPRequest)

    the HTTP request

Returns:

  • (Hash{String => String})

    the authentication header with OAuth signature



77
78
79
80
# File 'lib/x/oauth_authenticator.rb', line 77

def header(request)
  method, url, query_params = parse_request(request)
  {AUTHENTICATION_HEADER => build_oauth_header(method, url, query_params)}
end