Skip to content

Commit

Permalink
Add google provider
Browse files Browse the repository at this point in the history
  • Loading branch information
msa7 committed Jun 10, 2017
1 parent 79b8c83 commit d429ebd
Show file tree
Hide file tree
Showing 13 changed files with 757 additions and 190 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ MultiAuth is a library that standardizes multi-provider authentication for web a
- Github.com
- Facebook.com
- Vk.com
- Google.com

## Installation

Expand Down
66 changes: 32 additions & 34 deletions spec/providers/facebook_spec.cr
Original file line number Diff line number Diff line change
@@ -1,43 +1,41 @@
require "../spec_helper"

describe MultiAuth do
context "facebook" do
it "generates authorize_uri" do
uri = MultiAuth.make("facebook", "/callback").authorize_uri
uri.should eq("https://www.facebook.com/v2.9/dialog/oauth?client_id=facebook_id&redirect_uri=%2Fcallback&response_type=code&scope=email")
end

it "fetch user" do
WebMock.wrap do
WebMock
.stub(:post, "https://graph.facebook.com/v2.9/oauth/access_token")
.with(
body: "client_id=facebook_id&client_secret=facebook_secret&redirect_uri=%2Fcallback&grant_type=authorization_code&code=123",
headers: {"Accept" => "application/json", "Content-type" => "application/x-www-form-urlencoded"})
.to_return(
body: %({
"access_token" : "1111",
"token_type" : "Bearer",
"expires_in" : 899,
"refresh_token" : null,
"scope" : "user"
})
)
describe MultiAuth::Provider::Facebook do
it "generates authorize_uri" do
uri = MultiAuth.make("facebook", "/callback").authorize_uri
uri.should eq("https://www.facebook.com/v2.9/dialog/oauth?client_id=facebook_id&redirect_uri=%2Fcallback&response_type=code&scope=email")
end

WebMock
.stub(:get, "https://graph.facebook.com/v2.9/me?fields=id,name,last_name,first_name,email,location,about,website")
.to_return(
body: %({
"name" : "Sergey",
"id" : "3333"
it "fetch user" do
WebMock.wrap do
WebMock
.stub(:post, "https://graph.facebook.com/v2.9/oauth/access_token")
.with(
body: "client_id=facebook_id&client_secret=facebook_secret&redirect_uri=%2Fcallback&grant_type=authorization_code&code=123",
headers: {"Accept" => "application/json", "Content-type" => "application/x-www-form-urlencoded"})
.to_return(
body: %({
"access_token" : "1111",
"token_type" : "Bearer",
"expires_in" : 899,
"refresh_token" : null,
"scope" : "user"
})
)
)

WebMock
.stub(:get, "https://graph.facebook.com/v2.9/me?fields=id,name,last_name,first_name,email,location,about,website")
.to_return(
body: %({
"name" : "Sergey",
"id" : "3333"
})
)

user = MultiAuth.make("facebook", "/callback").user({"code" => "123"}).as(MultiAuth::User)
user = MultiAuth.make("facebook", "/callback").user({"code" => "123"}).as(MultiAuth::User)

user.name.should eq("Sergey")
user.uid.should eq("3333")
end
user.name.should eq("Sergey")
user.uid.should eq("3333")
end
end
end
89 changes: 27 additions & 62 deletions spec/providers/github_spec.cr
Original file line number Diff line number Diff line change
@@ -1,69 +1,34 @@
require "../spec_helper"

describe MultiAuth do
context "github" do
it "generates authorize_uri" do
uri = MultiAuth.make("github", "/callback").authorize_uri
uri.should eq("https://github.com/login/oauth/authorize?client_id=github_id&redirect_uri=&response_type=code&scope=user%3Aemail")
end

it "fetch user" do
WebMock.wrap do
WebMock.stub(:post, "https://github.com/login/oauth/access_token")
.with(
body: "client_id=github_id&client_secret=github_secret&redirect_uri=&grant_type=authorization_code&code=123",
headers: {"Accept" => "application/json", "Content-type" => "application/x-www-form-urlencoded"}
)
.to_return(
body: %({
"access_token" : "1111",
"token_type" : "Bearer",
"expires_in" : 899,
"refresh_token" : null,
"scope" : "user"
})
)

WebMock.stub(:get, "https://api.github.com/user")
.to_return(body: File.read("spec/support/github.json"))

user = MultiAuth.make("github", "/callback").user({"code" => "123"}).as(MultiAuth::User)

user.email.should eq("[email protected]")
end
end
describe MultiAuth::Provider::Github do
it "generates authorize_uri" do
uri = MultiAuth.make("github", "/callback").authorize_uri
uri.should eq("https://github.com/login/oauth/authorize?client_id=github_id&redirect_uri=&response_type=code&scope=user%3Aemail")
end

context "facebook" do
it "generates authorize_uri" do
uri = MultiAuth.make("github", "/callback").authorize_uri
uri.should eq("https://github.com/login/oauth/authorize?client_id=github_id&redirect_uri=&response_type=code&scope=user%3Aemail")
end

it "fetch user" do
WebMock.wrap do
WebMock.stub(:post, "https://github.com/login/oauth/access_token")
.with(
body: "client_id=github_id&client_secret=github_secret&redirect_uri=&grant_type=authorization_code&code=123",
headers: {"Accept" => "application/json", "Content-type" => "application/x-www-form-urlencoded"}
)
.to_return(
body: %({
"access_token" : "1111",
"token_type" : "Bearer",
"expires_in" : 899,
"refresh_token" : null,
"scope" : "user"
})
)

WebMock.stub(:get, "https://api.github.com/user")
.to_return(body: File.read("spec/support/github.json"))

user = MultiAuth.make("github", "/callback").user({"code" => "123"}).as(MultiAuth::User)

user.email.should eq("[email protected]")
end
it "fetch user" do
WebMock.wrap do
WebMock.stub(:post, "https://github.com/login/oauth/access_token")
.with(
body: "client_id=github_id&client_secret=github_secret&redirect_uri=&grant_type=authorization_code&code=123",
headers: {"Accept" => "application/json", "Content-type" => "application/x-www-form-urlencoded"}
)
.to_return(
body: %({
"access_token" : "1111",
"token_type" : "Bearer",
"expires_in" : 899,
"refresh_token" : null,
"scope" : "user"
})
)

WebMock.stub(:get, "https://api.github.com/user")
.to_return(body: File.read("spec/support/github.json"))

user = MultiAuth.make("github", "/callback").user({"code" => "123"}).as(MultiAuth::User)

user.email.should eq("[email protected]")
end
end
end
67 changes: 67 additions & 0 deletions spec/providers/google_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require "../spec_helper"

describe MultiAuth::Provider::Google do
it "generates authorize_uri" do
uri = MultiAuth.make("google", "/callback").authorize_uri
uri.should eq("https://accounts.google.com/o/oauth2/v2/auth?client_id=google_id&redirect_uri=%2Fcallback&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuser.emails.read+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuser.phonenumbers.read+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuser.addresses.read+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fplus.login+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcontacts.readonly")
end

it "fetch user" do
WebMock.wrap do
WebMock.stub(:post, "https://www.googleapis.com/oauth2/v4/token")
.with(
body: "client_id=google_id&client_secret=google_secret&redirect_uri=%2Fcallback&grant_type=authorization_code&code=123",
headers: {"Accept" => "application/json", "Content-Length" => "111", "Host" => "www.googleapis.com", "Content-type" => "application/x-www-form-urlencoded"}
)
.to_return(
body: %({
"access_token" : "1111",
"token_type" : "Bearer",
"expires_in" : 899,
"refresh_token" : null,
"scope" : "user"
})
)

WebMock.stub(:get, "https://people.googleapis.com/v1/people/me?requestMask.includeField=person.addresses,person.biographies,person.bragging_rights,person.cover_photos,person.email_addresses,person.im_clients,person.interests,person.names,person.nicknames,person.phone_numbers,person.photos,person.urls")
.to_return(body: File.read("spec/support/google.json"))

user = MultiAuth.make("google", "/callback").user({"code" => "123"}).as(MultiAuth::User)

user.email.should eq("[email protected]")
end
end
end

context "facebook" do
it "generates authorize_uri" do
uri = MultiAuth.make("github", "/callback").authorize_uri
uri.should eq("https://github.com/login/oauth/authorize?client_id=github_id&redirect_uri=&response_type=code&scope=user%3Aemail")
end

it "fetch user" do
WebMock.wrap do
WebMock.stub(:post, "https://github.com/login/oauth/access_token")
.with(
body: "client_id=github_id&client_secret=github_secret&redirect_uri=&grant_type=authorization_code&code=123",
headers: {"Accept" => "application/json", "Content-type" => "application/x-www-form-urlencoded"}
)
.to_return(
body: %({
"access_token" : "1111",
"token_type" : "Bearer",
"expires_in" : 899,
"refresh_token" : null,
"scope" : "user"
})
)

WebMock.stub(:get, "https://api.github.com/user")
.to_return(body: File.read("spec/support/github.json"))

user = MultiAuth.make("github", "/callback").user({"code" => "123"}).as(MultiAuth::User)

user.email.should eq("[email protected]")
end
end
end
70 changes: 34 additions & 36 deletions spec/providers/vk_spec.cr
Original file line number Diff line number Diff line change
@@ -1,45 +1,43 @@
require "../spec_helper"

describe MultiAuth do
context "vk" do
it "generates authorize_uri" do
uri = MultiAuth.make("vk", "/callback").authorize_uri
uri.should eq("https://oauth.vk.com/authorize?client_id=vk_id&redirect_uri=%2Fcallback&response_type=code&scope=email")
end
describe MultiAuth::Provider::Vk do
it "generates authorize_uri" do
uri = MultiAuth.make("vk", "/callback").authorize_uri
uri.should eq("https://oauth.vk.com/authorize?client_id=vk_id&redirect_uri=%2Fcallback&response_type=code&scope=email")
end

it "fetch user" do
WebMock.wrap do
WebMock
.stub(:post, "https://oauth.vk.com/access_token")
.with(
body: "client_id=vk_id&client_secret=vk_secret&redirect_uri=%2Fcallback&grant_type=authorization_code&code=123",
headers: {"Accept" => "application/json", "Content-Length" => "103", "Host" => "oauth.vk.com", "Content-type" => "application/x-www-form-urlencoded"})
.to_return(
body: %({
"access_token" : "1111",
"expires_in" : 899,
"refresh_token" : null,
"scope" : "email",
"user_id" : "3333",
"email" : "[email protected]"
})
)
it "fetch user" do
WebMock.wrap do
WebMock
.stub(:post, "https://oauth.vk.com/access_token")
.with(
body: "client_id=vk_id&client_secret=vk_secret&redirect_uri=%2Fcallback&grant_type=authorization_code&code=123",
headers: {"Accept" => "application/json", "Content-Length" => "103", "Host" => "oauth.vk.com", "Content-type" => "application/x-www-form-urlencoded"})
.to_return(
body: %({
"access_token" : "1111",
"expires_in" : 899,
"refresh_token" : null,
"scope" : "email",
"user_id" : "3333",
"email" : "[email protected]"
})
)

WebMock
.stub(:get, %(https://api.vk.com/method/users.get?fields=about,photo_max_orig,city,country,domain,contacts,site&user_id="3333"&v=5.52))
.to_return(
body: %({"response": [{
"first_name" : "Sergey",
"last_name" : "Makridenkov",
"id" : 3333
}]})
)
WebMock
.stub(:get, %(https://api.vk.com/method/users.get?fields=about,photo_max_orig,city,country,domain,contacts,site&user_id="3333"&v=5.52))
.to_return(
body: %({"response": [{
"first_name" : "Sergey",
"last_name" : "Makridenkov",
"id" : 3333
}]})
)

user = MultiAuth.make("vk", "/callback").user({"code" => "123"}).as(MultiAuth::User)
user = MultiAuth.make("vk", "/callback").user({"code" => "123"}).as(MultiAuth::User)

user.name.should eq("Makridenkov Sergey")
user.uid.should eq("3333")
end
user.name.should eq("Makridenkov Sergey")
user.uid.should eq("3333")
end
end
end
Loading

0 comments on commit d429ebd

Please sign in to comment.