Security & Access Control
Security & Access Control
Section titled “Security & Access Control”Manage authentication actors, authorization scopes, and access policies.
Loading
Section titled “Loading”local security = require("security")Returns the current security actor from the execution context.
local actor = security.actor()if actor then local id = actor:id() local meta = actor:meta()
logger:info("Request from", { user_id = id, role = meta.role })endReturns: Actor|nil
Returns the current security scope from the execution context.
local scope = security.scope()if scope then local policies = scope:policies() for _, policy in ipairs(policies) do print("Active policy:", policy:id()) endendReturns: Scope|nil
Checks if the current context allows an action on a resource.
-- Check read permissionif not security.can("read", "user:" .. user_id) then return nil, errors.new("PERMISSION_DENIED", "Cannot read user data")end
-- Check write permissionif not security.can("write", "order:" .. order_id) then return nil, errors.new("PERMISSION_DENIED", "Cannot modify order")end
-- Check with metadatalocal allowed = security.can("delete", "document:" .. doc_id, { owner_id = doc.owner_id, department = doc.department})| Parameter | Type | Description |
|---|---|---|
action | string | Action to check |
resource | string | Resource identifier |
meta | table | Additional metadata (optional) |
Returns: boolean
new_actor
Section titled “new_actor”Creates a new actor with ID and metadata.
-- Create user actorlocal actor = security.new_actor("user:" .. user.id, { role = user.role, department = user.department, email = user.email})
-- Create service actorlocal service_actor = security.new_actor("service:payment-processor", { type = "service", version = "1.0.0"})| Parameter | Type | Description |
|---|---|---|
id | string | Unique actor identifier |
meta | table | Metadata key-value pairs |
Returns: Actor
new_scope
Section titled “new_scope”Creates a new custom scope.
-- Empty scopelocal scope = security.new_scope()
-- Scope with policieslocal read_policy = security.policy("app:read-only")local scope = security.new_scope({read_policy})
-- Build scope incrementallylocal scope = security.new_scope()local policy1 = security.policy("app:read")local policy2 = security.policy("app:write")scope = scope:with(policy1):with(policy2)Returns: Scope
policy
Section titled “policy”Retrieves a policy from the registry.
local policy, err = security.policy("app:admin-access")if err then return nil, errend
-- Evaluate policylocal result = policy:evaluate(actor, "delete", "user:123")if result == "allow" then -- permittedelseif result == "deny" then -- forbiddenelse -- undefined, check other policiesend| Parameter | Type | Description |
|---|---|---|
id | string | Policy ID “namespace:name” |
Returns: Policy, error
named_scope
Section titled “named_scope”Retrieves a pre-defined policy group.
-- Get admin scopelocal admin_scope, err = security.named_scope("app:admin")if err then return nil, errend
-- Use for elevated operationslocal result = admin_scope:evaluate(actor, "delete", "user:123")| Parameter | Type | Description |
|---|---|---|
id | string | Policy group ID |
Returns: Scope, error
token_store
Section titled “token_store”Acquires a token store for managing authentication tokens.
local store, err = security.token_store("app:tokens")if err then return nil, errend
-- Use store...store:close()| Parameter | Type | Description |
|---|---|---|
id | string | Token store ID “namespace:name” |
Returns: TokenStore, error
Actor Methods
Section titled “Actor Methods”| Method | Returns | Description |
|---|---|---|
actor:id() | string | Actor identifier |
actor:meta() | table | Actor metadata |
Scope Methods
Section titled “Scope Methods”with / without
Section titled “with / without”Add or remove policies from scope.
local scope = security.new_scope()
-- Add policylocal write_policy = security.policy("app:write")scope = scope:with(write_policy)
-- Remove policyscope = scope:without("app:read-only")evaluate
Section titled “evaluate”Evaluate all policies in scope.
local result = scope:evaluate(actor, "read", "document:123")-- "allow", "deny", or "undefined"
if result ~= "allow" then return nil, errors.new("PERMISSION_DENIED", "Access denied")endcontains
Section titled “contains”Check if scope contains a policy.
if scope:contains("app:admin") then show_admin_features()endpolicies
Section titled “policies”Returns all policies in scope.
local policies = scope:policies()for _, policy in ipairs(policies) do print(policy:id())endReturns: Policy[]
Policy Methods
Section titled “Policy Methods”| Method | Returns | Description |
|---|---|---|
policy:id() | string | Policy identifier |
policy:evaluate(actor, action, resource, meta?) | string | "allow", "deny", or "undefined" |
TokenStore Methods
Section titled “TokenStore Methods”create
Section titled “create”Create authentication token.
local actor = security.new_actor("user:123", {role = "user"})local scope = security.named_scope("app:default")
local token, err = store:create(actor, scope, { expiration = "24h", -- or milliseconds meta = { login_ip = request_ip, user_agent = user_agent }})| Parameter | Type | Description |
|---|---|---|
actor | Actor | Actor for the token |
scope | Scope | Permissions scope |
options.expiration | string/number | Duration string or ms |
options.meta | table | Token metadata |
Returns: string, error
validate
Section titled “validate”Validate token and get actor/scope.
local actor, scope, err = store:validate(token)if err then return nil, errors.new("UNAUTHENTICATED", "Invalid token")endReturns: Actor, Scope, error
revoke
Section titled “revoke”Invalidate a token.
local ok, err = store:revoke(token)Returns: boolean, error
Release the token store resource.
store:close()Returns: boolean
Permissions
Section titled “Permissions”Security operations are subject to security policy evaluation.
Security Actions
Section titled “Security Actions”| Action | Resource | Description |
|---|---|---|
security.policy.get | Policy ID | Access policy definitions |
security.policy_group.get | Group ID | Access named scopes |
security.scope.create | custom | Create custom scopes |
security.actor.create | Actor ID | Create actors |
security.token_store.get | Store ID | Access token stores |
security.token.validate | Store ID | Validate tokens |
security.token.create | Store ID | Create tokens |
security.token.revoke | Store ID | Revoke tokens |
See Security Model for policy configuration.
Errors
Section titled “Errors”| Condition | Kind | Retryable |
|---|---|---|
| No context | errors.INTERNAL | no |
| Empty token store ID | errors.INVALID | no |
| Permission denied | errors.INVALID | no |
| Policy not found | errors.INTERNAL | no |
| Token store not found | errors.INTERNAL | no |
| Token store closed | errors.INTERNAL | no |
| Invalid expiration format | errors.INVALID | no |
| Token validation failed | errors.INTERNAL | no |
local store, err = security.token_store("app:tokens")if err then if errors.is(err, errors.INVALID) then print("Invalid request:", err:message()) end return nil, errendSee Error Handling for working with errors.
See Also
Section titled “See Also”- Security Model - Actors, policies, scopes configuration
- HTTP Middleware - Endpoint and resource firewall