Replies: 2 comments 9 replies
-
Let me rephrase this question slightly: We would like to implement server secret rotation (with an overlap period) in our Java server (currently Play 2.8.x), using Guice. What interfaces, if any, are available to which we can |
Beta Was this translation helpful? Give feedback.
-
Theoretically, you can bind a custom This might be a different use-case, but maybe it helps you for yours. // CustomSecretProvider.scala
import org.example.common.utils.Utils
import org.example.settings.SettingsService
import play.api.http.SecretConfiguration
import javax.inject.{Inject, Provider, Singleton}
@Singleton
class CustomSecretProvider @Inject()(settingsService: SettingsService) extends Provider[SecretConfiguration] {
lazy val get: SecretConfiguration = {
val secret = settingsService.getEncrypted[String](MainStatics.SETTING_PLAY_SECRET) match {
case s if s.isEmpty =>
println(s"generating a new secret secret...")
// note: play has an internal check whether a secret is secure enough where it just
// checks the length of the secret. This check may not apply because we use a custom secret provider
// but just in case, we'll use a secret that has a length of 64 bytes to satisfy the
// length requirement play would set for 512bit signatures
val generated = Utils.generateRandomBase64Url(64);
settingsService.setEncrypted(MainStatics.SETTING_PLAY_SECRET, generated)
generated
case s => s.get()
}
SecretConfiguration(secret = secret, provider = None)
}
} Then bind that inside a custom application loader. // CustomApplicationLoader.java
import play.api.http.SecretConfiguration;
import play.inject.guice.GuiceApplicationBuilder;
import play.inject.guice.GuiceApplicationLoader;
public class CustomApplicationLoader extends GuiceApplicationLoader {
@Override
public GuiceApplicationBuilder builder(Context context) {
return super.builder(context)
.overrides(binder -> binder.bind(SecretConfiguration.class).toProvider(CustomSecretProvider.class));
}
} Use the custom application loader play.application.loader = org.example.CustomApplicationLoader In this implementation, the play secret itself is not rotated, but it should be fairly easy to implement it here. I'm not rotating the play session cookie here because I don't really use the play session for anything important other than CSRF. The reason I don't is that once you have more complex requirements, the default Play session handling gets quite limiting. If you want a seamless transition between secret rotations (the cookie not being invalidated, but being superseded by a new one, without logging the user out), you probably have to implement your own cookie handling for that. I am working on an Identity Provider written in Play that includes asymmetric JWT signatures, seamless key rotations, and rolling session IDs to somewhat protect against cookie stealing. I have been thinking about writing my solutions down in a blog post, but haven't come around to do so yet. |
Beta Was this translation helpful? Give feedback.
-
I work on CiviForm, an open-source project that simplifies the application process for government benefits programs by re-using applicant data for multiple benefits applications.
My goal is to enable server secret rotation with an overlap period. I found
play-secret-rotation
, which seemed very promising. However, it is written in Scala and I am having trouble wiring it into my Java server.I am now considering writing my own (Java) code, inspired by the approach of
play-secret-rotation
. It seems that a fundamental step is providing a customRequestFactory
, as shown here. Unfortunately, I do not see aRequestFactory
class in the Javadoc for 2.8.x.Is there some alternative approach I can use in a Java server?
Beta Was this translation helpful? Give feedback.
All reactions