Skip to content

Commit

Permalink
feat: adapt reference
Browse files Browse the repository at this point in the history
  • Loading branch information
Magnus-Kuhn committed Sep 24, 2024
1 parent 32c5326 commit 2b79db8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
38 changes: 23 additions & 15 deletions packages/transport/src/core/Reference.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
import { ISerializable, Serializable, serialize, validate, ValidationError } from "@js-soft/ts-serval";
import { CoreAddress, CoreId, ICoreAddress, ICoreId } from "@nmshd/core-types";
import { CoreId, ICoreId } from "@nmshd/core-types";
import { CoreBuffer, CryptoSecretKey, ICryptoSecretKey } from "@nmshd/crypto";
import { CoreIdHelper } from "./CoreIdHelper";
import { TransportCoreErrors } from "./TransportCoreErrors";

export interface IReference extends ISerializable {
id: ICoreId;
backboneBaseUrl?: string;
key: ICryptoSecretKey;
backbone: string;
forIdentity?: ICoreAddress;
forIdentityTruncated?: string;
passwordType?: number;
}

export class Reference extends Serializable implements IReference {
@validate()
@validate({ regExp: new RegExp("^[A-Za-z0-9]{20}$") })
@serialize()
public id: CoreId;

@validate({ nullable: true })
@serialize()
public backboneBaseUrl?: string;

@validate()
@serialize()
public key: CryptoSecretKey;

@validate({ nullable: true })
@validate({ nullable: true, regExp: new RegExp("^[0-9a-f]{4}$") })
@serialize()
public forIdentity?: CoreAddress;
public forIdentityTruncated?: string;

@validate()
@validate({ nullable: true, min: 1, max: 12, customValidator: (v) => (Number.isInteger(v) ? "must be an integer" : undefined) })
@serialize()
public backbone: string;
public passwordType?: number;

public truncate(): string {
const idPart = this.backboneBaseUrl ? `${this.id.toString()}@${this.backboneBaseUrl}` : this.id.toString();
const truncatedReference = CoreBuffer.fromUtf8(
`${this.id.toString()}|${this.key.algorithm}|${this.key.secretKey.toBase64URL()}|${this.forIdentity?.toString() ?? ""}|${this.backbone}`
`${idPart}|${this.key.algorithm}|${this.key.secretKey.toBase64URL()}|${this.forIdentityTruncated}|${this.passwordType?.toString()}`
);
return truncatedReference.toBase64URL();
}
Expand All @@ -39,26 +45,28 @@ export class Reference extends Serializable implements IReference {
const truncatedBuffer = CoreBuffer.fromBase64URL(value);
const splitted = truncatedBuffer.toUtf8().split("|");

if (splitted.length !== 5) {
if (![3, 5].includes(splitted.length)) {
throw TransportCoreErrors.general.invalidTruncatedReference();
}

try {
const id = CoreId.from(splitted[0]);
const idPart = splitted[0];
const [id, backboneBaseUrl] = idPart.split("@");
const alg = parseInt(splitted[1]);
const key = splitted[2];
const forIdentity = splitted[3] ? CoreAddress.from(splitted[3]) : undefined;
const backbone = splitted[4];
const forIdentityTruncated = splitted[3];
const passwordType = parseInt(splitted[4]);
const secretKey = CryptoSecretKey.from({
algorithm: alg,
secretKey: CoreBuffer.fromBase64URL(key)
});

return this.from({
id: CoreId.from(id),
backboneBaseUrl,
key: secretKey,
forIdentity,
backbone
forIdentityTruncated,
passwordType
});
} catch (e) {
throw TransportCoreErrors.general.invalidTruncatedReference();
Expand Down
2 changes: 1 addition & 1 deletion packages/transport/src/modules/files/local/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class File extends CoreSynchronizable implements IFile {
throw new TransportError(`The cache of RelationshipTemplate with id "${this.id.toString()}" is empty.`);
}

return FileReference.from({ id: this.id, key: this.secretKey, backbone: this.cache.createdBy.toString().split(":")[2] });
return FileReference.from({ id: this.id, key: this.secretKey, backboneBaseUrl: this.cache.createdBy.toString().split(":")[2] });
}

public truncate(): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class RelationshipTemplate extends CoreSynchronizable implements IRelatio
throw new TransportError(`The cache of RelationshipTemplate with id "${this.id.toString()}" is empty.`);
}

return RelationshipTemplateReference.from({ id: this.id, key: this.secretKey, backbone: this.cache.createdBy.toString().split(":")[2] });
return RelationshipTemplateReference.from({ id: this.id, key: this.secretKey, backboneBaseUrl: this.cache.createdBy.toString().split(":")[2] });
}

public truncate(): string {
Expand Down
2 changes: 1 addition & 1 deletion packages/transport/src/modules/tokens/local/Token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class Token extends CoreSynchronizable implements IToken {
if (!this.cache) {
throw new TransportError(`The cache of Token with id "${this.id.toString()}" is empty.`);
}
return TokenReference.from({ id: this.id, key: this.secretKey, backbone: this.cache.createdBy.toString().split(":")[2] });
return TokenReference.from({ id: this.id, key: this.secretKey, backboneBaseUrl: this.cache.createdBy.toString().split(":")[2] });
}

public truncate(): string {
Expand Down

0 comments on commit 2b79db8

Please sign in to comment.