-
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.
1433: Create user hashing for Koblenz
- Loading branch information
Showing
13 changed files
with
285 additions
and
21 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
10 changes: 10 additions & 0 deletions
10
backend/src/main/kotlin/app/ehrenamtskarte/backend/common/utils/Environment.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,10 @@ | ||
package app.ehrenamtskarte.backend.common.utils | ||
|
||
// This helper class was created to enable mocking getenv in Tests | ||
class Environment { | ||
companion object { | ||
fun getVariable(name: String): String? { | ||
return System.getenv(name) | ||
} | ||
} | ||
} |
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
80 changes: 80 additions & 0 deletions
80
backend/src/main/kotlin/app/ehrenamtskarte/backend/verification/Argon2IdHasher.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,80 @@ | ||
import app.ehrenamtskarte.backend.common.utils.Environment | ||
import app.ehrenamtskarte.backend.common.webservice.KOBLENZ_PEPPER_SYS_ENV | ||
import app.ehrenamtskarte.backend.verification.CanonicalJson | ||
import org.apache.commons.codec.binary.Hex | ||
import org.bouncycastle.crypto.generators.Argon2BytesGenerator | ||
import org.bouncycastle.crypto.params.Argon2Parameters | ||
import java.nio.charset.StandardCharsets | ||
import java.util.Base64 | ||
|
||
class Argon2IdHasher { | ||
companion object { | ||
/** | ||
* Copied from spring-security Argon2EncodingUtils.java licenced under Apache 2.0 | ||
* | ||
* Encodes a raw Argon2-hash and its parameters into the standard Argon2-hash-string | ||
* as specified in the reference implementation | ||
* (https://github.com/P-H-C/phc-winner-argon2/blob/master/src/encoding.c#L244): | ||
* | ||
* {@code $argon2<T>[$v=<num>]$m=<num>,t=<num>,p=<num>$<bin>$<bin>} | ||
**/ | ||
@Throws(IllegalArgumentException::class) | ||
fun encode(hash: ByteArray?, parameters: Argon2Parameters): String? { | ||
val b64encoder: Base64.Encoder = Base64.getEncoder().withoutPadding() | ||
val stringBuilder = StringBuilder() | ||
val type = when (parameters.type) { | ||
Argon2Parameters.ARGON2_d -> "\$argon2d" | ||
Argon2Parameters.ARGON2_i -> "\$argon2i" | ||
Argon2Parameters.ARGON2_id -> "\$argon2id" | ||
else -> throw IllegalArgumentException("Invalid algorithm type: " + parameters.type) | ||
} | ||
stringBuilder.append(type) | ||
stringBuilder.append("\$v=") | ||
.append(parameters.version) | ||
.append("\$m=") | ||
.append(parameters.memory) | ||
.append(",t=") | ||
.append(parameters.iterations) | ||
.append(",p=") | ||
.append(parameters.lanes) | ||
if (parameters.salt != null) { | ||
stringBuilder.append("$").append(b64encoder.encodeToString(parameters.salt)) | ||
} | ||
stringBuilder.append("$").append(b64encoder.encodeToString(hash)) | ||
return stringBuilder.toString() | ||
} | ||
|
||
fun hashUserData(cardInfo: Card.CardInfo): String? { | ||
val canonicalJson = CanonicalJson.messageToMap(cardInfo) | ||
val hashLength = 32 | ||
if (!isCanonicalJsonValid(canonicalJson)) { | ||
throw Exception("Invalid Json input for hashing") | ||
} | ||
|
||
val pepper = Environment.getVariable(KOBLENZ_PEPPER_SYS_ENV) //TODO handle if Null | ||
val pepperByteArray = pepper?.toByteArray(StandardCharsets.UTF_8) | ||
val params = Argon2Parameters.Builder(Argon2Parameters.ARGON2_id) | ||
.withVersion(19) | ||
.withIterations(2) | ||
.withSalt(pepperByteArray) | ||
.withParallelism(1) | ||
.withMemoryAsKB(16) | ||
.build() | ||
|
||
val generator = Argon2BytesGenerator() | ||
generator.init(params) | ||
val result = ByteArray(hashLength) | ||
generator.generateBytes(CanonicalJson.serializeToString(canonicalJson).toCharArray(), result) | ||
return encode(result, params); | ||
} | ||
|
||
private fun isCanonicalJsonValid(canonicalJson: Map<String, Any>): Boolean { | ||
val hasName = canonicalJson.get("1") != null | ||
val hasExtensions = canonicalJson.get("3") as? Map<String, Any> | ||
val hasKoblenzPassExtension = hasExtensions?.get("6") as? Map<String, String> | ||
val hasKoblenzPassId = hasKoblenzPassExtension?.get("1") != null | ||
return hasName && hasKoblenzPassId | ||
} | ||
} | ||
|
||
} |
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
28 changes: 28 additions & 0 deletions
28
backend/src/test/kotlin/app/ehrenamtskarte/backend/verification/Argon2IdHasherTest.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,28 @@ | ||
package app.ehrenamtskarte.backend.verification | ||
import Argon2IdHasher | ||
import app.ehrenamtskarte.backend.common.utils.Environment | ||
import app.ehrenamtskarte.backend.common.webservice.KOBLENZ_PEPPER_SYS_ENV | ||
import app.ehrenamtskarte.backend.helper.CardInfoTestSample | ||
import app.ehrenamtskarte.backend.helper.ExampleCardInfo | ||
import io.mockk.every | ||
import io.mockk.mockkObject | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import io.mockk.mockkStatic | ||
|
||
internal class Argon2IdHasherTest { | ||
@Test | ||
fun isHashingCorrectly() { | ||
mockkObject(Environment) | ||
every {Environment.getVariable(KOBLENZ_PEPPER_SYS_ENV)} returns "123456789ABC" | ||
|
||
assertEquals(Environment.getVariable("KOBLENZ_PEPPER"),"123456789ABC") | ||
|
||
val userData = ExampleCardInfo.get(CardInfoTestSample.KoblenzPass) | ||
|
||
val hash = Argon2IdHasher.hashUserData(userData) | ||
val expectedHash = "\$argon2id\$v=19\$m=16,t=2,p=1\$MTIzNDU2Nzg5QUJD\$xJd35mCTBZT8u+FCGWCnmOtxWzcDTb1Pnt5DHWDap7Y"//This expected output was created with https://argon2.online/ | ||
|
||
assertEquals(expectedHash, hash) | ||
} | ||
} |
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.