-
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
18 changed files
with
396 additions
and
17 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
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
7 changes: 7 additions & 0 deletions
7
...les/federation-server/src/main/kotlin/com/sphereon/oid/fed/server/federation/Constants.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,7 @@ | ||
package com.sphereon.oid.fed.server.federation | ||
|
||
class Constants { | ||
companion object { | ||
const val SUBORDINATE_STATEMENT_NOT_FOUND = "Subordinate Statement not found" | ||
} | ||
} |
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
...ver/src/main/kotlin/com/sphereon/oid/fed/server/federation/services/SubordinateService.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.server.federation.services | ||
|
||
import com.sphereon.oid.fed.persistence.Persistence | ||
import com.sphereon.oid.fed.persistence.Persistence.subordinateStatementQueries | ||
import com.sphereon.oid.fed.persistence.models.Subordinate | ||
import com.sphereon.oid.fed.server.federation.Constants | ||
|
||
|
||
class SubordinateService { | ||
private val accountQueries = Persistence.accountQueries | ||
private val subordinateQueries = Persistence.subordinateQueries | ||
|
||
private fun findSubordinatesByAccount(accountUsername: String): Array<Subordinate> { | ||
val account = accountQueries.findByUsername(accountUsername).executeAsOne() | ||
|
||
return subordinateQueries.findByAccountId(account.id).executeAsList().toTypedArray() | ||
} | ||
|
||
fun findSubordinatesByAccountAsArray(accountUsername: String): Array<String> { | ||
val subordinates = findSubordinatesByAccount(accountUsername) | ||
return subordinates.map { it.identifier }.toTypedArray() | ||
} | ||
|
||
fun fetchSubordinateStatement(iss: String, sub: String): String { | ||
val subordinateStatement = subordinateStatementQueries.findByIssAndSub(iss, sub).executeAsOneOrNull() | ||
?: throw IllegalArgumentException(Constants.SUBORDINATE_STATEMENT_NOT_FOUND) | ||
|
||
return subordinateStatement.statement | ||
} | ||
|
||
} |
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
76 changes: 76 additions & 0 deletions
76
.../src/commonMain/kotlin/com/sphereon/oid/fed/common/builder/SubordinateStatementBuilder.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,76 @@ | ||
package com.sphereon.oid.fed.common.builder | ||
|
||
import com.sphereon.oid.fed.openapi.models.JwkDTO | ||
import com.sphereon.oid.fed.openapi.models.SubordinateStatement | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.builtins.ArraySerializer | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.JsonArray | ||
import kotlinx.serialization.json.JsonObject | ||
import kotlinx.serialization.json.buildJsonObject | ||
|
||
class SubordinateStatementBuilder { | ||
private var iss: String? = null | ||
private var sub: String? = null | ||
private var exp: Int? = null | ||
private var iat: Int? = null | ||
private lateinit var jwks: Array<JwkDTO> | ||
private var metadata: MutableMap<String, JsonObject> = mutableMapOf() | ||
private var metadata_policy: MutableMap<String, JsonObject> = mutableMapOf() | ||
private var metadata_policy_crit: MutableMap<String, JsonObject> = mutableMapOf() | ||
private var constraints: MutableMap<String, JsonObject> = mutableMapOf() | ||
private val crit: MutableList<String> = mutableListOf() | ||
private var source_endpoint: String? = null | ||
|
||
fun iss(iss: String) = apply { this.iss = iss } | ||
fun sub(sub: String) = apply { this.sub = sub } | ||
fun exp(exp: Int) = apply { this.exp = exp } | ||
fun iat(iat: Int) = apply { this.iat = iat } | ||
fun jwks(jwks: JwkDTO) = apply { this.jwks = arrayOf(jwks) } | ||
|
||
fun metadata(metadata: Pair<String, JsonObject>) = apply { | ||
this.metadata[metadata.first] = metadata.second | ||
} | ||
|
||
fun metadataPolicy(metadataPolicy: Pair<String, JsonObject>) = apply { | ||
this.metadata_policy[metadataPolicy.first] = metadataPolicy.second | ||
} | ||
|
||
fun metadataPolicyCrit(metadataPolicyCrit: Pair<String, JsonObject>) = apply { | ||
this.metadata_policy_crit[metadataPolicyCrit.first] = metadataPolicyCrit.second | ||
} | ||
|
||
fun crit(claim: String) = apply { | ||
this.crit.add(claim) | ||
} | ||
|
||
fun sourceEndpoint(sourceEndpoint: String) = apply { | ||
this.source_endpoint = sourceEndpoint | ||
} | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
private fun createJwks(jwks: Array<JwkDTO>): JsonObject { | ||
val jsonArray: JsonArray = | ||
Json.encodeToJsonElement(ArraySerializer(JwkDTO.serializer()), jwks) as JsonArray | ||
|
||
return buildJsonObject { | ||
put("keys", jsonArray) | ||
} | ||
} | ||
|
||
fun build(): SubordinateStatement { | ||
return SubordinateStatement( | ||
iss = iss ?: throw IllegalArgumentException("iss must be provided"), | ||
sub = sub ?: throw IllegalArgumentException("sub must be provided"), | ||
exp = exp ?: throw IllegalArgumentException("exp must be provided"), | ||
iat = iat ?: throw IllegalArgumentException("iat must be provided"), | ||
jwks = createJwks(jwks), | ||
crit = if (crit.isNotEmpty()) crit.toTypedArray() else null, | ||
metadata = JsonObject(metadata), | ||
metadataPolicy = JsonObject(metadata_policy), | ||
metadataPolicyCrit = JsonObject(metadata_policy_crit), | ||
constraints = JsonObject(constraints), | ||
sourceEndpoint = source_endpoint, | ||
) | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
modules/persistence/src/commonMain/sqldelight/com/sphereon/oid/fed/persistence/models/8.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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
CREATE TABLE SubordinateStatement ( | ||
id SERIAL PRIMARY KEY, | ||
subordinate_id INT NOT NULL, | ||
iss TEXT NOT NULL, | ||
sub TEXT NOT NULL, | ||
statement TEXT NOT NULL, | ||
expires_at BIGINT NOT NULL, | ||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||
CONSTRAINT FK_ParentSubordinateStatement FOREIGN KEY (subordinate_id) REFERENCES Subordinate (id) | ||
); | ||
|
||
CREATE INDEX subordinate_statement_account_id_index ON SubordinateStatement (subordinate_id); |
10 changes: 10 additions & 0 deletions
10
modules/persistence/src/commonMain/sqldelight/com/sphereon/oid/fed/persistence/models/9.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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CREATE TABLE SubordinateJwk ( | ||
id SERIAL PRIMARY KEY, | ||
subordinate_id INT NOT NULL, | ||
key TEXT NOT NULL, | ||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||
deleted_at TIMESTAMP, | ||
CONSTRAINT FK_ParentSubordinateJwk FOREIGN KEY (subordinate_id) REFERENCES Subordinate (id) | ||
); | ||
|
||
CREATE INDEX subordinate_jwk_account_id_index ON SubordinateJwk (subordinate_id); |
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
14 changes: 14 additions & 0 deletions
14
...tence/src/commonMain/sqldelight/com/sphereon/oid/fed/persistence/models/SubordinateJwk.sq
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,14 @@ | ||
findBySubordinateId: | ||
SELECT * FROM SubordinateJwk WHERE subordinate_id = ?; | ||
|
||
findById: | ||
SELECT * FROM SubordinateJwk WHERE id = ?; | ||
|
||
create: | ||
INSERT INTO SubordinateJwk ( | ||
subordinate_id, | ||
key | ||
) VALUES (?, ?) RETURNING *; | ||
|
||
delete: | ||
UPDATE SubordinateJwk SET deleted_at = CURRENT_TIMESTAMP WHERE id = ? RETURNING *; |
27 changes: 27 additions & 0 deletions
27
...src/commonMain/sqldelight/com/sphereon/oid/fed/persistence/models/SubordinateStatement.sq
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,27 @@ | ||
findBySubordinateId: | ||
SELECT * FROM SubordinateStatement WHERE subordinate_id = ?; | ||
|
||
findById: | ||
SELECT * FROM SubordinateStatement WHERE id = ?; | ||
|
||
create: | ||
INSERT INTO SubordinateStatement ( | ||
subordinate_id, | ||
iss, | ||
sub, | ||
statement, | ||
expires_at | ||
) VALUES (?, ?, ?, ?, ?) RETURNING *; | ||
|
||
findLatestBySubordinateId: | ||
SELECT * FROM SubordinateStatement WHERE subordinate_id = ? ORDER BY id DESC LIMIT 1; | ||
|
||
findPublishedByAccountId: | ||
SELECT s.* | ||
FROM Subordinate s | ||
JOIN SubordinateStatement ss ON ss.subordinate_id = s.id | ||
WHERE s.account_id = ? | ||
AND s.deleted_at IS NULL; | ||
|
||
findByIssAndSub: | ||
SELECT * FROM SubordinateStatement WHERE iss = ? AND sub = ? ORDER BY id DESC LIMIT 1; |
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.