Permalink
Browse files

Adding Bearer Token support

- Added :bearer_token to the list of configuration parameters
- The default value of the bearer_token is read from the environment
  variable
- Requests send the "Bearer AAA..." type authentication header if a
  bearer token exists
- Added tests to verify that bearer token implementation works as
  intended
  • Loading branch information...
1 parent b30e18c commit 2ec61427008e61b30c294e708a40da3c01218d4a @paracycle committed Apr 16, 2013
Showing with 37 additions and 6 deletions.
  1. +6 −2 lib/twitter/client.rb
  2. +6 −3 lib/twitter/configurable.rb
  3. +5 −0 lib/twitter/default.rb
  4. +13 −0 spec/twitter/client_spec.rb
  5. +7 −1 spec/twitter_spec.rb
View
@@ -101,8 +101,12 @@ def connection
end
def auth_header(method, path, params={})
- uri = URI(@endpoint + path)
- SimpleOAuth::Header.new(method, uri, params, credentials)
+ if application_only_auth?
+ "Bearer #{@bearer_token}"
+ else
+ uri = URI(@endpoint + path)
+ SimpleOAuth::Header.new(method, uri, params, credentials)
+ end
end
end
@@ -4,7 +4,7 @@
module Twitter
module Configurable
extend Forwardable
- attr_writer :consumer_key, :consumer_secret, :oauth_token, :oauth_token_secret
+ attr_writer :consumer_key, :consumer_secret, :oauth_token, :oauth_token_secret, :bearer_token
attr_accessor :endpoint, :connection_options, :identity_map, :middleware
def_delegator :options, :hash
@@ -16,6 +16,7 @@ def keys
:consumer_secret,
:oauth_token,
:oauth_token_secret,
+ :bearer_token,
:endpoint,
:connection_options,
:identity_map,
@@ -37,7 +38,7 @@ def configure
# @return [Boolean]
def credentials?
- credentials.values.all?
+ credentials.values.all? || @bearer_token
end
def reset!
@@ -49,7 +50,9 @@ def reset!
alias setup reset!
private
-
+ def application_only_auth?
+ not @bearer_token.nil?
+ end
# @return [Hash]
def credentials
{
View
@@ -69,6 +69,11 @@ def oauth_token_secret
ENV['TWITTER_OAUTH_TOKEN_SECRET']
end
+ # @return [String]
+ def bearer_token
+ ENV['TWITTER_BEARER_TOKEN']
+ end
+
# @note This is configurable in case you want to use a Twitter-compatible endpoint.
# @see http://status.net/wiki/Twitter-compatible_API
# @see http://en.blog.wordpress.com/2009/12/12/twitter-api/
@@ -38,6 +38,7 @@
:middleware => Proc.new{},
:oauth_token => 'OT',
:oauth_token_secret => 'OS',
+ :bearer_token => 'BT',
:identity_map => ::Hash
}
end
@@ -152,4 +153,16 @@
end
end
+ describe "#auth_header" do
+ subject do
+ Twitter::Client.new(:bearer_token => "BT")
+ end
+
+ it "creates the correct auth headers with supplied bearer_token" do
+ uri = "/1.1/direct_messages.json"
+ authorization = subject.send(:auth_header, :get, uri)
+ expect(authorization).to eq "Bearer BT"
+ end
+ end
+
end
View
@@ -91,7 +91,13 @@
end
describe ".credentials?" do
- it "returns true if all credentials are present" do
+ it "returns true if only bearer_token is supplied" do
+ Twitter.configure do |config|
+ config.bearer_token = 'BT'
+ end
+ expect(Twitter.credentials?).to be_true
+ end
+ it "returns true if all oauth credentials are present" do
Twitter.configure do |config|
config.consumer_key = 'CK'
config.consumer_secret = 'CS'

0 comments on commit 2ec6142

Please sign in to comment.