Skip to content

Commit

Permalink
upgrade to typed session materials
Browse files Browse the repository at this point in the history
  • Loading branch information
fupelaqu committed Dec 3, 2023
1 parent d9b0a8e commit 7d8c005
Show file tree
Hide file tree
Showing 41 changed files with 451 additions and 270 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,23 @@ import app.softnetwork.persistence.jdbc.query.{JdbcJournalProvider, JdbcOffsetPr
import app.softnetwork.persistence.schema.SchemaProvider
import app.softnetwork.session.CsrfCheck
import app.softnetwork.session.config.Settings
import app.softnetwork.session.model.SessionManagers
import app.softnetwork.session.service.{SessionEndpoints, SessionMaterials}
import com.softwaremill.session.{SessionConfig, SessionManager}
import app.softnetwork.session.model.{
SessionData,
SessionDataCompanion,
SessionDataDecorator,
SessionManagers
}
import app.softnetwork.session.service.SessionMaterials
import com.softwaremill.session.{RefreshTokenStorage, SessionConfig, SessionManager}
import com.typesafe.config.Config
import org.slf4j.{Logger, LoggerFactory}
import org.softnetwork.session.model.Session
import sttp.tapir.swagger.SwaggerUIOptions

import scala.concurrent.ExecutionContext

trait BasicAccountApi extends AccountApplication[BasicAccount, BasicAccountProfile] {
trait BasicAccountApi[SD <: SessionData with SessionDataDecorator[SD]]
extends AccountApplication[BasicAccount, BasicAccountProfile] {
self: SchemaProvider with ApiRoutes with CsrfCheck =>

override def accountDao: AccountDao = BasicAccountDao
Expand All @@ -43,43 +49,55 @@ trait BasicAccountApi extends AccountApplication[BasicAccount, BasicAccountProfi
override implicit def system: ActorSystem[_] = sys
}

def sessionConfig: SessionConfig = Settings.Session.DefaultSessionConfig
implicit def sessionConfig: SessionConfig = Settings.Session.DefaultSessionConfig

implicit def companion: SessionDataCompanion[SD]

override protected def sessionType: Session.SessionType =
Settings.Session.SessionContinuityAndTransport

override protected def manager(implicit sessionConfig: SessionConfig): SessionManager[Session] =
SessionManagers.basic
protected def manager: SessionManager[SD] = SessionManagers.basic

protected def refreshTokenStorage: ActorSystem[_] => RefreshTokenStorage[SD]

def accountSwagger: ActorSystem[_] => SwaggerEndpoint = sys =>
new BasicAccountServiceEndpoints with SwaggerEndpoint with SessionMaterials {
new BasicAccountServiceEndpoints[SD] with SwaggerEndpoint with SessionMaterials[SD] {
lazy val log: Logger = LoggerFactory getLogger getClass.getName
override implicit def system: ActorSystem[_] = sys
override implicit lazy val ec: ExecutionContext = sys.executionContext
override implicit def manager(implicit
sessionConfig: SessionConfig
): SessionManager[Session] = self.manager
override protected def sessionType: Session.SessionType = self.sessionType
override implicit def sessionConfig: SessionConfig = self.sessionConfig
override implicit def manager(implicit
sessionConfig: SessionConfig,
companion: SessionDataCompanion[SD]
): SessionManager[SD] = self.manager
override protected val manifestWrapper: ManifestW = ManifestW()
override implicit def refreshTokenStorage: RefreshTokenStorage[SD] =
self.refreshTokenStorage(sys)
override implicit def companion: SessionDataCompanion[SD] = self.companion
override val applicationVersion: String = self.systemVersion()
override val swaggerUIOptions: SwaggerUIOptions =
SwaggerUIOptions.default.pathPrefix(List("swagger", AccountSettings.Path))
}

def oauthSwagger: ActorSystem[_] => SwaggerEndpoint =
sys =>
new OAuthServiceEndpoints
new OAuthServiceEndpoints[SD]
with BasicAccountTypeKey
with SwaggerEndpoint
with SessionMaterials {
with SessionMaterials[SD] {
override implicit def system: ActorSystem[_] = sys
override implicit lazy val ec: ExecutionContext = sys.executionContext
override implicit def manager(implicit
sessionConfig: SessionConfig
): SessionManager[Session] = self.manager
override protected def sessionType: Session.SessionType = self.sessionType
override implicit def sessionConfig: SessionConfig = self.sessionConfig
override implicit def manager(implicit
sessionConfig: SessionConfig,
companion: SessionDataCompanion[SD]
): SessionManager[SD] = self.manager
override def log: Logger = LoggerFactory getLogger getClass.getName
override implicit def refreshTokenStorage: RefreshTokenStorage[SD] =
self.refreshTokenStorage(sys)
override implicit def companion: SessionDataCompanion[SD] = self.companion
override val applicationVersion: String = self.systemVersion()
override val swaggerUIOptions: SwaggerUIOptions =
SwaggerUIOptions.default.pathPrefix(List("swagger", AccountSettings.OAuthPath))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,50 @@ import app.softnetwork.account.service.{
import app.softnetwork.api.server.Endpoint
import app.softnetwork.persistence.schema.SchemaProvider
import app.softnetwork.session.CsrfCheck
import app.softnetwork.session.service.{SessionEndpoints, SessionMaterials}
import com.softwaremill.session.{SessionConfig, SessionManager}
import app.softnetwork.session.model.{SessionData, SessionDataCompanion, SessionDataDecorator}
import app.softnetwork.session.service.SessionMaterials
import com.softwaremill.session.{RefreshTokenStorage, SessionConfig, SessionManager}
import org.slf4j.{Logger, LoggerFactory}
import org.softnetwork.session.model.Session

import scala.concurrent.ExecutionContext

trait BasicAccountEndpoints
extends AccountEndpoints[BasicAccount, BasicAccountProfile, BasicAccountSignUp] {
self: BasicAccountApi with SchemaProvider with CsrfCheck =>
override def accountEndpoints: ActorSystem[_] => AccountServiceEndpoints[BasicAccountSignUp] =
trait BasicAccountEndpoints[SD <: SessionData with SessionDataDecorator[SD]]
extends AccountEndpoints[BasicAccount, BasicAccountProfile, BasicAccountSignUp, SD] {
self: BasicAccountApi[SD] with SchemaProvider with CsrfCheck =>
override def accountEndpoints: ActorSystem[_] => AccountServiceEndpoints[BasicAccountSignUp, SD] =
sys =>
new BasicAccountServiceEndpoints with SessionMaterials {
override implicit def manager(implicit
sessionConfig: SessionConfig
): SessionManager[Session] = self.manager
new BasicAccountServiceEndpoints[SD] with SessionMaterials[SD] {
override protected def sessionType: Session.SessionType = self.sessionType
override implicit def system: ActorSystem[_] = sys
override implicit lazy val ec: ExecutionContext = sys.executionContext
override implicit def sessionConfig: SessionConfig = self.sessionConfig
override implicit def manager(implicit
sessionConfig: SessionConfig,
companion: SessionDataCompanion[SD]
): SessionManager[SD] = self.manager
override protected val manifestWrapper: ManifestW = ManifestW()
override def log: Logger = LoggerFactory getLogger getClass.getName
override implicit def refreshTokenStorage: RefreshTokenStorage[SD] =
self.refreshTokenStorage(sys)
override implicit def companion: SessionDataCompanion[SD] = self.companion
}

override def oauthEndpoints: ActorSystem[_] => OAuthServiceEndpoints =
override def oauthEndpoints: ActorSystem[_] => OAuthServiceEndpoints[SD] =
sys =>
new OAuthServiceEndpoints with BasicAccountTypeKey with SessionMaterials {
override implicit def manager(implicit
sessionConfig: SessionConfig
): SessionManager[Session] = self.manager
new OAuthServiceEndpoints[SD] with BasicAccountTypeKey with SessionMaterials[SD] {
override protected def sessionType: Session.SessionType = self.sessionType
override implicit def system: ActorSystem[_] = sys
override implicit lazy val ec: ExecutionContext = sys.executionContext
override implicit def sessionConfig: SessionConfig = self.sessionConfig
override implicit def manager(implicit
sessionConfig: SessionConfig,
companion: SessionDataCompanion[SD]
): SessionManager[SD] = self.manager
override def log: Logger = LoggerFactory getLogger getClass.getName
override implicit def refreshTokenStorage: RefreshTokenStorage[SD] =
self.refreshTokenStorage(sys)
override implicit def companion: SessionDataCompanion[SD] = self.companion
}

override def endpoints: ActorSystem[_] => List[Endpoint] = system =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package app.softnetwork.account.api

import app.softnetwork.persistence.schema.SchemaProvider
import app.softnetwork.session.CsrfCheck
import app.softnetwork.session.model.{SessionData, SessionDataDecorator}

trait BasicAccountEndpointsApi extends BasicAccountApi with BasicAccountEndpoints {
trait BasicAccountEndpointsApi[SD <: SessionData with SessionDataDecorator[SD]]
extends BasicAccountApi[SD]
with BasicAccountEndpoints[SD] {
_: SchemaProvider with CsrfCheck =>
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package app.softnetwork.account.api

import akka.actor.typed.ActorSystem
import app.softnetwork.persistence.jdbc.schema.{JdbcSchemaProvider, JdbcSchemaTypes}
import app.softnetwork.persistence.schema.SchemaType
import app.softnetwork.session.CsrfCheckHeader
import app.softnetwork.session.handlers.SessionRefreshTokenDao
import app.softnetwork.session.model.SessionDataCompanion
import com.softwaremill.session.RefreshTokenStorage
import org.slf4j.{Logger, LoggerFactory}
import org.softnetwork.session.model.Session

object BasicAccountEndpointsPostgresLauncher
extends BasicAccountEndpointsApi
extends BasicAccountEndpointsApi[Session]
with JdbcSchemaProvider
with CsrfCheckHeader {
lazy val log: Logger = LoggerFactory getLogger getClass.getName

override def schemaType: SchemaType = JdbcSchemaTypes.Postgres

override implicit def companion: SessionDataCompanion[Session] = Session

override protected def refreshTokenStorage: ActorSystem[_] => RefreshTokenStorage[Session] =
sys => SessionRefreshTokenDao(sys)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package app.softnetwork.account.api

import akka.actor.typed.ActorSystem
import app.softnetwork.account.handlers.BasicAccountTypeKey
import app.softnetwork.account.launch.AccountRoutes
import app.softnetwork.account.model.{
BasicAccount,
Expand All @@ -19,51 +18,62 @@ import app.softnetwork.account.service.{
import app.softnetwork.api.server.ApiRoute
import app.softnetwork.persistence.schema.SchemaProvider
import app.softnetwork.session.CsrfCheck
import app.softnetwork.session.service.{SessionMaterials, SessionService}
import com.softwaremill.session.{SessionConfig, SessionManager}
import app.softnetwork.session.model.{SessionData, SessionDataCompanion, SessionDataDecorator}
import app.softnetwork.session.service.SessionMaterials
import com.softwaremill.session.{RefreshTokenStorage, SessionConfig, SessionManager}
import org.slf4j.{Logger, LoggerFactory}
import org.softnetwork.session.model.Session

import scala.concurrent.ExecutionContext

trait BasicAccountRoutes
trait BasicAccountRoutes[SD <: SessionData with SessionDataDecorator[SD]]
extends AccountRoutes[
BasicAccount,
BasicAccountProfile,
DefaultProfileView,
DefaultAccountDetailsView,
DefaultAccountView[DefaultProfileView, DefaultAccountDetailsView]
DefaultAccountView[DefaultProfileView, DefaultAccountDetailsView],
SD
] {
self: BasicAccountApi with SchemaProvider with CsrfCheck =>
self: BasicAccountApi[SD] with SchemaProvider with CsrfCheck =>
override def accountService: ActorSystem[_] => AccountService[
DefaultProfileView,
DefaultAccountDetailsView,
DefaultAccountView[DefaultProfileView, DefaultAccountDetailsView]
DefaultAccountView[DefaultProfileView, DefaultAccountDetailsView],
SD
] =
sys =>
new BasicAccountService with SessionMaterials {
new BasicAccountService[SD] with SessionMaterials[SD] {
override implicit def manager(implicit
sessionConfig: SessionConfig
): SessionManager[Session] = self.manager
sessionConfig: SessionConfig,
companion: SessionDataCompanion[SD]
): SessionManager[SD] = self.manager
override protected def sessionType: Session.SessionType = self.sessionType
override implicit def system: ActorSystem[_] = sys
override implicit lazy val ec: ExecutionContext = sys.executionContext
override implicit def sessionConfig: SessionConfig = self.sessionConfig
override protected val manifestWrapper: ManifestW = ManifestW()
override def log: Logger = LoggerFactory getLogger getClass.getName
override implicit def refreshTokenStorage: RefreshTokenStorage[SD] =
self.refreshTokenStorage(sys)
override implicit def companion: SessionDataCompanion[SD] = self.companion
}

override def oauthService: ActorSystem[_] => OAuthService =
override def oauthService: ActorSystem[_] => OAuthService[SD] =
sys =>
new BasicOAuthService with SessionMaterials {
new BasicOAuthService[SD] with SessionMaterials[SD] {
override implicit def manager(implicit
sessionConfig: SessionConfig
): SessionManager[Session] = self.manager
sessionConfig: SessionConfig,
companion: SessionDataCompanion[SD]
): SessionManager[SD] = self.manager
override protected def sessionType: Session.SessionType = self.sessionType
override implicit def system: ActorSystem[_] = sys
override implicit lazy val ec: ExecutionContext = sys.executionContext
override implicit def sessionConfig: SessionConfig = self.sessionConfig
override def log: Logger = LoggerFactory getLogger getClass.getName
override implicit def refreshTokenStorage: RefreshTokenStorage[SD] =
self.refreshTokenStorage(sys)
override implicit def companion: SessionDataCompanion[SD] = self.companion
}

override def apiRoutes: ActorSystem[_] => List[ApiRoute] = system =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package app.softnetwork.account.api

import app.softnetwork.persistence.schema.SchemaProvider
import app.softnetwork.session.CsrfCheck
import app.softnetwork.session.model.{SessionData, SessionDataDecorator}

trait BasicAccountRoutesApi extends BasicAccountApi with BasicAccountRoutes {
trait BasicAccountRoutesApi[SD <: SessionData with SessionDataDecorator[SD]]
extends BasicAccountApi[SD]
with BasicAccountRoutes[SD] {
_: SchemaProvider with CsrfCheck =>
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package app.softnetwork.account.api

import akka.actor.typed.ActorSystem
import app.softnetwork.persistence.jdbc.schema.{JdbcSchemaProvider, JdbcSchemaTypes}
import app.softnetwork.persistence.schema.SchemaType
import app.softnetwork.session.CsrfCheckHeader
import app.softnetwork.session.handlers.SessionRefreshTokenDao
import app.softnetwork.session.model.SessionDataCompanion
import com.softwaremill.session.RefreshTokenStorage
import org.slf4j.{Logger, LoggerFactory}
import org.softnetwork.session.model.Session

object BasicAccountRoutesPostgresLauncher
extends BasicAccountRoutesApi
extends BasicAccountRoutesApi[Session]
with JdbcSchemaProvider
with CsrfCheckHeader {
lazy val log: Logger = LoggerFactory getLogger getClass.getName

override def schemaType: SchemaType = JdbcSchemaTypes.Postgres

override implicit def companion: SessionDataCompanion[Session] = Session

override protected def refreshTokenStorage: ActorSystem[_] => RefreshTokenStorage[Session] =
sys => SessionRefreshTokenDao(sys)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import app.softnetwork.persistence.launch.PersistentEntity
import app.softnetwork.persistence.query.EventProcessorStream
import app.softnetwork.persistence.schema.SchemaType
import app.softnetwork.session.CsrfCheckHeader
import app.softnetwork.session.handlers.SessionRefreshTokenDao
import app.softnetwork.session.model.SessionDataCompanion
import com.softwaremill.session.RefreshTokenStorage
import org.slf4j.{Logger, LoggerFactory}
import org.softnetwork.session.model.Session

object BasicAccountRoutesWithNotificationsPostgresLauncher
extends AllNotificationsApi
with BasicAccountRoutesApi
with BasicAccountRoutesApi[Session]
with JdbcSchemaProvider
with CsrfCheckHeader {

Expand All @@ -28,4 +32,9 @@ object BasicAccountRoutesWithNotificationsPostgresLauncher
*/
override def eventProcessorStreams: ActorSystem[_] => Seq[EventProcessorStream[_]] = sys =>
super.eventProcessorStreams(sys) ++ notificationEventProcessorStreams(sys)

override implicit def companion: SessionDataCompanion[Session] = Session

override protected def refreshTokenStorage: ActorSystem[_] => RefreshTokenStorage[Session] =
sys => SessionRefreshTokenDao(sys)
}
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ThisBuild / organization := "app.softnetwork"

name := "account"

ThisBuild / version := "0.5.1"
ThisBuild / version := "0.6.0"

ThisBuild / scalaVersion := "2.12.18"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ trait OAuth2ServiceCompanion {
case "facebook" => new FacebookApiService()
case "github" => new GitHubApiService()
case "instagram" => new InstagramApiService()
case _ => throw new Exception(s"OAuth2Service for $networkName not defined")
case _ => throw new Exception(s"OAuth2Service for $networkName not defined")
}

def apply(): Seq[OAuth2Service] = AccountSettings.OAuthSettings.providers.keys
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ import app.softnetwork.account.service.{AccountServiceEndpoints, OAuthServiceEnd
import app.softnetwork.api.server.{ApiEndpoints, Endpoint}
import app.softnetwork.persistence.schema.SchemaProvider
import app.softnetwork.session.CsrfCheck
import app.softnetwork.session.model.{SessionData, SessionDataDecorator}
import org.json4s.Formats

trait AccountEndpoints[
T <: Account with AccountDecorator,
P <: Profile with ProfileDecorator,
SU
SU,
SD <: SessionData with SessionDataDecorator[SD]
] extends ApiEndpoints
with AccountGuardian[T, P] { _: SchemaProvider with CsrfCheck =>

override implicit def formats: Formats = accountFormats

def accountEndpoints: ActorSystem[_] => AccountServiceEndpoints[SU]
def accountEndpoints: ActorSystem[_] => AccountServiceEndpoints[SU, SD]

def oauthEndpoints: ActorSystem[_] => OAuthServiceEndpoints
def oauthEndpoints: ActorSystem[_] => OAuthServiceEndpoints[SD]

override def endpoints: ActorSystem[_] => List[Endpoint] =
system =>
Expand Down
Loading

0 comments on commit 7d8c005

Please sign in to comment.