flat-student-27991
06/14/2024, 10:25 PMchilly-king-10285
06/14/2024, 10:25 PMchilly-king-10285
06/14/2024, 10:26 PMDocument
class has different types of related users (owners, editors, viewers) and define what actions each type of user can perform on a document. Here is an example of how you can define such permissions in Ory Keto: typescript import { Namespace, Context } from "@ory/keto-namespace-types" class User implements Namespace {} class Document implements Namespace { related: { owners: User[] editors: User[] viewers: User[] parents: Folder[] } permits = { view: (ctx: Context): boolean => this.related.viewers.includes(ctx.subject) || this.related.editors.includes(ctx.subject) || this.related.owners.includes(ctx.subject) || this.related.parents.traverse((parent) => parent.permits.view(ctx)), edit: (ctx: Context): boolean => this.related.editors.includes(ctx.subject) || this.related.owners.includes(ctx.subject) || this.related.parents.traverse((parent) => parent.permits.edit(ctx)), delete: (ctx: Context): boolean => this.related.owners.includes(ctx.subject) || this.related.parents.traverse((parent) => parent.permits.delete(ctx)), share: (ctx: Context): boolean => this.related.owners.includes(ctx.subject) || this.related.parents.traverse((parent) => parent.permits.share(ctx)), } }
(Source: [Ory Keto Documentation](https://www.ory.sh/docs/keto/modeling/create-permission-model#define-permissions-in-the-opl)) For unregistered users, you can use Ory Kratos to invite them to your application. The process involves creating a basic user account for the user you want to invite using their email address as the account identifier, triggering account recovery for the user account you created, getting the recovery link from the API response and sending it to the user's email address. The user then performs account recovery which forces them to define new credentials and gets a fully functional account with the credentials they defined. (Source: [Ory Kratos Documentation](https://www.ory.sh/docs/kratos/manage-identities/invite-users)) Remember to follow best practices such as not caching permission checks, performing permission checks in every critical path of the application, using fine-grained permissions, and auditing permissions regularly. (Source: [Ory Keto Documentation](https://www.ory.sh/docs/keto/guides/simple-access-check-guide#best-practices))flat-student-27991
06/14/2024, 10:30 PMchilly-king-10285
06/14/2024, 10:30 PMchilly-king-10285
06/14/2024, 10:31 PMjsonnet local claims = std.extVar('claims'); { identity: { traits: { email: claims.email }, metadata_public: { discord_username: claims.discord_username, }, metadata_admin: { // ... }, } }
(Source: [Ory Kratos Documentation](https://www.ory.sh/docs/kratos/social-signin/data-mapping#set-identity-metadata)) If the social sign-in provider does not return all the data you expect, Ory will ask the user to fill in the missing data. For example, if the user did not agree to share their email address when performing the consent step, they will need to agree to your terms of service and provide their email address. The data provided by the user and the data coming from the OpenID Connect / OAuth2 provider will be merged. This process repeats itself until the Identity's traits are valid against the defined JSON Schema. (Source: [Ory Kratos Documentation](https://www.ory.sh/docs/kratos/social-signin/data-mapping#fill-in-the-data-gaps)) If you're still having issues, please ensure that your Jsonnet data mapper is correctly mapping the data from the social sign-in provider to the Identity traits of your project. You can find examples of how to create a data mapping for different social sign-in providers in the Ory Kratos documentation. (Source: [Ory Kratos Documentation](https://www.ory.sh/docs/kratos/social-signin/data-mapping))flat-student-27991
06/14/2024, 10:33 PMchilly-king-10285
06/14/2024, 10:33 PMchilly-king-10285
06/14/2024, 10:34 PMjsonnet local claims = std.extVar('claims'); { identity: { traits: { email: claims.email }, metadata_public: { discord_username: claims.discord_username, }, metadata_admin: { // ... }, } }
(Source: [Ory Kratos Documentation](https://www.ory.sh/docs/kratos/social-signin/data-mapping#set-identity-metadata)) If the social sign-in provider does not return all the data you expect, Ory will ask the user to fill in the missing data. For example, if the user did not agree to share their email address when performing the consent step, they will need to agree to your terms of service and provide their email address. The data provided by the user and the data coming from the OpenID Connect / OAuth2 provider will be merged. This process repeats itself until the Identity's traits are valid against the defined JSON Schema. (Source: [Ory Kratos Documentation](https://www.ory.sh/docs/kratos/social-signin/data-mapping#fill-in-the-data-gaps)) If you're still having issues, please ensure that your Jsonnet data mapper is correctly mapping the data from the social sign-in provider to the Identity traits of your project. You can find examples of how to create a data mapping for different social sign-in providers in the Ory Kratos documentation. (Source: [Ory Kratos Documentation](https://www.ory.sh/docs/kratos/social-signin/data-mapping))flat-student-27991
06/14/2024, 11:04 PMchilly-king-10285
06/14/2024, 11:04 PMchilly-king-10285
06/14/2024, 11:05 PMflat-student-27991
06/14/2024, 11:06 PMchilly-king-10285
06/14/2024, 11:06 PMchilly-king-10285
06/14/2024, 11:07 PMbash # Encode your schema to Base64 and export it to a variable. schema=$(cat {path-to-json-with-identity-schema} | base64) # Update your project's configuration. ory patch identity-config {your-project-id} \ --replace '/identity/default_schema_id="{unique-schema-id}"' \ --replace '/identity/schemas=[{"id":"{unique-schema-id}","url":"base64://'$schema'"}]'
([source](https://www.ory.sh/docs/identities/model/manage-identity-schema))flat-student-27991
06/14/2024, 11:22 PMchilly-king-10285
06/14/2024, 11:22 PMchilly-king-10285
06/14/2024, 11:23 PMtypescript import { Namespace, Context } from "@ory/keto-namespace-types" class User implements Namespace {} class Document implements Namespace { related: { owners: User[] editors: User[] viewers: User[] parents: Folder[] } permits = { view: (ctx: Context): boolean => this.related.viewers.includes(ctx.subject) || this.related.editors.includes(ctx.subject) || this.related.owners.includes(ctx.subject) || this.related.parents.traverse((parent) => parent.permits.view(ctx)), edit: (ctx: Context): boolean => this.related.editors.includes(ctx.subject) || this.related.owners.includes(ctx.subject) || this.related.parents.traverse((parent) => parent.permits.edit(ctx)), delete: (ctx: Context): boolean => this.related.owners.includes(ctx.subject) || this.related.parents.traverse((parent) => parent.permits.delete(ctx)), share: (ctx: Context): boolean => this.related.owners.includes(ctx.subject) || this.related.parents.traverse((parent) => parent.permits.share(ctx)), } } class Folder implements Namespace { related: { owners: User[] editors: User[] viewers: User[] parents: Folder[] } permits = { delete: (ctx: Context): boolean => this.related.owners.includes(ctx.subject) || this.related.parents.traverse((parent) => parent.permits.delete(ctx)), share: (ctx: Context): boolean => this.related.owners.includes(ctx.subject) || this.related.parents.traverse((parent) => parent.permits.share(ctx)), } }
(Source: [Ory Keto Documentation](https://www.ory.sh/docs/keto/modeling/create-permission-model#define-permissions-in-the-opl)) For more detailed examples of Identity Schemas, you can refer to the [Ory Kratos Identity Schema documentation](https://www.ory.sh/kratos/docs/concepts/identity-schema).flat-student-27991
06/15/2024, 3:37 AMchilly-king-10285
06/15/2024, 3:37 AMchilly-king-10285
06/15/2024, 3:37 AMFolder
as a subject, you could potentially create an Identity Schema for it. Here's an example of how you might define an Identity Schema for a Folder
subject, identified by a `path`: json { "$id": "<https://schemas.ory.sh/presets/kratos/quickstart/folder.schema.json>", "$schema": "<http://json-schema.org/draft-07/schema#>", "title": "Folder", "type": "object", "properties": { "path": { "type": "string", "description": "The path of the folder", "format": "uri" } }, "required": ["path"] }
In this schema, path
is a required property of type string
and it should be a valid URI. Please note that this is a hypothetical example. In a typical use case, Ory Kratos is used to manage user identities, and Ory Keto is used to manage permissions for these identities on various resources (like folders). The identities (subjects) are usually users, not resources themselves. For more information on how to define Identity Schemas, you can refer to the [Ory Kratos Identity Schema documentation](https://www.ory.sh/kratos/docs/concepts/identity-schema).flat-student-27991
06/15/2024, 4:53 AMchilly-king-10285
06/15/2024, 4:53 AMchilly-king-10285
06/15/2024, 4:54 AM<http://mydomain.com/schemas/v2/customer.schema.json>
- Grandfather Accounts: <http://mydomain.com/schemas/v1/customer.schema.json>
- Service Accounts: <http://mydomain.com/schemas/service-account.schema.json>
Ory Identities expects the JSON Schemas in its configuration file: json identity: # This will be the default JSON Schema. If `schema_id` is empty when creating an identity using the # Admin API or a user signs up using a selfservice flow, this schema will be used. # # This is a required configuration field! default_schema_id: person # Optionally define additional schemas here: schemas: # When creating an identity that uses this schema, `traits_schema_id: customer` are set for that identity. - id: customer
(Source: [Ory Kratos Documentation](https://www.ory.sh/docs/kratos/manage-identities/managing-users-identities-metadata#traits))