Class: X::OAuth1Authenticator

Inherits:
Authenticator show all
Defined in:
x-core/lib/x/core/oauth1_authenticator.rb

Overview

Authenticator for OAuth 1.0a authentication

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initialize a new OAuth1Authenticator

Examples:

Create an OAuth authenticator

authenticator = X::OAuth1Authenticator.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

Raises:

  • (ArgumentError) —

    if a credential is nil or empty



40
41
42
43
44
45
46
# File 'x-core/lib/x/core/oauth1_authenticator.rb', line 40

def initialize(api_key:, api_key_secret:, access_token:, access_token_secret:)
  CredentialValidator.validate_required!({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

#api_key ⇒ String (readonly)

The API key (consumer key)

Examples:

Get the API key

authenticator.api_key

Returns:

  • (String) —

    the API key (consumer key)



22
23
24
# File 'x-core/lib/x/core/oauth1_authenticator.rb', line 22

def api_key
  @api_key
end

Instance Method Details

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

Generate the OAuth authentication headers for a request

The signature covers the HTTP method, the URL, its query parameters, and a form-encoded body. Bodies of any other media type, such as the JSON and multipart bodies the X API takes, are not signed.

Examples:

Generate an OAuth authentication header

authenticator.headers(request)

Parameters:

  • request (#http_method, #uri, #body, #[]) —

    the request, whose method, uri, body, and Content-Type the signature reads

Returns:

  • (Hash{String => String}) —

    the authentication header with OAuth signature



73
74
75
76
# File 'x-core/lib/x/core/oauth1_authenticator.rb', line 73

def headers(request)
  oauth_header = SimpleOAuth::Header.new(request.http_method, request.uri, form_params(request), credentials)
  {AUTHENTICATION_HEADER => oauth_header.to_s}
end

#user_id ⇒ Integer?

The identifier of the user the access token acts for

An OAuth 1.0a access token begins with the identifier of the user who authorized it, so a client that signs with one knows the user it acts for without asking the API.

Examples:

Read the user a client acts for without a request

client.authenticator.user_id # => 7505382

Returns:

  • (Integer, nil) —

    the identifier, or nil for a token that begins with none



57
58
59
60
# File 'x-core/lib/x/core/oauth1_authenticator.rb', line 57

def user_id
  prefix = access_token[/\A(\d+)-/, 1]
  Integer(prefix, 10) if prefix
end