Forked from efrohnhoefer/VerifyCredentialsController.scala
Last active
August 29, 2015 14:21
-
-
Save rallat/e07ea50830d2671b63fa to your computer and use it in GitHub Desktop.
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
package controllers | |
import play.api.libs.ws.WS | |
import play.api.mvc.{Action, Controller} | |
import scala.concurrent.Future | |
class VerifyCredentialsController extends Controller { | |
implicit val context = scala.concurrent.ExecutionContext.Implicits.global | |
def verify = Action.async(parse.multipartFormData) { request => | |
val urlOpt = request.headers.get("X-Auth-Service-Provider") | |
val tokenOpt = request.headers.get("X-Verify-Credentials-Authorization") | |
getTwitterUserId(urlOpt, tokenOpt).map { user => | |
user match { | |
case Some(id) => | |
// Do something interesting here here | |
Ok("Success") | |
case None => Unauthorized("Failure") | |
} | |
} | |
} | |
def getTwitterUserId(urlOpt: Option[String], tokenOpt: Option[String]): Future[Option[Long]] = { | |
(urlOpt, tokenOpt) match { | |
case (Some(url), Some(token)) => executeVerifyCredentialsResquest(url, token) | |
case _ => Future.successful(None) | |
} | |
} | |
def executeVerifyCredentialsResquest(url: String, token: String): Future[Option[Long]] = { | |
WS.url(url).withHeaders("Authorization" -> token).get.map { response => | |
response.status match { | |
case 200 => (response.json \ "id").asOpt[Long] | |
case _ => None | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment