Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3787e0f3ed | ||
| 45dec00b86 | |||
|
|
fbb3f48b6d | ||
| e32f4eb8ff | |||
|
|
66edab8ffe | ||
| 9ca1813f06 |
15
CHANGELOG.md
15
CHANGELOG.md
@@ -383,3 +383,18 @@
|
|||||||
### Features
|
### Features
|
||||||
|
|
||||||
* Disable default JPA and Hibernate modules and enhance EntityManagerProvider for HikariCP integration ([9fa1e5e](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Web/commit/9fa1e5e07122aebd0391d47c3513013243a72a0f))
|
* Disable default JPA and Hibernate modules and enhance EntityManagerProvider for HikariCP integration ([9fa1e5e](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Web/commit/9fa1e5e07122aebd0391d47c3513013243a72a0f))
|
||||||
|
## (2026-01-20)
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* Add logging for user management operations in HibernateUserManager ([9ca1813](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Web/commit/9ca1813f06539cffeb573d0e00571e4f2d5144f1))
|
||||||
|
## (2026-01-20)
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* Integrate UserManager and HibernateUserManager in session management ([e32f4eb](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Web/commit/e32f4eb8fff9daec46f20284e28e94a59231d033))
|
||||||
|
## (2026-01-20)
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* Implement transaction management for user addition and removal in HibernateUserManager ([45dec00](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Web/commit/45dec00b86a1395457226ed62ac319c61e38739a))
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
package di
|
|
||||||
|
|
||||||
import com.google.inject.AbstractModule
|
|
||||||
import com.google.inject.name.Names
|
|
||||||
import logic.user.impl.HibernateUserManager
|
|
||||||
import play.api.db.DBApi
|
|
||||||
import play.api.{Configuration, Environment}
|
|
||||||
|
|
||||||
class ProductionModule(
|
|
||||||
environment: Environment,
|
|
||||||
configuration: Configuration
|
|
||||||
) extends AbstractModule {
|
|
||||||
|
|
||||||
override def configure(): Unit = {
|
|
||||||
// Bind HibernateUserManager for production
|
|
||||||
bind(classOf[logic.user.UserManager])
|
|
||||||
.to(classOf[logic.user.impl.HibernateUserManager])
|
|
||||||
.asEagerSingleton()
|
|
||||||
|
|
||||||
// Bind EntityManager for JPA
|
|
||||||
bind(classOf[jakarta.persistence.EntityManager])
|
|
||||||
.toProvider(classOf[EntityManagerProvider])
|
|
||||||
.asEagerSingleton()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -4,7 +4,7 @@ import com.auth0.jwt.algorithms.Algorithm
|
|||||||
import com.auth0.jwt.{JWT, JWTVerifier}
|
import com.auth0.jwt.{JWT, JWTVerifier}
|
||||||
import com.github.benmanes.caffeine.cache.{Cache, Caffeine}
|
import com.github.benmanes.caffeine.cache.{Cache, Caffeine}
|
||||||
import com.typesafe.config.Config
|
import com.typesafe.config.Config
|
||||||
import logic.user.SessionManager
|
import logic.user.{SessionManager, UserManager}
|
||||||
import model.users.User
|
import model.users.User
|
||||||
import scalafx.util.Duration
|
import scalafx.util.Duration
|
||||||
import services.JwtKeyProvider
|
import services.JwtKeyProvider
|
||||||
@@ -16,7 +16,7 @@ import javax.inject.{Inject, Singleton}
|
|||||||
import scala.util.Try
|
import scala.util.Try
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
class BaseSessionManager @Inject()(val keyProvider: JwtKeyProvider, val userManager: StubUserManager, val config: Config) extends SessionManager {
|
class BaseSessionManager @Inject()(val keyProvider: JwtKeyProvider, val userManager: UserManager, val config: Config) extends SessionManager {
|
||||||
|
|
||||||
private val algorithm = Algorithm.RSA512(keyProvider.publicKey, keyProvider.privateKey)
|
private val algorithm = Algorithm.RSA512(keyProvider.publicKey, keyProvider.privateKey)
|
||||||
private val verifier: JWTVerifier = JWT.require(algorithm)
|
private val verifier: JWTVerifier = JWT.require(algorithm)
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import jakarta.inject.Inject
|
|||||||
import jakarta.persistence.EntityManager
|
import jakarta.persistence.EntityManager
|
||||||
import logic.user.UserManager
|
import logic.user.UserManager
|
||||||
import model.users.{User, UserEntity}
|
import model.users.{User, UserEntity}
|
||||||
|
import play.api.Logger
|
||||||
import services.OpenIDUserInfo
|
import services.OpenIDUserInfo
|
||||||
import util.UserHash
|
import util.UserHash
|
||||||
|
|
||||||
@@ -14,14 +15,20 @@ import scala.jdk.CollectionConverters.*
|
|||||||
@Singleton
|
@Singleton
|
||||||
class HibernateUserManager @Inject()(em: EntityManager, config: Config) extends UserManager {
|
class HibernateUserManager @Inject()(em: EntityManager, config: Config) extends UserManager {
|
||||||
|
|
||||||
|
private val logger = Logger(getClass.getName)
|
||||||
|
|
||||||
override def addUser(name: String, password: String): Boolean = {
|
override def addUser(name: String, password: String): Boolean = {
|
||||||
|
val tx = em.getTransaction
|
||||||
try {
|
try {
|
||||||
|
tx.begin()
|
||||||
// Check if user already exists
|
// Check if user already exists
|
||||||
val existing = em.createQuery("SELECT u FROM UserEntity u WHERE u.username = :username", classOf[UserEntity])
|
val existing = em.createQuery("SELECT u FROM UserEntity u WHERE u.username = :username", classOf[UserEntity])
|
||||||
.setParameter("username", name)
|
.setParameter("username", name)
|
||||||
.getResultList
|
.getResultList
|
||||||
|
|
||||||
if (!existing.isEmpty) {
|
if (!existing.isEmpty) {
|
||||||
|
logger.warn(s"User $name already exists")
|
||||||
|
tx.rollback()
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,20 +42,30 @@ class HibernateUserManager @Inject()(em: EntityManager, config: Config) extends
|
|||||||
|
|
||||||
em.persist(userEntity)
|
em.persist(userEntity)
|
||||||
em.flush()
|
em.flush()
|
||||||
|
tx.commit()
|
||||||
|
|
||||||
true
|
true
|
||||||
} catch {
|
} catch {
|
||||||
case _: Exception => false
|
case e: Exception => {
|
||||||
|
if (tx.isActive) tx.rollback()
|
||||||
|
logger.error(s"Error adding user $name", e)
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override def addOpenIDUser(name: String, userInfo: OpenIDUserInfo): Boolean = {
|
override def addOpenIDUser(name: String, userInfo: OpenIDUserInfo): Boolean = {
|
||||||
|
val tx = em.getTransaction
|
||||||
try {
|
try {
|
||||||
|
tx.begin()
|
||||||
// Check if user already exists
|
// Check if user already exists
|
||||||
val existing = em.createQuery("SELECT u FROM UserEntity u WHERE u.username = :username", classOf[UserEntity])
|
val existing = em.createQuery("SELECT u FROM UserEntity u WHERE u.username = :username", classOf[UserEntity])
|
||||||
.setParameter("username", name)
|
.setParameter("username", name)
|
||||||
.getResultList
|
.getResultList
|
||||||
|
|
||||||
if (!existing.isEmpty) {
|
if (!existing.isEmpty) {
|
||||||
|
logger.warn(s"User $name already exists")
|
||||||
|
tx.rollback()
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,6 +79,8 @@ class HibernateUserManager @Inject()(em: EntityManager, config: Config) extends
|
|||||||
.getResultList
|
.getResultList
|
||||||
|
|
||||||
if (!existingOpenID.isEmpty) {
|
if (!existingOpenID.isEmpty) {
|
||||||
|
logger.warn(s"OpenID user ${userInfo.provider}_${userInfo.id} already exists")
|
||||||
|
tx.rollback()
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,9 +89,14 @@ class HibernateUserManager @Inject()(em: EntityManager, config: Config) extends
|
|||||||
|
|
||||||
em.persist(userEntity)
|
em.persist(userEntity)
|
||||||
em.flush()
|
em.flush()
|
||||||
|
tx.commit()
|
||||||
true
|
true
|
||||||
} catch {
|
} catch {
|
||||||
case _: Exception => false
|
case e: Exception => {
|
||||||
|
if (tx.isActive) tx.rollback()
|
||||||
|
logger.error(s"Error adding OpenID user ${userInfo.provider}_${userInfo.id}", e)
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,7 +117,10 @@ class HibernateUserManager @Inject()(em: EntityManager, config: Config) extends
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
case _: Exception => None
|
case e: Exception => {
|
||||||
|
logger.error(s"Error authenticating user $name", e)
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,7 +140,10 @@ class HibernateUserManager @Inject()(em: EntityManager, config: Config) extends
|
|||||||
Some(users.get(0).toUser)
|
Some(users.get(0).toUser)
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
case _: Exception => None
|
case e: Exception => {
|
||||||
|
logger.error(s"Error authenticating OpenID user ${provider}_$providerId", e)
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,7 +159,10 @@ class HibernateUserManager @Inject()(em: EntityManager, config: Config) extends
|
|||||||
Some(users.get(0).toUser)
|
Some(users.get(0).toUser)
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
case _: Exception => None
|
case e: Exception => {
|
||||||
|
logger.error(s"Error checking if user $name exists", e)
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,25 +170,35 @@ class HibernateUserManager @Inject()(em: EntityManager, config: Config) extends
|
|||||||
try {
|
try {
|
||||||
Option(em.find(classOf[UserEntity], id)).map(_.toUser)
|
Option(em.find(classOf[UserEntity], id)).map(_.toUser)
|
||||||
} catch {
|
} catch {
|
||||||
case _: Exception => None
|
case e: Exception => {
|
||||||
|
logger.error(s"Error checking if user with ID $id exists", e)
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override def removeUser(name: String): Boolean = {
|
override def removeUser(name: String): Boolean = {
|
||||||
|
val tx = em.getTransaction
|
||||||
try {
|
try {
|
||||||
|
tx.begin()
|
||||||
val users = em.createQuery("SELECT u FROM UserEntity u WHERE u.username = :username", classOf[UserEntity])
|
val users = em.createQuery("SELECT u FROM UserEntity u WHERE u.username = :username", classOf[UserEntity])
|
||||||
.setParameter("username", name)
|
.setParameter("username", name)
|
||||||
.getResultList
|
.getResultList
|
||||||
|
|
||||||
if (users.isEmpty) {
|
if (users.isEmpty) {
|
||||||
|
tx.rollback()
|
||||||
false
|
false
|
||||||
} else {
|
} else {
|
||||||
em.remove(users.get(0))
|
em.remove(users.get(0))
|
||||||
em.flush()
|
em.flush()
|
||||||
|
tx.commit()
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
case _: Exception => false
|
case _: Exception => {
|
||||||
|
if (tx.isActive) tx.rollback()
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,24 @@
|
|||||||
package modules
|
package modules
|
||||||
|
|
||||||
import com.google.inject.AbstractModule
|
import com.google.inject.AbstractModule
|
||||||
|
import di.EntityManagerProvider
|
||||||
|
import jakarta.persistence.EntityManager
|
||||||
import logic.Gateway
|
import logic.Gateway
|
||||||
|
import logic.user.UserManager
|
||||||
|
import logic.user.impl.HibernateUserManager
|
||||||
|
|
||||||
class GatewayModule extends AbstractModule {
|
class GatewayModule extends AbstractModule {
|
||||||
override def configure(): Unit = {
|
override def configure(): Unit = {
|
||||||
bind(classOf[Gateway]).asEagerSingleton()
|
bind(classOf[Gateway]).asEagerSingleton()
|
||||||
|
|
||||||
|
// Bind HibernateUserManager for production (when GatewayModule is used)
|
||||||
|
bind(classOf[UserManager])
|
||||||
|
.to(classOf[HibernateUserManager])
|
||||||
|
.asEagerSingleton()
|
||||||
|
|
||||||
|
// Bind EntityManager for JPA
|
||||||
|
bind(classOf[EntityManager])
|
||||||
|
.toProvider(classOf[EntityManagerProvider])
|
||||||
|
.asEagerSingleton()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,9 @@
|
|||||||
|
|
||||||
<persistence-unit name="defaultPersistenceUnit">
|
<persistence-unit name="defaultPersistenceUnit">
|
||||||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||||
|
|
||||||
|
<class>model.users.UserEntity</class>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<!-- Hibernate specific settings -->
|
<!-- Hibernate specific settings -->
|
||||||
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
|
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ openid {
|
|||||||
clientId = ${?DISCORD_CLIENT_ID}
|
clientId = ${?DISCORD_CLIENT_ID}
|
||||||
clientSecret = ${?DISCORD_CLIENT_SECRET}
|
clientSecret = ${?DISCORD_CLIENT_SECRET}
|
||||||
redirectUri = ${?DISCORD_REDIRECT_URI}
|
redirectUri = ${?DISCORD_REDIRECT_URI}
|
||||||
redirectUri = "https://knockout.janis-eccarius.de/auth/discord/callback"
|
redirectUri = "https://knockout.janis-eccarius.de/api/auth/discord/callback"
|
||||||
}
|
}
|
||||||
|
|
||||||
keycloak {
|
keycloak {
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ openid {
|
|||||||
clientId = ${?DISCORD_CLIENT_ID}
|
clientId = ${?DISCORD_CLIENT_ID}
|
||||||
clientSecret = ${?DISCORD_CLIENT_SECRET}
|
clientSecret = ${?DISCORD_CLIENT_SECRET}
|
||||||
redirectUri = ${?DISCORD_REDIRECT_URI}
|
redirectUri = ${?DISCORD_REDIRECT_URI}
|
||||||
redirectUri = "https://st.knockout.janis-eccarius.de/auth/discord/callback"
|
redirectUri = "https://st.knockout.janis-eccarius.de/api/auth/discord/callback"
|
||||||
}
|
}
|
||||||
|
|
||||||
keycloak {
|
keycloak {
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
MAJOR=4
|
MAJOR=4
|
||||||
MINOR=38
|
MINOR=41
|
||||||
PATCH=0
|
PATCH=0
|
||||||
|
|||||||
Reference in New Issue
Block a user