Field-level authorization controls access to specific parts of a resource rather than the entire resource. For example, allowing community moderators to edit usernames but not email addresses.Documentation Index
Fetch the complete documentation index at: https://www.osohq.com/docs/llms.txt
Use this file to discover all available pages before exploring further.
When to Use Field-Level Authorization
Use field-level authorization when both conditions are true: 1. The field depends on parent resource: The field only makes sense in relation to its parent resource. A username belongs to an account and can’t exist independently. 2. The field identifier isn’t globally unique:Field{"username"} doesn’t uniquely identify a specific username, every account has one.
Counter-Examples:
- Files in folders: Files can exist independently and have unique paths.
- Comments on posts: Comments have unique IDs and can be modeled as separate resources.
Field-Level vs Attribute-Based Access Control (ABAC)
Field-level authorization and attribute-based access control (ABAC) serve different purposes:- Field-level: Grants permissions to attributes (e.g., “allow editing the email field”).
- ABAC: Grants permissions based on attributes (e.g., “allow if user’s department = ‘HR’”).
Implementation Strategies
Oso Cloud supports two strategies for field-level authorization.Fields in Permissions
Encode field permissions directly into the parent resource using dot notation like"email.read" and "username.update".
- Best for: Simple policies with few fields
- Pros: Straightforward policy structure
- Cons: Multiplicative permissions to manage, requires client-side processing
How It Works
Add field-specific permissions to the parent resource using dot notation. The permission name combines the field name and action:"username.update", "email.read".
Implementation
We’ll model a social app where community admins can update usernames but not emails, and visitors are limited to reading usernames without access to other field-level data.Client
The benefit of fields in permissions is that you can derive field-level authorization using the actions subcommand. To determine a community_admin’s permissions on an account that is not their own:Fields as Resources
Model each field as its own resource with explicit relationships and customallow_field() rules.
- Best for: Complex field logic with many conditional rules
- Pros: More flexible, integrates well with Oso’s query API
- Cons: More complex policy rules, additional resource modeling
How It Works
Create a separateField resource and use custom allow_field() rules to define field-level permissions. This approach treats fields as first-class resources. In this example, community admins on an organization can update the username field on any account belonging to that organization.
Implementation
In this example, admins can edit any account field, community admins can only update usernames, and all members can read all fields.Client
By modeling fields as resources and introducing a new allow_field rule, we can use the Oso client query subcommand to determine users’ field-level authorization for accounts. To determine charlie’s permissions on alice’s Account:Choosing Between Approaches
| Factor | Fields in permissions | Fields as resources |
|---|---|---|
| Policy complexity | Simple | More complex |
| Number of fields | Best for few fields | Scales well |
| Client integration | Requires parsing | Native query support |
| Conditional logic | Limited | Highly flexible |
| Performance | Fewer rules to evaluate | More rules but more targeted |
- You have a small, stable set of fields
- Field logic is straightforward
- You prefer simpler policies
- You have many fields or complex field logic
- You need flexible conditional rules
- You want native query API support
Further Resources
Field-level authorization enables granular control within resources. Consider these other patterns for your application:- Combine with the ABAC patterns for attribute-based field access.
- Explore conditional roles for dynamic field permissions.