|
| 1 | +package controllers |
| 2 | + |
| 3 | +import javax.inject.Inject |
| 4 | + |
| 5 | +import scala.concurrent.Future |
| 6 | +import scala.concurrent.duration.FiniteDuration |
| 7 | +import net.ceedubs.ficus.Ficus.{finiteDurationReader, toFicusConfig, optionValueReader} |
| 8 | +import com.mohiva.play.silhouette.api.Authenticator.Implicits.RichDateTime |
| 9 | +import com.mohiva.play.silhouette.api.{Environment, Silhouette} |
| 10 | +import com.mohiva.play.silhouette.api.exceptions.ProviderException |
| 11 | +import com.mohiva.play.silhouette.api.util.Credentials |
| 12 | +import com.mohiva.play.silhouette.impl.providers.CredentialsProvider |
| 13 | +import play.api.Configuration |
| 14 | +import play.api.data.Form |
| 15 | +import play.api.data.Forms.{boolean, email, mapping, nonEmptyText} |
| 16 | +import play.api.mvc.{AbstractController, Action, AnyContent, ControllerComponents} |
| 17 | +import play.api.i18n.{I18nSupport, Messages, MessagesApi} |
| 18 | +import scala.concurrent.ExecutionContext.Implicits.global |
| 19 | +import services.UserService |
| 20 | +import org.joda.time.DateTime |
| 21 | +import AuthForms.SignInForm |
| 22 | +import utils.auth.DefaultEnv |
| 23 | + |
| 24 | +/** |
| 25 | + * Form used to store the three input variables when logging in: E-Mail, Password, RememberMe |
| 26 | + */ |
| 27 | +object AuthForms { |
| 28 | + |
| 29 | + case class SignInData(email:String, password:String, rememberMe:Boolean) |
| 30 | + val SignInForm = Form(mapping( |
| 31 | + "email" -> email, |
| 32 | + "password" -> nonEmptyText, |
| 33 | + "rememberMe" -> boolean |
| 34 | + )(SignInData.apply)(SignInData.unapply) |
| 35 | + ) |
| 36 | + |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Controller handling authentification related GET requests |
| 41 | + * |
| 42 | + * @param messagesApi Injected handle of current play MessagesApi |
| 43 | + * @param env Injected handle of current silhouette Environment |
| 44 | + * @param credentialsProvider Injected handle of the CredetialsProvider |
| 45 | + * @param userService Injected handle of the current UserService (defined in services/UserService.scala, bound in Module.scala) |
| 46 | + * @param configuration Injected handle of current application configuration |
| 47 | + * @param cc Injected handle of current ControllerComponents, needed to extend superclass AbstractController |
| 48 | + * @param silhouette Injected handle of current silhouette instance |
| 49 | + */ |
| 50 | +class AuthController @Inject() ( |
| 51 | + override val messagesApi:MessagesApi, |
| 52 | + val env:Environment[DefaultEnv], |
| 53 | + credentialsProvider: CredentialsProvider, |
| 54 | + userService: UserService, |
| 55 | + configuration: Configuration, |
| 56 | + cc: ControllerComponents, |
| 57 | + silhouette: Silhouette[DefaultEnv]) extends AbstractController(cc) with I18nSupport{ |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + /** |
| 62 | + * Called on GET /auth/signIn, will show the signIn page. If the user already is logged on, he will be redirected to |
| 63 | + * the index page |
| 64 | + * |
| 65 | + * @return Action redirecting the user |
| 66 | + */ |
| 67 | + def signIn: Action[AnyContent] = silhouette.UserAwareAction.async { implicit request => |
| 68 | + Future.successful(request.identity match { |
| 69 | + case Some(_) => Redirect(routes.HomeController.index()) |
| 70 | + case None => Ok(views.html.auth.signIn(SignInForm)) |
| 71 | + }) |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Called when the user hits Enter / Login-Button on the login-page. Validates login data from the Form defined above |
| 76 | + * and redirects the user to the index page if supplied credentials are valid. Handles any error during login. |
| 77 | + * |
| 78 | + * @return Corrsponding action |
| 79 | + */ |
| 80 | + def authenticate : Action[AnyContent] = Action.async { implicit request => |
| 81 | + SignInForm.bindFromRequest.fold( |
| 82 | + bogusForm => Future.successful(BadRequest(views.html.auth.signIn(bogusForm))), |
| 83 | + signInData => { |
| 84 | + val credentials = Credentials(signInData.email, signInData.password) |
| 85 | + credentialsProvider.authenticate(credentials).flatMap { loginInfo => |
| 86 | + userService.retrieve(loginInfo).flatMap { |
| 87 | + case None => |
| 88 | + Future.successful(Redirect(routes.AuthController.signIn()).flashing("error" -> Messages("error.noUser"))) |
| 89 | + case Some(user) if !user.profileFor(loginInfo).exists(_.confirmed) => |
| 90 | + Future.successful(Redirect(routes.AuthController.signIn()).flashing("error" -> Messages("error.unregistered", signInData.email))) |
| 91 | + case Some(_) => for { |
| 92 | + authenticator <- env.authenticatorService.create(loginInfo).map { |
| 93 | + case authenticator:Any if signInData.rememberMe => |
| 94 | + val c = configuration.underlying |
| 95 | + authenticator.copy( |
| 96 | + expirationDateTime = new DateTime() + c.as[FiniteDuration]("silhouette.authenticator.rememberMe.authenticatorExpiry"), |
| 97 | + idleTimeout = c.getAs[FiniteDuration]("silhouette.authenticator.rememberMe.authenticatorIdleTimeout"), |
| 98 | + cookieMaxAge = c.getAs[FiniteDuration]("silhouette.authenticator.rememberMe.cookieMaxAge") |
| 99 | + ) |
| 100 | + case authenticator:Any => authenticator |
| 101 | + } |
| 102 | + value <- env.authenticatorService.init(authenticator) |
| 103 | + result <- env.authenticatorService.embed(value, Redirect(routes.HomeController.index())) |
| 104 | + } yield result |
| 105 | + } |
| 106 | + }.recover { |
| 107 | + case _:ProviderException => Redirect(routes.AuthController.signIn()).flashing("error" -> Messages("error.invalidCredentials")) |
| 108 | + } |
| 109 | + } |
| 110 | + ) |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Called when signing out. Deletes the cookie that marked the user as logged in, and redirects him to the index page, which |
| 115 | + * will again redirect him to the signin page |
| 116 | + * |
| 117 | + * @return Action redirecting the user |
| 118 | + */ |
| 119 | + def signOut : Action[AnyContent] = silhouette.SecuredAction.async { implicit request => |
| 120 | + env.authenticatorService.discard(request.authenticator, Redirect(routes.HomeController.index())) |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | +} |
0 commit comments