-
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.
feat: create Entity Configuration Statement JWT
- Loading branch information
Showing
31 changed files
with
327 additions
and
333 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
APP_KEY=Nit5tWts42QeCynT1Q476LyStDeSd4xb | ||
|
||
ROOT_IDENTIFIER=http://localhost:8080 | ||
|
||
DATASOURCE_URL=jdbc:postgresql://db:5432/openid-federation-db | ||
DATASOURCE_USER=openid-federation-db-user | ||
DATASOURCE_PASSWORD=openid-federation-db-password | ||
DATASOURCE_DB=openid-federation-db | ||
APP_KEY=Nit5tWts42QeCynT1Q476LyStDeSd4xb | ||
ROOT_IDENTIFIER=http://localhost:8080 | ||
|
||
KMS_PROVIDER=local | ||
|
||
LOCAL_KMS_DATASOURCE_URL=jdbc:postgresql://local-kms-db:5432/openid-federation-local-kms-db | ||
LOCAL_KMS_DATASOURCE_USER=openid-federation-local-kms-db-user | ||
LOCAL_KMS_DATASOURCE_PASSWORD=openid-federation-local-kms-db-password | ||
LOCAL_KMS_DATASOURCE_DB=openid-federation-local-kms-db |
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
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
43 changes: 18 additions & 25 deletions
43
modules/local-kms/src/commonMain/kotlin/com/sphereon/oid/fed/kms/local/LocalKms.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 |
---|---|---|
@@ -1,51 +1,44 @@ | ||
package com.sphereon.oid.fed.kms.local | ||
|
||
import com.sphereon.oid.fed.kms.local.database.LocalKmsDatabase | ||
import com.sphereon.oid.fed.kms.local.encryption.AesEncryption | ||
import com.sphereon.oid.fed.kms.local.extensions.toJwkAdminDto | ||
import com.sphereon.oid.fed.kms.local.jwk.generateKeyPair | ||
import com.sphereon.oid.fed.kms.local.jwt.sign | ||
import com.sphereon.oid.fed.kms.local.jwt.verify | ||
import com.sphereon.oid.fed.openapi.models.JWTHeader | ||
import com.sphereon.oid.fed.openapi.models.Jwk | ||
import kotlinx.serialization.json.* | ||
import com.sphereon.oid.fed.openapi.models.JwkAdminDTO | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.JsonObject | ||
|
||
class LocalKms { | ||
|
||
private val database: LocalKmsDatabase = LocalKmsDatabase() | ||
private val aesEncryption: AesEncryption = AesEncryption() | ||
|
||
fun generateKey(keyId: String) { | ||
fun generateKey(): JwkAdminDTO { | ||
val jwk = generateKeyPair() | ||
database.insertKey(keyId = keyId, privateKey = jwk.toString()) | ||
|
||
database.insertKey( | ||
keyId = jwk.kid!!, | ||
key = aesEncryption.encrypt(Json.encodeToString(Jwk.serializer(), jwk)) | ||
) | ||
|
||
return jwk.toJwkAdminDto() | ||
} | ||
|
||
fun sign(header: JWTHeader, payload: JsonObject, keyId: String): String { | ||
val jwk = database.getKey(keyId) | ||
val jwkString: String = Json.decodeFromString(jwk.private_key) | ||
val jwkObject: Jwk = Json.decodeFromString(jwkString) | ||
|
||
// Adding necessary parameter is header | ||
val jwkObject: Jwk = Json.decodeFromString(aesEncryption.decrypt(jwk.key)) | ||
|
||
val mHeader = header.copy(alg = jwkObject.alg, kid = jwkObject.kid) | ||
|
||
// Adding JWKs object in payload | ||
val mutablePayload = payload.toMutableMap() | ||
mutablePayload["kid"] = JsonPrimitive(jwkObject.kid) | ||
val keyArrayOfJwks = buildJsonObject { | ||
putJsonArray("keys") { | ||
addJsonObject { | ||
put("kty", jwkObject.kty) | ||
put("n", jwkObject.n) | ||
put("e", jwkObject.e) | ||
put("kid", jwkObject.kid) | ||
put("use", jwkObject.use) | ||
} | ||
} | ||
} | ||
mutablePayload["jwks"] = keyArrayOfJwks | ||
val mPayload = JsonObject(mutablePayload) | ||
|
||
return sign(header = mHeader, payload = mPayload, key = jwkObject) | ||
return sign(header = mHeader, payload = payload, key = jwkObject) | ||
} | ||
|
||
fun verify(token: String, jwk: Jwk): Boolean { | ||
return verify(jwt = token, key = jwk) | ||
} | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
...ocal-kms/src/commonMain/kotlin/com/sphereon/oid/fed/kms/local/encryption/AesEncryption.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,31 @@ | ||
package com.sphereon.oid.fed.kms.local.encryption | ||
|
||
import java.util.* | ||
import javax.crypto.Cipher | ||
import javax.crypto.spec.SecretKeySpec | ||
|
||
private const val KEY_SIZE = 32 | ||
private const val ALGORITHM = "AES" | ||
|
||
class AesEncryption { | ||
|
||
private val secretKey: SecretKeySpec = | ||
SecretKeySpec(System.getenv("APP_KEY").padEnd(KEY_SIZE, '0').toByteArray(Charsets.UTF_8), ALGORITHM) | ||
|
||
fun encrypt(data: String): String { | ||
val cipher = Cipher.getInstance(ALGORITHM) | ||
cipher.init(Cipher.ENCRYPT_MODE, secretKey) | ||
|
||
val encryptedValue = cipher.doFinal(data.toByteArray(Charsets.UTF_8)) | ||
return Base64.getEncoder().encodeToString(encryptedValue) | ||
} | ||
|
||
fun decrypt(data: String): String { | ||
val cipher = Cipher.getInstance(ALGORITHM) | ||
cipher.init(Cipher.DECRYPT_MODE, secretKey) | ||
|
||
val decodedValue = Base64.getDecoder().decode(data) | ||
val decryptedValue = cipher.doFinal(decodedValue) | ||
return String(decryptedValue, Charsets.UTF_8) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...local-kms/src/commonMain/kotlin/com/sphereon/oid/fed/kms/local/extensions/JwkExtension.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,20 @@ | ||
package com.sphereon.oid.fed.kms.local.extensions | ||
|
||
import com.sphereon.oid.fed.openapi.models.Jwk | ||
import com.sphereon.oid.fed.openapi.models.JwkAdminDTO | ||
|
||
fun Jwk.toJwkAdminDto(): JwkAdminDTO = JwkAdminDTO( | ||
kid = this.kid, | ||
use = this.use, | ||
crv = this.crv, | ||
n = this.n, | ||
e = this.e, | ||
x = this.x, | ||
y = this.y, | ||
kty = this.kty, | ||
alg = this.alg, | ||
x5u = this.x5u, | ||
x5t = this.x5t, | ||
x5c = this.x5c, | ||
x5tHashS256 = this.x5tS256 | ||
) |
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
4 changes: 2 additions & 2 deletions
4
modules/local-kms/src/commonMain/sqldelight/com/sphereon/oid/fed/kms/local/models/1.sqm
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
CREATE TABLE Keys ( | ||
id TEXT PRIMARY KEY, | ||
private_key TEXT NOT NULL, | ||
key TEXT NOT NULL, | ||
deleted_at TIMESTAMP | ||
); | ||
); |
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.