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

Add MyRequest #118

wants to merge 6 commits into from

Conversation

wsargent
Copy link
Contributor

@wsargent wsargent commented Dec 30, 2019

This is a sample PR to show extending a custom request type on top of SecuredRequest.

Note that since MyRequest extends MessagesRequest it is automatically a MessagesProvider so that's one fewer implicit to pass around

@wsargent
Copy link
Contributor Author

@akkie I think this is a good practice because people will want to customize the request, also see the XXX bits for where traits would be used.

@wsargent
Copy link
Contributor Author

@akkie
Copy link
Contributor

akkie commented Jan 15, 2020

@wsargent What's the advantage over using the action refiner? For me it looks like a lot of boilerplate. Could you assemble a really small example that can then be used in the documentation?

@wsargent
Copy link
Contributor Author

wsargent commented Jan 15, 2020

I did some refactoring and switched to ActionTransformer. Does this work?

abstract class SomeAbstractController @Inject() (controllerComponents: SilhouetteControllerComponents) {
  type SecuredEnvRequest[A] = SecuredRequest[DefaultEnv, A]
  
  protected abstract class AbstractActionTransformer[-R[_], +P[_]](cc: SilhouetteControllerComponents) extends ActionTransformer[R, P] {
    override protected def executionContext: ExecutionContext =
      cc.executionContext
  }

  class MySecuredActionTransformer(cc: SilhouetteControllerComponents) extends AbstractActionTransformer[SecuredEnvRequest, MySecuredRequest](cc) {
    override protected def transform[A](request: SecuredEnvRequest[A]): Future[MySecuredRequest[A]] = {
      Future.successful( new MySecuredRequest[A](
        messagesApi = cc.messagesApi,
        identity = request.identity,
        authenticator = request.authenticator,
        request = request
      ))
    }
  }

  private val mySecuredActionTransformer = new MySecuredActionTransformer(controllerComponents)

  def SecuredAction: ActionBuilder[MySecuredRequest, AnyContent] = {
    controllerComponents.silhouette.SecuredAction.andThen(mySecuredActionTransformer)
  }
}

@will-sargent-eero
Copy link

Probably should split up into IdentityProvider and AuthenticatorProvider traits really

@wsargent
Copy link
Contributor Author

Okay, broken out some more.

@akkie
Copy link
Contributor

akkie commented Jan 16, 2020

@wsargent Thanks 👍 It looks much cleaner and more understandable now.

I've some notes to your implementation. Some are global some I will comment inline.

  • You have hardcoded the type of the environment. There are a lot of users which are use different environments. Maybe the environment could be a type parameter of the SilhouetteComponents trait. This makes it easier to switch the environment without rewriting a lot of code.

  • Could you please move the auth related traits to the utils.auth package?

  • I don't like the name MyRequest. MAybe we find an mane that is more descriptive? Maybe AppRequest?

@wsargent
Copy link
Contributor Author

@akkie updated with comment fixes

@will-sargent-eero
Copy link

will-sargent-eero commented Jan 17, 2020 via email

@wsargent
Copy link
Contributor Author

@akkie how does it look?

@akkie
Copy link
Contributor

akkie commented Jan 27, 2020

I thought we should wait for the next Silhouette release, so that you can depend on mohiva/play-silhouette#574. But the next Silhouette release depends on the new Play 2.8.1 release because of mohiva/play-silhouette#570

@will-sargent-eero
Copy link

will-sargent-eero commented Jan 27, 2020 via email

@wsargent
Copy link
Contributor Author

wsargent commented Feb 3, 2020

@akkie
Copy link
Contributor

akkie commented Feb 27, 2020

@wsargent I've released the final 7.0.0

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants