From 989d6797e885561112b9a35486fa68e68b566e61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= Date: Mon, 23 Sep 2024 12:57:53 +0200 Subject: [PATCH 01/11] refactor: update anonymous tokens --- .../anonymous/AnonymousTokensFacade.ts | 20 +++-------- ...Reference.ts => LoadPeerTokenAnonymous.ts} | 10 +++--- .../LoadPeerTokenAnonymousByIdAndKey.ts | 35 ------------------- .../src/useCases/anonymous/tokens/index.ts | 3 +- 4 files changed, 10 insertions(+), 58 deletions(-) rename packages/runtime/src/useCases/anonymous/tokens/{LoadPeerTokenAnonymousByTruncatedReference.ts => LoadPeerTokenAnonymous.ts} (71%) delete mode 100644 packages/runtime/src/useCases/anonymous/tokens/LoadPeerTokenAnonymousByIdAndKey.ts diff --git a/packages/runtime/src/extensibility/facades/anonymous/AnonymousTokensFacade.ts b/packages/runtime/src/extensibility/facades/anonymous/AnonymousTokensFacade.ts index a5c57353a..088fc82a0 100644 --- a/packages/runtime/src/extensibility/facades/anonymous/AnonymousTokensFacade.ts +++ b/packages/runtime/src/extensibility/facades/anonymous/AnonymousTokensFacade.ts @@ -1,24 +1,12 @@ import { ApplicationError, Result } from "@js-soft/ts-utils"; import { Inject } from "typescript-ioc"; import { TokenDTO } from "../../../types"; -import { - LoadPeerTokenAnonymousByIdAndKeyRequest, - LoadPeerTokenAnonymousByIdAndKeyUseCase, - LoadPeerTokenAnonymousByTruncatedReferenceRequest, - LoadPeerTokenAnonymousByTruncatedReferenceUseCase -} from "../../../useCases"; +import { LoadPeerTokenAnonymousRequest, LoadPeerTokenAnonymousUseCase } from "../../../useCases"; export class AnonymousTokensFacade { - public constructor( - @Inject private readonly loadPeerTokenByTruncatedReferenceUseCase: LoadPeerTokenAnonymousByTruncatedReferenceUseCase, - @Inject private readonly loadPeerTokenByIdAndKeyUseCase: LoadPeerTokenAnonymousByIdAndKeyUseCase - ) {} + public constructor(@Inject private readonly loadPeerTokenUseCase: LoadPeerTokenAnonymousUseCase) {} - public async loadPeerTokenByTruncatedReference(request: LoadPeerTokenAnonymousByTruncatedReferenceRequest): Promise> { - return await this.loadPeerTokenByTruncatedReferenceUseCase.execute(request); - } - - public async loadPeerTokenByIdAndKey(request: LoadPeerTokenAnonymousByIdAndKeyRequest): Promise> { - return await this.loadPeerTokenByIdAndKeyUseCase.execute(request); + public async loadPeerToken(request: LoadPeerTokenAnonymousRequest): Promise> { + return await this.loadPeerTokenUseCase.execute(request); } } diff --git a/packages/runtime/src/useCases/anonymous/tokens/LoadPeerTokenAnonymousByTruncatedReference.ts b/packages/runtime/src/useCases/anonymous/tokens/LoadPeerTokenAnonymous.ts similarity index 71% rename from packages/runtime/src/useCases/anonymous/tokens/LoadPeerTokenAnonymousByTruncatedReference.ts rename to packages/runtime/src/useCases/anonymous/tokens/LoadPeerTokenAnonymous.ts index 108ebab54..3eec7c492 100644 --- a/packages/runtime/src/useCases/anonymous/tokens/LoadPeerTokenAnonymousByTruncatedReference.ts +++ b/packages/runtime/src/useCases/anonymous/tokens/LoadPeerTokenAnonymous.ts @@ -5,17 +5,17 @@ import { TokenDTO } from "../../../types"; import { SchemaRepository, SchemaValidator, TokenReferenceString, UseCase } from "../../common"; import { TokenMapper } from "../../transport/tokens/TokenMapper"; -export interface LoadPeerTokenAnonymousByTruncatedReferenceRequest { +export interface LoadPeerTokenAnonymousRequest { reference: TokenReferenceString; } -class Validator extends SchemaValidator { +class Validator extends SchemaValidator { public constructor(@Inject schemaRepository: SchemaRepository) { - super(schemaRepository.getSchema("LoadPeerTokenAnonymousByTruncatedReferenceRequest")); + super(schemaRepository.getSchema("LoadPeerTokenAnonymousRequest")); } } -export class LoadPeerTokenAnonymousByTruncatedReferenceUseCase extends UseCase { +export class LoadPeerTokenAnonymousUseCase extends UseCase { public constructor( @Inject private readonly anonymousTokenController: AnonymousTokenController, @Inject validator: Validator @@ -23,7 +23,7 @@ export class LoadPeerTokenAnonymousByTruncatedReferenceUseCase extends UseCase> { + protected async executeInternal(request: LoadPeerTokenAnonymousRequest): Promise> { const createdToken = await this.anonymousTokenController.loadPeerTokenByTruncated(request.reference); return Result.ok(TokenMapper.toTokenDTO(createdToken, true)); } diff --git a/packages/runtime/src/useCases/anonymous/tokens/LoadPeerTokenAnonymousByIdAndKey.ts b/packages/runtime/src/useCases/anonymous/tokens/LoadPeerTokenAnonymousByIdAndKey.ts deleted file mode 100644 index 65e853332..000000000 --- a/packages/runtime/src/useCases/anonymous/tokens/LoadPeerTokenAnonymousByIdAndKey.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { Result } from "@js-soft/ts-utils"; -import { CoreId } from "@nmshd/core-types"; -import { CryptoSecretKey } from "@nmshd/crypto"; -import { AnonymousTokenController } from "@nmshd/transport"; -import { Inject } from "typescript-ioc"; -import { TokenDTO } from "../../../types"; -import { SchemaRepository, SchemaValidator, TokenIdString, UseCase } from "../../common"; -import { TokenMapper } from "../../transport/tokens/TokenMapper"; - -export interface LoadPeerTokenAnonymousByIdAndKeyRequest { - id: TokenIdString; - secretKey: string; -} - -class Validator extends SchemaValidator { - public constructor(@Inject schemaRepository: SchemaRepository) { - super(schemaRepository.getSchema("LoadPeerTokenAnonymousByIdAndKeyRequest")); - } -} - -export class LoadPeerTokenAnonymousByIdAndKeyUseCase extends UseCase { - public constructor( - @Inject private readonly anonymousTokenController: AnonymousTokenController, - @Inject validator: Validator - ) { - super(validator); - } - - protected async executeInternal(request: LoadPeerTokenAnonymousByIdAndKeyRequest): Promise> { - const key = CryptoSecretKey.fromBase64(request.secretKey); - const createdToken = await this.anonymousTokenController.loadPeerToken(CoreId.from(request.id), key); - - return Result.ok(TokenMapper.toTokenDTO(createdToken, true)); - } -} diff --git a/packages/runtime/src/useCases/anonymous/tokens/index.ts b/packages/runtime/src/useCases/anonymous/tokens/index.ts index d934aa8f0..795bfa08f 100644 --- a/packages/runtime/src/useCases/anonymous/tokens/index.ts +++ b/packages/runtime/src/useCases/anonymous/tokens/index.ts @@ -1,2 +1 @@ -export * from "./LoadPeerTokenAnonymousByIdAndKey"; -export * from "./LoadPeerTokenAnonymousByTruncatedReference"; +export * from "./LoadPeerTokenAnonymous"; From 049215a2bf04d964b1bfd7b89a0ef4b77778b17f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= Date: Mon, 23 Sep 2024 12:58:05 +0200 Subject: [PATCH 02/11] refactor: update files --- .../useCases/transport/files/GetOrLoadFile.ts | 70 ++----------------- 1 file changed, 4 insertions(+), 66 deletions(-) diff --git a/packages/runtime/src/useCases/transport/files/GetOrLoadFile.ts b/packages/runtime/src/useCases/transport/files/GetOrLoadFile.ts index a07f0f4ea..175f197be 100644 --- a/packages/runtime/src/useCases/transport/files/GetOrLoadFile.ts +++ b/packages/runtime/src/useCases/transport/files/GetOrLoadFile.ts @@ -4,72 +4,19 @@ import { CryptoSecretKey } from "@nmshd/crypto"; import { AccountController, FileController, Token, TokenContentFile, TokenController } from "@nmshd/transport"; import { Inject } from "typescript-ioc"; import { FileDTO } from "../../../types"; -import { - Base64ForIdPrefix, - FileIdString, - FileReferenceString, - JsonSchema, - RuntimeErrors, - SchemaRepository, - SchemaValidator, - TokenReferenceString, - UseCase, - ValidationFailure, - ValidationResult -} from "../../common"; +import { Base64ForIdPrefix, FileReferenceString, RuntimeErrors, SchemaRepository, SchemaValidator, TokenReferenceString, UseCase } from "../../common"; import { FileMapper } from "./FileMapper"; -export interface GetOrLoadFileViaSecretRequest { - id: FileIdString; - /** - * @minLength 10 - */ - secretKey: string; -} - /** * @errorMessage token / file reference invalid */ -export interface GetOrLoadFileViaReferenceRequest { +export interface GetOrLoadFileRequest { reference: TokenReferenceString | FileReferenceString; } -export type GetOrLoadFileRequest = GetOrLoadFileViaSecretRequest | GetOrLoadFileViaReferenceRequest; - -function isViaSecret(request: GetOrLoadFileRequest): request is GetOrLoadFileViaSecretRequest { - return "id" in request && "secretKey" in request; -} - -function isViaReference(request: GetOrLoadFileRequest): request is GetOrLoadFileViaSecretRequest { - return "reference" in request; -} - class Validator extends SchemaValidator { - private readonly loadViaSecretSchema: JsonSchema; - private readonly loadViaReferenceSchema: JsonSchema; - public constructor(@Inject schemaRepository: SchemaRepository) { super(schemaRepository.getSchema("GetOrLoadFileRequest")); - this.loadViaSecretSchema = schemaRepository.getSchema("GetOrLoadFileViaSecretRequest"); - this.loadViaReferenceSchema = schemaRepository.getSchema("GetOrLoadFileViaReferenceRequest"); - } - - public override validate(input: GetOrLoadFileRequest): ValidationResult { - if (this.schema.validate(input).isValid) return new ValidationResult(); - - // any-of in combination with missing properties is a bit weird - // when { reference: null | undefined } is passed, it ignores reference - // and treats it like a GetOrLoadFileViaSecret. - // That's why we validate with the specific schema afterwards - if (isViaReference(input)) { - return this.convertValidationResult(this.loadViaReferenceSchema.validate(input)); - } else if (isViaSecret(input)) { - return this.convertValidationResult(this.loadViaSecretSchema.validate(input)); - } - - const result = new ValidationResult(); - result.addFailure(new ValidationFailure(RuntimeErrors.general.invalidPayload())); - return result; } } @@ -84,20 +31,11 @@ export class GetOrLoadFileUseCase extends UseCase } protected async executeInternal(request: GetOrLoadFileRequest): Promise> { - let createdFileResult: Result; - - if (isViaSecret(request)) { - const key = CryptoSecretKey.fromBase64(request.secretKey); - createdFileResult = await this.loadFile(CoreId.from(request.id), key); - } else if (isViaReference(request)) { - createdFileResult = await this.loadFileFromReference(request.reference); - } else { - throw new Error("Invalid request format."); - } + const result = await this.loadFileFromReference(request.reference); await this.accountController.syncDatawallet(); - return createdFileResult; + return result; } private async loadFileFromReference(reference: string): Promise> { From 5e8f5947b842e206ccb2605dbab102ac17499dd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= Date: Mon, 23 Sep 2024 12:58:16 +0200 Subject: [PATCH 03/11] refactor: update templates --- .../LoadPeerRelationshipTemplate.ts | 71 ++----------------- 1 file changed, 4 insertions(+), 67 deletions(-) diff --git a/packages/runtime/src/useCases/transport/relationshipTemplates/LoadPeerRelationshipTemplate.ts b/packages/runtime/src/useCases/transport/relationshipTemplates/LoadPeerRelationshipTemplate.ts index 5abda01e9..b0ab68548 100644 --- a/packages/runtime/src/useCases/transport/relationshipTemplates/LoadPeerRelationshipTemplate.ts +++ b/packages/runtime/src/useCases/transport/relationshipTemplates/LoadPeerRelationshipTemplate.ts @@ -4,73 +4,19 @@ import { CryptoSecretKey } from "@nmshd/crypto"; import { AccountController, RelationshipTemplateController, Token, TokenContentRelationshipTemplate, TokenController } from "@nmshd/transport"; import { Inject } from "typescript-ioc"; import { RelationshipTemplateDTO } from "../../../types"; -import { - Base64ForIdPrefix, - JsonSchema, - RelationshipTemplateIdString, - RelationshipTemplateReferenceString, - RuntimeErrors, - SchemaRepository, - SchemaValidator, - TokenReferenceString, - UseCase, - ValidationFailure, - ValidationResult -} from "../../common"; +import { Base64ForIdPrefix, RelationshipTemplateReferenceString, RuntimeErrors, SchemaRepository, SchemaValidator, TokenReferenceString, UseCase } from "../../common"; import { RelationshipTemplateMapper } from "./RelationshipTemplateMapper"; -export interface LoadPeerRelationshipTemplateViaSecretRequest { - id: RelationshipTemplateIdString; - /** - * @minLength 10 - */ - secretKey: string; -} - /** * @errorMessage token / relationship template reference invalid */ -export interface LoadPeerRelationshipTemplateViaReferenceRequest { +export interface LoadPeerRelationshipTemplateRequest { reference: TokenReferenceString | RelationshipTemplateReferenceString; } -export type LoadPeerRelationshipTemplateRequest = LoadPeerRelationshipTemplateViaSecretRequest | LoadPeerRelationshipTemplateViaReferenceRequest; - -function isLoadPeerRelationshipTemplateViaSecret(request: LoadPeerRelationshipTemplateRequest): request is LoadPeerRelationshipTemplateViaSecretRequest { - return "id" in request && "secretKey" in request; -} - -function isLoadPeerRelationshipTemplateViaReference(request: LoadPeerRelationshipTemplateRequest): request is LoadPeerRelationshipTemplateViaReferenceRequest { - return "reference" in request; -} - class Validator extends SchemaValidator { - private readonly loadViaSecretSchema: JsonSchema; - private readonly loadViaReferenceSchema: JsonSchema; - public constructor(@Inject schemaRepository: SchemaRepository) { super(schemaRepository.getSchema("LoadPeerRelationshipTemplateRequest")); - - this.loadViaSecretSchema = schemaRepository.getSchema("LoadPeerRelationshipTemplateViaSecretRequest"); - this.loadViaReferenceSchema = schemaRepository.getSchema("LoadPeerRelationshipTemplateViaReferenceRequest"); - } - - public override validate(input: LoadPeerRelationshipTemplateRequest): ValidationResult { - if (this.schema.validate(input).isValid) return new ValidationResult(); - - // any-of in combination with missing properties is a bit weird - // when { reference: null | undefined } is passed, it ignores reference - // and treats it like a LoadPeerFileViaSecret. - // That's why we validate with the specific schema afterwards - if (isLoadPeerRelationshipTemplateViaReference(input)) { - return this.convertValidationResult(this.loadViaReferenceSchema.validate(input)); - } else if (isLoadPeerRelationshipTemplateViaSecret(input)) { - return this.convertValidationResult(this.loadViaSecretSchema.validate(input)); - } - - const result = new ValidationResult(); - result.addFailure(new ValidationFailure(RuntimeErrors.general.invalidPayload())); - return result; } } @@ -85,20 +31,11 @@ export class LoadPeerRelationshipTemplateUseCase extends UseCase> { - let createdTemplateResult: Result; - - if (isLoadPeerRelationshipTemplateViaSecret(request)) { - const key = CryptoSecretKey.fromBase64(request.secretKey); - createdTemplateResult = await this.loadTemplate(CoreId.from(request.id), key); - } else if (isLoadPeerRelationshipTemplateViaReference(request)) { - createdTemplateResult = await this.loadRelationshipTemplateFromReference(request.reference); - } else { - throw new Error("Invalid request format."); - } + const result = await this.loadRelationshipTemplateFromReference(request.reference); await this.accountController.syncDatawallet(); - return createdTemplateResult; + return result; } private async loadRelationshipTemplateFromReference(reference: string): Promise> { From a58706884c5d87eb8ab6456202737bae862a0ac3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= Date: Mon, 23 Sep 2024 12:58:22 +0200 Subject: [PATCH 04/11] refactor: update tokens --- .../transport/tokens/LoadPeerToken.ts | 63 ++----------------- .../runtime/test/anonymous/tokens.test.ts | 13 +--- 2 files changed, 6 insertions(+), 70 deletions(-) diff --git a/packages/runtime/src/useCases/transport/tokens/LoadPeerToken.ts b/packages/runtime/src/useCases/transport/tokens/LoadPeerToken.ts index a4c79837f..f8c987e3b 100644 --- a/packages/runtime/src/useCases/transport/tokens/LoadPeerToken.ts +++ b/packages/runtime/src/useCases/transport/tokens/LoadPeerToken.ts @@ -1,65 +1,21 @@ import { Result } from "@js-soft/ts-utils"; -import { CoreId } from "@nmshd/core-types"; -import { CryptoSecretKey } from "@nmshd/crypto"; -import { AccountController, Token, TokenController } from "@nmshd/transport"; +import { AccountController, TokenController } from "@nmshd/transport"; import { Inject } from "typescript-ioc"; import { TokenDTO } from "../../../types"; -import { JsonSchema, RuntimeErrors, SchemaRepository, SchemaValidator, TokenIdString, TokenReferenceString, UseCase, ValidationFailure, ValidationResult } from "../../common"; +import { SchemaRepository, SchemaValidator, TokenReferenceString, UseCase } from "../../common"; import { TokenMapper } from "./TokenMapper"; /** * @errorMessage token reference invalid */ -export interface LoadPeerTokenViaReferenceRequest { +export interface LoadPeerTokenRequest { reference: TokenReferenceString; ephemeral: boolean; } -export interface LoadPeerTokenViaSecretRequest { - id: TokenIdString; - /** - * @minLength 10 - */ - secretKey: string; - ephemeral: boolean; -} - -export type LoadPeerTokenRequest = LoadPeerTokenViaReferenceRequest | LoadPeerTokenViaSecretRequest; - -function isLoadPeerTokenViaSecret(request: LoadPeerTokenRequest): request is LoadPeerTokenViaSecretRequest { - return "id" in request && "secretKey" in request; -} - -function isLoadPeerTokenViaReference(request: LoadPeerTokenRequest): request is LoadPeerTokenViaReferenceRequest { - return "reference" in request; -} - class Validator extends SchemaValidator { - private readonly loadViaSecretSchema: JsonSchema; - private readonly loadViaReferenceSchema: JsonSchema; - public constructor(@Inject schemaRepository: SchemaRepository) { super(schemaRepository.getSchema("LoadPeerTokenRequest")); - this.loadViaSecretSchema = schemaRepository.getSchema("LoadPeerTokenViaSecretRequest"); - this.loadViaReferenceSchema = schemaRepository.getSchema("LoadPeerTokenViaReferenceRequest"); - } - - public override validate(input: LoadPeerTokenRequest): ValidationResult { - if (this.schema.validate(input).isValid) return new ValidationResult(); - - // any-of in combination with missing properties is a bit weird - // when { reference: null | undefined } is passed, it ignores reference - // and treats it like a LoadPeerTokenViaSecret. - // That's why we validate with the specific schema afterwards - if (isLoadPeerTokenViaReference(input)) { - return this.convertValidationResult(this.loadViaReferenceSchema.validate(input)); - } else if (isLoadPeerTokenViaSecret(input)) { - return this.convertValidationResult(this.loadViaSecretSchema.validate(input)); - } - - const result = new ValidationResult(); - result.addFailure(new ValidationFailure(RuntimeErrors.general.invalidPayload())); - return result; } } @@ -73,21 +29,12 @@ export class LoadPeerTokenUseCase extends UseCase> { - let createdToken: Token; - - if (isLoadPeerTokenViaSecret(request)) { - const key = CryptoSecretKey.fromBase64(request.secretKey); - createdToken = await this.tokenController.loadPeerToken(CoreId.from(request.id), key, request.ephemeral); - } else if (isLoadPeerTokenViaReference(request)) { - createdToken = await this.tokenController.loadPeerTokenByTruncated(request.reference, request.ephemeral); - } else { - throw new Error("Invalid request format."); - } + const result = await this.tokenController.loadPeerTokenByTruncated(request.reference, request.ephemeral); if (!request.ephemeral) { await this.accountController.syncDatawallet(); } - return Result.ok(TokenMapper.toTokenDTO(createdToken, request.ephemeral)); + return Result.ok(TokenMapper.toTokenDTO(result, request.ephemeral)); } } diff --git a/packages/runtime/test/anonymous/tokens.test.ts b/packages/runtime/test/anonymous/tokens.test.ts index 4f6f5ddad..dcbdb0c62 100644 --- a/packages/runtime/test/anonymous/tokens.test.ts +++ b/packages/runtime/test/anonymous/tokens.test.ts @@ -25,7 +25,7 @@ describe("Anonymous tokens", () => { }); test("should get the token anonymous by truncated reference", async () => { - const result = await noLoginRuntime.anonymousServices.tokens.loadPeerTokenByTruncatedReference({ + const result = await noLoginRuntime.anonymousServices.tokens.loadPeerToken({ reference: uploadedToken.truncatedReference }); expect(result).toBeSuccessful(); @@ -33,15 +33,4 @@ describe("Anonymous tokens", () => { const token = result.value; expect(token.content).toStrictEqual(uploadedToken.content); }); - - test("should get the token anonymous by id and key", async () => { - const result = await noLoginRuntime.anonymousServices.tokens.loadPeerTokenByIdAndKey({ - id: uploadedToken.id, - secretKey: uploadedToken.secretKey - }); - expect(result).toBeSuccessful(); - - const token = result.value; - expect(token.content).toStrictEqual(uploadedToken.content); - }); }); From 08540c36203445a4dbc287907b4d145230324eca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= Date: Mon, 23 Sep 2024 12:58:41 +0200 Subject: [PATCH 05/11] chore: update schemas --- .../runtime/src/useCases/common/Schemas.ts | 316 +----------------- 1 file changed, 3 insertions(+), 313 deletions(-) diff --git a/packages/runtime/src/useCases/common/Schemas.ts b/packages/runtime/src/useCases/common/Schemas.ts index 42537be14..fdf631b6a 100644 --- a/packages/runtime/src/useCases/common/Schemas.ts +++ b/packages/runtime/src/useCases/common/Schemas.ts @@ -1,35 +1,8 @@ -export const LoadPeerTokenAnonymousByIdAndKeyRequest: any = { +export const LoadPeerTokenAnonymousRequest: any = { "$schema": "http://json-schema.org/draft-07/schema#", - "$ref": "#/definitions/LoadPeerTokenAnonymousByIdAndKeyRequest", + "$ref": "#/definitions/LoadPeerTokenAnonymousRequest", "definitions": { - "LoadPeerTokenAnonymousByIdAndKeyRequest": { - "type": "object", - "properties": { - "id": { - "$ref": "#/definitions/TokenIdString" - }, - "secretKey": { - "type": "string" - } - }, - "required": [ - "id", - "secretKey" - ], - "additionalProperties": false - }, - "TokenIdString": { - "type": "string", - "pattern": "TOK[A-Za-z0-9]{17}" - } - } -} - -export const LoadPeerTokenAnonymousByTruncatedReferenceRequest: any = { - "$schema": "http://json-schema.org/draft-07/schema#", - "$ref": "#/definitions/LoadPeerTokenAnonymousByTruncatedReferenceRequest", - "definitions": { - "LoadPeerTokenAnonymousByTruncatedReferenceRequest": { + "LoadPeerTokenAnonymousRequest": { "type": "object", "properties": { "reference": { @@ -21500,105 +21473,11 @@ export const GetFilesRequest: any = { } } -export const GetOrLoadFileViaSecretRequest: any = { - "$schema": "http://json-schema.org/draft-07/schema#", - "$ref": "#/definitions/GetOrLoadFileViaSecretRequest", - "definitions": { - "GetOrLoadFileViaSecretRequest": { - "type": "object", - "properties": { - "id": { - "$ref": "#/definitions/FileIdString" - }, - "secretKey": { - "type": "string", - "minLength": 10 - } - }, - "required": [ - "id", - "secretKey" - ], - "additionalProperties": false - }, - "FileIdString": { - "type": "string", - "pattern": "FIL[A-Za-z0-9]{17}" - } - } -} - -export const GetOrLoadFileViaReferenceRequest: any = { - "$schema": "http://json-schema.org/draft-07/schema#", - "$ref": "#/definitions/GetOrLoadFileViaReferenceRequest", - "definitions": { - "GetOrLoadFileViaReferenceRequest": { - "type": "object", - "properties": { - "reference": { - "anyOf": [ - { - "$ref": "#/definitions/TokenReferenceString" - }, - { - "$ref": "#/definitions/FileReferenceString" - } - ] - } - }, - "required": [ - "reference" - ], - "additionalProperties": false, - "errorMessage": "token / file reference invalid" - }, - "TokenReferenceString": { - "type": "string", - "pattern": "VE9L.{84}" - }, - "FileReferenceString": { - "type": "string", - "pattern": "RklM.{84}" - } - } -} - export const GetOrLoadFileRequest: any = { "$schema": "http://json-schema.org/draft-07/schema#", "$ref": "#/definitions/GetOrLoadFileRequest", "definitions": { "GetOrLoadFileRequest": { - "anyOf": [ - { - "$ref": "#/definitions/GetOrLoadFileViaSecretRequest" - }, - { - "$ref": "#/definitions/GetOrLoadFileViaReferenceRequest" - } - ] - }, - "GetOrLoadFileViaSecretRequest": { - "type": "object", - "properties": { - "id": { - "$ref": "#/definitions/FileIdString" - }, - "secretKey": { - "type": "string", - "minLength": 10 - } - }, - "required": [ - "id", - "secretKey" - ], - "additionalProperties": false - }, - "FileIdString": { - "type": "string", - "pattern": "FIL[A-Za-z0-9]{17}" - }, - "GetOrLoadFileViaReferenceRequest": { "type": "object", "properties": { "reference": { @@ -22304,105 +22183,11 @@ export const GetRelationshipTemplatesRequest: any = { } } -export const LoadPeerRelationshipTemplateViaSecretRequest: any = { - "$schema": "http://json-schema.org/draft-07/schema#", - "$ref": "#/definitions/LoadPeerRelationshipTemplateViaSecretRequest", - "definitions": { - "LoadPeerRelationshipTemplateViaSecretRequest": { - "type": "object", - "properties": { - "id": { - "$ref": "#/definitions/RelationshipTemplateIdString" - }, - "secretKey": { - "type": "string", - "minLength": 10 - } - }, - "required": [ - "id", - "secretKey" - ], - "additionalProperties": false - }, - "RelationshipTemplateIdString": { - "type": "string", - "pattern": "RLT[A-Za-z0-9]{17}" - } - } -} - -export const LoadPeerRelationshipTemplateViaReferenceRequest: any = { - "$schema": "http://json-schema.org/draft-07/schema#", - "$ref": "#/definitions/LoadPeerRelationshipTemplateViaReferenceRequest", - "definitions": { - "LoadPeerRelationshipTemplateViaReferenceRequest": { - "type": "object", - "properties": { - "reference": { - "anyOf": [ - { - "$ref": "#/definitions/TokenReferenceString" - }, - { - "$ref": "#/definitions/RelationshipTemplateReferenceString" - } - ] - } - }, - "required": [ - "reference" - ], - "additionalProperties": false, - "errorMessage": "token / relationship template reference invalid" - }, - "TokenReferenceString": { - "type": "string", - "pattern": "VE9L.{84}" - }, - "RelationshipTemplateReferenceString": { - "type": "string", - "pattern": "UkxU.{84}" - } - } -} - export const LoadPeerRelationshipTemplateRequest: any = { "$schema": "http://json-schema.org/draft-07/schema#", "$ref": "#/definitions/LoadPeerRelationshipTemplateRequest", "definitions": { "LoadPeerRelationshipTemplateRequest": { - "anyOf": [ - { - "$ref": "#/definitions/LoadPeerRelationshipTemplateViaSecretRequest" - }, - { - "$ref": "#/definitions/LoadPeerRelationshipTemplateViaReferenceRequest" - } - ] - }, - "LoadPeerRelationshipTemplateViaSecretRequest": { - "type": "object", - "properties": { - "id": { - "$ref": "#/definitions/RelationshipTemplateIdString" - }, - "secretKey": { - "type": "string", - "minLength": 10 - } - }, - "required": [ - "id", - "secretKey" - ], - "additionalProperties": false - }, - "RelationshipTemplateIdString": { - "type": "string", - "pattern": "RLT[A-Za-z0-9]{17}" - }, - "LoadPeerRelationshipTemplateViaReferenceRequest": { "type": "object", "properties": { "reference": { @@ -22962,81 +22747,11 @@ export const GetTokensRequest: any = { } } -export const LoadPeerTokenViaReferenceRequest: any = { - "$schema": "http://json-schema.org/draft-07/schema#", - "$ref": "#/definitions/LoadPeerTokenViaReferenceRequest", - "definitions": { - "LoadPeerTokenViaReferenceRequest": { - "type": "object", - "properties": { - "reference": { - "$ref": "#/definitions/TokenReferenceString" - }, - "ephemeral": { - "type": "boolean" - } - }, - "required": [ - "reference", - "ephemeral" - ], - "additionalProperties": false, - "errorMessage": "token reference invalid" - }, - "TokenReferenceString": { - "type": "string", - "pattern": "VE9L.{84}" - } - } -} - -export const LoadPeerTokenViaSecretRequest: any = { - "$schema": "http://json-schema.org/draft-07/schema#", - "$ref": "#/definitions/LoadPeerTokenViaSecretRequest", - "definitions": { - "LoadPeerTokenViaSecretRequest": { - "type": "object", - "properties": { - "id": { - "$ref": "#/definitions/TokenIdString" - }, - "secretKey": { - "type": "string", - "minLength": 10 - }, - "ephemeral": { - "type": "boolean" - } - }, - "required": [ - "id", - "secretKey", - "ephemeral" - ], - "additionalProperties": false - }, - "TokenIdString": { - "type": "string", - "pattern": "TOK[A-Za-z0-9]{17}" - } - } -} - export const LoadPeerTokenRequest: any = { "$schema": "http://json-schema.org/draft-07/schema#", "$ref": "#/definitions/LoadPeerTokenRequest", "definitions": { "LoadPeerTokenRequest": { - "anyOf": [ - { - "$ref": "#/definitions/LoadPeerTokenViaReferenceRequest" - }, - { - "$ref": "#/definitions/LoadPeerTokenViaSecretRequest" - } - ] - }, - "LoadPeerTokenViaReferenceRequest": { "type": "object", "properties": { "reference": { @@ -23056,31 +22771,6 @@ export const LoadPeerTokenRequest: any = { "TokenReferenceString": { "type": "string", "pattern": "VE9L.{84}" - }, - "LoadPeerTokenViaSecretRequest": { - "type": "object", - "properties": { - "id": { - "$ref": "#/definitions/TokenIdString" - }, - "secretKey": { - "type": "string", - "minLength": 10 - }, - "ephemeral": { - "type": "boolean" - } - }, - "required": [ - "id", - "secretKey", - "ephemeral" - ], - "additionalProperties": false - }, - "TokenIdString": { - "type": "string", - "pattern": "TOK[A-Za-z0-9]{17}" } } } \ No newline at end of file From 1f0228be0959cf437357f0859c6082789d6b244b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= Date: Mon, 23 Sep 2024 13:00:09 +0200 Subject: [PATCH 06/11] fix: tests --- packages/app-runtime/src/AppStringProcessor.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/app-runtime/src/AppStringProcessor.ts b/packages/app-runtime/src/AppStringProcessor.ts index 7f8fabd52..c8ed7d334 100644 --- a/packages/app-runtime/src/AppStringProcessor.ts +++ b/packages/app-runtime/src/AppStringProcessor.ts @@ -63,9 +63,7 @@ export class AppStringProcessor { return UserfriendlyResult.fail(error); } - const tokenResult = await this.runtime.anonymousServices.tokens.loadPeerTokenByTruncatedReference({ - reference: truncatedReference - }); + const tokenResult = await this.runtime.anonymousServices.tokens.loadPeerToken({ reference: truncatedReference }); if (tokenResult.isError) { return UserfriendlyResult.fail(UserfriendlyApplicationError.fromError(tokenResult.error)); } From c5e98d3598b5d3c022aa50fffe65f593e04ffee8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= Date: Mon, 23 Sep 2024 13:01:00 +0200 Subject: [PATCH 07/11] fix: tests --- packages/runtime/test/transport/files.test.ts | 45 ------------------- 1 file changed, 45 deletions(-) diff --git a/packages/runtime/test/transport/files.test.ts b/packages/runtime/test/transport/files.test.ts index bf7e40f4a..5448b45c7 100644 --- a/packages/runtime/test/transport/files.test.ts +++ b/packages/runtime/test/transport/files.test.ts @@ -333,15 +333,6 @@ describe("Load peer file with file id and secret", () => { expect(response).toBeAnError("File not found. Make sure the ID exists and the record is not expired.", "error.runtime.recordNotFound"); }); - test("peer file can be loaded with file id and secret key", async () => { - expect(file).toBeDefined(); - - const response = await transportServices2.files.getOrLoadFile({ id: file.id, secretKey: file.secretKey }); - - expect(response).toBeSuccessful(); - expect(response.value).toMatchObject({ ...file, isOwn: false }); - }); - test("after peer file is loaded the file can be accessed under /Files/{id}", async () => { expect(file).toBeDefined(); @@ -357,42 +348,6 @@ describe("Load peer file with file id and secret", () => { expect(response).toBeSuccessful(); expect(response.value).toContainEqual({ ...file, isOwn: false }); }); - - test("cannot pass an unkown file id", async () => { - expect(file).toBeDefined(); - - const response = await transportServices2.files.getOrLoadFile({ id: UNKNOWN_FILE_ID, secretKey: file.secretKey }); - - expect(response).toBeAnError("File not found. Make sure the ID exists and the record is not expired.", "error.runtime.recordNotFound"); - }); - - test("cannot pass an unkown token id as file id", async () => { - expect(file).toBeDefined(); - - const response = await transportServices2.files.getOrLoadFile({ id: UNKNOWN_TOKEN_ID, secretKey: file.secretKey }); - - expect(response).toBeAnError("id must match pattern FIL.*", "error.runtime.validation.invalidPropertyValue"); - }); - - test.each([ - [null, "secretKey must be string"], - [undefined, "must have required property 'secretKey'"], - ["", "secretKey must NOT have fewer than 10 characters"] - ])("cannot pass %p as secret key", async (secretKey, expectedMessage) => { - const response = await transportServices2.files.getOrLoadFile({ id: file.id, secretKey: secretKey! }); - - expect(response).toBeAnError(expectedMessage, "error.runtime.validation.invalidPropertyValue"); - }); - - test.each([ - [null, "id must be string"], - [undefined, "must have required property 'id'"], - ["", "id must match pattern FIL.*"] - ])("cannot pass %p as file id", async (fileId, expectedMessage) => { - const response = await transportServices2.files.getOrLoadFile({ id: fileId!, secretKey: file.secretKey }); - - expect(response).toBeAnError(expectedMessage, "error.runtime.validation.invalidPropertyValue"); - }); }); describe("Load peer file with the FileReference", () => { From 6865ebd0703c8437cb870cf684bd6ea09446e539 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= Date: Mon, 23 Sep 2024 13:14:24 +0200 Subject: [PATCH 08/11] fix: test --- packages/runtime/test/transport/files.test.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/runtime/test/transport/files.test.ts b/packages/runtime/test/transport/files.test.ts index 5448b45c7..a25080ca7 100644 --- a/packages/runtime/test/transport/files.test.ts +++ b/packages/runtime/test/transport/files.test.ts @@ -315,7 +315,7 @@ describe("Load peer file with token reference", () => { test("passing empty object causes an error", async () => { const response = await transportServices2.files.getOrLoadFile({} as any); - expect(response).toBeAnError("The given combination of properties in the payload is not supported.", "error.runtime.validation.invalidPayload"); + expect(response).toBeAnError("token / file reference invalid", "error.runtime.validation.invalidPropertyValue"); }); }); @@ -336,6 +336,8 @@ describe("Load peer file with file id and secret", () => { test("after peer file is loaded the file can be accessed under /Files/{id}", async () => { expect(file).toBeDefined(); + await transportServices2.files.getOrLoadFile({ reference: file.truncatedReference }); + const response = await transportServices2.files.getFile({ id: file.id }); expect(response).toBeSuccessful(); expect(response.value).toMatchObject({ ...file, isOwn: false }); @@ -344,6 +346,8 @@ describe("Load peer file with file id and secret", () => { test("after peer file is loaded it can be accessed under /Files", async () => { expect(file).toBeDefined(); + await transportServices2.files.getOrLoadFile({ reference: file.truncatedReference }); + const response = await transportServices2.files.getFiles({ query: { createdAt: file.createdAt } }); expect(response).toBeSuccessful(); expect(response.value).toContainEqual({ ...file, isOwn: false }); From 58eca20ae9b27f8a0228f36a21c79498177cd446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= Date: Mon, 23 Sep 2024 14:11:34 +0200 Subject: [PATCH 09/11] chore: remove secretKey's from DTOs --- packages/runtime/src/dataViews/transport/FileDVO.ts | 1 - packages/runtime/src/types/transport/FileDTO.ts | 1 - packages/runtime/src/types/transport/RelationshipTemplateDTO.ts | 1 - packages/runtime/src/types/transport/TokenDTO.ts | 1 - packages/runtime/src/useCases/transport/files/FileMapper.ts | 1 - .../relationshipTemplates/RelationshipTemplateMapper.ts | 1 - packages/runtime/src/useCases/transport/tokens/TokenMapper.ts | 1 - 7 files changed, 7 deletions(-) diff --git a/packages/runtime/src/dataViews/transport/FileDVO.ts b/packages/runtime/src/dataViews/transport/FileDVO.ts index a20643141..15c5f6722 100644 --- a/packages/runtime/src/dataViews/transport/FileDVO.ts +++ b/packages/runtime/src/dataViews/transport/FileDVO.ts @@ -12,6 +12,5 @@ export interface FileDVO extends DataViewObject { mimetype: string; isOwn: boolean; title: string; - secretKey: string; truncatedReference: string; } diff --git a/packages/runtime/src/types/transport/FileDTO.ts b/packages/runtime/src/types/transport/FileDTO.ts index 0151489ce..7006b6498 100644 --- a/packages/runtime/src/types/transport/FileDTO.ts +++ b/packages/runtime/src/types/transport/FileDTO.ts @@ -9,7 +9,6 @@ export interface FileDTO { mimetype: string; isOwn: boolean; title: string; - secretKey: string; description?: string; truncatedReference: string; } diff --git a/packages/runtime/src/types/transport/RelationshipTemplateDTO.ts b/packages/runtime/src/types/transport/RelationshipTemplateDTO.ts index 3b5f1a6a2..1a6f3cb02 100644 --- a/packages/runtime/src/types/transport/RelationshipTemplateDTO.ts +++ b/packages/runtime/src/types/transport/RelationshipTemplateDTO.ts @@ -11,6 +11,5 @@ export interface RelationshipTemplateDTO { content: RelationshipTemplateContentDerivation; expiresAt?: string; maxNumberOfAllocations?: number; - secretKey: string; truncatedReference: string; } diff --git a/packages/runtime/src/types/transport/TokenDTO.ts b/packages/runtime/src/types/transport/TokenDTO.ts index 4eca98d39..a83ac83d4 100644 --- a/packages/runtime/src/types/transport/TokenDTO.ts +++ b/packages/runtime/src/types/transport/TokenDTO.ts @@ -5,7 +5,6 @@ export interface TokenDTO { content: any; createdAt: string; expiresAt: string; - secretKey: string; truncatedReference: string; isEphemeral: boolean; } diff --git a/packages/runtime/src/useCases/transport/files/FileMapper.ts b/packages/runtime/src/useCases/transport/files/FileMapper.ts index 3d642775d..463b6778d 100644 --- a/packages/runtime/src/useCases/transport/files/FileMapper.ts +++ b/packages/runtime/src/useCases/transport/files/FileMapper.ts @@ -32,7 +32,6 @@ export class FileMapper { mimetype: file.cache.mimetype, isOwn: file.isOwn, title: file.cache.title ?? "", - secretKey: file.secretKey.toBase64(false), description: file.cache.description, truncatedReference: file.truncate() }; diff --git a/packages/runtime/src/useCases/transport/relationshipTemplates/RelationshipTemplateMapper.ts b/packages/runtime/src/useCases/transport/relationshipTemplates/RelationshipTemplateMapper.ts index 6d7e2fdfe..207d2af49 100644 --- a/packages/runtime/src/useCases/transport/relationshipTemplates/RelationshipTemplateMapper.ts +++ b/packages/runtime/src/useCases/transport/relationshipTemplates/RelationshipTemplateMapper.ts @@ -19,7 +19,6 @@ export class RelationshipTemplateMapper { content: this.toTemplateContent(template.cache.content), expiresAt: template.cache.expiresAt?.toString(), maxNumberOfAllocations: template.cache.maxNumberOfAllocations, - secretKey: template.secretKey.toBase64(false), truncatedReference: template.truncate() }; } diff --git a/packages/runtime/src/useCases/transport/tokens/TokenMapper.ts b/packages/runtime/src/useCases/transport/tokens/TokenMapper.ts index 7827d20b7..9441e6fbf 100644 --- a/packages/runtime/src/useCases/transport/tokens/TokenMapper.ts +++ b/packages/runtime/src/useCases/transport/tokens/TokenMapper.ts @@ -16,7 +16,6 @@ export class TokenMapper { content: token.cache.content.toJSON(), createdAt: token.cache.createdAt.toString(), expiresAt: token.cache.expiresAt.toString(), - secretKey: token.secretKey.toBase64(false), truncatedReference: reference.truncate(), isEphemeral: ephemeral }; From 7eee1f50f0294cd96ca6fe1c594e1e7fe849e0f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= Date: Mon, 23 Sep 2024 15:28:45 +0200 Subject: [PATCH 10/11] chore: remove whole describe block --- packages/runtime/test/transport/files.test.ts | 35 ------------------- 1 file changed, 35 deletions(-) diff --git a/packages/runtime/test/transport/files.test.ts b/packages/runtime/test/transport/files.test.ts index a25080ca7..b67cd06cd 100644 --- a/packages/runtime/test/transport/files.test.ts +++ b/packages/runtime/test/transport/files.test.ts @@ -319,41 +319,6 @@ describe("Load peer file with token reference", () => { }); }); -describe("Load peer file with file id and secret", () => { - let file: FileDTO; - - beforeAll(async () => { - file = await uploadFile(transportServices1); - }); - - test("before the peer file is loaded another client cannot access it", async () => { - expect(file).toBeDefined(); - - const response = await transportServices2.files.getFile({ id: file.id }); - expect(response).toBeAnError("File not found. Make sure the ID exists and the record is not expired.", "error.runtime.recordNotFound"); - }); - - test("after peer file is loaded the file can be accessed under /Files/{id}", async () => { - expect(file).toBeDefined(); - - await transportServices2.files.getOrLoadFile({ reference: file.truncatedReference }); - - const response = await transportServices2.files.getFile({ id: file.id }); - expect(response).toBeSuccessful(); - expect(response.value).toMatchObject({ ...file, isOwn: false }); - }); - - test("after peer file is loaded it can be accessed under /Files", async () => { - expect(file).toBeDefined(); - - await transportServices2.files.getOrLoadFile({ reference: file.truncatedReference }); - - const response = await transportServices2.files.getFiles({ query: { createdAt: file.createdAt } }); - expect(response).toBeSuccessful(); - expect(response.value).toContainEqual({ ...file, isOwn: false }); - }); -}); - describe("Load peer file with the FileReference", () => { let file: FileDTO; From 36bf83ff4f13fe889ce85a49aa841b2c4669ff18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= Date: Mon, 23 Sep 2024 16:11:51 +0200 Subject: [PATCH 11/11] chore: move tests instead of deleting them --- packages/runtime/test/transport/files.test.ts | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/packages/runtime/test/transport/files.test.ts b/packages/runtime/test/transport/files.test.ts index b67cd06cd..3cfb69699 100644 --- a/packages/runtime/test/transport/files.test.ts +++ b/packages/runtime/test/transport/files.test.ts @@ -326,8 +326,31 @@ describe("Load peer file with the FileReference", () => { file = await uploadFile(transportServices1); }); + test("before the peer file is loaded another client cannot access it", async () => { + expect(file).toBeDefined(); + + const response = await transportServices2.files.getFile({ id: file.id }); + expect(response).toBeAnError("File not found. Make sure the ID exists and the record is not expired.", "error.runtime.recordNotFound"); + }); + test("load the file using the FileReference", async () => { const fileResult = await transportServices2.files.getOrLoadFile({ reference: file.truncatedReference }); expect(fileResult).toBeSuccessful(); }); + + test("after peer file is loaded the file can be accessed under /Files/{id}", async () => { + expect(file).toBeDefined(); + + const response = await transportServices2.files.getFile({ id: file.id }); + expect(response).toBeSuccessful(); + expect(response.value).toMatchObject({ ...file, isOwn: false }); + }); + + test("after peer file is loaded it can be accessed under /Files", async () => { + expect(file).toBeDefined(); + + const response = await transportServices2.files.getFiles({ query: { createdAt: file.createdAt } }); + expect(response).toBeSuccessful(); + expect(response.value).toContainEqual({ ...file, isOwn: false }); + }); });