Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rallat/e07ea50830d2671b63fa to your computer and use it in GitHub Desktop.
Save rallat/e07ea50830d2671b63fa to your computer and use it in GitHub Desktop.
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