Skip to content

Commit

Permalink
Allow use of Twitter::Token in place of bearer token string
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jun 13, 2013
1 parent 0fdb7de commit 13596bc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/twitter/api/oauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ def token
# @rate_limited No
# @authentication Required
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @param access_token [String] The value of the bearer token to revoke.
# @param access_token [String, Twitter::Token] The bearer token to revoke.
# @return [Twitter::Token] The invalidated token. token_type should be nil.
# @example Revoke a token
# Twitter.invalidate_token("AAAA%2FAAA%3DAAAAAAAA")
def invalidate_token(access_token)
access_token = access_token.access_token if access_token.is_a?(Twitter::Token)
object_from_response(Twitter::Token, :post, "/oauth2/invalidate_token", :access_token => access_token)
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/twitter/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def request_setup(method, path, params, signature_params)
request.headers[:accept] = '*/*' # It is important we set this, otherwise we get an error.
elsif params.delete(:app_auth) || !user_token?
unless bearer_token?
@bearer_token = token[:access_token]
@bearer_token = token
Twitter.client.bearer_token = @bearer_token if Twitter.client?
end
request.headers[:authorization] = bearer_auth_header
Expand Down Expand Up @@ -132,7 +132,11 @@ def encode_value(value)
end

def bearer_auth_header
"Bearer #{@bearer_token}"
if @bearer_token.is_a?(Twitter::Token) && @bearer_token.token_type == "bearer"
"Bearer #{@bearer_token.access_token}"
else
"Bearer #{@bearer_token}"
end
end

def oauth_auth_header(method, path, params={})
Expand Down
7 changes: 7 additions & 0 deletions spec/twitter/api/oauth_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@
expect(token.access_token).to eq "AAAA%2FAAA%3DAAAAAAAA"
expect(token.token_type).to eq nil
end
context "with a token" do
it "requests the correct resource" do
token = Twitter::Token.new(:access_token => "AAAA%2FAAA%3DAAAAAAAA")
@client.invalidate_token(token)
expect(a_post("/oauth2/invalidate_token").with(:body => {:access_token => "AAAA%2FAAA%3DAAAAAAAA"})).to have_been_made
end
end
end

end

0 comments on commit 13596bc

Please sign in to comment.