Skip to content

Commit

Permalink
Merge pull request #12 from getlarge/11-feat-create-new-cli-app-to-in…
Browse files Browse the repository at this point in the history
…teract-with-self-service-public-api-from-ory-kratos

feat: create new cli app to interact with self service public api from ory kratos
  • Loading branch information
getlarge authored Apr 4, 2024
2 parents 76eba48 + e0e30d3 commit 5fc5427
Show file tree
Hide file tree
Showing 30 changed files with 872 additions and 47 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This is a mono repository containing the following packages:
| [keto-relations-parser](./packages/keto-relations-parser/README.md) | A TS library to parse a string representation of a Relation tuple using [Zanzibar](https://zanzibar.academy) notation. | [![npm](https://img.shields.io/npm/v/@getlarge/keto-relations-parser.svg?style=flat)](https://npmjs.org/package/@getlarge/keto-relations-parser) |
| [kratos-client-wrapper](./packages/kratos-client-wrapper/README.md) | A NestJS library to integrate [Ory Kratos](https://www.ory.sh/kratos/docs/) API | [![npm](https://img.shields.io/npm/v/@getlarge/kratos-client-wrapper.svg?style=flat)](https://npmjs.org/package/@getlarge/kratos-client-wrapper) |
| [keto-cli](./packages/keto-cli/README.md) | A CLI to interact with the [Ory Keto](https://www.ory.sh/keto/docs/) API | [![npm](https://img.shields.io/npm/v/@getlarge/keto-cli.svg?style=flat)](https://npmjs.org/package/@getlarge/keto-cli) |
| [kratos-cli](./packages/kratos-cli/README.md) | A CLI to interact with the [Ory Kratos](https://www.ory.sh/kratos/docs/) self-service API | [![npm](https://img.shields.io/npm/v/@getlarge/kratos-cli.svg?style=flat)](https://npmjs.org/package/@getlarge/kratos-cli) |

## Installation and usage

Expand Down
123 changes: 115 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions packages/keto-cli/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
"checkObsoleteDependencies": true,
"checkVersionMismatches": true,
"includeTransitiveDependencies": false,
"ignoredDependencies": [
"@getlarge/base-client-wrapper",
"@nestjs/testing"
]
"ignoredDependencies": ["@getlarge/base-client-wrapper"]
}
]
}
Expand Down
21 changes: 11 additions & 10 deletions packages/keto-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
[npm-image]: https://img.shields.io/npm/v/@getlarge/keto-cli.svg?style=flat
[npm-url]: https://npmjs.org/package/@getlarge/keto-cli

This library is a wrapper around the [Ory Keto](https://www.ory.sh/keto/docs/) client - [@ory/client](https://github.com/ory/client-js). It provides :

- `OryRelationshipsModule`: a module to interact with the Ory Keto Relationships API
- `OryPermissionsModule`: a module to interact with the Ory Keto Permissions API
- `OryAuthorizationGuard`: a guard to protect your routes based on the Ory Keto permissions
This CLI interacts with the [Ory Keto](https://www.ory.sh/keto/docs/) API.

## Install

Expand All @@ -23,37 +19,42 @@ npm install @getlarge/keto-cli

```sh
# base path is the Ory Keto Public API URL - it can be passed via ORY_KETO_PUBLIC_URL environment variable
npx @getlarge/keto-cli check --tuple Group:admin#members@User:1 --basePath http://localhost:4466
npx @getlarge/keto-cli check --tuple Group:admin#members@User:1 \
--basePath http://localhost:4466
```

### To create a relationship

```sh
# base path is the Ory Keto Admin API URL - it can be passed via ORY_KETO_PUBLIC_URL environment variable
# access token is the Ory Keto Admin API access token - it should be passed via ORY_KETO_API_KEY environment variable
npx @getlarge/keto-cli create --tuple Group:admin#members@User:1 --basePath http://localhost:4467 --accessToken my-access-token
npx @getlarge/keto-cli create --tuple Group:admin#members@User:1 \
--basePath http://localhost:4467 --accessToken my-access-token
```

### To delete a relationship

```sh
# base path is the Ory Keto Admin API URL - it can be passed via ORY_KETO_PUBLIC_URL environment variable
# access token is the Ory Keto Admin API access token - it should be passed via ORY_KETO_API_KEY environment variable
npx @getlarge/keto-cli delete --tuple Group:admin#members@User:1 --basePath http://localhost:4467 --accessToken my-access-token
npx @getlarge/keto-cli delete --tuple Group:admin#members@User:1 \
--basePath http://localhost:4467 --accessToken my-access-token
```

### To expand a relation tuple

```sh
npx @getlarge/keto-cli expand --tuple Group:admin#members --depth 2 --basePath http://localhost:4466
npx @getlarge/keto-cli expand --tuple Group:admin#members --depth 2 \
--basePath http://localhost:4466
```

### To list all relationships

```sh
# base path is the Ory Keto Admin API URL - it can be passed via ORY_KETO_PUBLIC_URL environment variable
# access token is the Ory Keto Admin API access token - it should be passed via ORY_KETO_API_KEY environment variable
npx @getlarge/keto-cli get --namespace Group --object admin --basePath http://localhost:4467 --accessToken my-access-token
npx @getlarge/keto-cli get --namespace Group --object admin \
--basePath http://localhost:4467 --accessToken my-access-token
```

## Development
Expand Down
11 changes: 5 additions & 6 deletions packages/keto-cli/src/app/check-permission.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import {
} from '@ory/client';
import { Command, CommandRunner, Option } from 'nest-commander';

interface CommandOptions
extends Pick<Configuration, 'basePath' | 'accessToken'> {
interface CommandOptions extends Pick<Configuration, 'basePath'> {
tuple: PermissionApiCheckPermissionRequest;
}

Expand All @@ -31,8 +30,8 @@ export class CheckPermissionCommand extends CommandRunner {
...options,
});
}
const isAllowed = await this.oryPermissionsService.checkPermission(tuple);
this.logger.log(`Permission ${isAllowed ? 'granted' : 'denied'}`);
const { data } = await this.oryPermissionsService.checkPermission(tuple);
this.logger.log(`Permission ${data.allowed ? 'granted' : 'denied'}`);
}

@Option({
Expand All @@ -50,8 +49,8 @@ export class CheckPermissionCommand extends CommandRunner {
}

@Option({
flags: '-b, --base-path [string]',
description: 'Ory Keto Admin URL',
flags: '-b, --basePath [string]',
description: 'Ory Keto Public API URL',
required: false,
})
parseBasePath(val: string): string {
Expand Down
2 changes: 1 addition & 1 deletion packages/keto-cli/src/app/create-relation.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class CreateRelationCommand extends CommandRunner {

@Option({
flags: '-b, --basePath [string]',
description: 'Ory Keto Admin URL',
description: 'Ory Keto Admin API URL',
required: false,
})
parseBasePath(val: string): string | undefined {
Expand Down
2 changes: 1 addition & 1 deletion packages/keto-cli/src/app/delete-relation.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class DeleteRelationCommand extends CommandRunner {

@Option({
flags: '-b, --basePath [string]',
description: 'Ory Keto Admin URL',
description: 'Ory Keto Admin API URL',
required: false,
})
parseBasePath(val: string): string | undefined {
Expand Down
27 changes: 26 additions & 1 deletion packages/keto-cli/src/app/expand-permissions.command.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('ExpandPermissionsCommand', () => {
});

describe('run', () => {
it('should process tuple and check permission', async () => {
it('should process tuple and expand permission', async () => {
const tuple = {
namespace: 'Group',
object: 'admin',
Expand All @@ -49,6 +49,31 @@ describe('ExpandPermissionsCommand', () => {
oryPermissionsService.expandPermissions = jest.fn().mockResolvedValue({
data: {
type: 'union',
children: [
{
type: 'leaf',
tuple: {
namespace: '',
object: '',
relation: '',
subject_set: {
namespace: 'User',
object: '1',
relation: '',
},
},
},
],
tuple: {
namespace: '',
object: '',
relation: '',
subject_set: {
namespace: 'Group',
object: 'admin',
relation: 'members',
},
},
},
});

Expand Down
4 changes: 2 additions & 2 deletions packages/keto-cli/src/app/expand-permissions.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class ExpandPermissionsCommand extends CommandRunner {
...options,
});
}
const tree = await this.oryPermissionsService.expandPermissions({
const { data: tree } = await this.oryPermissionsService.expandPermissions({
...tuple,
maxDepth: depth,
});
Expand Down Expand Up @@ -66,7 +66,7 @@ export class ExpandPermissionsCommand extends CommandRunner {

@Option({
flags: '-b, --basePath [string]',
description: 'Ory Keto Public URL',
description: 'Ory Keto Public API URL',
required: false,
})
parseBasePath(val: string): string | undefined {
Expand Down
Loading

0 comments on commit 5fc5427

Please sign in to comment.