Implementing Authorization

1. Setting Up Role-Based Access Control (RBAC)

ComponentExample
Roleadmin, editor, viewer
Permissionorders:read, orders:write
AssignmentUser → Role(s) → Permissions
StorageJWT roles claim or RBAC service

Example: Role check (Lua)

local roles = kong.ctx.shared.jwt_payload.roles or {}
local required = "admin"
local allowed = false
for _, r in ipairs(roles) do
  if r == required then allowed = true break end
end
if not allowed then
  return kong.response.exit(403, { error = "forbidden" })
end

2. Using Attribute-Based Access Control (ABAC)

Attribute TypeExample
Subjectuser.dept, user.clearance
Resourceresource.owner, resource.tier
Actionread, write, delete
Environmenttime.now, request.ip

3. Implementing Policy-Based Authorization

Example: OPA Rego policy

package http.authz

default allow = false

allow {
  input.method == "GET"
  input.path[0] == "v1"
  input.path[1] == "orders"
  token.payload.scope[_] == "orders:read"
}

token := { "payload": payload } {
  [_, payload, _] := io.jwt.decode(input.headers.authorization)
}

4. Configuring Scope-Based Access

ScopeAllows
read:usersGET /users
write:usersPOST/PUT/PATCH /users
delete:usersDELETE /users
admin:*All operations
openid emailOIDC standard

5. Using Claims-Based Authorization

Example: Tenant claim check

local payload = kong.ctx.shared.jwt_payload
local req_tenant = kong.request.get_query_arg("tenant")
if payload.tenant ~= req_tenant then
  return kong.response.exit(403, { error = "tenant_mismatch" })
end

6. Implementing Resource-Level Permissions

CheckExample
Ownershipresource.owner_id == user.id
SharingUser in resource ACL
VisibilityPublic/private flag
Group membershipUser in resource's group

7. Configuring Permission Hierarchies

super_admin
   ├── admin
   │     ├── editor (read+write)
   │     │     └── viewer (read)
   │     └── moderator
   └── auditor (read all, no write)
      

8. Setting Up Access Control Lists (ACL)

Example: ACL plugin

plugins:
  - name: acl
    config:
      allow: ["premium-tier", "enterprise-tier"]
      deny: ["banned-users"]
      hide_groups_header: true

9. Using Dynamic Authorization Rules

SourceExample
External PDPOPA, Cedar, Zanzibar
Feature flagsLaunchDarkly, Unleash
DatabasePer-tenant policy table
Config serviceHot-reloaded JSON rules

10. Implementing Authorization Caching

Cache LayerTTLTrade-off
Per-request memoRequest lifetimeNone
Local LRU1-5 minStale revocation
Shared (Redis)5-15 minNetwork hop
Negative cache30sLimit DoS