Updated external user service to return email. The email is a new identifier.

This commit is contained in:
Šesták Vít
2016-02-11 09:54:49 +01:00
parent 2d3e3b5164
commit f4fa0ee948
5 changed files with 31 additions and 19 deletions

View File

@@ -2,13 +2,13 @@ package controllers
import javax.inject.Inject
import _root_.services.CredentialsVerificationService
import _root_.services.{UserService, CredentialsVerificationService}
import com.mohiva.play.silhouette.api._
import com.mohiva.play.silhouette.api.util.Clock
import com.mohiva.play.silhouette.impl.authenticators.CookieAuthenticator
import models.User
import play.api.data.Form
import play.api.data.Forms._
import play.api.data.Forms.{email =>_, _}
import play.api.i18n.{Messages, MessagesApi}
import play.api.libs.concurrent.Execution.Implicits._
@@ -20,7 +20,8 @@ class AuthController @Inject() (
val messagesApi: MessagesApi,
val env: Environment[User, CookieAuthenticator],
clock: Clock,
credentialsVerificationService: CredentialsVerificationService
credentialsVerificationService: CredentialsVerificationService,
userService: UserService
) extends AuthenticatedController {
val signInForm = Form(mapping(
@@ -41,16 +42,19 @@ class AuthController @Inject() (
formWithErrors => Future.successful(BadRequest(views.html.auth.signIn(formWithErrors/*, socialProviderRegistry*/))),
loginRequest => {
credentialsVerificationService.verifyCredentials(loginRequest.username, loginRequest.password).flatMap{
case true =>
val loginInfo: LoginInfo = LoginInfo(providerID = "credentials-verification", providerKey = loginRequest.username)
val user: User = User(username = loginRequest.username)
env.authenticatorService.create(loginInfo) flatMap { authenticator =>
env.eventBus.publish(LoginEvent(user, request, implicitly[Messages]))
env.authenticatorService.init(authenticator).flatMap(cookie =>
case Right(email) =>
val loginInfo: LoginInfo = LoginInfo(providerID = "credentials-verification", providerKey = email)
for{
userOption <- userService.retrieve(loginInfo)
user = userOption.getOrElse(???)
authenticator <- env.authenticatorService.create(loginInfo)
_ = env.eventBus.publish(LoginEvent(user, request, implicitly[Messages]))
res <- env.authenticatorService.init(authenticator).flatMap(cookie =>
env.authenticatorService.embed(cookie.copy(secure = request.secure), Redirect(routes.Application.index(Map())))
)
}
case false => Future.successful(Redirect(routes.AuthController.signIn()).flashing("error" -> Messages("invalid.credentials")))
} yield res
case Left(errorMessage) =>
Future.successful(Redirect(routes.AuthController.signIn()).flashing("error" -> Messages("invalid.credentials")))
}
}
)

View File

@@ -1,5 +1,7 @@
package models
import com.mohiva.play.silhouette.api.Identity
import com.mohiva.play.silhouette.api.{LoginInfo, Identity}
case class User(username: String) extends Identity
case class User(username: String) extends Identity{
def loginInfo = LoginInfo(providerID = "credentials-verification", providerKey = username)
}

View File

@@ -8,6 +8,6 @@ class AllowAllCredentialsVerificationService(app: play.api.Application) extends
sys.error("allow-all can be used in dev mode only")
}
override def verifyCredentials(username: String, password: String): Future[Boolean] = Future.successful(true)
override def verifyCredentials(username: String, password: String): Future[Either[String, String]] = Future.successful(Right(username))
}

View File

@@ -3,5 +3,5 @@ package services
import scala.concurrent.Future
trait CredentialsVerificationService {
def verifyCredentials(username: String, password: String): Future[Boolean]
def verifyCredentials(username: String, password: String): Future[Either[String, String]]
}

View File

@@ -5,12 +5,18 @@ import play.api.libs.ws.{WS, WSClient}
import scala.concurrent.{Future, ExecutionContext}
case class LoginResponse(error: Option[String], email: Option[String])
class ExternalCredentialsVerificationService(url: String)(implicit executionContext: ExecutionContext, wSClient: WSClient) extends CredentialsVerificationService{
override def verifyCredentials(username: String, password: String): Future[Boolean] = {
private implicit val loginResponseFormat = Json.format[LoginResponse]
override def verifyCredentials(username: String, password: String): Future[Either[String, String]] = {
WS.clientUrl(url).post(Json.toJson(Map("username" -> username, "password" -> password))).map{ response =>
response.body match {
case "OK" => true
case "bad" => false
val loginResponse = loginResponseFormat.reads(response.json).get
loginResponse.error match {
case Some(err) => Left(err)
case None => Right(loginResponse.email.get)
}
}
}