Skip to content

Commit 003ddf6

Browse files
committed
Make LoginAuditService creation unconditional
1 parent bda6a0d commit 003ddf6

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

src/main/kotlin/fi/hsl/jore4/auth/audit/LoginAuditService.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@ package fi.hsl.jore4.auth.audit
22

33
import org.slf4j.Logger
44
import org.slf4j.LoggerFactory
5-
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean
5+
import org.springframework.beans.factory.annotation.Autowired
66
import org.springframework.stereotype.Service
77
import org.springframework.transaction.annotation.Transactional
88

99
/**
1010
* Service for logging login events.
11-
* Only enabled when JPA is available.
1211
*/
1312
@Service
14-
@ConditionalOnBean(LoginAuditRepository::class)
1513
open class LoginAuditService(
16-
private val loginAuditRepository: LoginAuditRepository
14+
@Autowired(required = false)
15+
private val loginAuditRepository: LoginAuditRepository?
1716
) {
1817
companion object {
1918
private val LOGGER: Logger = LoggerFactory.getLogger(LoginAuditService::class.java)
@@ -27,6 +26,11 @@ open class LoginAuditService(
2726
userId: String,
2827
userName: String?
2928
) {
29+
if (loginAuditRepository == null) {
30+
LOGGER.warn("LoginAuditRepository is not available, cannot record login")
31+
return
32+
}
33+
3034
try {
3135
val auditRecord =
3236
LoginAudit(

src/main/kotlin/fi/hsl/jore4/auth/oidc/OIDCCodeExchangeService.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ open class OIDCCodeExchangeService(
9191
val accessToken = successResponse.oidcTokens.accessToken
9292
val refreshToken = successResponse.oidcTokens.refreshToken
9393
val idToken = successResponse.oidcTokens.idToken
94+
val accessTokenValue = accessToken.value
95+
val userId = idToken.jwtClaimsSet.subject
9496

9597
// verify token authenticity and validity if not using Entra, as it uses an unverifiable internal token
9698
// See https://learn.microsoft.com/en-us/entra/identity-platform/access-tokens#validate-tokens
@@ -102,10 +104,13 @@ open class OIDCCodeExchangeService(
102104

103105
// Record the login event in the audit log
104106
try {
105-
loginAuditService?.let {
106-
val userId = idToken.jwtClaimsSet.subject
107-
val userName = fetchUserNameFromUserInfo(accessToken.value, userId)
108-
it.recordLogin(userId, userName)
107+
loginAuditService?.let { auditService ->
108+
LOGGER.info("Recording login for userId: {}", userId)
109+
val userName = fetchUserNameFromUserInfo(accessTokenValue, userId)
110+
auditService.recordLogin(userId, userName)
111+
}
112+
if (loginAuditService == null) {
113+
LOGGER.warn("LoginAuditService is not available")
109114
}
110115
} catch (e: Exception) {
111116
LOGGER.warn("Could not record login audit", e)

0 commit comments

Comments
 (0)