Skip to main content

authorize

Attribute Macro authorize 

Source
#[authorize]
Expand description

Attribute macro for method-level authorization with structured scopes.

Generates a wrapper that validates JWT tokens and checks structured scopes before calling the original method. This provides compile-time enforcement of authorization requirements.

§Attributes

  • action - The action being performed (e.g., “infer”, “read”, “write”)
  • resource - The resource type (e.g., “model”, “stream”, “policy”)
  • identifier_field - Field name in request containing the resource ID

§Example

ⓘ
impl InferenceService {
    #[authorize(action = "infer", resource = "model", identifier_field = "model_name")]
    fn handle_generate(&self, ctx: &EnvelopeContext, request: GenerateRequest) -> Result<Vec<u8>> {
        // Authorization already validated - just implement business logic
        // ctx.user_claims is guaranteed to exist here
    }
}

§Generated Code

The macro generates:

  1. JWT token validation via ctx.validate_jwt()
  2. Structured scope construction: Scope::new(action, resource, request.identifier_field)
  3. Casbin policy check (single source of truth for authorization)
  4. Original method call if all checks pass

§Security

Defense-in-depth with three validation layers:

  • Layer 1: Envelope signature (service identity)
  • Layer 2: Casbin policy (RBAC/ABAC)
  • Layer 3: JWT scope (structured, least privilege)