-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding Bearer Token support #387
Changes from 2 commits
2ec6142
54cb2c7
c742400
8c7d59f
15baf20
afb9301
d756d29
79a7ead
2c20ecc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,9 @@ module Twitter | |
module API | ||
module OAuth | ||
include Twitter::API::Utils | ||
|
||
def token | ||
object_from_response(Twitter::Token, :bearer_request, "/oauth2/token", :grant_type => "client_credentials") | ||
end | ||
# Allows a registered application to revoke an issued OAuth 2 Bearer Token by presenting its client credentials. | ||
# | ||
# @see https://dev.twitter.com/docs/api/1.1/post/oauth2/invalidate_token | ||
|
@@ -19,6 +21,19 @@ module OAuth | |
def invalidate_token(access_token) | ||
object_from_response(Twitter::Token, :post, "/oauth2/invalidate_token", :access_token => access_token) | ||
end | ||
|
||
private | ||
def bearer_request(path, params={}) | ||
connection.send(:post, path, params) do |request| | ||
request.headers[:accept] = "*/*" | ||
request.headers[:authorization] = "Basic #{encoded_bearer_token_credentials}" | ||
request.headers[:content_type] = "application/x-www-form-urlencoded; charset=UTF-8" | ||
end.env | ||
rescue Faraday::Error::ClientError | ||
raise Twitter::Error::ClientError | ||
rescue MultiJson::DecodeError | ||
raise Twitter::Error::DecodeError | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand why you put this here, but feels like this is also misplaced. Does it make sense to put There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I understand you correctly, you propose I move However, I am not happy supporting two separate pathways that both call |
||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
require 'forwardable' | ||
require 'twitter/error/configuration_error' | ||
require 'base64' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This require statement belongs in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed. |
||
|
||
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 +17,7 @@ def keys | |
:consumer_secret, | ||
:oauth_token, | ||
:oauth_token_secret, | ||
:bearer_token, | ||
:endpoint, | ||
:connection_options, | ||
:identity_map, | ||
|
@@ -37,7 +39,7 @@ def configure | |
|
||
# @return [Boolean] | ||
def credentials? | ||
credentials.values.all? | ||
credentials.values.all? || @bearer_token | ||
end | ||
|
||
def reset! | ||
|
@@ -49,6 +51,13 @@ def reset! | |
alias setup reset! | ||
|
||
private | ||
def application_only_auth? | ||
not @bearer_token.nil? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stylistic preference for me, but i loathe double negatives like this, perhaps change to: !!@bearer_token There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, that sounds perfectly fine, will change. |
||
end | ||
|
||
def encoded_bearer_token_credentials | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
Base64.strict_encode64("#{@consumer_key}:#{@consumer_secret}") | ||
end | ||
|
||
# @return [Hash] | ||
def credentials | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
alias
this method tobearer_token
?