Class: X::OAuth1Authenticator
- Inherits:
-
Authenticator
- Object
- Authenticator
- X::OAuth1Authenticator
- Defined in:
- x-core/lib/x/core/oauth1_authenticator.rb
Overview
Authenticator for OAuth 1.0a authentication
Instance Attribute Summary collapse
-
#api_key ⇒ String
readonly
The API key (consumer key).
Instance Method Summary collapse
-
#headers(request) ⇒ Hash{String => String}
Generate the OAuth authentication headers for a request.
-
#initialize(api_key:, api_key_secret:, access_token:, access_token_secret:) ⇒ OAuth1Authenticator
constructor
Initialize a new OAuth1Authenticator.
-
#user_id ⇒ Integer?
The identifier of the user the access token acts for.
Constructor Details
#initialize(api_key:, api_key_secret:, access_token:, access_token_secret:) ⇒ OAuth1Authenticator
Initialize a new OAuth1Authenticator
41 42 43 44 45 |
# File 'x-core/lib/x/core/oauth1_authenticator.rb', line 41 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_secret = SettingValidator.frozen(api_key), SettingValidator.frozen(api_key_secret) @access_token, @access_token_secret = SettingValidator.frozen(access_token), SettingValidator.frozen(access_token_secret) end |
Instance Attribute Details
#api_key ⇒ String (readonly)
The API key (consumer key)
23 24 25 |
# File 'x-core/lib/x/core/oauth1_authenticator.rb', line 23 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.
72 73 74 75 |
# File 'x-core/lib/x/core/oauth1_authenticator.rb', line 72 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.
56 57 58 59 |
# File 'x-core/lib/x/core/oauth1_authenticator.rb', line 56 def user_id prefix = access_token[/\A(\d+)-/, 1] Integer(prefix, 10) if prefix end |