Skip to content

Commit

Permalink
[#34] begin implementation of silhouette, the project builds and appl…
Browse files Browse the repository at this point in the history
…ication is launched
  • Loading branch information
Vincent Doba committed Oct 19, 2014
1 parent 329b173 commit 2bec82e
Show file tree
Hide file tree
Showing 55 changed files with 431 additions and 408 deletions.
25 changes: 24 additions & 1 deletion app/Global.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import actors.{ActivityActor, InvoiceActor}
import akka.actor.Props
import com.mohiva.play.silhouette.api.SecuredSettings
import com.softwaremill.macwire.MacwireMacros._
import controllers.api.ClientApiController
import controllers.routes
import play.Logger
import play.api.i18n.{Lang, Messages}
import play.api.mvc.Results._
import play.api.mvc.{RequestHeader, Result}
import play.api.{Application, GlobalSettings}
import play.libs.Akka
import search.SimpleSearchEngine

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

object Global extends GlobalSettings {
object Global extends GlobalSettings with SecuredSettings {
val wired = wiredInModule(new ProductionModule)

override def onStart(app: Application) = {
Expand All @@ -32,4 +39,20 @@ object Global extends GlobalSettings {
override def getControllerInstance[A](controllerClass: Class[A]) = {
wired.lookupSingleOrThrow(controllerClass)
}

override def onNotAuthenticated(request: RequestHeader, lang: Lang): Option[Future[Result]] = {
Some(Future.successful(Redirect(routes.AuthenticationController.signIn)))
}
/**
* Called when a user is authenticated but not authorized.
*
* As defined by RFC 2616, the status code of the response should be 403 Forbidden.
*
* @param request The request header.
* @param lang The currently selected language.
* @return The result to send to the client.
*/
override def onNotAuthorized(request: RequestHeader, lang: Lang): Option[Future[Result]] = {
Some(Future.successful(Redirect(routes.AuthenticationController.signIn).flashing("error" -> Messages("access.denied"))))
}
}
80 changes: 53 additions & 27 deletions app/ProductionModule.scala
Original file line number Diff line number Diff line change
@@ -1,44 +1,68 @@
import com.mohiva.play.silhouette.api.services.{AuthenticatorService, IdentityService}
import com.mohiva.play.silhouette.api.util.{Clock, FingerprintGenerator, IDGenerator, PlayHTTPLayer}
import com.mohiva.play.silhouette.api.{Environment, EventBus, Provider}
import com.mohiva.play.silhouette.impl.authenticators._
import com.mohiva.play.silhouette.impl.providers.oauth2.GoogleProvider
import com.mohiva.play.silhouette.impl.providers.oauth2.state.{CookieStateProvider, CookieStateSettings}
import com.mohiva.play.silhouette.impl.providers.{OAuth2Settings, OAuth2StateProvider}
import com.mohiva.play.silhouette.impl.util.{DefaultFingerprintGenerator, SecureRandomIDGenerator}
import com.softwaremill.macwire.MacwireMacros._
import controllers._
import controllers.api._
import play.api.data.Form
import play.api.i18n.Lang
import play.api.mvc.{Flash, RequestHeader}
import domain.User
import play.api.Play
import play.api.Play.current
import search.SimpleSearchEngine
import securesocial.controllers._
import securesocial.core._
import securesocial.core.providers._
import service.MongoBasedUserService

import scala.collection.immutable.ListMap


class ProductionModule {

object ProductionRuntimeEnvironment extends RuntimeEnvironment.Default[BasicProfile] {
override lazy val userService = new MongoBasedUserService
//override lazy val eventListeners = List(new MyEventListener())
override lazy val providers = ListMap(
// oauth 2 client providers
include(new GoogleProvider(routes, cacheService, oauth2ClientFor(GoogleProvider.Google)))
)
override lazy val viewTemplates = new ViewTemplates.Default(this){
override def getLoginPage(form: Form[(String, String)], msg: Option[String])(implicit request: RequestHeader, lang: Lang)= {
views.html.login(form, msg)(request, lang, env, request.flash)
}

override def getNotAuthorizedPage(implicit request : RequestHeader, lang : Lang) = {
views.html.unauthorized()(request, lang, env, request.flash)
}
object ProductionRuntimeEnvironment extends Environment[User, SessionAuthenticator] {
override def identityService: IdentityService[User] = new MongoBasedUserService

override def authenticatorService: AuthenticatorService[SessionAuthenticator] = {
provideAuthenticatorService(new DefaultFingerprintGenerator(false))
}

def provideAuthenticatorService(
fingerprintGenerator: FingerprintGenerator): SessionAuthenticatorService = {
new SessionAuthenticatorService(SessionAuthenticatorSettings(
sessionKey = Play.configuration.getString("silhouette.authenticator.sessionKey").get,
encryptAuthenticator = Play.configuration.getBoolean("silhouette.authenticator.encryptAuthenticator").get,
useFingerprinting = Play.configuration.getBoolean("silhouette.authenticator.useFingerprinting").get,
authenticatorIdleTimeout = Play.configuration.getInt("silhouette.authenticator.authenticatorIdleTimeout"),
authenticatorExpiry = Play.configuration.getInt("silhouette.authenticator.authenticatorExpiry").get
), fingerprintGenerator, Clock())
}

override def providers: Map[String, Provider] = {
val googleProvider = GoogleProvider(new PlayHTTPLayer(), provideOAuth2StateProvider(new SecureRandomIDGenerator()), OAuth2Settings(
authorizationURL = Play.configuration.getString("silhouette.google.authorizationURL").get,
accessTokenURL = Play.configuration.getString("silhouette.google.accessTokenURL").get,
redirectURL = Play.configuration.getString("silhouette.google.redirectURL").get,
clientID = Play.configuration.getString("silhouette.google.clientID").get,
clientSecret = Play.configuration.getString("silhouette.google.clientSecret").get,
scope = Play.configuration.getString("silhouette.google.scope")))
Map(googleProvider.id -> googleProvider)
}

def provideOAuth2StateProvider(idGenerator: IDGenerator): OAuth2StateProvider = {
new CookieStateProvider(CookieStateSettings(
cookieName = Play.configuration.getString("silhouette.oauth2StateProvider.cookieName").get,
cookiePath = Play.configuration.getString("silhouette.oauth2StateProvider.cookiePath").get,
cookieDomain = Play.configuration.getString("silhouette.oauth2StateProvider.cookieDomain"),
secureCookie = Play.configuration.getBoolean("silhouette.oauth2StateProvider.secureCookie").get,
httpOnlyCookie = Play.configuration.getBoolean("silhouette.oauth2StateProvider.httpOnlyCookie").get,
expirationTime = Play.configuration.getInt("silhouette.oauth2StateProvider.expirationTime").get
), idGenerator, Clock())
}

override def eventBus: EventBus = EventBus()
}

implicit val env = ProductionRuntimeEnvironment

// secure-social modules
lazy val loginPage = wire[LoginPage]
lazy val providerController = wire[ProviderController]

// application modules
lazy val app = wire[Application]
lazy val clientController = wire[ClientController]
Expand All @@ -60,4 +84,6 @@ class ProductionModule {
lazy val configurationApiController = wire[ConfigurationApiController]
lazy val membersApiController = wire[MembersApiController]
lazy val contributionsApiController = wire[ContributionsApiController]
lazy val authenticationController = wire[AuthenticationController]
lazy val authenticationApiController = wire[AuthenticationApiController]
}
6 changes: 3 additions & 3 deletions app/actors/ActivityActor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package actors

import akka.actor.Actor
import domain.{Activity, ActivitySerializer}
import play.Logger
import play.api.libs.json.Json
import play.modules.reactivemongo.ReactiveMongoPlugin
import play.modules.reactivemongo.json.collection.JSONCollection
import play.api.libs.json.Json
import play.Logger

case class ActivityActor() extends Actor with ActivitySerializer {

import play.api.Play.current

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.ExecutionContext.Implicits.global

def receive = {

Expand Down
25 changes: 0 additions & 25 deletions app/auth/WithDomain.scala

This file was deleted.

9 changes: 5 additions & 4 deletions app/controllers/AccountsController.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package controllers

import play.api.mvc.Controller
import com.mohiva.play.silhouette.api.{Environment, Silhouette}
import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator
import domain.User
import play.modules.reactivemongo.MongoController
import play.modules.reactivemongo.json.collection.JSONCollection
import securesocial.core.{BasicProfile, RuntimeEnvironment}

class AccountsController (override implicit val env: RuntimeEnvironment[BasicProfile]) extends Controller
class AccountsController (override implicit val env: Environment[User, SessionAuthenticator]) extends Silhouette[User, SessionAuthenticator]
with MongoController
with securesocial.core.SecureSocial[BasicProfile]{
{

def collection = db.collection[JSONCollection]("accounts")

Expand Down
14 changes: 7 additions & 7 deletions app/controllers/ActivityController.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package controllers

import auth.WithDomain
import securesocial.core.{BasicProfile, RuntimeEnvironment}
import play.api.mvc._
import com.mohiva.play.silhouette.api.{Environment, Silhouette}
import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator
import domain.User

class ActivityController(override implicit val env: RuntimeEnvironment[BasicProfile]) extends Controller
with securesocial.core.SecureSocial[BasicProfile] {
class ActivityController(override implicit val env: Environment[User, SessionAuthenticator]) extends Silhouette[User, SessionAuthenticator]
{

def index = SecuredAction(WithDomain()) {
def index = SecuredAction {
implicit request =>
Ok(views.html.activity.index(request.user))
Ok(views.html.activity.index(request.identity))
}

}
15 changes: 7 additions & 8 deletions app/controllers/Application.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package controllers

import auth.WithDomain
import play.api.mvc.Controller
import securesocial.core.{BasicProfile, RuntimeEnvironment}
import com.mohiva.play.silhouette.api.{Environment, Silhouette}
import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator
import domain.User

class Application(override implicit val env: RuntimeEnvironment[BasicProfile])
extends Controller
with securesocial.core.SecureSocial[BasicProfile] {
class Application(override implicit val env: Environment[User, SessionAuthenticator])
extends Silhouette[User, SessionAuthenticator] {

def index = SecuredAction(WithDomain()) {
def index = SecuredAction() {
implicit request =>
Ok(views.html.index(request.user)).flashing("welcome" -> "connard")
Ok(views.html.index(request.identity)).flashing("welcome" -> "connard")
}
}
25 changes: 25 additions & 0 deletions app/controllers/AuthenticationController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package controllers

import com.mohiva.play.silhouette.api.{LogoutEvent, Environment, Silhouette}
import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator
import domain.User

import scala.concurrent.Future

class AuthenticationController(override implicit val env: Environment[User, SessionAuthenticator])
extends Silhouette[User, SessionAuthenticator] {

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 signUp() = play.mvc.Results.TODO

def authenticate(provider: String) = play.mvc.Results.TODO

def credentials() = play.mvc.Results.TODO
}
13 changes: 6 additions & 7 deletions app/controllers/ClientController.scala
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package controllers

import auth.WithDomain
import com.mohiva.play.silhouette.api.{Environment, Silhouette}
import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator
import domain._
import play.api.mvc._
import securesocial.core.{BasicProfile, RuntimeEnvironment}

// Reactive Mongo imports

// Reactive Mongo plugin, including the JSON-specialized collection
import play.modules.reactivemongo.MongoController


class ClientController(override implicit val env: RuntimeEnvironment[BasicProfile]) extends Controller
class ClientController(override implicit val env: Environment[User, SessionAuthenticator]) extends Silhouette[User, SessionAuthenticator]
with InvoiceSerializer
with MongoController
with securesocial.core.SecureSocial[BasicProfile] {
{

def clientsView = SecuredAction(WithDomain()) {
def clientsView = SecuredAction {
implicit request =>
Ok(views.html.clients(request.user))
Ok(views.html.clients(request.identity))
}
}
14 changes: 7 additions & 7 deletions app/controllers/ConfigurationController.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package controllers

import auth.WithDomain
import play.api.mvc._
import securesocial.core.{BasicProfile, RuntimeEnvironment}
import com.mohiva.play.silhouette.api.{Environment, Silhouette}
import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator
import domain.User

class ConfigurationController(override implicit val env: RuntimeEnvironment[BasicProfile]) extends Controller
with securesocial.core.SecureSocial[BasicProfile] {
class ConfigurationController(override implicit val env: Environment[User, SessionAuthenticator]) extends Silhouette[User, SessionAuthenticator]
{

def index = SecuredAction(WithDomain()) {
def index = SecuredAction {
implicit request =>
Ok(views.html.configurations.index(request.user))
Ok(views.html.configurations.index(request.identity))
}

}
14 changes: 7 additions & 7 deletions app/controllers/ContributionsController.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package controllers

import auth.WithDomain
import play.api.mvc._
import securesocial.core.{BasicProfile, RuntimeEnvironment}
import com.mohiva.play.silhouette.api.{Environment, Silhouette}
import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator
import domain.User

class ContributionsController(override implicit val env: RuntimeEnvironment[BasicProfile]) extends Controller
with securesocial.core.SecureSocial[BasicProfile] {
class ContributionsController(override implicit val env: Environment[User, SessionAuthenticator]) extends Silhouette[User, SessionAuthenticator]
{

def index = SecuredAction(WithDomain()) {
def index = SecuredAction {
implicit request =>
Ok(views.html.contributions.index(request.user))
Ok(views.html.contributions.index(request.identity))
}
}
16 changes: 8 additions & 8 deletions app/controllers/ExpenseController.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package controllers

import auth.WithDomain
import play.api.mvc._
import securesocial.core.{BasicProfile, RuntimeEnvironment}
import com.mohiva.play.silhouette.api.{Environment, Silhouette}
import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator
import domain.User

class ExpenseController(override implicit val env: RuntimeEnvironment[BasicProfile])
extends Controller
with securesocial.core.SecureSocial[BasicProfile] {
class ExpenseController(override implicit val env: Environment[User, SessionAuthenticator])
extends Silhouette[User, SessionAuthenticator]
{

def index = SecuredAction(WithDomain()) {
def index = SecuredAction {
implicit request =>
Ok(views.html.expense.index(request.user))
Ok(views.html.expense.index(request.identity))
}

}
Loading

0 comments on commit 2bec82e

Please sign in to comment.