Skip to content

Commit

Permalink
all: Play 2.9.0 / add Scala 3 / drop Scala 2.12 (#588)
Browse files Browse the repository at this point in the history
- add support for Play 2.9.0
 - add support for Scala 3
 - drop support for Scala 2.12 as Play 2.9.0 does not support Scala 2.12
 - adjust web context and security wrt. changes in play 2.9.0
 - upgrade various dependencies
 - downgrade guice / scalatest as Play uses those.

 See: playframework/playframework@b5ef04a
  • Loading branch information
ahjohannessen committed Oct 31, 2023
1 parent 18358a5 commit fa494f5
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 48 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ perfStats.log
.fbExcludeFilterFile
.pmd
.pmdruleset.xml
.DS_Store
.DS_Store
.bloop
.vscode
.metals
2 changes: 1 addition & 1 deletion play-pac4j_2.13/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.pac4j</groupId>
<artifactId>play-pac4j-parent</artifactId>
<version>12.0.0-PLAY2.8-RC2-SNAPSHOT</version>
<version>12.0.0-PLAY2.9</version>
</parent>

<artifactId>play-pac4j_2.13</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.pac4j.play.scala

/**
* @since 12.0.0
*/
object ScalaCompat {

val Converters = _root_.scala.jdk.CollectionConverters

}
18 changes: 9 additions & 9 deletions play-pac4j_2.12/pom.xml → play-pac4j_3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@
<parent>
<groupId>org.pac4j</groupId>
<artifactId>play-pac4j-parent</artifactId>
<version>12.0.0-PLAY2.8-RC2-SNAPSHOT</version>
<version>12.0.0-PLAY2.9</version>
</parent>

<artifactId>play-pac4j_2.12</artifactId>
<artifactId>play-pac4j_3</artifactId>
<packaging>jar</packaging>
<name>pac4j implementation for Play framework (Scala 2.12)</name>
<description>Security library for Play framework based on pac4j using Scala 2.12</description>

<properties>
<scala.version>2.12</scala.version>
<scala.maven.version>2.12.11</scala.maven.version>
</properties>
<name>pac4j implementation for Play framework (Scala 3)</name>
<description>Security library for Play framework based on pac4j using Scala 3</description>

<dependencies>
<dependency>
Expand All @@ -31,6 +26,11 @@
<version>${play.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala3-library_3</artifactId>
<version>3.3.1</version>
</dependency>

<!-- tests -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
<Match>
<Package name="org.pac4j.play.scala" />
</Match>
<Match>
<Package name="org.pac4j.play.scala.deadbolt2" />
</Match>
<Match>
<Class name="~.*MockInMemoryAsyncCacheApi.*" />
</Match>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package org.pac4j.play;

import play.api.mvc.AnyContentAsFormUrlEncoded;
import scala.collection.JavaConverters;
import scala.collection.Seq;
import scala.collection.immutable.Seq;
import scala.jdk.CollectionConverters;

import java.util.HashMap;
import java.util.Map;

/**
* @author Karel Cemus
* @author Jerome LELEU
* @since 12.0.0
*/
class ScalaCompatibility {

static Map<String, String[]> parseBody(final AnyContentAsFormUrlEncoded body) {
final Map<String, String[]> p = new HashMap<>();
final scala.collection.immutable.Map<String, Seq<String>> scalaParameters = body.asFormUrlEncoded().get();
for (final String key : JavaConverters.setAsJavaSet(scalaParameters.keySet())) {
for (final String key : CollectionConverters.SetHasAsJava(scalaParameters.keySet()).asJava()) {
final Seq<String> v = scalaParameters.get(key).get();
final String[] values = new String[v.size()];
v.copyToArray(values);
Expand Down
10 changes: 10 additions & 0 deletions play-pac4j_3/src/main/scala/org/pac4j/play/scala/ScalaCompat.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.pac4j.play.scala

/**
* @since 12.0.0
*/
object ScalaCompat {

val Converters = _root_.scala.jdk.CollectionConverters

}
21 changes: 14 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<artifactId>play-pac4j-parent</artifactId>
<packaging>pom</packaging>
<name>pac4j parent for Play</name>
<version>12.0.0-PLAY2.8-RC2-SNAPSHOT</version>
<version>12.0.0-PLAY2.9</version>
<url>https://github.com/pac4j/play-pac4j</url>

<licenses>
Expand Down Expand Up @@ -62,23 +62,24 @@
</repositories>

<properties>
<pac4j.version>6.0.0-RC9</pac4j.version>
<play.version>2.8.20</play.version>
<pac4j.version>6.0.0-RC8</pac4j.version>
<play.version>2.9.0</play.version>
<java.version>17</java.version>
<guice.version>7.0.0</guice.version>
<guice.version>6.0.0</guice.version>
<shiro.version>1.12.0</shiro.version>
<ehcache.version>2.10.9.2</ehcache.version>
<scalatest.version>3.3.0-SNAP4</scalatest.version>
<scalatest.version>3.2.17</scalatest.version>
<scala.version>2.13</scala.version>
<scala.maven.version>2.13.10</scala.maven.version>
<scala.maven.version>2.13.12</scala.maven.version>
<junit.version>4.13.2</junit.version>
<selenium.version>4.12.1</selenium.version>
<mockito.version>5.6.0</mockito.version>
<pmdVersion>6.55.0</pmdVersion>
</properties>

<modules>
<module>play-pac4j_2.12</module>
<module>play-pac4j_2.13</module>
<module>play-pac4j_3</module>
</modules>

<dependencies>
Expand Down Expand Up @@ -131,6 +132,12 @@
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
7 changes: 6 additions & 1 deletion shared/src/main/java/org/pac4j/play/PlayWebContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public Map<String, String[]> getRequestParameters() {

protected Object getBody() {
if (scalaRequest != null && scalaRequest.hasBody() && scalaRequest instanceof Request) {
return ((Request) scalaRequest).body();
return ((Request<?>) scalaRequest).body();
} else if (javaRequest.hasBody() && javaRequest instanceof Http.Request) {
return ((Http.Request) javaRequest).body();
}
Expand Down Expand Up @@ -255,6 +255,11 @@ public Http.RequestHeader supplementRequest(final Http.RequestHeader request) {
return request.withAttrs(this.javaRequest.attrs()).addAttr(RequestAttrKey.Session().asJava(), new AssignedCell<>(session.asScala()));
}

public <A> Request<A> supplementRequest(Request<A> request) {
logger.trace("supplement request with: {} and session: {}", this.javaRequest.attrs(), session);
return request.withAttrs(this.javaRequest.attrs().asScala()).addAttr(RequestAttrKey.Session(), new AssignedCell<>(session.asScala()));
}

public Result supplementResponse(final Result result) {
Result r = result;
if (responseCookies.size() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.pac4j.core.profile.{ProfileManager, UserProfile}
import org.pac4j.play.context.PlayFrameworkParameters
import play.api.mvc.RequestHeader

import scala.collection.JavaConverters._
import ScalaCompat.Converters._

/**
* This is a helper which can be used to access the current user profile from a twirl template.
Expand Down
42 changes: 21 additions & 21 deletions shared/src/main/scala/org/pac4j/play/scala/Security.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package org.pac4j.play.scala

import javax.inject.{Inject, Singleton}
import scala.concurrent.ExecutionContext
import scala.language.higherKinds
import play.api.mvc._
import org.pac4j.core.config.Config
import org.pac4j.core.profile.UserProfile
import org.pac4j.play.PlayWebContext
import org.pac4j.play.context.PlayFrameworkParameters
import org.pac4j.play.result.PlayWebContextResultHolder
import play.api.http.FileMimeTypes
import play.api.i18n.{Langs, MessagesApi}

/**
* <p>To protect a resource, the {@link #Secure} methods must be used.</p>
Expand All @@ -35,14 +35,15 @@ trait Security[P<:UserProfile] extends BaseController {
}


case class SecureAction[P<:UserProfile, ContentType, R[X]>:AuthenticatedRequest[P, X]<:Request[X]](clients: String, authorizers: String, matchers: String, parser: BodyParser[ContentType], config: Config)(implicit implicitExecutionContext: ExecutionContext) extends ActionBuilder[R, ContentType] {
import scala.collection.JavaConverters._
case class SecureAction[P <: UserProfile, ContentType, R[X]>:AuthenticatedRequest[P, X]<:Request[X]](
clients: String, authorizers: String, matchers: String, parser: BodyParser[ContentType], config: Config
)(implicit implicitExecutionContext: ExecutionContext) extends ActionBuilder[R, ContentType] {
import ScalaCompat.Converters._
import scala.compat.java8.FutureConverters._
import scala.concurrent.Future
import org.pac4j.core.profile.ProfileManager
import org.pac4j.play.scala.SecureAction._

protected def executionContext = implicitExecutionContext
protected def executionContext: ExecutionContext = implicitExecutionContext


/**
Expand All @@ -66,25 +67,24 @@ case class SecureAction[P<:UserProfile, ContentType, R[X]>:AuthenticatedRequest[
def apply[A](action: Action[A]): Action[A] =
copy[P,A,R](parser = action.parser).async(action.parser)(r => action.apply(r))

def invokeBlock[A](request: Request[A], block: R[A] => Future[Result]) = {
def invokeBlock[A](request: Request[A], block: R[A] => Future[Result]): Future[Result] = {
val secureAction = new org.pac4j.play.java.SecureAction(config)
val parameters = new PlayFrameworkParameters(request)
secureAction.call(parameters, clients, authorizers, matchers).toScala.flatMap[play.api.mvc.Result](r =>
if (r.isInstanceOf[PlayWebContextResultHolder]) {
val webContext = r.asInstanceOf[PlayWebContextResultHolder].getPlayWebContext
secureAction.call(parameters, clients, authorizers, matchers).toScala.flatMap[play.api.mvc.Result] {
case holder: PlayWebContextResultHolder =>
val webContext = holder.getPlayWebContext
val sessionStore = config.getSessionStoreFactory.newSessionStore(parameters)
val profileManager = config.getProfileManagerFactory.apply(webContext, sessionStore)
val profiles = profileManager.getProfiles()
val profiles = profileManager.getProfiles
logger.debug("profiles: {}", profiles)
val sProfiles = profiles.asScala.toList.asInstanceOf[List[P]]
val sRequest = webContext.supplementRequest(request.asJava).asScala.asInstanceOf[Request[A]]
val sRequest = webContext.supplementRequest(request)
block(AuthenticatedRequest(sProfiles, sRequest))
} else {
case r =>
Future successful {
r.asScala
}
}
)
}
}
}

Expand All @@ -100,12 +100,12 @@ trait SecurityComponents extends ControllerComponents {
def config: Config
def parser: BodyParsers.Default

@inline def actionBuilder = components.actionBuilder
@inline def parsers = components.parsers
@inline def messagesApi = components.messagesApi
@inline def langs = components.langs
@inline def fileMimeTypes = components.fileMimeTypes
@inline def executionContext = components.executionContext
@inline def actionBuilder: ActionBuilder[Request, AnyContent] = components.actionBuilder
@inline def parsers: PlayBodyParsers = components.parsers
@inline def messagesApi: MessagesApi = components.messagesApi
@inline def langs: Langs = components.langs
@inline def fileMimeTypes: FileMimeTypes = components.fileMimeTypes
@inline def executionContext: ExecutionContext = components.executionContext
}

@Singleton
Expand Down
4 changes: 4 additions & 0 deletions spotbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<Class name="~.*PlayCacheSessionStore"/>
<Bug pattern="EI_EXPOSE_REP" />
</Match>
<Match>
<Class name="~.*MockInMemoryAsyncCacheApi.*" />
<Bug pattern="EI_EXPOSE_REP" />
</Match>
<Match>
<Class name="~.*PlayCacheStore"/>
<Bug pattern="EI_EXPOSE_REP2" />
Expand Down

0 comments on commit fa494f5

Please sign in to comment.