-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
146 additions
and
7 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
...main/kotlin/com/vauthenticator/server/mfa/adapter/jdbc/JdbcMfaAccountMethodsRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package com.vauthenticator.server.mfa.adapter.jdbc | ||
|
||
import com.vauthenticator.server.keys.domain.* | ||
import com.vauthenticator.server.mfa.domain.MfaAccountMethod | ||
import com.vauthenticator.server.mfa.domain.MfaAccountMethodsRepository | ||
import com.vauthenticator.server.mfa.domain.MfaDeviceId | ||
import com.vauthenticator.server.mfa.domain.MfaMethod | ||
import org.springframework.jdbc.core.JdbcTemplate | ||
import org.springframework.transaction.annotation.Transactional | ||
import java.sql.ResultSet | ||
import java.util.* | ||
|
||
@Transactional | ||
class JdbcMfaAccountMethodsRepository( | ||
private val jdbcTemplate: JdbcTemplate, | ||
private val keyRepository: KeyRepository, | ||
private val masterKid: MasterKid, | ||
private val mfaDeviceIdGenerator: () -> MfaDeviceId | ||
) : MfaAccountMethodsRepository { | ||
override fun findBy(userName: String, mfaMfaMethod: MfaMethod, mfaChannel: String): Optional<MfaAccountMethod> = | ||
Optional.ofNullable( | ||
jdbcTemplate.query( | ||
"SELECT * FROM MFA_ACCOUNT_METHODS WHERE user_name=? AND mfa_method=? AND mfa_channel=?", | ||
{ rs, _ -> mfaAccountMethodFrom(rs) }, | ||
userName, mfaMfaMethod.name, mfaChannel | ||
).firstOrNull() | ||
) | ||
|
||
|
||
override fun findBy(deviceId: MfaDeviceId): Optional<MfaAccountMethod> = | ||
Optional.ofNullable( | ||
jdbcTemplate.query( | ||
"SELECT * FROM MFA_ACCOUNT_METHODS WHERE mfa_device_id=?", | ||
{ rs, _ -> mfaAccountMethodFrom(rs) }, | ||
deviceId.content | ||
).firstOrNull() | ||
) | ||
|
||
override fun findAll(userName: String): List<MfaAccountMethod> = | ||
jdbcTemplate.query("SELECT * FROM MFA_ACCOUNT_METHODS") | ||
{ rs, _ -> mfaAccountMethodFrom(rs) } | ||
|
||
|
||
override fun save( | ||
userName: String, | ||
mfaMfaMethod: MfaMethod, | ||
mfaChannel: String, | ||
associated: Boolean | ||
): MfaAccountMethod { | ||
val kid = keyRepository.createKeyFrom(masterKid, KeyType.SYMMETRIC, KeyPurpose.MFA) | ||
val mfaDeviceId = mfaDeviceIdGenerator.invoke() | ||
|
||
jdbcTemplate.update( | ||
"INSERT INTO MFA_ACCOUNT_METHODS (user_name, mfa_device_id, mfa_method, mfa_channel, key_id, associated) VALUES (?,?,?,?,?,?)", | ||
userName, mfaDeviceId.content, mfaMfaMethod.name, mfaChannel, kid.content(), associated | ||
) | ||
|
||
return MfaAccountMethod(userName, mfaDeviceId, kid, mfaMfaMethod, mfaChannel, associated) | ||
} | ||
|
||
|
||
override fun setAsDefault(userName: String, deviceId: MfaDeviceId) { | ||
Optional.ofNullable( | ||
jdbcTemplate.query( | ||
"SELECT mfa_device_id FROM MFA_ACCOUNT_METHODS WHERE user_name=? AND default_mfa_method=true", | ||
{ rs, _ -> MfaDeviceId(rs.getString("mfa_device_id")) }, | ||
userName | ||
).firstOrNull() | ||
).ifPresent { | ||
jdbcTemplate.update( | ||
"UPDATE MFA_ACCOUNT_METHODS SET default_mfa_method = false WHERE user_name=? AND mfa_device_id=?", | ||
userName, it.content | ||
) | ||
} | ||
|
||
jdbcTemplate.update( | ||
"UPDATE MFA_ACCOUNT_METHODS SET default_mfa_method = true WHERE user_name=? AND mfa_device_id=?", | ||
userName, deviceId.content | ||
) | ||
} | ||
|
||
override fun getDefaultDevice(userName: String): Optional<MfaDeviceId> = | ||
Optional.ofNullable( | ||
jdbcTemplate.query( | ||
"SELECT mfa_device_id FROM MFA_ACCOUNT_METHODS WHERE user_name=? AND default_mfa_method=true", | ||
{ rs, _ -> MfaDeviceId(rs.getString("mfa_device_id")) }, | ||
userName | ||
).firstOrNull() | ||
) | ||
|
||
private fun mfaAccountMethodFrom(rs: ResultSet) = MfaAccountMethod( | ||
userName = rs.getString("user_name"), | ||
mfaDeviceId = MfaDeviceId(rs.getString("mfa_device_id")), | ||
key = Kid(rs.getString("key_id")), | ||
mfaMethod = MfaMethod.valueOf(rs.getString("mfa_method")), | ||
mfaChannel = rs.getString("mfa_channel"), | ||
associated = rs.getBoolean("associated"), | ||
) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
.../kotlin/com/vauthenticator/server/mfa/adapter/jdbc/JdbcMfaAccountMethodsRepositoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.vauthenticator.server.mfa.adapter.jdbc | ||
|
||
import com.vauthenticator.server.mfa.adapter.AbstractMfaAccountMethodsRepositoryTest | ||
import com.vauthenticator.server.support.JdbcUtils.jdbcTemplate | ||
import com.vauthenticator.server.support.JdbcUtils.resetDb | ||
import io.mockk.junit5.MockKExtension | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
|
||
@ExtendWith(MockKExtension::class) | ||
class JdbcMfaAccountMethodsRepositoryTest : AbstractMfaAccountMethodsRepositoryTest() { | ||
override fun initMfaAccountMethodsRepository() = JdbcMfaAccountMethodsRepository( | ||
jdbcTemplate, | ||
keyRepository, | ||
masterKid | ||
) { mfaDeviceId } | ||
|
||
|
||
override fun resetDatabase() { | ||
resetDb() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters