Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PIN-4112 Added fields related to users #217

Merged
merged 10 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ final case class PersistentClient(
purposes: Seq[PersistentClientStatesChain],
description: Option[String],
relationships: Set[UUID],
users: Set[UUID],
kind: PersistentClientKind,
createdAt: OffsetDateTime
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import java.time.OffsetDateTime
import java.util.UUID

final case class PersistentKey(
relationshipId: UUID,
relationshipId: Option[UUID],
userId: Option[UUID],
kid: String,
name: String,
encodedPem: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,71 @@ object AuthorizationEventsSerde {
val authToJson: PartialFunction[ProjectableEvent, JsValue] = {
case x: PurposeStateUpdated => x.toJson
case x: RelationshipAdded => x.toJson
nttdata-rtorsoli marked this conversation as resolved.
Show resolved Hide resolved
case x: UserAdded => x.toJson
case x: AgreementStateUpdated => x.toJson
case x: ClientAdded => x.toJson
case x: ClientPurposeAdded => x.toJson
case x: ClientPurposeRemoved => x.toJson
case x: RelationshipRemoved => x.toJson
nttdata-rtorsoli marked this conversation as resolved.
Show resolved Hide resolved
case x: UserRemoved => x.toJson
case x: AgreementAndEServiceStatesUpdated => x.toJson
case x: KeyDeleted => x.toJson
case x: ClientDeleted => x.toJson
case x: KeysAdded => x.toJson
case x: EServiceStateUpdated => x.toJson
case x: KeyRelationshipToUserMigrated => x.toJson
}

private val purposeStateUpdated: String = "purpose-state-updated"
private val relationshipAdded: String = "relationship-added"
nttdata-rtorsoli marked this conversation as resolved.
Show resolved Hide resolved
private val userAdded: String = "user-added"
private val agreementStateUpdated: String = "agreement-state-updated"
private val clientAdded: String = "client-added"
private val clientPurposeAdded: String = "client-purpose-added"
private val clientPurposeRemoved: String = "client-purpose-removed"
private val relationshipRemoved: String = "relationship-removed"
nttdata-rtorsoli marked this conversation as resolved.
Show resolved Hide resolved
private val userRemoved: String = "user-removed"
private val agreementAndEServiceStatesUpdated: String = "agreement-and-e-service-states-updated"
private val keyDeleted: String = "key-deleted"
private val clientDeleted: String = "client-deleted"
private val keysAdded: String = "keys-added"
private val eServiceStateUpdated: String = "e-service-state-updated"
private val keyRelationshipToUserMigrated: String = "key-relationship-to-user-migrated"

val jsonToAuth: PartialFunction[String, JsValue => ProjectableEvent] = {
case `purposeStateUpdated` => _.convertTo[PurposeStateUpdated]
case `relationshipAdded` => _.convertTo[RelationshipAdded]
nttdata-rtorsoli marked this conversation as resolved.
Show resolved Hide resolved
case `userAdded` => _.convertTo[UserAdded]
case `agreementStateUpdated` => _.convertTo[AgreementStateUpdated]
case `clientAdded` => _.convertTo[ClientAdded]
case `clientPurposeAdded` => _.convertTo[ClientPurposeAdded]
case `clientPurposeRemoved` => _.convertTo[ClientPurposeRemoved]
case `relationshipRemoved` => _.convertTo[RelationshipRemoved]
nttdata-rtorsoli marked this conversation as resolved.
Show resolved Hide resolved
case `userRemoved` => _.convertTo[UserRemoved]
case `agreementAndEServiceStatesUpdated` => _.convertTo[AgreementAndEServiceStatesUpdated]
case `keyDeleted` => _.convertTo[KeyDeleted]
case `clientDeleted` => _.convertTo[ClientDeleted]
case `keysAdded` => _.convertTo[KeysAdded]
case `eServiceStateUpdated` => _.convertTo[EServiceStateUpdated]
case `keyRelationshipToUserMigrated` => _.convertTo[KeyRelationshipToUserMigrated]
}

def getKind(e: Event): String = e match {
case _: PurposeStateUpdated => purposeStateUpdated
case _: RelationshipAdded => relationshipAdded
nttdata-rtorsoli marked this conversation as resolved.
Show resolved Hide resolved
case _: UserAdded => userAdded
case _: AgreementStateUpdated => agreementStateUpdated
case _: ClientAdded => clientAdded
case _: ClientPurposeAdded => clientPurposeAdded
case _: ClientPurposeRemoved => clientPurposeRemoved
case _: RelationshipRemoved => relationshipRemoved
nttdata-rtorsoli marked this conversation as resolved.
Show resolved Hide resolved
case _: UserRemoved => userRemoved
case _: AgreementAndEServiceStatesUpdated => agreementAndEServiceStatesUpdated
case _: KeyDeleted => keyDeleted
case _: ClientDeleted => clientDeleted
case _: KeysAdded => keysAdded
case _: EServiceStateUpdated => eServiceStateUpdated
case _: KeyRelationshipToUserMigrated => keyRelationshipToUserMigrated
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@ object JsonFormats {
jsonFormat3(PersistentClientPurposeDetails.apply)

implicit val pcscFormat: RootJsonFormat[PersistentClientStatesChain] = jsonFormat4(PersistentClientStatesChain.apply)
implicit val pcFormat: RootJsonFormat[PersistentClient] = jsonFormat8(PersistentClient.apply)
implicit val pcFormat: RootJsonFormat[PersistentClient] = jsonFormat9(PersistentClient.apply)

implicit val pkFormat: RootJsonFormat[PersistentKey] = jsonFormat7(PersistentKey.apply)
implicit val pkFormat: RootJsonFormat[PersistentKey] = jsonFormat8(PersistentKey.apply)

implicit val kaFormat: RootJsonFormat[KeysAdded] = jsonFormat2(KeysAdded.apply)
implicit val kdFormat: RootJsonFormat[KeyDeleted] = jsonFormat3(KeyDeleted.apply)
implicit val caFormat: RootJsonFormat[ClientAdded] = jsonFormat1(ClientAdded.apply)
implicit val cdFormat: RootJsonFormat[ClientDeleted] = jsonFormat1(ClientDeleted.apply)
implicit val uaFormat: RootJsonFormat[UserAdded] = jsonFormat2(UserAdded.apply)
implicit val urFormat: RootJsonFormat[UserRemoved] = jsonFormat2(UserRemoved.apply)
implicit val raFormat: RootJsonFormat[RelationshipAdded] = jsonFormat2(RelationshipAdded.apply)
implicit val rrFormat: RootJsonFormat[RelationshipRemoved] = jsonFormat2(RelationshipRemoved.apply)
implicit val cpaFormat: RootJsonFormat[ClientPurposeAdded] = jsonFormat2(ClientPurposeAdded.apply)
Expand All @@ -76,6 +78,9 @@ object JsonFormats {
implicit val aaesuFormat: RootJsonFormat[AgreementAndEServiceStatesUpdated] = jsonFormat8(
AgreementAndEServiceStatesUpdated.apply
)
implicit val kuFormat: RootJsonFormat[KeyRelationshipToUserMigrated] = jsonFormat3(
KeyRelationshipToUserMigrated.apply
)
implicit val psuFormat: RootJsonFormat[PurposeStateUpdated] = jsonFormat3(PurposeStateUpdated.apply)

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ sealed trait Event extends Persistable with ProjectableEvent

final case class KeysAdded(clientId: String, keys: Keys) extends Event
final case class KeyDeleted(clientId: String, keyId: String, deactivationTimestamp: OffsetDateTime) extends Event
final case class KeyRelationshipToUserMigrated(clientId: String, keyId: String, userId: UUID) extends Event

final case class ClientAdded(client: PersistentClient) extends Event
final case class ClientDeleted(clientId: String) extends Event

final case class RelationshipAdded(client: PersistentClient, relationshipId: UUID) extends Event
final case class RelationshipRemoved(clientId: String, relationshipId: String) extends Event

final case class UserAdded(client: PersistentClient, userId: UUID) extends Event
final case class UserRemoved(client: PersistentClient, userId: UUID) extends Event

final case class ClientPurposeAdded(clientId: String, statesChain: PersistentClientStatesChain) extends Event
final case class ClientPurposeRemoved(clientId: String, purposeId: String) extends Event

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ trait ItSpecHelper
ActorSystem(Behaviors.ignore[Any], name = system.name, config = system.settings.config)
implicit val executionContext: ExecutionContextExecutor = httpSystem.executionContext
val classicSystem: actor.ActorSystem = httpSystem.classicSystem
final val timestamp = OffsetDateTime.of(2022, 12, 31, 11, 22, 33, 44, ZoneOffset.UTC)

override def startServer(): Unit = {
val persistentEntity: Entity[Command, ShardingEnvelope[Command]] =
Expand Down Expand Up @@ -217,11 +216,11 @@ trait ItSpecHelper
Await.result(Unmarshal(response).to[Seq[Key]], Duration.Inf)
}

def addRelationship(clientId: UUID, relationshipId: UUID): Client = {
val requestBody = s"""{"relationshipId": "${relationshipId.toString}"}"""
def addUser(clientId: UUID, userId: UUID): Client = {
val requestBody = s"""{"userId": "${userId.toString}"}"""

val response = request(
uri = s"$serviceURL/clients/${clientId.toString}/relationships",
uri = s"$serviceURL/clients/${clientId.toString}/users",
method = HttpMethods.POST,
data = Some(requestBody)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class CqrsProjectionSpec extends ScalaTestWithActorTestKit(ItSpecConfiguration.c
val relationshipId = UUID.randomUUID()

createClient(clientId, consumerId)
val client = addRelationship(clientId, relationshipId)
val client = addUser(clientId, relationshipId)

val expectedData = client.toPersistent

Expand All @@ -63,7 +63,7 @@ class CqrsProjectionSpec extends ScalaTestWithActorTestKit(ItSpecConfiguration.c
val relationshipId = UUID.randomUUID()

createClient(clientId, consumerId)
addRelationship(clientId, relationshipId)
addUser(clientId, relationshipId)
request(uri = s"$serviceURL/clients/$clientId/relationships/$relationshipId", method = HttpMethods.DELETE)

val persisted = findOne[PersistentClient](clientId.toString).futureValue
Expand All @@ -74,11 +74,12 @@ class CqrsProjectionSpec extends ScalaTestWithActorTestKit(ItSpecConfiguration.c
"succeed for event KeysAdded" in {
val clientId = UUID.randomUUID()
val consumerId = UUID.randomUUID()
val relationshipId = UUID.randomUUID()
val relationshipId = Some(UUID.randomUUID())
val userId = UUID.randomUUID()

val keySeed = Key(
kid = "kid",
relationshipId = relationshipId,
userId = userId,
encodedPem = generateEncodedKey(),
use = KeyUse.SIG,
algorithm = "123",
Expand All @@ -87,7 +88,7 @@ class CqrsProjectionSpec extends ScalaTestWithActorTestKit(ItSpecConfiguration.c
)

createClient(clientId, consumerId)
addRelationship(clientId, relationshipId)
addUser(clientId, userId)
val keysResponse = createKey(clientId, keySeed)

val persistedClient = findOne[JsObject](clientId.toString).futureValue
Expand All @@ -100,6 +101,7 @@ class CqrsProjectionSpec extends ScalaTestWithActorTestKit(ItSpecConfiguration.c
val expected = Seq(
PersistentKey(
relationshipId = relationshipId,
userId = Some(userId),
kid = keysResponse.head.kid,
name = keySeed.name,
encodedPem = keySeed.encodedPem,
Expand All @@ -118,7 +120,7 @@ class CqrsProjectionSpec extends ScalaTestWithActorTestKit(ItSpecConfiguration.c
val relationshipId = UUID.randomUUID()

createClient(clientId, consumerId)
addRelationship(clientId, relationshipId)
addUser(clientId, relationshipId)
val keysResponse1 = createKey(clientId, relationshipId)
val keysResponse2 = createKey(clientId, relationshipId)
val kid1 = keysResponse1.head.kid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ object ClientAdapters {
def toPersistent: PersistentClient =
PersistentClient(
id = p.id,
users = p.users,
consumerId = p.consumerId,
name = p.name,
purposes = p.purposes.map(_.toPersistent),
description = p.description,
relationships = p.relationships,
relationships = Set.empty,
kind = PersistentClientKind.fromApi(p.kind),
createdAt = p.createdAt
)
Expand Down
1 change: 1 addition & 0 deletions src/main/protobuf/v1/client.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ message PersistentClientV1 {
repeated ClientPurposesEntryV1 purposes = 7;
required ClientKindV1 kind = 8;
optional int64 createdAt = 9;
repeated string users = 10;
}

message ClientPurposesEntryV1 {
Expand Down
16 changes: 16 additions & 0 deletions src/main/protobuf/v1/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ message KeyDeletedV1 {
required string deactivationTimestamp = 3;
}

message KeyRelationshipToUserMigratedV1 {
required string clientId = 1;
required string keyId = 2;
required string userId = 3;
}

message ClientAddedV1 {
required PersistentClientV1 client = 1;
}
Expand All @@ -38,6 +44,16 @@ message RelationshipRemovedV1 {
required string relationshipId = 2;
}

message UserAddedV1 {
required PersistentClientV1 client = 1;
required string userId = 2;
}

message UserRemovedV1 {
required PersistentClientV1 client = 1;
required string userId = 2;
}

message ClientPurposeAddedV1 {
required string clientId = 1;
required ClientStatesChainV1 statesChain = 3;
Expand Down
3 changes: 2 additions & 1 deletion src/main/protobuf/v1/key.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ message PersistentKeyEntryV1 {
}

message PersistentKeyV1 {
required string relationshipId = 1;
optional string relationshipId = 1;
required string kid = 2;
required string encodedPem = 3;
required KeyUseV1 use = 4;
required string algorithm = 5;
required string createdAt = 6;
required string name = 7;
optional string userId = 8;
}

enum KeyUseV1 {
Expand Down
Loading
Loading