forked from manastech/webmock.cr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request manastech#5 from veelenga/feature/twitter_provider
Twitter provider
- Loading branch information
Showing
11 changed files
with
146 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
require "../spec_helper" | ||
|
||
describe MultiAuth::Provider::Twitter do | ||
request_token_params = { | ||
oauth_token: "NPcudxy0yU5T3tBzho7iCotZ3cnetKwcTIRlX0iwRl0", | ||
oauth_token_secret: "veNRnAWe6inFuo8o2u8SLLZLjolYDmDP7SzL0YfYI", | ||
oauth_callback_confirmed: "true", | ||
} | ||
|
||
access_token_params = { | ||
oauth_token: "7588892-kagSNqWge8gB1WwE3plnFsJHAZVfxWD7Vb57p0b4", | ||
oauth_token_secret: "PbKfYqSryyeKDWz4ebtY3o5ogNLG11WJuZBc9fQrQo", | ||
} | ||
|
||
verify_credentials_params = { | ||
id: 38895958, | ||
name: "Sean Cook", | ||
screen_name: "theSeanCook", | ||
location: "San Francisco", | ||
url: "http://twitter.com", | ||
description: "I taught your phone that thing you like. The Mobile Partner Engineer @Twitter.", | ||
profile_image_url: "http://a0.twimg.com/profile_images/1751506047/dead_sexy_normal.JPG", | ||
email: "[email protected]", | ||
} | ||
|
||
describe "#authorize_uri" do | ||
it "generates authorize uri" do | ||
WebMock.stub(:post, "https://api.twitter.com/oauth/request_token") | ||
.to_return(body: HTTP::Params.encode request_token_params) | ||
uri = MultiAuth.make("twitter", "/callback").authorize_uri | ||
uri.should eq "https://api.twitter.com/oauth/authorize?oauth_token=NPcudxy0yU5T3tBzho7iCotZ3cnetKwcTIRlX0iwRl0&oauth_callback=%2Fcallback" | ||
end | ||
end | ||
|
||
describe "#fetch_tw_user" do | ||
it "successfully fetches user params" do | ||
WebMock.stub(:post, "https://api.twitter.com/oauth/access_token") | ||
.to_return(body: HTTP::Params.encode request_token_params) | ||
|
||
WebMock.stub(:get, "https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true") | ||
.to_return(body: verify_credentials_params.to_json) | ||
|
||
user = MultiAuth.make("twitter", "/callback").user({"oauth_token" => "token", "oauth_verifier" => "verifier"}) | ||
|
||
user.uid.should eq verify_credentials_params[:id].to_s | ||
user.email.should eq verify_credentials_params[:email] | ||
user.name.should eq verify_credentials_params[:name] | ||
user.nickname.should eq verify_credentials_params[:screen_name] | ||
user.location.should eq verify_credentials_params[:location] | ||
user.description.should eq verify_credentials_params[:description] | ||
user.image.should eq verify_credentials_params[:profile_image_url] | ||
user.urls.should eq({"twitter" => verify_credentials_params[:url]}) | ||
|
||
user.provider.should eq "twitter" | ||
user.raw_json.should_not be_nil | ||
user.access_token.should_not be_nil | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
abstract class MultiAuth::Provider | ||
getter redirect_uri : String | ||
getter client_id : String | ||
getter client_secret : String | ||
|
||
def initialize(@redirect_uri : String, @client_id : String, @client_secret : String) | ||
end | ||
getter key : String | ||
getter secret : String | ||
|
||
abstract def authorize_uri(scope = nil) | ||
abstract def user(params : Hash(String, String)) | ||
|
||
def initialize(@redirect_uri : String, @key : String, @secret : String) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
class MultiAuth::Provider::Twitter < MultiAuth::Provider | ||
def authorize_uri(scope = nil) | ||
request_token = consumer.get_request_token(redirect_uri) | ||
consumer.get_authorize_uri(request_token, redirect_uri) | ||
end | ||
|
||
def user(params : Hash(String, String)) | ||
tw_user = fetch_tw_user(params["oauth_token"], params["oauth_verifier"]) | ||
|
||
User.new( | ||
"twitter", | ||
tw_user.id, | ||
tw_user.name, | ||
tw_user.raw_json.to_s, | ||
tw_user.access_token.not_nil! | ||
).tap do |user| | ||
user.email = tw_user.email | ||
user.nickname = tw_user.screen_name | ||
user.location = tw_user.location | ||
user.description = tw_user.description | ||
user.image = tw_user.profile_image_url | ||
if url = tw_user.url | ||
user.urls = {"twitter" => url} | ||
end | ||
end | ||
end | ||
|
||
private class TwUser | ||
property raw_json : String? | ||
property access_token : OAuth::AccessToken? | ||
|
||
JSON.mapping( | ||
id: {type: String, converter: String::RawConverter}, | ||
name: String, | ||
screen_name: String, | ||
location: String?, | ||
description: String?, | ||
url: String?, | ||
profile_image_url: String?, | ||
email: String? | ||
) | ||
end | ||
|
||
private def fetch_tw_user(oauth_token, oauth_verifier) | ||
request_token = OAuth::RequestToken.new oauth_token, "" | ||
access_token = consumer.get_access_token(request_token, oauth_verifier) | ||
|
||
client = HTTP::Client.new("api.twitter.com", tls: true) | ||
access_token.authenticate(client, key, secret) | ||
|
||
raw_json = client.get("/1.1/account/verify_credentials.json?include_email=true").body | ||
|
||
TwUser.from_json(raw_json).tap do |user| | ||
user.access_token = access_token | ||
user.raw_json = raw_json | ||
end | ||
end | ||
|
||
private def consumer | ||
@consumer ||= OAuth::Consumer.new("api.twitter.com", key, secret) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters