Skip to content
This repository was archived by the owner on Jul 1, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/opaclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ export class OPAClient {
this.opa = new Opa(sdk);
}

/** `authorize` is used to evaluate the policy at the specified.
/** `evaluate` is used to evaluate the policy at the specified.
*
* @param path - The path to the policy, without `/v1/data`: use `authz/allow` to evaluate policy `data.authz.allow`.
* @param input - The input to the policy, if needed.
* @param fromResult - A function that is used to transform the policy evaluation result (which could be `undefined`).
*/
async authorize<In extends Input | ToInput, Res>(
async evaluate<In extends Input | ToInput, Res>(
path: string,
input?: In,
fromResult?: (res?: Result) => Res,
Expand Down
20 changes: 10 additions & 10 deletions tests/authorizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,20 @@ allow if {
});

it("can be called without types, without input", async () => {
const res = await new OPAClient(serverURL).authorize("test/p_bool");
const res = await new OPAClient(serverURL).evaluate("test/p_bool");
assert.strictEqual(res, true);
});

it("can be called with input==false", async () => {
const res = await new OPAClient(serverURL).authorize(
const res = await new OPAClient(serverURL).evaluate(
"test/p_bool_false",
false,
);
assert.strictEqual(res, true);
});

it("supports rules with slashes", async () => {
const res = await new OPAClient(serverURL).authorize(
const res = await new OPAClient(serverURL).evaluate(
"has/weird%2fpackage/but/it_is",
);
assert.strictEqual(res, true);
Expand All @@ -108,7 +108,7 @@ allow if {
foo: string;
}
const inp: myInput = { name: "alice", list: [1, 2, true] };
const res = await new OPAClient(serverURL).authorize<myInput, myResult>(
const res = await new OPAClient(serverURL).evaluate<myInput, myResult>(
"test/compound_input",
inp,
);
Expand All @@ -120,7 +120,7 @@ allow if {
type: string;
}
const inp = true;
const res = await new OPAClient(serverURL).authorize<boolean, typeResult>(
const res = await new OPAClient(serverURL).evaluate<boolean, typeResult>(
"test/has_type",
inp,
);
Expand All @@ -143,7 +143,7 @@ allow if {
interface myResult {
foo: string;
}
const res = await new OPAClient(serverURL).authorize<A, myResult>(
const res = await new OPAClient(serverURL).evaluate<A, myResult>(
"test/compound_input",
inp,
);
Expand All @@ -170,15 +170,15 @@ allow if {
interface myResult {
foo: string;
}
const res = await new OPAClient(serverURL).authorize<A, myResult>(
const res = await new OPAClient(serverURL).evaluate<A, myResult>(
"test/compound_input",
inp,
);
assert.deepStrictEqual(res, { foo: "bar" });
});

it("supports result class implementing FromResult", async () => {
const res = await new OPAClient(serverURL).authorize<any, boolean>(
const res = await new OPAClient(serverURL).evaluate<any, boolean>(
"test/compound_result",
undefined, // input
(r?: Result) => (r as Record<string, any>)["allowed"] ?? false,
Expand All @@ -195,7 +195,7 @@ allow if {
});
const res = await new OPAClient(serverURL, {
sdk: { httpClient },
}).authorize("test/p_bool");
}).evaluate("test/p_bool");
assert.strictEqual(res, true);
assert.strictEqual(called, true);
});
Expand All @@ -204,7 +204,7 @@ allow if {
const authorization = "Bearer opensesame";
const res = await new OPAClient(serverURL, {
headers: { authorization },
}).authorize("token/p");
}).evaluate("token/p");
assert.strictEqual(res, true);
});

Expand Down