update user session on event

- update handleRemoteAccountEvent to update user session on change event in AccountListener
- add RedisSessionService
- add fromAccountResponse to RemoteAuthenticationUtil
- extract make remote authentication from RemoteAuthenticationManager to RemoteAuthenticationUtil
This commit is contained in:
2025-08-06 15:08:50 +02:00
parent 57565b4589
commit b33d7c9c09
4 changed files with 102 additions and 31 deletions

View File

@@ -0,0 +1,44 @@
package ltd.hlaeja.util
import io.jsonwebtoken.Claims
import io.jsonwebtoken.Jws
import java.util.UUID
import ltd.hlaeja.library.accountRegistry.Account
import ltd.hlaeja.security.user.RemoteAuthentication
import ltd.hlaeja.security.user.RemoteUserDetail
import org.springframework.security.core.authority.SimpleGrantedAuthority
object RemoteAuthenticationUtil {
fun fromJwtClaims(
claims: Jws<Claims>,
): RemoteAuthentication = makeRemoteAuthentication(
id = UUID.fromString(claims.payload["id"] as String),
username = claims.payload["username"] as String,
roles = (claims.payload["role"] as String).split(","),
authenticated = true,
)
fun fromAccountResponse(
response: Account.Response,
): RemoteAuthentication = makeRemoteAuthentication(
id = response.id,
username = response.username,
roles = response.roles,
authenticated = response.enabled,
)
private fun makeRemoteAuthentication(
id: UUID,
username: String,
roles: List<String>,
authenticated: Boolean,
) = RemoteAuthentication(
remoteUserDetail = RemoteUserDetail(
id = id,
username = username,
),
authorities = roles.map { SimpleGrantedAuthority("ROLE_$it") }.toMutableList(),
authenticated = authenticated,
)
}