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

expose CallOptions for RPC methods and Middleware #92

Merged
merged 2 commits into from
Nov 12, 2024
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
51 changes: 41 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ authClient
### Methods
```ts
// Is
// (method) Authorizer.Is(params: IsRequest): Promise<boolean>
// (method) Authorizer.Is(params: IsRequest, options?: CallOptions): Promise<boolean>
await authClient
.Is({
identityContext: identityContext(
Expand All @@ -126,7 +126,7 @@ await authClient
})

// Query
// (method) Authorizer.Query(params: QueryRequest): Promise<JsonObject>
// (method) Authorizer.Query(params: QueryRequest, options?: CallOptions): Promise<JsonObject>
await authClient
.Query({
identityContext: identityContext(
Expand All @@ -143,7 +143,7 @@ await authClient


// DecisionTree
// (method) Authorizer.DecisionTree(params: DecisionTreeRequest): Promise<{
// (method) Authorizer.DecisionTree(params: DecisionTreeRequest, options?: CallOptions): Promise<{
// path: Path;
// pathRoot: string;
// }>
Expand All @@ -162,16 +162,25 @@ await authClient


// ListPolicies
// (method) Authorizer.ListPolicies(params: PlainMessage<ListPoliciesRequest>): Promise<Module[]>
// (method) Authorizer.ListPolicies(params: PlainMessage<ListPoliciesRequest>, options?: CallOptions): Promise<Module[]>
await authClient
.ListPolicies({ policyInstance: policyInstance("todo", "todo") })
```

#### Custom Headers
```ts
await authClient.ListPolicies(
{ policyInstance: policyInstance("todo", "todo") },
{ headers: { customKey: "customValue" } }
);
```

### Middleware

When authorization middleware is configured and attached to a server, it examines incoming requests, extracts authorization parameters like the caller's identity, calls the Aserto authorizers, and rejects messages if their access is denied.

`failWithError`: When set to `true`, will forward errors to `next` instead of ending the response directly.
`callOptions`: Options for a call.(see: https://github.com/connectrpc/connect-es/blob/v1.5.0/packages/connect/src/call-options.ts#L21-L54)

```ts
interface Middleware {
Expand All @@ -181,6 +190,7 @@ interface Middleware {
identityMapper?: IdentityMapper;
policyMapper?: PolicyMapper;
failWithError?: boolean;
callOptions?: CallOptions;
}

type Policy = {
Expand Down Expand Up @@ -486,7 +496,7 @@ const directoryClient = DirectoryServiceV3({

#### 'object' function

`object({ objectType: "type-name", objectId: "object-id" })`:
`object({ objectType: "type-name", objectId: "object-id" }, options?: CallOptions)`:

Get an object instance with the type `type-name` and the id `object-id`. For example:

Expand Down Expand Up @@ -549,7 +559,7 @@ const relation = await directoryClient.relation({

#### 'setObject' function

`setObject({ object: $Object })`:
`setObject({ object: $Object }, options?: CallOptions)`:

Create an object instance with the specified fields. For example:

Expand All @@ -569,7 +579,7 @@ const user = await directoryClient.setObject(

#### 'setRelation' function

`setRelation({ relation: Relation })`:
`setRelation({ relation: Relation }, options?: CallOptions)`:

Create a relation with a specified name between two objects. For example:

Expand All @@ -585,7 +595,7 @@ const relation = await directoryClient.setRelation({

#### 'deleteObject' function

`deleteObject({ objectType: "type-name", objectId: "object-id", withRelations: false })`:
`deleteObject({ objectType: "type-name", objectId: "object-id", withRelations: false }, options?: CallOptions)`:

Deletes an object instance with the specified type and key. For example:

Expand Down Expand Up @@ -616,7 +626,7 @@ You can evaluate graph queries over the directory, to determine whether a subjec

#### 'checkPermission' function

`checkPermission({ objectType: string, objectId: string, permission: string, subjectType: string, subjectId: string, trace: boolean })`:
`checkPermission({ objectType: string, objectId: string, permission: string, subjectType: string, subjectId: string, trace: boolean }, options?: CallOptions)`:

Check that an `user` object with the key `[email protected]` has the `read` permission in the `admin` group:

Expand All @@ -632,7 +642,7 @@ const check = await directoryClient.checkPermission({

#### 'checkRelation' function

`checkRelation({ objectType: string, objectId: string, relation: string, subjectType: string, subjectId: string, trace: boolean })`:
`checkRelation({ objectType: string, objectId: string, relation: string, subjectType: string, subjectId: string, trace: boolean }, options?: CallOptions)`:

Check that `[email protected]` has an `identifier` relation to an object with key `[email protected]` and type `identity`:

Expand Down Expand Up @@ -773,8 +783,29 @@ await (readAsyncIterable(resp))
const response = await readAsyncIterable(
await directoryClient.export({ options: "DATA" })
)

```


### Custom Headers

```ts
// passing custom headers to a request
const user = await directoryClient.object(
{
objectType: "user",
objectId: "[email protected]",
},
{
headers: {
customKey: "customValue",
},
}
);
```



## Deprecated Methods

> Note: the `authorizerServiceUrl` option that is used throughout is no longer a URL, but the option name is retained for backward-compatibility. It is now expected to be a hostname that exposes a gRPC binding. Any "https://" prefix is stripped out of the value provided.
Expand Down
69 changes: 43 additions & 26 deletions __tests__/authorizer/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,20 @@ describe("Is", () => {
name: "todo",
},
};
const result = await authorizer.Is(params);
const options = {
headers: {
customKey: "customValue",
},
};
const result = await authorizer.Is(params, options);

expect(authorizer.AuthClient.is).toHaveBeenCalledWith({
...params,
policyInstance: { ...params.policyInstance, instanceLabel: "todo" },
});
expect(authorizer.AuthClient.is).toHaveBeenCalledWith(
{
...params,
policyInstance: { ...params.policyInstance, instanceLabel: "todo" },
},
options
);

expect(result).toBe(true);

Expand All @@ -66,11 +74,14 @@ describe("Is", () => {
};
const result = await authorizer.Is(params);

expect(authorizer.AuthClient.is).toHaveBeenCalledWith({
...params,
resourceContext: Struct.fromJson(params.resourceContext),
policyInstance: { ...params.policyInstance, instanceLabel: "todo" },
});
expect(authorizer.AuthClient.is).toHaveBeenCalledWith(
{
...params,
resourceContext: Struct.fromJson(params.resourceContext),
policyInstance: { ...params.policyInstance, instanceLabel: "todo" },
},
undefined
);

expect(result).toBe(true);

Expand All @@ -96,11 +107,14 @@ describe("Is", () => {
};
const result = await authorizer.Is(params);

expect(authorizer.AuthClient.is).toHaveBeenCalledWith({
...params,
resourceContext: Struct.fromJson(params.resourceContext),
policyInstance: { ...params.policyInstance, instanceLabel: "todo" },
});
expect(authorizer.AuthClient.is).toHaveBeenCalledWith(
{
...params,
resourceContext: Struct.fromJson(params.resourceContext),
policyInstance: { ...params.policyInstance, instanceLabel: "todo" },
},
undefined
);
expect(result).toBe(true);

mock.mockReset();
Expand All @@ -123,7 +137,7 @@ describe("Is", () => {
};
const result = await authorizer.Is(params);

expect(authorizer.AuthClient.is).toHaveBeenCalledWith(params);
expect(authorizer.AuthClient.is).toHaveBeenCalledWith(params, undefined);

expect(result).toBe(false);

Expand Down Expand Up @@ -241,16 +255,19 @@ describe("Query", () => {
}),
});

expect(authorizer.AuthClient.query).toHaveBeenCalledWith({
query: "query",
input: "input",
options: queryOptions({
metrics: true,
instrument: false,
trace: "FULL",
traceSummary: false,
}),
});
expect(authorizer.AuthClient.query).toHaveBeenCalledWith(
{
query: "query",
input: "input",
options: queryOptions({
metrics: true,
instrument: false,
trace: "FULL",
traceSummary: false,
}),
},
undefined
);

expect(result).toEqual({ key1: "value1", key2: 2 });

Expand Down
Loading
Loading