Skip to content

Commit

Permalink
[#34] link authentication page
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Doba committed Oct 20, 2014
1 parent 8657629 commit 64f9ed9
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 12 deletions.
1 change: 1 addition & 0 deletions app/ProductionModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
44 changes: 39 additions & 5 deletions app/controllers/AuthenticationController.scala
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 2 additions & 4 deletions app/domain/users.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
oAuth2Info: Option[OAuth2Info] = None
) extends SocialProfile with Identity
13 changes: 13 additions & 0 deletions app/service/MongoBasedUserService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

/**
Expand Down
4 changes: 2 additions & 2 deletions app/views/login.scala.html
Original file line number Diff line number Diff line change
@@ -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 =>
Expand All @@ -22,7 +22,7 @@
<form class="form-signin text-center">
<h2 class="form-signin-heading">Connexion requise</h2>
<div class="center">
<a class="btn btn-large btn-primary" href="/auth/authenticate/google">Se connecter avec Google</a>
<a class="btn btn-large btn-primary" href="/authenticate/google">Se connecter avec Google</a>
</div>
</form>
}
2 changes: 1 addition & 1 deletion conf/silhouette.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 64f9ed9

Please sign in to comment.