Skip to content
This repository has been archived by the owner on Sep 12, 2021. It is now read-only.

Add MyRequest #118

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/controllers/AbstractAuthController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.mohiva.play.silhouette.api._
import com.mohiva.play.silhouette.api.services.AuthenticatorResult
import models.User
import play.api.mvc._
import utils.auth.DefaultEnv
import utils.route.Calls

import scala.concurrent.{ ExecutionContext, Future }
Expand All @@ -16,7 +17,7 @@ import scala.concurrent.{ ExecutionContext, Future }
* @param ex The execution context.
*/
abstract class AbstractAuthController(
scc: SilhouetteControllerComponents
scc: SilhouetteControllerComponents[DefaultEnv]
)(implicit ex: ExecutionContext) extends SilhouetteController(scc) {

/**
Expand Down
9 changes: 5 additions & 4 deletions app/controllers/ActivateAccountController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import com.mohiva.play.silhouette.impl.providers.CredentialsProvider
import javax.inject.Inject
import play.api.i18n.Messages
import play.api.libs.mailer.Email
import play.api.mvc.{ AnyContent, Request }
import play.api.mvc.AnyContent
import utils.auth.DefaultEnv
import utils.route.Calls

import scala.concurrent.{ ExecutionContext, Future }
Expand All @@ -17,7 +18,7 @@ import scala.concurrent.{ ExecutionContext, Future }
* The `Activate Account` controller.
*/
class ActivateAccountController @Inject() (
scc: SilhouetteControllerComponents
scc: SilhouetteControllerComponents[DefaultEnv]
)(implicit ex: ExecutionContext) extends SilhouetteController(scc) {

/**
Expand All @@ -26,7 +27,7 @@ class ActivateAccountController @Inject() (
* @param email The email address of the user to send the activation mail to.
* @return The result to display.
*/
def send(email: String) = UnsecuredAction.async { implicit request: Request[AnyContent] =>
def send(email: String) = UnsecuredAction.async { implicit request: AppRequest[AnyContent] =>
val decodedEmail = URLDecoder.decode(email, "UTF-8")
val loginInfo = LoginInfo(CredentialsProvider.ID, decodedEmail)
val result = Redirect(Calls.signin).flashing("info" -> Messages("activation.email.sent", decodedEmail))
Expand Down Expand Up @@ -55,7 +56,7 @@ class ActivateAccountController @Inject() (
* @param token The token to identify a user.
* @return The result to display.
*/
def activate(token: UUID) = UnsecuredAction.async { implicit request: Request[AnyContent] =>
def activate(token: UUID) = UnsecuredAction.async { implicit request: AppRequest[AnyContent] =>
authTokenService.validate(token).flatMap {
case Some(authToken) => userService.retrieve(authToken.userID).flatMap {
case Some(user) if user.loginInfo.providerID == CredentialsProvider.ID =>
Expand Down
61 changes: 61 additions & 0 deletions app/controllers/AppRequest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package controllers

import com.mohiva.play.silhouette.api.Env
import org.slf4j.Marker
import play.api.MarkerContext
import play.api.i18n.MessagesApi
import play.api.mvc._

import scala.language.higherKinds

/**
* Defines our own request with extended features above and beyond what Silhouette provides.
*/
trait AppRequestHeader extends MessagesRequestHeader
with PreferredMessagesProvider
with MarkerContext

/**
* The request implementation with a parsed body (only relevant for POST requests)
*
* @param request the original request
* @param messagesApi the messages API, needed for producing a Messages instance
* @tparam B the type of the body, if any.
*/
class AppRequest[B](
request: Request[B],
messagesApi: MessagesApi
) extends MessagesRequest[B](request, messagesApi) with AppRequestHeader {
// Stubbed out here, but see marker context docs
// https://www.playframework.com/documentation/2.8.x/ScalaLogging#Using-Markers-and-Marker-Contexts
def marker: Option[Marker] = None
}

/**
* A request with identity and authenticator traits.
*/
trait AppSecuredRequestHeader[E <: Env] extends AppRequestHeader
with SecuredRequestHeader[E]

/**
* Implementation of secured request.
*/
class AppSecuredRequest[E <: Env, B](
request: Request[B],
messagesApi: MessagesApi,
val identity: E#I,
val authenticator: E#A,
) extends AppRequest(request, messagesApi) with AppSecuredRequestHeader[E]

/**
* A request with optional identity and authenticator traits.
*/
trait AppUserAwareRequestHeader[E <: Env] extends AppRequestHeader
with UserAwareRequestHeader[E]

class AppUserAwareRequest[E <: Env, B](
request: Request[B],
messagesApi: MessagesApi,
val identity: Option[E#I],
val authenticator: Option[E#A]
) extends AppRequest(request, messagesApi) with AppUserAwareRequestHeader[E]
8 changes: 4 additions & 4 deletions app/controllers/ApplicationController.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package controllers

import com.mohiva.play.silhouette.api.LogoutEvent
import com.mohiva.play.silhouette.api.actions._
import com.mohiva.play.silhouette.impl.providers.GoogleTotpInfo
import javax.inject.Inject
import play.api.mvc._
import utils.auth.DefaultEnv
import utils.route.Calls

import scala.concurrent.ExecutionContext
Expand All @@ -13,7 +13,7 @@ import scala.concurrent.ExecutionContext
* The basic application controller.
*/
class ApplicationController @Inject() (
scc: SilhouetteControllerComponents,
scc: SilhouetteControllerComponents[DefaultEnv],
home: views.html.home
)(implicit ex: ExecutionContext) extends SilhouetteController(scc) {

Expand All @@ -22,7 +22,7 @@ class ApplicationController @Inject() (
*
* @return The result to display.
*/
def index = SecuredAction.async { implicit request: SecuredRequest[EnvType, AnyContent] =>
def index = SecuredAction.async { implicit request: AppSecuredEnvRequest[AnyContent] =>
authInfoRepository.find[GoogleTotpInfo](request.identity.loginInfo).map { totpInfoOpt =>
Ok(home(request.identity, totpInfoOpt))
}
Expand All @@ -33,7 +33,7 @@ class ApplicationController @Inject() (
*
* @return The result to display.
*/
def signOut = SecuredAction.async { implicit request: SecuredRequest[EnvType, AnyContent] =>
def signOut = SecuredAction.async { implicit request: AppSecuredEnvRequest[AnyContent] =>
val result = Redirect(Calls.home)
eventBus.publish(LogoutEvent(request.identity, request))
authenticatorService.discard(request.authenticator, result)
Expand Down
12 changes: 5 additions & 7 deletions app/controllers/ChangePasswordController.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package controllers

import com.mohiva.play.silhouette.api.actions.SecuredRequest
import com.mohiva.play.silhouette.api.exceptions.ProviderException
import com.mohiva.play.silhouette.api.util.{ Credentials, PasswordInfo }
import com.mohiva.play.silhouette.impl.providers.CredentialsProvider
import forms.ChangePasswordForm
import javax.inject.Inject
import play.api.i18n.Messages
import play.api.mvc._
import utils.auth.{ DefaultEnv, WithProvider }

import scala.concurrent.{ ExecutionContext, Future }
Expand All @@ -16,7 +14,7 @@ import scala.concurrent.{ ExecutionContext, Future }
* The `Change Password` controller.
*/
class ChangePasswordController @Inject() (
scc: SilhouetteControllerComponents,
scc: SilhouetteControllerComponents[DefaultEnv],
changePassword: views.html.changePassword
)(implicit ex: ExecutionContext) extends SilhouetteController(scc) {

Expand All @@ -25,8 +23,8 @@ class ChangePasswordController @Inject() (
*
* @return The result to display.
*/
def view = SecuredAction(WithProvider[AuthType](CredentialsProvider.ID)) {
implicit request: SecuredRequest[DefaultEnv, AnyContent] =>
def view = SecuredAction(WithProvider(CredentialsProvider.ID)) {
implicit request =>
Ok(changePassword(ChangePasswordForm.form, request.identity))
}

Expand All @@ -35,8 +33,8 @@ class ChangePasswordController @Inject() (
*
* @return The result to display.
*/
def submit = SecuredAction(WithProvider[AuthType](CredentialsProvider.ID)).async {
implicit request: SecuredRequest[DefaultEnv, AnyContent] =>
def submit = SecuredAction(WithProvider(CredentialsProvider.ID)).async {
implicit request =>
ChangePasswordForm.form.bindFromRequest.fold(
form => Future.successful(BadRequest(changePassword(form, request.identity))),
password => {
Expand Down
9 changes: 5 additions & 4 deletions app/controllers/ForgotPasswordController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import forms.ForgotPasswordForm
import javax.inject.Inject
import play.api.i18n.Messages
import play.api.libs.mailer.Email
import play.api.mvc.{ AnyContent, Request }
import play.api.mvc.AnyContent
import utils.auth.DefaultEnv
import utils.route.Calls

import scala.concurrent.{ ExecutionContext, Future }
Expand All @@ -15,7 +16,7 @@ import scala.concurrent.{ ExecutionContext, Future }
* The `Forgot Password` controller.
*/
class ForgotPasswordController @Inject() (
components: SilhouetteControllerComponents,
components: SilhouetteControllerComponents[DefaultEnv],
forgotPassword: views.html.forgotPassword
)(implicit ex: ExecutionContext) extends SilhouetteController(components) {

Expand All @@ -24,7 +25,7 @@ class ForgotPasswordController @Inject() (
*
* @return The result to display.
*/
def view = UnsecuredAction.async { implicit request: Request[AnyContent] =>
def view = UnsecuredAction.async { implicit request: AppRequest[AnyContent] =>
Future.successful(Ok(forgotPassword(ForgotPasswordForm.form)))
}

Expand All @@ -36,7 +37,7 @@ class ForgotPasswordController @Inject() (
*
* @return The result to display.
*/
def submit = UnsecuredAction.async { implicit request: Request[AnyContent] =>
def submit = UnsecuredAction.async { implicit request: AppRequest[AnyContent] =>
ForgotPasswordForm.form.bindFromRequest.fold(
form => Future.successful(BadRequest(forgotPassword(form))),
email => {
Expand Down
9 changes: 5 additions & 4 deletions app/controllers/ResetPasswordController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import com.mohiva.play.silhouette.impl.providers.CredentialsProvider
import forms.ResetPasswordForm
import javax.inject.Inject
import play.api.i18n.Messages
import play.api.mvc.{ AnyContent, Request }
import play.api.mvc.AnyContent
import utils.auth.DefaultEnv
import utils.route.Calls

import scala.concurrent.{ ExecutionContext, Future }
Expand All @@ -16,7 +17,7 @@ import scala.concurrent.{ ExecutionContext, Future }
* The `Reset Password` controller.
*/
class ResetPasswordController @Inject() (
scc: SilhouetteControllerComponents,
scc: SilhouetteControllerComponents[DefaultEnv],
resetPassword: views.html.resetPassword
)(implicit ex: ExecutionContext) extends SilhouetteController(scc) {

Expand All @@ -26,7 +27,7 @@ class ResetPasswordController @Inject() (
* @param token The token to identify a user.
* @return The result to display.
*/
def view(token: UUID) = UnsecuredAction.async { implicit request: Request[AnyContent] =>
def view(token: UUID) = UnsecuredAction.async { implicit request: AppRequest[AnyContent] =>
authTokenService.validate(token).map {
case Some(_) => Ok(resetPassword(ResetPasswordForm.form, token))
case None => Redirect(Calls.signin).flashing("error" -> Messages("invalid.reset.link"))
Expand All @@ -39,7 +40,7 @@ class ResetPasswordController @Inject() (
* @param token The token to identify a user.
* @return The result to display.
*/
def submit(token: UUID) = UnsecuredAction.async { implicit request: Request[AnyContent] =>
def submit(token: UUID) = UnsecuredAction.async { implicit request: AppRequest[AnyContent] =>
authTokenService.validate(token).flatMap {
case Some(authToken) =>
ResetPasswordForm.form.bindFromRequest.fold(
Expand Down
9 changes: 5 additions & 4 deletions app/controllers/SignInController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import com.mohiva.play.silhouette.impl.providers._
import forms.{ SignInForm, TotpForm }
import javax.inject.Inject
import play.api.i18n.Messages
import play.api.mvc.{ AnyContent, Request }
import play.api.mvc.AnyContent
import utils.auth.DefaultEnv
import utils.route.Calls

import scala.concurrent.{ ExecutionContext, Future }
Expand All @@ -16,7 +17,7 @@ import scala.concurrent.{ ExecutionContext, Future }
* The `Sign In` controller.
*/
class SignInController @Inject() (
scc: SilhouetteControllerComponents,
scc: SilhouetteControllerComponents[DefaultEnv],
signIn: views.html.signIn,
activateAccount: views.html.activateAccount,
totp: views.html.totp
Expand All @@ -27,7 +28,7 @@ class SignInController @Inject() (
*
* @return The result to display.
*/
def view = UnsecuredAction.async { implicit request: Request[AnyContent] =>
def view = UnsecuredAction.async { implicit request: AppRequest[AnyContent] =>
Future.successful(Ok(signIn(SignInForm.form, socialProviderRegistry)))
}

Expand All @@ -36,7 +37,7 @@ class SignInController @Inject() (
*
* @return The result to display.
*/
def submit = UnsecuredAction.async { implicit request: Request[AnyContent] =>
def submit = UnsecuredAction.async { implicit request: AppRequest[AnyContent] =>
SignInForm.form.bindFromRequest.fold(
form => Future.successful(BadRequest(signIn(form, socialProviderRegistry))),
data => {
Expand Down
9 changes: 5 additions & 4 deletions app/controllers/SignUpController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import javax.inject.Inject
import models.User
import play.api.i18n.Messages
import play.api.libs.mailer.Email
import play.api.mvc.{ AnyContent, Request }
import play.api.mvc.AnyContent
import utils.auth.DefaultEnv
import utils.route.Calls

import scala.concurrent.{ ExecutionContext, Future }
Expand All @@ -18,7 +19,7 @@ import scala.concurrent.{ ExecutionContext, Future }
* The `Sign Up` controller.
*/
class SignUpController @Inject() (
components: SilhouetteControllerComponents,
components: SilhouetteControllerComponents[DefaultEnv],
signUp: views.html.signUp
)(implicit ex: ExecutionContext) extends SilhouetteController(components) {

Expand All @@ -27,7 +28,7 @@ class SignUpController @Inject() (
*
* @return The result to display.
*/
def view = UnsecuredAction.async { implicit request: Request[AnyContent] =>
def view = UnsecuredAction.async { implicit request: AppRequest[AnyContent] =>
Future.successful(Ok(signUp(SignUpForm.form)))
}

Expand All @@ -36,7 +37,7 @@ class SignUpController @Inject() (
*
* @return The result to display.
*/
def submit = UnsecuredAction.async { implicit request: Request[AnyContent] =>
def submit = UnsecuredAction.async { implicit request: AppRequest[AnyContent] =>
SignUpForm.form.bindFromRequest.fold(
form => Future.successful(BadRequest(signUp(form))),
data => {
Expand Down
Loading