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

Query RelationshipTemplates for password protection #353

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
20 changes: 20 additions & 0 deletions packages/runtime/src/useCases/common/Schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22772,6 +22772,26 @@ export const GetRelationshipTemplatesRequest: any = {
}
}
]
},
"passwordProtection.password": {
"anyOf": [
{
"type": "string"
},
{
"type": "array",
"items": {
"type": "string"
}
}
]
},
"passwordProtection.passwordIsPin": {
"type": "string",
"enum": [
"true",
"!"
]
}
},
"additionalProperties": false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { QueryTranslator } from "@js-soft/docdb-querytranslator";
import { Result } from "@js-soft/ts-utils";
import { CachedRelationshipTemplate, RelationshipTemplate, RelationshipTemplateController } from "@nmshd/transport";
import { CachedRelationshipTemplate, PasswordProtection, RelationshipTemplate, RelationshipTemplateController } from "@nmshd/transport";
import { Inject } from "@nmshd/typescript-ioc";
import { nameof } from "ts-simple-nameof";
import { RelationshipTemplateDTO } from "../../../types";
Expand All @@ -15,6 +15,8 @@ export interface GetRelationshipTemplatesQuery {
createdByDevice?: string | string[];
maxNumberOfAllocations?: string | string[];
forIdentity?: string | string[];
"passwordProtection.password"?: string | string[];
"passwordProtection.passwordIsPin"?: "true" | "!";
}

export interface GetRelationshipTemplatesRequest {
Expand All @@ -37,7 +39,9 @@ export class GetRelationshipTemplatesUseCase extends UseCase<GetRelationshipTemp
[nameof<RelationshipTemplateDTO>((r) => r.createdBy)]: true,
[nameof<RelationshipTemplateDTO>((r) => r.createdByDevice)]: true,
[nameof<RelationshipTemplateDTO>((r) => r.maxNumberOfAllocations)]: true,
[nameof<RelationshipTemplateDTO>((r) => r.forIdentity)]: true
[nameof<RelationshipTemplateDTO>((r) => r.forIdentity)]: true,
[`${nameof<RelationshipTemplateDTO>((r) => r.passwordProtection)}.password`]: true,
[`${nameof<RelationshipTemplateDTO>((r) => r.passwordProtection)}.passwordIsPin`]: true
},
alias: {
[nameof<RelationshipTemplateDTO>((r) => r.isOwn)]: nameof<RelationshipTemplate>((r) => r.isOwn),
Expand All @@ -51,6 +55,23 @@ export class GetRelationshipTemplatesUseCase extends UseCase<GetRelationshipTemp
(t) => t.maxNumberOfAllocations
)}`,
[nameof<RelationshipTemplateDTO>((r) => r.forIdentity)]: `${nameof<RelationshipTemplate>((r) => r.cache)}.${nameof<CachedRelationshipTemplate>((t) => t.forIdentity)}`
},
custom: {
[`${nameof<RelationshipTemplateDTO>((r) => r.passwordProtection)}.password`]: (query: any, input: string) => {
query[`${nameof<RelationshipTemplate>((t) => t.passwordProtection)}.${nameof<PasswordProtection>((t) => t.password)}`] = input;
},
[`${nameof<RelationshipTemplateDTO>((t) => t.passwordProtection)}.passwordIsPin`]: (query: any, input: string) => {
if (input === "true") {
query[`${nameof<RelationshipTemplate>((t) => t.passwordProtection)}.${nameof<PasswordProtection>((t) => t.passwordType)}`] = {
$regex: "^pin"
};
}
if (input === "!") {
query[`${nameof<RelationshipTemplate>((t) => t.passwordProtection)}.${nameof<PasswordProtection>((t) => t.passwordType)}`] = {
$in: ["pw", undefined]
};
}
}
}
});

Expand Down
52 changes: 51 additions & 1 deletion packages/runtime/test/transport/relationshipTemplates.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RelationshipTemplateContent, RelationshipTemplateContentJSON } from "@nmshd/content";
import { DateTime } from "luxon";
import { GetRelationshipTemplatesQuery, OwnerRestriction } from "../../src";
import { emptyRelationshipTemplateContent, QueryParamConditions, RuntimeServiceProvider, TestRuntimeServices } from "../lib";
import { createTemplate, emptyRelationshipTemplateContent, QueryParamConditions, RuntimeServiceProvider, TestRuntimeServices } from "../lib";

const serviceProvider = new RuntimeServiceProvider();
let runtimeServices1: TestRuntimeServices;
Expand Down Expand Up @@ -327,4 +327,54 @@ describe("RelationshipTemplates query", () => {

await conditions.executeTests((c, q) => c.relationshipTemplates.getRelationshipTemplates({ query: q, ownerRestriction: OwnerRestriction.Peer }));
});

test("password query for a template protected with a password that's not a PIN", async () => {
const passwordProtectedTemplate = await createTemplate(runtimeServices1.transport, undefined, { password: "password" });

const conditions = new QueryParamConditions<GetRelationshipTemplatesQuery>(passwordProtectedTemplate, runtimeServices1.transport).addStringSet(
"passwordProtection.password"
);
await conditions.executeTests((c, q) => c.relationshipTemplates.getRelationshipTemplates({ query: q }));
});

test("password query for a template protected with a PIN", async () => {
const pinProtectedTemplate = await createTemplate(runtimeServices1.transport, undefined, { password: "1234", passwordIsPin: true });

const conditions = new QueryParamConditions<GetRelationshipTemplatesQuery>(pinProtectedTemplate, runtimeServices1.transport).addStringSet("passwordProtection.password");
await conditions.executeTests((c, q) => c.relationshipTemplates.getRelationshipTemplates({ query: q }));
});

test("passwordIsPin query", async () => {
const passwordProtectedTemplate = await createTemplate(runtimeServices1.transport, undefined, { password: "password" });

const pinProtectedTemplate = await createTemplate(runtimeServices1.transport, undefined, { password: "1234", passwordIsPin: true });

const unprotectedTemplate = await createTemplate(runtimeServices1.transport);

const templatesWithoutPin = (
await runtimeServices1.transport.relationshipTemplates.getRelationshipTemplates({
query: {
"passwordProtection.passwordIsPin": "!"
}
})
).value;
const idsOfTemplatesWithoutPin = templatesWithoutPin.map((t) => t.id);

expect(idsOfTemplatesWithoutPin).toContain(passwordProtectedTemplate.id);
expect(idsOfTemplatesWithoutPin).not.toContain(pinProtectedTemplate.id);
expect(idsOfTemplatesWithoutPin).toContain(unprotectedTemplate.id);

const templatesWithPin = (
await runtimeServices1.transport.relationshipTemplates.getRelationshipTemplates({
query: {
"passwordProtection.passwordIsPin": "true"
}
})
).value;
const idsOfTemplatesWithPin = templatesWithPin.map((t) => t.id);

expect(idsOfTemplatesWithPin).not.toContain(passwordProtectedTemplate.id);
expect(idsOfTemplatesWithPin).toContain(pinProtectedTemplate.id);
expect(idsOfTemplatesWithPin).not.toContain(unprotectedTemplate.id);
});
});
Loading