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
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)
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.
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.
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 |