Skip to content

Commit

Permalink
feat: add third party address to share info of an attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbi08 committed Sep 23, 2024
1 parent 3ad1fa5 commit 3104a0a
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,8 @@ export class AttributesController extends ConsumptionBaseController {
const shareInfo = LocalAttributeShareInfo.from({
peer: parsedParams.peer,
requestReference: parsedParams.requestReference,
sourceAttribute: parsedParams.sourceAttributeId
sourceAttribute: parsedParams.sourceAttributeId,
thirdPartyAddress: parsedParams.thirdPartyAddress
});

const sharedLocalAttributeCopy = await LocalAttribute.fromAttribute(sourceAttribute.content, undefined, shareInfo, parsedParams.attributeId);
Expand All @@ -353,7 +354,8 @@ export class AttributesController extends ConsumptionBaseController {
public async createSharedLocalAttribute(params: ICreateSharedLocalAttributeParams): Promise<LocalAttribute> {
const shareInfo = LocalAttributeShareInfo.from({
peer: params.peer,
requestReference: params.requestReference
requestReference: params.requestReference,
thirdPartyAddress: params.thirdPartyAddress
});
const peerLocalAttribute = LocalAttribute.from({
id: params.id ?? (await ConsumptionIds.attribute.generate()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ export interface CreateSharedLocalAttributeCopyParamsJSON {
sourceAttributeId: string;
peer: string;
requestReference: string;
thirdPartyAddress?: string;
}

export interface ICreateSharedLocalAttributeCopyParams extends ISerializable {
attributeId?: ICoreId;
sourceAttributeId: ICoreId;
peer: ICoreAddress;
requestReference: ICoreId;
thirdPartyAddress?: ICoreAddress;
}

export class CreateSharedLocalAttributeCopyParams extends Serializable implements ICreateSharedLocalAttributeCopyParams {
Expand All @@ -32,6 +34,10 @@ export class CreateSharedLocalAttributeCopyParams extends Serializable implement
@validate()
public requestReference: CoreId;

@serialize()
@validate({ nullable: true })
public thirdPartyAddress: CoreAddress;

public static from(value: ICreateSharedLocalAttributeCopyParams | CreateSharedLocalAttributeCopyParamsJSON): CreateSharedLocalAttributeCopyParams {
return this.fromAny(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ export interface CreateSharedLocalAttributeParamsJSON {
content: IdentityAttributeJSON | RelationshipAttributeJSON;
requestReferece: string;
peer: string;
thirdPartyAddress?: string;
}

export interface ICreateSharedLocalAttributeParams extends ISerializable {
id?: ICoreId; // needs to be optional because sometimes (e.g. when accepting a CreateAttributeRequestItem) the id is not known yet
content: IIdentityAttribute | IRelationshipAttribute;
requestReference: ICoreId;
peer: ICoreAddress;
thirdPartyAddress?: ICoreAddress;
}

export class CreateSharedLocalAttributeParams extends Serializable implements ICreateSharedLocalAttributeParams {
Expand All @@ -33,6 +35,10 @@ export class CreateSharedLocalAttributeParams extends Serializable implements IC
@validate()
public peer: CoreAddress;

@serialize()
@validate({ nullable: true })
public thirdPartyAddress: CoreAddress;

public static from(value: ICreateSharedLocalAttributeParams | CreateSharedLocalAttributeParamsJSON): CreateSharedLocalAttributeParams {
return this.fromAny(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ import { CoreAddress, CoreId, ICoreAddress, ICoreId } from "@nmshd/core-types";
import { nameof } from "ts-simple-nameof";
import { ConsumptionError } from "../../../consumption/ConsumptionError";

/* Either of requestReference or noticicationReference must be set, but not both. */
/* Either of requestReference or notificationReference must be set, but not both. */
export interface LocalAttributeShareInfoJSON {
requestReference?: string;
notificationReference?: string;

peer: string;
sourceAttribute?: string;
thirdPartyAddress?: string;
}

/* Either of requestReference or noticicationReference must be set, but not both. */
/* Either of requestReference or notificationReference must be set, but not both. */
export interface ILocalAttributeShareInfo extends ISerializable {
requestReference?: ICoreId;
notificationReference?: ICoreId;

peer: ICoreAddress;
sourceAttribute?: ICoreId;
thirdPartyAddress?: ICoreAddress;
}

export class LocalAttributeShareInfo extends Serializable implements ILocalAttributeShareInfo {
Expand All @@ -38,6 +38,10 @@ export class LocalAttributeShareInfo extends Serializable implements ILocalAttri
@validate({ nullable: true })
public sourceAttribute?: CoreId;

@serialize()
@validate({ nullable: true })
public thirdPartyAddress: CoreAddress;

public static from(value: ILocalAttributeShareInfo | LocalAttributeShareInfoJSON): LocalAttributeShareInfo {
return super.fromAny(value) as LocalAttributeShareInfo;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,14 @@ export class ReadAttributeRequestItemProcessor extends GenericRequestItemProcess
sharedLocalAttribute = await this.consumptionController.attributes.createSharedLocalAttributeCopy({
sourceAttributeId: CoreId.from(existingSourceAttribute.id),
peer: CoreAddress.from(requestInfo.peer),
requestReference: CoreId.from(requestInfo.id)
requestReference: CoreId.from(requestInfo.id),
thirdPartyAddress: existingSourceAttribute.shareInfo?.peer ? CoreAddress.from(existingSourceAttribute.shareInfo.peer) : undefined
});
return ReadAttributeAcceptResponseItem.from({
result: ResponseItemResult.Accepted,
attributeId: sharedLocalAttribute.id,
attribute: sharedLocalAttribute.content
attribute: sharedLocalAttribute.content,
thirdPartyAddress: sharedLocalAttribute.shareInfo?.thirdPartyAddress
});
}

Expand Down Expand Up @@ -361,7 +363,8 @@ export class ReadAttributeRequestItemProcessor extends GenericRequestItemProcess
id: responseItem.attributeId,
content: responseItem.attribute,
peer: requestInfo.peer,
requestReference: requestInfo.id
requestReference: requestInfo.id,
thirdPartyAddress: responseItem.thirdPartyAddress
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ShareAttributeAcceptResponseItem,
ShareAttributeRequestItem
} from "@nmshd/content";
import { CoreAddress } from "@nmshd/core-types";
import { CoreAddress, CoreError } from "@nmshd/core-types";
import { RelationshipStatus } from "@nmshd/transport";
import _ from "lodash";
import { ConsumptionCoreErrors } from "../../../../consumption/ConsumptionCoreErrors";
Expand Down Expand Up @@ -130,6 +130,18 @@ export class ShareAttributeRequestItemProcessor extends GenericRequestItemProces
if (nonPendingRelationshipsToPeer.length === 0) {
return ValidationResult.error(ConsumptionCoreErrors.requests.cannotShareRelationshipAttributeOfPendingRelationship());
}

if (recipient && !foundAttribute.shareInfo.peer.equals(recipient) && !requestItem.thirdPartyAddress) {
return ValidationResult.error(
ConsumptionCoreErrors.requests.invalidRequestItem(
"The source attribute provided is a relationship attribute. You must provide a third party address that is the original peer when sharing with a third party."
)
);
}

if (requestItem.thirdPartyAddress && !requestItem.thirdPartyAddress.equals(foundAttribute.shareInfo.peer)) {
return ValidationResult.error(new CoreError("The third party address must be the peer of the relationship attribute."));
}
}

if (requestItem.attribute instanceof IdentityAttribute) {
Expand Down Expand Up @@ -172,7 +184,8 @@ export class ShareAttributeRequestItemProcessor extends GenericRequestItemProces
const localAttribute = await this.consumptionController.attributes.createSharedLocalAttribute({
content: requestItem.attribute,
peer: requestInfo.peer,
requestReference: requestInfo.id
requestReference: requestInfo.id,
thirdPartyAddress: requestItem.thirdPartyAddress
});

return ShareAttributeAcceptResponseItem.from({
Expand All @@ -194,7 +207,8 @@ export class ShareAttributeRequestItemProcessor extends GenericRequestItemProces
attributeId: responseItem.attributeId,
sourceAttributeId: requestItem.sourceAttributeId,
peer: requestInfo.peer,
requestReference: requestInfo.id
requestReference: requestInfo.id,
thirdPartyAddress: requestItem.thirdPartyAddress
});
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { serialize, type, validate } from "@js-soft/ts-serval";
import { CoreId, ICoreId } from "@nmshd/core-types";
import { CoreAddress, CoreId, ICoreId } from "@nmshd/core-types";
import { IdentityAttribute, IdentityAttributeJSON, IIdentityAttribute, IRelationshipAttribute, RelationshipAttribute, RelationshipAttributeJSON } from "../../../attributes";
import { AcceptResponseItem, AcceptResponseItemJSON, IAcceptResponseItem } from "../../response";

export interface ReadAttributeAcceptResponseItemJSON extends AcceptResponseItemJSON {
"@type": "ReadAttributeAcceptResponseItem";
attributeId: string;
attribute: IdentityAttributeJSON | RelationshipAttributeJSON;
thirdPartyAddress?: string;
}

export interface IReadAttributeAcceptResponseItem extends IAcceptResponseItem {
attributeId: ICoreId;
attribute: IIdentityAttribute | IRelationshipAttribute;
thirdPartyAddress?: CoreAddress;
}

@type("ReadAttributeAcceptResponseItem")
Expand All @@ -24,6 +26,10 @@ export class ReadAttributeAcceptResponseItem extends AcceptResponseItem implemen
@validate()
public attribute: IdentityAttribute | RelationshipAttribute;

@serialize()
@validate({ nullable: true })
public thirdPartyAddress?: CoreAddress;

public static override from(
value: IReadAttributeAcceptResponseItem | Omit<ReadAttributeAcceptResponseItemJSON, "@type"> | ReadAttributeAcceptResponseItemJSON
): ReadAttributeAcceptResponseItem {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { serialize, type, validate } from "@js-soft/ts-serval";
import { CoreId, ICoreId } from "@nmshd/core-types";
import { CoreAddress, CoreId, ICoreId } from "@nmshd/core-types";
import { RequestItemJSON } from "../..";
import { IdentityAttribute, IdentityAttributeJSON, IIdentityAttribute, IRelationshipAttribute, RelationshipAttribute, RelationshipAttributeJSON } from "../../../attributes";
import { IRequestItem, RequestItem } from "../../RequestItem";
Expand All @@ -8,11 +8,13 @@ export interface ShareAttributeRequestItemJSON extends RequestItemJSON {
"@type": "ShareAttributeRequestItem";
attribute: IdentityAttributeJSON | RelationshipAttributeJSON;
sourceAttributeId: string;
thirdPartyAddress?: string;
}

export interface IShareAttributeRequestItem extends IRequestItem {
attribute: IIdentityAttribute | IRelationshipAttribute;
sourceAttributeId: ICoreId;
thirdPartyAddress?: CoreAddress;
}

@type("ShareAttributeRequestItem")
Expand All @@ -25,6 +27,10 @@ export class ShareAttributeRequestItem extends RequestItem implements IShareAttr
@validate()
public sourceAttributeId: CoreId;

@serialize()
@validate({ nullable: true })
public thirdPartyAddress?: CoreAddress;

public static from(value: IShareAttributeRequestItem | Omit<ShareAttributeRequestItemJSON, "@type"> | ShareAttributeRequestItemJSON): ShareAttributeRequestItem {
return this.fromAny(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface LocalAttributeShareInfoDTO {
notificationReference?: string;
peer: string;
sourceAttribute?: string;
thirdPartyAddress?: string;
}

export enum LocalAttributeDeletionStatus {
Expand Down
Loading

0 comments on commit 3104a0a

Please sign in to comment.