|
| 1 | +package controllers |
| 2 | + |
| 3 | +import java.util.UUID |
| 4 | +import javax.inject.Inject |
| 5 | + |
| 6 | +import com.mohiva.play.silhouette.api.util.{Clock, Credentials, PasswordHasher} |
| 7 | +import play.api.mvc._ |
| 8 | +import com.mohiva.play.silhouette.api._ |
| 9 | +import com.mohiva.play.silhouette.impl.providers.CredentialsProvider |
| 10 | +import play.api.mvc.ControllerComponents |
| 11 | +import com.mohiva.play.silhouette.api.exceptions.ProviderException |
| 12 | +import com.mohiva.play.silhouette.api.repositories.AuthInfoRepository |
| 13 | +import com.mohiva.play.silhouette.impl.exceptions.IdentityNotFoundException |
| 14 | +import forms.{SignInForm, SignUpForm} |
| 15 | +import models.User |
| 16 | +import models.services.UserService |
| 17 | +import play.api.Configuration |
| 18 | +import play.api.libs.json._ |
| 19 | +import play.api.libs.functional.syntax._ |
| 20 | +import utils.auth.DefaultEnv |
| 21 | + |
| 22 | +import scala.concurrent.{ExecutionContext, Future} |
| 23 | + |
| 24 | + |
| 25 | +class AuthenticationController @Inject() ( |
| 26 | + cc: ControllerComponents, |
| 27 | + silhouette: SilhouetteProvider[DefaultEnv], |
| 28 | + userService: UserService, |
| 29 | + authInfoRepository: AuthInfoRepository, |
| 30 | + passwordHasher: PasswordHasher, |
| 31 | + credentialsProvider: CredentialsProvider, |
| 32 | + configuration: Configuration, |
| 33 | + clock: Clock |
| 34 | +)( implicit ec: ExecutionContext ) |
| 35 | + extends AbstractController(cc) { |
| 36 | + |
| 37 | + |
| 38 | + /** |
| 39 | + * Converts the JSON into a `SignInForm.Data` object. |
| 40 | + */ |
| 41 | + implicit val dataReads = ( |
| 42 | + (__ \ 'email).read[String] and |
| 43 | + (__ \ 'password).read[String] and |
| 44 | + (__ \ 'rememberMe).read[Boolean] |
| 45 | + )(SignInForm.Data.apply _) |
| 46 | + |
| 47 | + |
| 48 | + /** |
| 49 | + * Handles the submitted JSON data. |
| 50 | + * |
| 51 | + * @return The result to display. |
| 52 | + */ |
| 53 | + def signIn = Action.async(parse.json) { implicit request => |
| 54 | + request.body.validate[SignInForm.Data].map { data => |
| 55 | + credentialsProvider.authenticate(Credentials(data.email, data.password)).flatMap { loginInfo => |
| 56 | + userService.retrieve(loginInfo).flatMap { |
| 57 | + case Some(user) => silhouette.env.authenticatorService.create(loginInfo).flatMap { authenticator => |
| 58 | + silhouette.env.authenticatorService.init(authenticator).map { token => |
| 59 | + Ok(Json.obj("token" -> token)) |
| 60 | + } |
| 61 | + } |
| 62 | + case None => Future.failed(new IdentityNotFoundException("Couldn't find user")) |
| 63 | + } |
| 64 | + }.recover { |
| 65 | + case e: ProviderException => |
| 66 | + Unauthorized(Json.obj("message" -> "invalid credentials")) |
| 67 | + } |
| 68 | + }.recoverTotal { |
| 69 | + case error => |
| 70 | + Future.successful(Unauthorized(Json.obj("message" -> "invalid credentials"))) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Handles the submitted JSON data. |
| 76 | + * |
| 77 | + * @return The result to display. |
| 78 | + */ |
| 79 | + def signUp = Action.async(parse.json) { implicit request => |
| 80 | + request.body.validate[SignUpForm.Data].map { data => |
| 81 | + val loginInfo = LoginInfo(CredentialsProvider.ID, data.email) |
| 82 | + userService.retrieve(loginInfo).flatMap { |
| 83 | + case Some(user) => |
| 84 | + Future.successful(BadRequest(Json.obj("message" -> "user exists"))) |
| 85 | + case None => |
| 86 | + val authInfo = passwordHasher.hash(data.password) |
| 87 | + val user = User( |
| 88 | + userID = UUID.randomUUID(), |
| 89 | + loginInfo = loginInfo, |
| 90 | + firstName = Some(data.firstName), |
| 91 | + lastName = Some(data.lastName), |
| 92 | + fullName = Some(data.firstName + " " + data.lastName), |
| 93 | + email = Some(data.email), |
| 94 | + avatarURL = None |
| 95 | + ) |
| 96 | + for { |
| 97 | + user <- userService.save(user) |
| 98 | + authInfo <- authInfoRepository.add(loginInfo, authInfo) |
| 99 | + authenticator <- silhouette.env.authenticatorService.create(loginInfo) |
| 100 | + token <- silhouette.env.authenticatorService.init(authenticator) |
| 101 | + } yield { |
| 102 | + Ok(Json.obj("token" -> token)) |
| 103 | + } |
| 104 | + } |
| 105 | + }.recoverTotal { |
| 106 | + case error => |
| 107 | + Future.successful(Unauthorized(Json.obj("message" -> "invalid.data"))) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Manages the sign out action. |
| 113 | + */ |
| 114 | + def signOut = silhouette.SecuredAction.async { implicit request => |
| 115 | + silhouette.env.authenticatorService.discard(request.authenticator, Ok) |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments