-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use hmac token for ergochat authentication due to irc 510 message cha…
…racter limit (#178)
- Loading branch information
Showing
11 changed files
with
322 additions
and
107 deletions.
There are no files selected for viewing
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
51 changes: 51 additions & 0 deletions
51
src/main/kotlin/com/faforever/userservice/backend/security/HmacService.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,51 @@ | ||
package com.faforever.userservice.backend.security | ||
|
||
import jakarta.enterprise.context.ApplicationScoped | ||
import java.net.URLEncoder | ||
import java.nio.charset.StandardCharsets | ||
import java.time.Instant | ||
import java.util.* | ||
import javax.crypto.Mac | ||
import javax.crypto.spec.SecretKeySpec | ||
|
||
@ApplicationScoped | ||
class HmacService { | ||
|
||
companion object { | ||
private const val HMAC_SHA256 = "HmacSHA256" | ||
} | ||
|
||
fun generateHmacToken(message: String, secret: String): String { | ||
val timeStamp = Instant.now().epochSecond | ||
val hmacEncoded = generateEncodedMessage(message, secret, timeStamp) | ||
return "$timeStamp-$hmacEncoded" | ||
} | ||
|
||
private fun generateEncodedMessage(message: String, secret: String, epochSecond: Long): String { | ||
val mac = Mac.getInstance(HMAC_SHA256) | ||
mac.init(SecretKeySpec(secret.toByteArray(StandardCharsets.UTF_8), HMAC_SHA256)) | ||
val macMessage = (message + epochSecond).toByteArray(StandardCharsets.UTF_8) | ||
|
||
return URLEncoder.encode( | ||
String(Base64.getEncoder().encode(mac.doFinal(macMessage)), StandardCharsets.UTF_8), | ||
StandardCharsets.UTF_8, | ||
) | ||
} | ||
|
||
fun isValidHmacToken(token: String, expectedMessage: String, secret: String, ttl: Long): Boolean { | ||
val (epochSecond, encodedMessage) = token.split("-").let { | ||
if (it.size != 2) { | ||
return false | ||
} | ||
|
||
it[0].toLongOrNull() to it[1] | ||
} | ||
|
||
if (epochSecond == null) { | ||
return false | ||
} | ||
|
||
return Instant.now().isBefore(Instant.ofEpochSecond(epochSecond).plusSeconds(ttl)) && | ||
generateEncodedMessage(expectedMessage, secret, epochSecond) == encodedMessage | ||
} | ||
} |
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
32 changes: 32 additions & 0 deletions
32
src/test/kotlin/com/faforever/userservice/backend/cloudflare/CloudflareServiceTest.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,32 @@ | ||
package com.faforever.userservice.backend.cloudflare | ||
|
||
import com.faforever.userservice.backend.security.HmacService | ||
import io.quarkus.test.junit.QuarkusTest | ||
import jakarta.inject.Inject | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.Test | ||
import java.net.URI | ||
|
||
@QuarkusTest | ||
class CloudflareServiceTest { | ||
|
||
@Inject | ||
private lateinit var cloudflareService: CloudflareService | ||
|
||
@Inject | ||
private lateinit var hmacService: HmacService | ||
|
||
@Test | ||
fun testCloudFlareToken() { | ||
val secret = "secret" | ||
val token = cloudflareService.generateCloudFlareHmacToken(URI.create("http://localhost/test"), secret) | ||
assertTrue(hmacService.isValidHmacToken(token, "/test", secret, 1)) | ||
} | ||
|
||
@Test | ||
fun testCloudFlareTokenEmptyPath() { | ||
val secret = "secret" | ||
val token = cloudflareService.generateCloudFlareHmacToken(URI.create("http://localhost"), secret) | ||
assertTrue(hmacService.isValidHmacToken(token, "/", secret, 1)) | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/test/kotlin/com/faforever/userservice/backend/security/HmacServiceTest.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,58 @@ | ||
package com.faforever.userservice.backend.security | ||
|
||
import io.quarkus.test.junit.QuarkusTest | ||
import jakarta.inject.Inject | ||
import org.junit.jupiter.api.Assertions.assertFalse | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.Test | ||
|
||
@QuarkusTest | ||
class HmacServiceTest { | ||
|
||
@Inject | ||
private lateinit var hmacService: HmacService | ||
|
||
@Test | ||
fun testHmacGenerationAndValidation() { | ||
val message = "message" | ||
val secret = "secret" | ||
val token = hmacService.generateHmacToken(message, secret) | ||
assertTrue(hmacService.isValidHmacToken(token, message, secret, 1)) | ||
} | ||
|
||
@Test | ||
fun testHmacGenerationAndValidationExpires() { | ||
val message = "message" | ||
val secret = "secret" | ||
val token = hmacService.generateHmacToken(message, secret) | ||
|
||
Thread.sleep(1000) | ||
|
||
assertFalse(hmacService.isValidHmacToken(token, message, secret, 1)) | ||
} | ||
|
||
@Test | ||
fun testHmacGenerationAndValidationDifferentMessage() { | ||
val message = "message" | ||
val secret = "secret" | ||
val token = hmacService.generateHmacToken(message, secret) | ||
|
||
Thread.sleep(1000) | ||
|
||
assertFalse(hmacService.isValidHmacToken(token, "differentMessage", secret, 1)) | ||
} | ||
|
||
@Test | ||
fun testHmacGenerationAndValidationMisformattedToken() { | ||
val message = "message" | ||
val secret = "secret" | ||
val token = hmacService.generateHmacToken(message, secret) | ||
|
||
assertFalse(hmacService.isValidHmacToken(token.replace("-", ""), "differentMessage", secret, 1)) | ||
} | ||
|
||
@Test | ||
fun testHmacGenerationAndValidationBadTimestamp() { | ||
assertFalse(hmacService.isValidHmacToken("a-b", "differentMessage", "secret", 1)) | ||
} | ||
} |
Oops, something went wrong.