Skip to content
This repository has been archived by the owner on Sep 12, 2021. It is now read-only.

Commit

Permalink
Merge bfc73c4 into 7424543
Browse files Browse the repository at this point in the history
  • Loading branch information
vbarbanyagra committed Mar 10, 2018
2 parents 7424543 + bfc73c4 commit c2b64ad
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ import scala.concurrent.Future
/**
* Base Vk OAuth 2 provider.
*
* @see http://vk.com/dev/auth_sites
* @see http://vk.com/dev/api_requests
* @see http://vk.com/pages.php?o=-1&p=getProfiles
* @see https://vk.com/dev/auth_sites
* @see https://vk.com/dev/api_requests
* @see https://vk.com/dev/users.get
* @see https://vk.com/dev/objects/user
*/
trait BaseVKProvider extends OAuth2Provider {

Expand Down Expand Up @@ -96,10 +97,12 @@ class VKProfileParser extends SocialProfileParser[JsValue, CommonSocialProfile,
*/
override def parse(json: JsValue, authInfo: OAuth2Info) = Future.successful {
val response = (json \ "response").apply(0)
val userId = (response \ "uid").as[Long]
// `uid` field was deprecated in v.5.0
val userId = (response \ "uid").asOpt[Long].getOrElse((response \ "id").as[Long])
val firstName = (response \ "first_name").asOpt[String]
val lastName = (response \ "last_name").asOpt[String]
val avatarURL = (response \ "photo").asOpt[String]
// `photo` field was deprecated in v.5.4
val avatarURL = (response \ "photo").asOpt[String].orElse((response \ "photo_max_orig").asOpt[String])

CommonSocialProfile(
loginInfo = LoginInfo(ID, userId.toString),
Expand Down Expand Up @@ -156,7 +159,7 @@ object VKProvider {
* The VK constants.
*/
val ID = "vk"
val API = "https://api.vk.com/method/getProfiles?fields=uid,first_name,last_name,photo&access_token=%s"
val API = "https://api.vk.com/method/users.get?fields=id,first_name,last_name,photo_max_orig&v=5.73&access_token=%s"

/**
* Converts the JSON into a [[OAuth2Info]] object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,44 @@ class VKProviderSpec extends OAuth2ProviderSpec {
}
}

"return the social profile from response without photo" in new WithApplication with Context {
val wsRequest = mock[MockWSRequest]
val wsResponse = mock[MockWSRequest#Response]
wsResponse.status returns 200
wsResponse.json returns Helper.loadJson("providers/oauth2/vk.success.without.photo.json").as[JsObject]
wsRequest.get() returns Future.successful(wsResponse)
httpLayer.url(API.format("my.access.token")) returns wsRequest

profile(provider.retrieveProfile(oAuthInfo.as[OAuth2Info])) { p =>
p must be equalTo CommonSocialProfile(
loginInfo = LoginInfo(provider.id, "66748"),
firstName = Some("Apollonia"),
lastName = Some("Vanova"),
email = Some("apollonia.vanova@watchmen.com"),
avatarURL = None
)
}
}

"return the social profile from deprecated API response" in new WithApplication with Context {
val wsRequest = mock[MockWSRequest]
val wsResponse = mock[MockWSRequest#Response]
wsResponse.status returns 200
wsResponse.json returns Helper.loadJson("providers/oauth2/vk.success.deprecated.json")
wsRequest.get() returns Future.successful(wsResponse)
httpLayer.url(API.format("my.access.token")) returns wsRequest

profile(provider.retrieveProfile(oAuthInfo.as[OAuth2Info])) { p =>
p must be equalTo CommonSocialProfile(
loginInfo = LoginInfo(provider.id, "66748"),
firstName = Some("Apollonia"),
lastName = Some("Vanova"),
email = Some("apollonia.vanova@watchmen.com"),
avatarURL = Some("http://vk.com/images/camera_b.gif")
)
}
}

"return the social profile without email" in new WithApplication with Context {
val wsRequest = mock[MockWSRequest]
val wsResponse = mock[MockWSRequest#Response]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"response": [
{
"uid": 66748,
"first_name": "Apollonia",
"last_name": "Vanova",
"photo": "http://vk.com/images/camera_b.gif"
}
]
}
4 changes: 2 additions & 2 deletions silhouette/test/resources/providers/oauth2/vk.success.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"response": [
{
"uid": 66748,
"id": 66748,
"first_name": "Apollonia",
"last_name": "Vanova",
"photo": "http://vk.com/images/camera_b.gif"
"photo_max_orig": "http://vk.com/images/camera_b.gif"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"response": [
{
"id": 66748,
"first_name": "Apollonia",
"last_name": "Vanova"
}
]
}

0 comments on commit c2b64ad

Please sign in to comment.