Skip to content

Commit

Permalink
fix: losing params while creating Auth Predicate (#281)
Browse files Browse the repository at this point in the history
* fix: loosing params while creating Auth Predicate

* fix: typos

* fix: typo
  • Loading branch information
mathix420 authored Jun 30, 2021
1 parent 00871c8 commit ba9ee02
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 1 deletion.
142 changes: 142 additions & 0 deletions packages/graphql/src/translate/create-auth-and-params.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,4 +548,146 @@ describe("createAuthAndParams", () => {
});
});
});

describe("params", () => {
test("should convert undefined $jwt parameters to null", () => {
const idField = {
fieldName: "id",
typeMeta: {
name: "ID",
array: false,
required: false,
pretty: "String",
input: {
where: {
type: "String",
pretty: "String",
},
create: {
type: "String",
pretty: "String",
},
update: {
type: "String",
pretty: "String",
},
},
},
otherDirectives: [],
arguments: [],
};

// @ts-ignore
const node: Node = {
name: "Movie",
relationFields: [],
cypherFields: [],
enumFields: [],
scalarFields: [],
primitiveFields: [idField],
dateTimeFields: [],
interfaceFields: [],
objectFields: [],
pointFields: [],
authableFields: [idField],
auth: {
rules: [
{ allow: { id: "$jwt.sub" } },
{ operations: ["CREATE"], roles: ["admin"] },
{ roles: ["admin"] },
],
type: "JWT",
},
};

// @ts-ignore
const neoSchema: Neo4jGraphQL = {
nodes: [node],
};

// @ts-ignore
const context: Context = { neoSchema, jwt: {} };

const result = createAuthAndParams({
context,
entity: node,
operation: "READ",
allow: { parentNode: node, varName: "this" },
});

expect(result[1]).toMatchObject({
this_auth_allow0_id: null,
});
});

test("should convert undefined $context parameters to null", () => {
const idField = {
fieldName: "id",
typeMeta: {
name: "ID",
array: false,
required: false,
pretty: "String",
input: {
where: {
type: "String",
pretty: "String",
},
create: {
type: "String",
pretty: "String",
},
update: {
type: "String",
pretty: "String",
},
},
},
otherDirectives: [],
arguments: [],
};

// @ts-ignore
const node: Node = {
name: "Movie",
relationFields: [],
cypherFields: [],
enumFields: [],
scalarFields: [],
primitiveFields: [idField],
dateTimeFields: [],
interfaceFields: [],
objectFields: [],
pointFields: [],
authableFields: [idField],
auth: {
rules: [
{ allow: { id: "$context.nop" } },
{ operations: ["CREATE"], roles: ["admin"] },
{ roles: ["admin"] },
],
type: "JWT",
},
};

// @ts-ignore
const neoSchema: Neo4jGraphQL = {
nodes: [node],
};

// @ts-ignore
const context: Context = { neoSchema, jwt: {} };

const result = createAuthAndParams({
context,
entity: node,
operation: "READ",
allow: { parentNode: node, varName: "this" },
});

expect(result[1]).toMatchObject({
this_auth_allow0_id: null,
});
});
});
});
6 changes: 5 additions & 1 deletion packages/graphql/src/translate/create-auth-and-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,17 @@ function createAuthPredicate({
if (authableField) {
const [, jwtPath] = (value as string).split("$jwt.");
const [, ctxPath] = (value as string).split("$context.");
let paramValue: string = value as string;
let paramValue: string | null = value as string;

if (jwtPath) {
paramValue = dotProp.get({ value: jwt }, `value.${jwtPath}`) as string;
} else if (ctxPath) {
paramValue = dotProp.get({ value: context }, `value.${ctxPath}`) as string;
}
// To avoid losing params on query execution
if ((jwtPath || ctxPath) && paramValue === undefined) {
paramValue = null;
}

const param = `${chainStr}_${key}`;
res.params[param] = paramValue;
Expand Down

0 comments on commit ba9ee02

Please sign in to comment.