-
Notifications
You must be signed in to change notification settings - Fork 0
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
15 changed files
with
189 additions
and
225 deletions.
There are no files selected for viewing
17 changes: 0 additions & 17 deletions
17
komok-app/src/main/kotlin/io/heapy/komok/TimeSourceContext.kt
This file was deleted.
Oops, something went wrong.
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
2 changes: 0 additions & 2 deletions
2
komok-app/src/main/kotlin/io/heapy/komok/business/login/LoginService.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
146 changes: 0 additions & 146 deletions
146
komok-app/src/main/kotlin/io/heapy/komok/business/user/totp/TOTP.kt
This file was deleted.
Oops, something went wrong.
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
13 changes: 13 additions & 0 deletions
13
komok-app/src/main/kotlin/io/heapy/komok/infra/time/TimeSourceModule.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,13 @@ | ||
package io.heapy.komok.infra.time | ||
|
||
import io.heapy.komok.tech.di.lib.Module | ||
import java.time.Clock | ||
|
||
@Module | ||
open class TimeSourceModule { | ||
open val timeSource: TimeSource by lazy { | ||
TimeSource( | ||
clock = Clock.systemDefaultZone(), | ||
) | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
komok-app/src/main/kotlin/io/heapy/komok/infra/totp/TimeBasedOneTimePassword.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,68 @@ | ||
package io.heapy.komok.infra.totp | ||
|
||
import io.heapy.komok.infra.time.TimeSource | ||
import io.heapy.komok.infra.base32.Base32 | ||
import java.nio.ByteBuffer | ||
import java.nio.ByteOrder | ||
import javax.crypto.Mac | ||
import javax.crypto.spec.SecretKeySpec | ||
import kotlin.math.pow | ||
|
||
class TimeBasedOneTimePassword( | ||
private val base32: Base32, | ||
private val timeSource: TimeSource, | ||
) { | ||
fun generate( | ||
secret: String, | ||
): String { | ||
val key = base32.decode(secret) | ||
val timeWindow = timeSource.instant().epochSecond / TIME_STEP_SECONDS | ||
|
||
val data = ByteBuffer | ||
.allocate(8) | ||
.order(ByteOrder.BIG_ENDIAN) | ||
.putLong(timeWindow) | ||
.array() | ||
|
||
val mac = Mac.getInstance(HMAC_ALGO) | ||
mac.init( | ||
SecretKeySpec( | ||
key, | ||
HMAC_ALGO | ||
) | ||
) | ||
|
||
val hmac = mac.doFinal(data) | ||
|
||
val offset = hmac[hmac.size - 1].toInt() and 0x0F | ||
val binary = (hmac[offset].toInt() and 0x7f shl 24) or | ||
(hmac[offset + 1].toInt() and 0xff shl 16) or | ||
(hmac[offset + 2].toInt() and 0xff shl 8) or | ||
(hmac[offset + 3].toInt() and 0xff) | ||
|
||
val otp = binary % 10.0.pow(TOTP_DIGITS.toDouble()) | ||
.toInt() | ||
|
||
return otp | ||
.toString() | ||
.padStart( | ||
TOTP_DIGITS, | ||
'0' | ||
) | ||
} | ||
|
||
fun validate( | ||
secret: String, | ||
otp: String, | ||
): Boolean { | ||
val calculatedOtp = generate(secret) | ||
|
||
return calculatedOtp == otp | ||
} | ||
|
||
private companion object { | ||
private const val HMAC_ALGO = "HmacSHA1" | ||
private const val TIME_STEP_SECONDS = 30L | ||
private const val TOTP_DIGITS = 6 | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
komok-app/src/main/kotlin/io/heapy/komok/infra/totp/TimeBasedOneTimePasswordModule.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,18 @@ | ||
package io.heapy.komok.infra.totp | ||
|
||
import io.heapy.komok.infra.base32.Base32Module | ||
import io.heapy.komok.infra.time.TimeSourceModule | ||
import io.heapy.komok.tech.di.lib.Module | ||
|
||
@Module | ||
open class TimeBasedOneTimePasswordModule( | ||
private val base32Module: Base32Module, | ||
private val timeSourceModule: TimeSourceModule, | ||
) { | ||
open val timeBasedOneTimePassword by lazy { | ||
TimeBasedOneTimePassword( | ||
base32 = base32Module.base32, | ||
timeSource = timeSourceModule.timeSource, | ||
) | ||
} | ||
} |
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
Oops, something went wrong.