Skip to content

Commit 44ad5f7

Browse files
committed
Refactored api key verifier
1 parent 1c03ecd commit 44ad5f7

File tree

4 files changed

+26
-29
lines changed

4 files changed

+26
-29
lines changed

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ subprojects {
103103

104104
spotless {
105105
kotlin {
106-
ktlint()
106+
ktlint("com.github.shyiko:ktlint:0.31.0")
107107
}
108108
kotlinGradle {
109109
target("*.gradle.kts", "additionalScripts/*.gradle.kts")

ktor-utils/build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ val ktorVersion = "1.2.0"
33
dependencies {
44
implementation("io.ktor:ktor-server:$ktorVersion")
55
implementation("io.ktor:ktor-auth:$ktorVersion")
6+
implementation("commons-codec:commons-codec:1.12")
67
testImplementation("io.ktor:ktor-server-test-host:$ktorVersion")
78
}
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
11
package no.nav.dagpenger.ktor.auth
22

3+
import org.apache.commons.codec.binary.Hex
4+
import java.nio.charset.StandardCharsets
35
import javax.crypto.Mac
46
import javax.crypto.spec.SecretKeySpec
57

6-
class ApiKeyVerifier(private val secret: ByteArray) {
8+
class ApiKeyVerifier(private val secret: String) {
79

810
private val algorithm = "HmacSHA256"
911

10-
fun verify(apiKey: ByteArray, expectedApiKey: ByteArray): Boolean {
11-
12-
val hmac = generate(apiKey)
13-
14-
if (hmac.size != expectedApiKey.size) return false
15-
var result = 0
16-
for (i in 0 until hmac.size) {
17-
result = result.or(hmac[i].toInt().xor(expectedApiKey[i].toInt()))
18-
}
12+
fun verify(apiKey: String, expectedApiKey: String): Boolean {
13+
return apiKey == generate(expectedApiKey)
14+
}
1915

20-
return result == 0
16+
fun generate(apiKey: String): String {
17+
return String(Hex.encodeHex(generateDigest(apiKey.toByteArray(StandardCharsets.UTF_8))))
2118
}
2219

23-
fun generate(apiKey: ByteArray): ByteArray {
24-
val keySpec = SecretKeySpec(secret, algorithm)
20+
private fun generateDigest(apiKey: ByteArray): ByteArray {
21+
val secret = SecretKeySpec(secret.toByteArray(StandardCharsets.UTF_8), algorithm)
2522
val mac = Mac.getInstance(algorithm)
26-
mac.init(keySpec)
27-
28-
val hmac = mac.doFinal(apiKey)
29-
return hmac
23+
mac.init(secret)
24+
return mac.doFinal(apiKey)
3025
}
31-
}
26+
}
27+

ktor-utils/src/test/kotlin/no/nav/dagpenger/ktor/auth/ApiKeyVerifierTest.kt

+10-10
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ internal class ApiKeyVerifierTest {
88

99
@Test
1010
fun `Should be able to verify api key with same secret`() {
11-
val verifier = ApiKeyVerifier("secret".toByteArray())
12-
val enc = verifier.generate("apikey".toByteArray())
13-
assertTrue { verifier.verify("apikey".toByteArray(), enc) }
11+
val verifier = ApiKeyVerifier("secret")
12+
val enc = verifier.generate("apikey")
13+
assertTrue { verifier.verify( enc, "apikey") }
1414
}
1515

1616
@Test
1717
fun `Should not be able to verify api if key has changed key with same secret`() {
18-
val verifier = ApiKeyVerifier("secret".toByteArray())
19-
val enc = verifier.generate("apikey".toByteArray())
20-
assertFalse { verifier.verify("Apikey".toByteArray(), enc) }
18+
val verifier = ApiKeyVerifier("secret")
19+
val enc = verifier.generate("apikey")
20+
assertFalse { verifier.verify( enc, "Apikey") }
2121
}
2222

2323
@Test
2424
fun `Should not be able to verify api if with secret changed`() {
25-
val generator = ApiKeyVerifier("another secret".toByteArray())
26-
val enc = generator.generate("apikey".toByteArray())
27-
val verifier = ApiKeyVerifier("secret".toByteArray())
28-
assertFalse { verifier.verify("apikey".toByteArray(), enc) }
25+
val generator = ApiKeyVerifier("another secret")
26+
val enc = generator.generate("apikey")
27+
val verifier = ApiKeyVerifier("secret")
28+
assertFalse { verifier.verify(enc, "apikey") }
2929
}
3030
}

0 commit comments

Comments
 (0)