diff --git a/app/ProductionModule.scala b/app/ProductionModule.scala index ccc3fb4..de5fea7 100644 --- a/app/ProductionModule.scala +++ b/app/ProductionModule.scala @@ -14,6 +14,7 @@ import play.api.Play import play.api.Play.current import search.SimpleSearchEngine import service.MongoBasedUserService +import scala.concurrent.ExecutionContext.Implicits.global class ProductionModule { diff --git a/app/controllers/AuthenticationController.scala b/app/controllers/AuthenticationController.scala index 40d2685..3029a31 100644 --- a/app/controllers/AuthenticationController.scala +++ b/app/controllers/AuthenticationController.scala @@ -1,25 +1,59 @@ package controllers -import com.mohiva.play.silhouette.api.{LogoutEvent, Environment, Silhouette} +import com.mohiva.play.silhouette.api.exceptions.AuthenticationException +import com.mohiva.play.silhouette.api.{LoginEvent, LogoutEvent, Environment, Silhouette} import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator +import com.mohiva.play.silhouette.impl.providers.{CommonSocialProfileBuilder, SocialProvider} +import com.mohiva.play.silhouette.impl.services.DelegableAuthInfoService import domain.User +import play.api.mvc.Action +import service.MongoBasedUserService -import scala.concurrent.Future +import scala.concurrent.{ExecutionContext, Future} -class AuthenticationController(override implicit val env: Environment[User, SessionAuthenticator]) +class AuthenticationController(override implicit val env: Environment[User, SessionAuthenticator], implicit val executionContext : ExecutionContext) extends Silhouette[User, SessionAuthenticator] { + val userService = new MongoBasedUserService + + val authInfoService = new DelegableAuthInfoService + def signOut = SecuredAction.async { implicit request => val result = Future.successful(Redirect(routes.Application.index)) env.eventBus.publish(LogoutEvent(request.identity, request, request2lang)) env.authenticatorService.discard(request.authenticator, result) } - def signIn() = play.mvc.Results.TODO + def signIn() = UserAwareAction.async { implicit request => + request.identity match { + case Some(user) => Future.successful(Redirect(routes.Application.index)) + case None => Future.successful(Ok(views.html.login())) + } + } def signUp() = play.mvc.Results.TODO - def authenticate(provider: String) = play.mvc.Results.TODO + def authenticate(provider: String) = Action.async { implicit request => + (env.providers.get(provider) match { + case Some(p: SocialProvider with CommonSocialProfileBuilder) => + p.authenticate().flatMap { + case Left(result) => Future.successful(result) + case Right(authInfo) => for { + profile <- p.retrieveProfile(authInfo) + user <- userService.save(profile) + authInfo <- authInfoService.save(profile.loginInfo, authInfo) + authenticator <- env.authenticatorService.create(user.loginInfo) + result <- env.authenticatorService.init(authenticator, Future.successful( + Redirect(routes.Application.index) + )) + } yield { + env.eventBus.publish(LoginEvent(user, request, request2lang)) + result + } + } + case _ => Future.failed(new AuthenticationException(s"Cannot authenticate with unexpected social provider $provider")) + }).recoverWith(exceptionHandler) + } def credentials() = play.mvc.Results.TODO } diff --git a/app/domain/users.scala b/app/domain/users.scala index 29ede80..2bdd877 100644 --- a/app/domain/users.scala +++ b/app/domain/users.scala @@ -9,7 +9,6 @@ import com.mohiva.play.silhouette.impl.providers.{OAuth2Info, SocialProfile} /** * The user object. * - * @param id The unique ID of the user. * @param loginInfo The linked login info. * @param firstName Maybe the first name of the authenticated user. * @param lastName Maybe the last name of the authenticated user. @@ -18,12 +17,11 @@ import com.mohiva.play.silhouette.impl.providers.{OAuth2Info, SocialProfile} * @param avatarURL Maybe the avatar URL of the authenticated provider. */ case class User( - id: UUID, loginInfo: LoginInfo, firstName: Option[String] = None, lastName: Option[String] = None, fullName: Option[String] = None, email: Option[String] = None, avatarURL: Option[String] = None, - oAuth2Info: Option[OAuth2Info] = None, - gender: Option[String] = None) extends SocialProfile with Identity \ No newline at end of file + oAuth2Info: Option[OAuth2Info] = None + ) extends SocialProfile with Identity \ No newline at end of file diff --git a/app/service/MongoBasedUserService.scala b/app/service/MongoBasedUserService.scala index 8a91f83..80e9a22 100644 --- a/app/service/MongoBasedUserService.scala +++ b/app/service/MongoBasedUserService.scala @@ -2,6 +2,7 @@ package service import com.mohiva.play.silhouette.api.LoginInfo import com.mohiva.play.silhouette.api.services.IdentityService +import com.mohiva.play.silhouette.impl.providers.{CommonSocialProfileBuilder, SocialProvider, CommonSocialProfile} import domain._ import play.Logger import play.api.Play.current @@ -15,6 +16,18 @@ import scala.concurrent.Future class MongoBasedUserService() extends IdentityService[User] with UserSerializer { + + def save(profile: CommonSocialProfile):Future[User] = { + val user = User( + profile.loginInfo, + profile.firstName, + profile.lastName, + profile.fullName, + profile.email, + profile.avatarURL) + save(user) + } + import scala.concurrent.ExecutionContext.Implicits.global /** diff --git a/app/views/login.scala.html b/app/views/login.scala.html index 86e6e9b..f45886a 100644 --- a/app/views/login.scala.html +++ b/app/views/login.scala.html @@ -1,4 +1,4 @@ -@(loginForm: Form[(String,String)], errorMsg: Option[String] = None)(implicit request: RequestHeader, lang: Lang, env: com.mohiva.play.silhouette.api.Environment[domain.User, com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator], flash: Flash) +@(errorMsg: Option[String] = None)(implicit request: RequestHeader, lang: Lang, env: com.mohiva.play.silhouette.api.Environment[domain.User, com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator], flash: Flash) @main() { @errorMsg.map { msg => @@ -22,7 +22,7 @@

Connexion requise

- Se connecter avec Google + Se connecter avec Google
} \ No newline at end of file diff --git a/conf/silhouette.conf b/conf/silhouette.conf index 7939559..a2eb516 100644 --- a/conf/silhouette.conf +++ b/conf/silhouette.conf @@ -16,7 +16,7 @@ silhouette { # Google provider google.authorizationURL="https://accounts.google.com/o/oauth2/auth" google.accessTokenURL="https://accounts.google.com/o/oauth2/token" - google.redirectURL="http://localhost:9000/authenticate/google" + google.redirectURL="http://82.225.76.110:9000/authenticate/google" google.clientID="85340425815.apps.googleusercontent.com" google.clientSecret="1cVOk9mreGMBVGOPF9B_5ivb" google.scope="https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/drive.appdata https://www.googleapis.com/auth/drive.apps.readonly"