Skip to content

Commit

Permalink
fix: entity configuration database constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmelati committed Aug 21, 2024
1 parent 8e02389 commit f92b63e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
package com.sphereon.oid.fed.server.federation.controllers

import com.sphereon.oid.fed.persistence.Persistence
import com.sphereon.oid.fed.services.SubordinateService
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController


@RestController
@RequestMapping()
class FederationController {
private val accountQueries = Persistence.accountQueries
private val entityConfigurationStatementQueries = Persistence.entityConfigurationStatementQueries
private val subordinateService = SubordinateService()

@GetMapping("/.well-known/openid-federation")
fun getRootEntityConfigurationStatement(): String {
throw NotImplementedError()
val account = accountQueries.findByUsername("root").executeAsOneOrNull()
?: throw IllegalArgumentException("Account not found")
val entityConfigurationStatement =
entityConfigurationStatementQueries.findLatestByAccountId(account.id).executeAsOneOrNull()
?: throw IllegalArgumentException("Entity Configuration Statement not found")

return entityConfigurationStatement.statement
}

@GetMapping("/{username}/.well-known/openid-federation")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ CREATE TABLE entityConfigurationStatement (
statement TEXT NOT NULL,
expires_at BIGINT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT FK_ParentEntityStatement FOREIGN KEY (account_id) REFERENCES account (id),
UNIQUE (account_id)
CONSTRAINT FK_ParentEntityConfigurationStatement FOREIGN KEY (account_id) REFERENCES account (id)
);

CREATE INDEX entity_statement_account_id_index ON subordinate (account_id);
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ INSERT INTO entityConfigurationStatement (
) VALUES (?, ?, ?) RETURNING *;

findLatestByAccountId:
SELECT * FROM entityConfigurationStatement WHERE account_id = ? ORDER BY created_at DESC LIMIT 1;
SELECT * FROM entityConfigurationStatement WHERE account_id = ? ORDER BY id DESC LIMIT 1;
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AccountService {
}

fun getAccountIdentifier(accountUsername: String): String {
val rootIdentifier = System.getenv("ROOT_IDENTIFIER") ?: "https://www.sphereon.com"
val rootIdentifier = System.getenv("ROOT_IDENTIFIER") ?: "http://localhost:8080"

if (accountUsername == "root") {
return rootIdentifier
Expand All @@ -36,6 +36,7 @@ class AccountService {
}

fun getAccountByUsername(accountUsername: String): Account {
return accountQueries.findByUsername(accountUsername).executeAsOne()
return accountQueries.findByUsername(accountUsername).executeAsOneOrNull()
?: throw IllegalArgumentException(Constants.ACCOUNT_NOT_FOUND)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ import kotlinx.serialization.json.jsonObject
class EntityStatementService {
private val accountService = AccountService()
private val keyService = KeyService()
private val subordinateService = SubordinateService()
private val entityConfigurationStatementQueries = Persistence.entityConfigurationStatementQueries
private val accountQueries = Persistence.accountQueries
private val subordinateQueries = Persistence.subordinateQueries

fun findByUsername(accountUsername: String): EntityConfigurationStatement {
val account = accountService.getAccountByUsername(accountUsername)
val account = accountQueries.findByUsername(accountUsername).executeAsOneOrNull()
?: throw IllegalArgumentException("Account not found")

val keys = keyService.getKeys(accountUsername).map { it.toJwkDTO() }.toTypedArray()

val hasSubordinates = subordinateService.findSubordinatesByAccount(accountUsername).isNotEmpty()
val hasSubordinates = subordinateQueries.findByAccountId(account.id).executeAsList().isNotEmpty()

val identifier = accountService.getAccountIdentifier(account.username)

Expand Down Expand Up @@ -49,17 +51,16 @@ class EntityStatementService {
fun publishByUsername(accountUsername: String): EntityConfigurationStatement {
val account = accountService.getAccountByUsername(accountUsername)

// fetching
val entityConfigurationStatement = findByUsername(accountUsername)
// signing

// publishing
// @TO-DO JWT creation and signing

entityConfigurationStatementQueries.create(
account_id = account.id,
expires_at = entityConfigurationStatement.exp.toLong(),
statement = Json.encodeToString(EntityConfigurationStatement.serializer(), entityConfigurationStatement)
)
).executeAsOne()

throw UnsupportedOperationException("Not implemented")
return entityConfigurationStatement
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class SubordinateService {
}

fun createSubordinate(accountUsername: String, subordinateDTO: CreateSubordinateDTO): Subordinate {
val account = accountQueries.findByUsername(accountUsername).executeAsOne()
val account = accountQueries.findByUsername(accountUsername).executeAsOneOrNull()
?: throw IllegalArgumentException(Constants.ACCOUNT_NOT_FOUND)

val subordinateAlreadyExists =
subordinateQueries.findByAccountIdAndIdentifier(account.id, subordinateDTO.identifier).executeAsList()
Expand Down

0 comments on commit f92b63e

Please sign in to comment.