clean-piano-39737
01/21/2025, 10:34 PMbrief-egg-61061
01/21/2025, 10:34 PMbrief-egg-61061
01/21/2025, 10:34 PMjson { "id": "some-id", "upstream": { "url": "<http://my-backend-service>" }, "match": { "url": "<http://my-app/some-route>", "methods": [ "GET" ] }, "authenticators": [{ "handler": "oauth2_introspection", "config": { "required_scope": ["scope-a", "scope-b"], "target_audience": ["example_audience"] } }], "authorizer": { "handler": "allow", "config": { "required_claims": { "role": ["admin"] } } }, "mutators": [{ "handler": "noop" }] }
In this example, the OAuth2 Introspection authenticator validates the access token, and the Allow authorizer checks for a "role" claim with the value "admin". You would create similar rules for different endpoints and roles. This approach leverages Ory Oathkeeper's decision engine, which processes requests in four steps: access rule matching, authentication, authorization, and mutation, as described in the [Ory Oathkeeper Decision Engine documentation](https://www.ory.sh/docs/oathkeeper#decision-engine). Remember to configure your OAuth2 server (such as Ory Hydra) to include role information in the access tokens or introspection responses. For more detailed information on configuring Ory Oathkeeper, you can refer to the [Ory Oathkeeper API Access Rules documentation](https://www.ory.sh/docs/oathkeeper/api-access-rules#access-rule-format).clean-piano-39737
01/21/2025, 10:36 PMbrief-egg-61061
01/21/2025, 10:36 PMbrief-egg-61061
01/21/2025, 10:36 PMjson
{
"$id": "<https://schemas.ory.sh/presets/kratos/quickstart/phone-password/identity.schema.json>",
"$schema": "<http://json-schema.org/draft-07/schema#>",
"title": "Person",
"type": "object",
"properties": {
"traits": {
"type": "object",
"properties": {
"phone": {
"type": "string",
"title": "Phone number",
"format": "tel",
"<http://ory.sh/kratos|ory.sh/kratos>": {
"credentials": {
"password": {
"identifier": true
}
}
}
}
},
"additionalProperties": false
}
}
}
This configuration is based on the example provided in the [Ory Kratos Phone Number and Password documentation](https://www.ory.sh/docs/kratos/manage-identities/customize-identity-schema#phone-number-and-password).
2. Use Ory Kratos for authentication:
Users will authenticate through Ory Kratos using their phone number and password. Kratos will handle the authentication process and create a session for the user.
3. Configure Ory Oathkeeper:
Set up Ory Oathkeeper as an Identity and Access Proxy in front of your service. Configure it to use the Cookie Session Authenticator, which can validate sessions created by Ory Kratos.
Here's an example configuration for Oathkeeper:
yaml
authenticators:
cookie_session:
enabled: true
config:
check_session_url: <http://kratos:4433/sessions/whoami>
preserve_path: true
extra_from: "@this"
subject_from: "identity.id"
only:
- ory_kratos_session
This configuration is based on the example provided in the [Ory Oathkeeper Cookie Session Authenticator documentation](https://www.ory.sh/docs/oathkeeper/pipeline/authn#cookie_session).
4. Define access rules in Oathkeeper:
Create access rules that use the cookie session authenticator and an appropriate authorizer to check user roles. Here's an example:
yaml
- id: "protected-api"
upstream:
url: "<http://my-api-server>"
match:
url: "<http://my-domain/api/<**>>"
methods:
- GET
- POST
authenticators:
- handler: cookie_session
authorizer:
handler: allow
config:
required_claims:
role: ["admin"]
mutators:
- handler: noop
This rule will allow access to the protected API only for users with the "admin" role.
5. Store user roles:brief-egg-61061
01/21/2025, 10:36 PMjson { "$id": "<https://schemas.ory.sh/presets/kratos/quickstart/phone-password/identity.schema.json>", "$schema": "<http://json-schema.org/draft-07/schema#>", "title": "Person", "type": "object", "properties": { "traits": { "type": "object", "properties": { "phone": { "type": "string", "title": "Phone number", "format": "tel", "<http://ory.sh/kratos|ory.sh/kratos>": { "credentials": { "password": { "identifier": true } } } } }, "additionalProperties": false } } }
This configuration is based on the example provided in the [Ory Kratos Phone Number and Password documentation](https://www.ory.sh/docs/kratos/manage-identities/customize-identity-schema#phone-number-and-password). 2. Use Ory Kratos for authentication: Users will authenticate through Ory Kratos using their phone number and password. Kratos will handle the authentication process and create a session for the user. 3. Configure Ory Oathkeeper: Set up Ory Oathkeeper as an Identity and Access Proxy in front of your service. Configure it to use the Cookie Session Authenticator, which can validate sessions created by Ory Kratos. Here's an example configuration for Oathkeeper: yaml authenticators: cookie_session: enabled: true config: check_session_url: <http://kratos:4433/sessions/whoami> preserve_path: true extra_from: "@this" subject_from: "identity.id" only: - ory_kratos_session
This configuration is based on the example provided in the [Ory Oathkeeper Cookie Session Authenticator documentation](https://www.ory.sh/docs/oathkeeper/pipeline/authn#cookie_session). 4. Define access rules in Oathkeeper: Create access rules that use the cookie session authenticator and an appropriate authorizer to check user roles. Here's an example: yaml - id: "protected-api" upstream: url: "<http://my-api-server>" match: url: "<http://my-domain/api/<**>>" methods: - GET - POST authenticators: - handler: cookie_session authorizer: handler: allow config: required_claims: role: ["admin"] mutators: - handler: noop
This rule will allow access to the protected API only for users with the "admin" role. 5. Store user roles: Ensure that user roles are stored as part of the identity in Ory Kratos. You can add a "role" field to your identity schema and update it when necessary. By following these steps, you can set up a system where users authenticate with phone numbers and passwords through Ory Kratos, and then Ory Oathkeeper handles authorization based on user roles for accessing your service endpoints. Remember to secure your Ory Kratos and Ory Oathkeeper instances as mentioned in the [Ory Self-Hosted API Access Control documentation](https://www.ory.sh/docs/self-hosted/operations/api-access-control). If you need further assistance or have more specific requirements, consider reaching out to the Ory community on their Slack channel for additional support.clean-piano-39737
01/21/2025, 10:38 PMbrief-egg-61061
01/21/2025, 10:38 PMbrief-egg-61061
01/21/2025, 10:38 PMyaml authenticators: bearer_token: enabled: true config: check_session_url: <http://kratos:4433/sessions/whoami> preserve_path: true token_from: header: Authorization subject_from: "identity.id" extra_from: "@this"
This configuration is based on the [Ory Oathkeeper Bearer Token Authenticator documentation](https://www.ory.sh/docs/oathkeeper/pipeline/authn#bearer_token). 3. Define access rules in Oathkeeper: Create access rules that use the bearer token authenticator and an appropriate authorizer to check user roles. Here's an example: yaml - id: "protected-api" upstream: url: "<http://my-api-server>" match: url: "<http://my-domain/api/<**>>" methods: - GET - POST authenticators: - handler: bearer_token authorizer: handler: allow config: required_claims: role: ["admin"] mutators: - handler: noop
This rule will allow access to the protected API only for users with the "admin" role, using the Bearer token for authentication. 4. Token handling: Ensure that your client applications include the Bearer token in the Authorization header of their requests to the protected endpoints. By using this approach, you can leverage Ory Oathkeeper to authorize access based on Bearer tokens issued by Ory Kratos, while still maintaining role-based access control for your service endpoints. Remember to secure your Ory Kratos and Ory Oathkeeper instances, and ensure that your Bearer tokens are handled securely throughout your system. [Ory Oathkeeper Bearer Token Authenticator documentation](https://www.ory.sh/docs/oathkeeper/pipeline/authn#bearer_token) provides more details on configuring and using the Bearer Token Authenticator in Oathkeeper.clean-piano-39737
01/21/2025, 10:39 PMbrief-egg-61061
01/21/2025, 10:39 PMbrief-egg-61061
01/21/2025, 10:39 PMjson { "$id": "<https://schemas.ory.sh/presets/kratos/quickstart/email-password/identity.schema.json>", "$schema": "<http://json-schema.org/draft-07/schema#>", "title": "Person", "type": "object", "properties": { "traits": { "type": "object", "properties": { "email": { "type": "string", "format": "email", "title": "E-Mail", "<http://ory.sh/kratos|ory.sh/kratos>": { "credentials": { "password": { "identifier": true } } } }, "phone": { "type": "string", "title": "Phone number", "format": "tel" }, "role": { "type": "string", "enum": ["admin", "customer"], "title": "Role" } }, "required": ["email", "role"], "additionalProperties": false } } }
2. After updating the schema, you can create an identity with a specific role using the Admin API. Here's an example using curl: bash curl --request POST -sL \ --header "Content-Type: application/json" \ --data '{ "schema_id": "default", "traits": { "email": "<mailto:admin@example.com|admin@example.com>", "phone": "+1234567890", "role": "admin" } }' <http://kratos:4434/admin/identities>
Replace <http://kratos:4434>
with the actual URL of your Kratos Admin API. This will create an identity with the admin role. To create an identity with a customer role, simply change the "role" value to "customer" in the JSON payload. Remember that creating identities through the Admin API bypasses the normal registration flow and any associated hooks or verifications. You should only use this method for administrative purposes, such as creating initial admin accounts. For more details on managing identities, you can refer to the [Ory Kratos Create Identities documentation](https://www.ory.sh/docs/kratos/manage-identities/create-users-identities). After creating the identity, you can use Ory Oathkeeper to enforce role-based access control as described in the previous responses.clean-piano-39737
01/22/2025, 8:20 AMcustomer
?brief-egg-61061
01/22/2025, 8:20 AMbrief-egg-61061
01/22/2025, 8:20 AMjson { "$id": "<https://schemas.ory.sh/presets/kratos/quickstart/email-password/identity.schema.json>", "$schema": "<http://json-schema.org/draft-07/schema#>", "title": "Person", "type": "object", "properties": { "traits": { "type": "object", "properties": { "email": { "type": "string", "format": "email", "title": "E-Mail", "<http://ory.sh/kratos|ory.sh/kratos>": { "credentials": { "password": { "identifier": true } } } }, "phone": { "type": "string", "title": "Phone number", "format": "tel" }, "role": { "type": "string", "enum": ["admin", "customer"], "title": "Role", "default": "customer" } }, "required": ["email"], "additionalProperties": false } } }
In this updated schema, we've added a "default": "customer"
to the role
property. This means that if no role is specified when creating an identity, it will automatically be set to "customer". To apply this updated schema, you need to update your Ory Kratos configuration. You can do this through the Ory Console or by updating your configuration file, as described in the [Ory Kratos Identity Schema documentation](https://www.ory.sh/docs/kratos/manage-identities/identity-schema). After updating the schema, new identities created without specifying a role will have the "customer" role by default. However, you can still explicitly set the role to "admin" when creating identities through the Admin API if needed. Remember that changing the schema doesn't automatically update existing identities. If you need to update existing identities to have the "customer" role, you'll need to do that separately using the Admin API.clean-piano-39737
01/22/2025, 8:41 AMrequired_claims
in oathkeeper access rules. But it didn't make any difference. Users with different roles or with no roles at all are passing. Is there a better way to configure authorization?brief-egg-61061
01/22/2025, 8:41 AMbrief-egg-61061
01/22/2025, 8:41 AMrequired_claims
approach in Oathkeeper's access rules might not be working as expected in your case. Let's explore a better way to configure authorization using Ory Keto, which is specifically designed for fine-grained permission management. Ory Keto is Ory's access control server that can handle complex authorization scenarios. Here's how you can use it in combination with Oathkeeper: 1. First, set up Ory Keto alongside your Ory Kratos and Ory Oathkeeper instances. 2. In Oathkeeper, instead of using the allow
authorizer with required_claims
, you can use the keto_engine_acp_ory
authorizer. This authorizer integrates with Ory Keto to perform access control checks. 3. Configure your Oathkeeper access rule to use the Keto authorizer. Here's an example: yaml - id: "protected-api" upstream: url: "<http://my-api-server>" match: url: "<http://my-domain/api/<**>>" methods: - GET - POST authenticators: - handler: bearer_token authorizer: handler: keto_engine_acp_ory config: required_action: "read" required_resource: "articles" subject: "{{ print .Subject }}" flavor: exact mutators: - handler: noop
4. In Ory Keto, define your permission model. For example: class Role { related: { member: User } permits = { read: Articles } } class User { related: { roles: [Role] } } class Articles { related: { readers: [Role] } permits = { read: readers } }
5. Use the Ory Keto API to create relationships that define your roles and permissions. For example, to give the "admin" role read access to articles: POST /admin/relation-tuples Content-Type: application/json { "namespace": "Role", "object": "admin", "relation": "member", "subject": "user:alice" } POST /admin/relation-tuples Content-Type: application/json { "namespace": "Articles", "object": "all_articles", "relation": "readers", "subject": "Role:admin" }
This approach allows for more flexible and powerful authorization rules. It separates the concerns of authentication (handled by Kratos and verified by Oathkeeper) and authorization (handled by Keto and enforced by Oathkeeper). For more detailed information on setting up and using Ory Keto with Oathkeeper, you can refer to the [Ory Keto documentation](https://www.ory.sh/docs/keto/guides/simple-access-check-guide) and the [Ory Oathkeeper Keto Authorizer documentation](https://www.ory.sh/docs/oathkeeper/pipeline/authz#keto_engine_acp_ory). Remember to thoroughly test your authorization setup to ensure it's working as expected. If you encounter any issues, the Ory community on Slack can be a great resource for troubleshooting and getting more specific advice.