Skip to content

Instantly share code, notes, and snippets.

@markusjura
Created March 17, 2014 09:48
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 markusjura/9596606 to your computer and use it in GitHub Desktop.
Save markusjura/9596606 to your computer and use it in GitHub Desktop.
package controllers
import scala.concurrent._
import scala.concurrent.duration._
import play.api.Play._
import play.api.libs.ws.WS
import play.api.libs.json.Json
import play.api.mvc.Controller
import models.LoginData
import play.api.libs.concurrent.Execution.Implicits._
import play.api.Logger
import org.joda.time.DateTime
/**
* Secured Controller
*/
trait SecuredController extends Controller {
sealed trait AuthResult
case class AuthError(authErrorMessage: String) extends AuthResult
case class AuthOK(token: String) extends AuthResult
/**
* Authenticate user with password against the auth service
* @param loginData contains username and password
* @return AuthOK with token if user has been authenticated successfully
* AuthError with error message if authentication failed
*/
def authenticate(loginData: LoginData): Future[AuthResult] = {
WS.url(s"http://eplay-auth-service.herokuapp.com/auth")
.post(Json.toJson(loginData))
.map { response =>
response.status match {
case OK => AuthOK((response.json \ "token").as[String])
case _ => AuthError((response.json \ "error").as[String])
}
}
}
/**
* Authenticate user with password against the auth service
* @param loginData contains username and password
* @return AuthOK with token if user has been authenticated successfully
* AuthError with error message if authentication failed
*/
def authenticateBlocking(loginData: LoginData): AuthResult = {
val authServiceUrl = configuration.getString("service.auth.url").get
val future = WS.url(s"http://eplay-auth-service.herokuapp.com/auth")
.post(Json.toJson(loginData))
val response = Await.result(future, 2.seconds)
response.status match {
case OK => AuthOK((response.json \ "token").as[String])
case _ => AuthError((response.json \ "error").as[String])
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment