some-painting-28549
05/16/2024, 7:05 PMchilly-king-10285
05/16/2024, 7:05 PMchilly-king-10285
05/16/2024, 7:05 PMbash ory update identity-config {project-id} --file /path/to/your/config.yaml
In this command, replace {project-id}
with the ID of your project, and /path/to/your/config.yaml
with the path to your configuration file. The --file
flag is used to specify the configuration file(s) that you want to use to update the identity config. You can specify multiple files by repeating the --file
flag followed by the path to the file. The files can be in JSON or YAML format, and can be local files or URLs. Here are some other flags that you can use with this command: - `--format`: Set the output format. One of default, json, yaml, json-pretty, jsonpath and jsonpointer. (default "default") - `-h, --help`: Show help for the identity-config
command. - `-q, --quiet`: Be quiet with output printing. - `-y, --yes`: Confirm all dialogs with yes. You can find more information about this command in the [Ory documentation](https://www.ory.sh/docs/cli/ory-update-identity-config#ory-update-identity-config).some-painting-28549
05/16/2024, 7:18 PMchilly-king-10285
05/16/2024, 7:18 PMchilly-king-10285
05/16/2024, 7:19 PMjavascript // Create a relationship await ory .relationship .createRelationship({createRelationshipBody: {/* ... */}})
2. Assign Permissions: Permissions are just another form of relations. Therefore, assigning permissions is similar to creating relationships. For example, you could create a relationship that says "members of group1 are readers of file1", which effectively gives all members of group1 read access to file1. 3. Check Permissions: You can check whether a subject has a certain relation to an object, possibly through one or more indirections. Here's an example of how to check a permission using the SDK: javascript // Check a permission const isAllowed = await ory .permission .checkPermission({namespace: "n", /* ... */}) .then(({data}) => data.allowed)
In this example, isAllowed
will be true
if the subject has the specified permission, and false
otherwise. For more detailed information, you can refer to the [Ory Permissions Guide](https://www.ory.sh/docs/guides/permissions/overview#basic-concepts) and the [Ory SDK Documentation](https://www.ory.sh/docs/guides/upgrade/sdk-v1#permissions--access-control).some-painting-28549
05/16/2024, 7:22 PMsome-painting-28549
05/16/2024, 7:22 PMchilly-king-10285
05/16/2024, 7:22 PMchilly-king-10285
05/16/2024, 7:23 PMpython from __future__ import print_function import time import ory_keto_client from ory_keto_client.rest import ApiException from pprint import pprint # Defining the host is optional and defaults to <http://localhost> # See configuration.py for a list of all supported configuration parameters. configuration = ory_keto_client.Configuration( host = "<http://localhost>" ) # Enter a context with an instance of the API client with ory_keto_client.ApiClient(configuration) as api_client: # Create an instance of the API class api_instance = ory_keto_client.PermissionApi(api_client) namespace = 'namespace_example' # str | object = 'object_example' # str | relation = 'relation_example' # str | subject_id = 'subject_id_example' # str | subject_set_namespace = 'subject_set_namespace_example' # str | subject_set_object = 'subject_set_object_example' # str | subject_set_relation = 'subject_set_relation_example' # str | max_depth = 56 # int | (optional) try: # Check a permission api_response = api_instance.check_permission(namespace=namespace, object=object, relation=relation, subject_id=subject_id, subject_set_namespace=subject_set_namespace, subject_set_object=subject_set_object, subject_set_relation=subject_set_relation, max_depth=max_depth) pprint(api_response) except ApiException as e: print("Exception when calling PermissionApi->check_permission: %s\n" % e)
In this example, replace the namespace
, object
, relation
, subject_id
, subject_set_namespace
, subject_set_object
, subject_set_relation
, and max_depth
variables with your own values. You can find more information about the Python SDK in the [Ory Keto Python SDK Documentation](https://www.ory.sh/docs/keto/sdk/python#example).some-painting-28549
05/17/2024, 10:48 AMchilly-king-10285
05/17/2024, 10:48 AMchilly-king-10285
05/17/2024, 10:48 AMmember
, admin
, and superadmin
. You can define these namespaces in your Ory Keto configuration file as shown in the [Ory Keto RBAC Guide](https://www.ory.sh/docs/keto/guides/rbac#rbac-with-ory-keto). Here's an example of how you might define these namespaces: yaml namespaces: - id: 0 name: member - id: 1 name: admin - id: 2 name: superadmin
Once you've defined your namespaces, you can create relationships between subjects (users) and objects (API calls) using the Ory Keto Python SDK. For example, you might create a relationship that says "user1 is a member and can access api_call1". To check permissions at the endpoint level, you would use the check_permission
method of the PermissionApi
class in the Ory Keto Python SDK. Here's an example of how you might do this: python from ory_keto_client.api import permission_api from ory_keto_client.model.check_permission_result import CheckPermissionResult from ory_keto_client.model.error_generic import ErrorGeneric from pprint import pprint # Create an instance of the API class api_instance = permission_api.PermissionApi(api_client) # Define the namespace, object, relation, and subject namespace = "member" # str | Namespace of the Relationship object = "api_call1" # str | Object of the Relationship relation = "access" # str | Relation of the Relationship subject_id = "user1" # str | SubjectID of the Relationship # Check the permission api_response = api_instance.check_permission(namespace=namespace, object=object, relation=relation, subject_id=subject_id) pprint(api_response)
In this example, api_response
will contain the result of the permission check. If api_response.allowed
is True
, then the subject has the specified permission. If it's False
, then the subject does not have the permission. You can find more information about the Python SDK in the [Ory Keto Python SDK Documentation](https://www.ory.sh/docs/keto/sdk/python#example).some-painting-28549
05/17/2024, 10:50 AMchilly-king-10285
05/17/2024, 10:50 AMchilly-king-10285
05/17/2024, 10:51 AMcreate_relation_tuple
method of the RelationTupleApi
class in the Ory Keto Python SDK. Here's an example: python from ory_keto_client.api import relation_tuple_api from ory_keto_client.model.relation_tuple import RelationTuple from pprint import pprint # Create an instance of the API class api_instance = relation_tuple_api.RelationTupleApi(api_client) # Define the namespace, object, relation, and subject namespace = "documents" # str | Namespace of the Relationship object = "document1" # str | Object of the Relationship relation = "read" # str | Relation of the Relationship subject = "user1" # str | Subject of the Relationship # Create the relation tuple relation_tuple = RelationTuple(namespace=namespace, object=object, relation=relation, subject=subject) # Create the relationship api_instance.create_relation_tuple(relation_tuple)
To get the relationships of an identity within a namespace, you would use the get_relation_tuples
method of the RelationTupleApi
class. Here's an example: python from ory_keto_client.api import relation_tuple_api from pprint import pprint # Create an instance of the API class api_instance = relation_tuple_api.RelationTupleApi(api_client) # Define the namespace and subject namespace = "documents" # str | Namespace of the Relationship subject = "user1" # str | Subject of the Relationship # Get the relationships api_response = api_instance.get_relation_tuples(namespace=namespace, subject=subject) pprint(api_response)
In this example, api_response
will contain the relationships of the subject within the namespace. You can find more information about the Python SDK in the [Ory Keto Python SDK Documentation](https://www.ory.sh/docs/keto/sdk/python#example).some-painting-28549
05/17/2024, 11:06 AMchilly-king-10285
05/17/2024, 11:06 AMchilly-king-10285
05/17/2024, 11:06 AMyaml namespaces: - id: 0 name: OrgMember - id: 1 name: Admin - id: 2 name: SuperAdmin - id: 3 name: MemberApi - id: 4 name: AdminApi
Next, you would create relationships between these groups and the APIs they are allowed to access. For example, you might create relationships that say "OrgMember can access MemberApi", "Admin can access MemberApi and AdminApi", and "SuperAdmin can access MemberApi and AdminApi". Here's an example of how you might define these relationships in a policies.rts
file: plaintext MemberApi#access@(OrgMember#member) MemberApi#access@(Admin#member) AdminApi#access@(Admin#member) MemberApi#access@(SuperAdmin#member) AdminApi#access@(SuperAdmin#member)
Then, you can use the Ory Keto Python SDK to create these relationships: python from ory_keto_client.api import relation_tuple_api from ory_keto_client.model.relation_tuple import RelationTuple from pprint import pprint # Create an instance of the API class api_instance = relation_tuple_api.RelationTupleApi(api_client) # Define the relationships relationships = [ RelationTuple(namespace="MemberApi", object="access", relation="OrgMember", subject="member"), RelationTuple(namespace="MemberApi", object="access", relation="Admin", subject="member"), RelationTuple(namespace="AdminApi", object="access", relation="Admin", subject="member"), RelationTuple(namespace="MemberApi", object="access", relation="SuperAdmin", subject="member"), RelationTuple(namespace="AdminApi", object="access", relation="SuperAdmin", subject="member"), ] # Create the relationships for relationship in relationships: api_instance.create_relation_tuple(relationship)
You can find more information about the Python SDK in the [Ory Keto Python SDK Documentation](https://www.ory.sh/docs/keto/sdk/python#example).some-painting-28549
05/17/2024, 11:10 AMimport { Namespace, SubjectSet, Context } from "@ory/permission-namespace-types"
class User implements Namespace {
related: {
manager: User[]
}
}
class Group implements Namespace {
related: {
members: (User | Group)[]
}
}
/*
Define your OPL rules here. Some examples:
class User implements Namespace {
related: {
manager: User[]
}
}
class Group implements Namespace {
related: {
members: (User | Group)[]
}
}
class Folder implements Namespace {
related: {
parents: (File | Folder)[]
viewers: SubjectSet<Group, "members">[]
}
permits = {
view: (ctx: Context): boolean =>
this.related.viewers.includes(ctx.subject) ||
this.related.parents.traverse((p) => p.permits.view(ctx)),
}
}
class File implements Namespace {
related: {
parents: (File | Folder)[]
viewers: (User | SubjectSet<Group, "members">)[]
owners: (User | SubjectSet<Group, "members">)[]
}
permits = {
view: (ctx: Context): boolean =>
this.related.parents.traverse((p) => p.permits.view(ctx)) ||
this.related.viewers.includes(ctx.subject) ||
this.related.owners.includes(ctx.subject),
edit: (ctx: Context) => this.related.owners.includes(ctx.subject),
}
}
*/
chilly-king-10285
05/17/2024, 11:10 AMchilly-king-10285
05/17/2024, 11:11 AMtypescript import { Namespace, Context } from "@ory/keto-namespace-types" class OrgMember implements Namespace { related: { member: User[] } } class Admin implements Namespace { related: { member: User[] } } class SuperAdmin implements Namespace { related: { member: User[] } } class MemberApi implements Namespace { related: { access: (OrgMember | Admin | SuperAdmin)[] } permits = { use: (ctx: Context): boolean => this.related.access.includes(ctx.subject), } } class AdminApi implements Namespace { related: { access: (Admin | SuperAdmin)[] } permits = { use: (ctx: Context): boolean => this.related.access.includes(ctx.subject), } }
In this example, OrgMember
, Admin
, and SuperAdmin
are the groups (roles), and MemberApi
and AdminApi
are the namespaces (APIs). The related
property of each class defines the relationships between the groups and the APIs. The permits
property of each API class defines the permissions for using the API. You can find more information about defining permissions in the Ory Permission Language (OPL) in the [Ory Documentation](https://www.ory.sh/docs/keto/modeling/create-permission-model#define-permissions-in-the-opl).