pub struct SecurityContext {Show 13 fields
pub user_id: UserId,
pub roles: Vec<String>,
pub tenant_id: Option<TenantId>,
pub scopes: Vec<String>,
pub attributes: HashMap<String, Value>,
pub request_id: String,
pub ip_address: Option<String>,
pub authenticated_at: DateTime<Utc>,
pub expires_at: DateTime<Utc>,
pub issuer: Option<String>,
pub audience: Option<String>,
pub email: Option<String>,
pub display_name: Option<String>,
}Expand description
Security context for authorization evaluation.
Carries information about the authenticated user and their permissions throughout the request lifecycle.
§Fields
user_id: Unique identifier for the authenticated user (from JWT ‘sub’ claim)roles: User’s roles (e.g.,["admin", "moderator"], from JWT ‘roles’ claim)tenant_id: Organization/tenant identifier for multi-tenant systemsscopes: OAuth/permission scopes (e.g.,["read:user", "write:post"])attributes: Custom claims from JWT (e.g., department, region, tier)request_id: Correlation ID for audit logging and tracingip_address: Client IP address for geolocation and fraud detectionauthenticated_at: When the JWT was issuedexpires_at: When the JWT expiresissuer: Token issuer for multi-issuer systemsaudience: Token audience for validation
Fields§
§user_id: UserIdUser ID (from JWT ‘sub’ claim)
roles: Vec<String>User’s roles (e.g., ["admin", "moderator"])
Extracted from JWT ‘roles’ claim or derived from other claims. Used for role-based access control (RBAC) decisions.
tenant_id: Option<TenantId>Tenant/organization ID (for multi-tenancy)
When present, RLS policies can enforce tenant isolation.
Extracted from JWT ‘tenant_id’ or X-Tenant-Id header.
scopes: Vec<String>OAuth/permission scopes
Format: {action}:{resource} or {action}:{type}.{field}
Examples:
read:userwrite:postread:User.emailadmin:*
Extracted from JWT ‘scope’ claim.
attributes: HashMap<String, Value>Custom attributes from JWT claims
Arbitrary key-value pairs from JWT payload. Examples: “department”, “region”, “tier”, “country”
Used by custom RLS policies that need domain-specific attributes.
request_id: StringRequest correlation ID for audit trails
Extracted from X-Request-Id header or generated. Used for tracing and audit logging across services.
ip_address: Option<String>Client IP address
Extracted from X-Forwarded-For or connection socket. Used for geolocation and fraud detection in RLS policies.
authenticated_at: DateTime<Utc>When the JWT was issued
expires_at: DateTime<Utc>When the JWT expires
issuer: Option<String>Token issuer (for multi-issuer systems)
audience: Option<String>Token audience (for audience validation)
email: Option<String>Normalised email address from the JWT email claim.
Available as jwt:email in session variable mappings for RLS policies.
display_name: Option<String>Normalised display name from the JWT name claim.
Available as jwt:name or jwt:display_name in session variable mappings.
Implementations§
Source§impl SecurityContext
impl SecurityContext
Sourcepub fn from_user(user: &AuthenticatedUser, request_id: String) -> Self
pub fn from_user(user: &AuthenticatedUser, request_id: String) -> Self
Create a security context from an authenticated user and request metadata.
§Arguments
user- Authenticated user from JWT validationrequest_id- Correlation ID for this request
§Example
// Requires: a live AuthenticatedUser from JWT validation.
// See: tests/integration/ for runnable examples.
let context = SecurityContext::from_user(&authenticated_user, "req-123".to_string());Sourcepub fn get_attribute(&self, key: &str) -> Option<&Value>
pub fn get_attribute(&self, key: &str) -> Option<&Value>
Sourcepub fn is_expired(&self) -> bool
pub fn is_expired(&self) -> bool
Sourcepub const fn is_multi_tenant(&self) -> bool
pub const fn is_multi_tenant(&self) -> bool
Check if the context has a tenant ID (multi-tenancy enabled).
§Returns
true if tenant_id is present, false otherwise.
Sourcepub fn with_role(self, role: String) -> Self
pub fn with_role(self, role: String) -> Self
Set or override a role (for testing or runtime role modification).
Sourcepub fn with_scopes(self, scopes: Vec<String>) -> Self
pub fn with_scopes(self, scopes: Vec<String>) -> Self
Set or override scopes (for testing or runtime permission modification).
Sourcepub fn with_tenant(self, tenant_id: impl Into<TenantId>) -> Self
pub fn with_tenant(self, tenant_id: impl Into<TenantId>) -> Self
Set tenant ID (for multi-tenancy).
Sourcepub fn with_attribute(self, key: String, value: Value) -> Self
pub fn with_attribute(self, key: String, value: Value) -> Self
Set a custom attribute (for testing or runtime attribute addition).
Sourcepub fn can_access_scope(
&self,
security_config: &SecurityConfig,
required_scope: &str,
) -> bool
pub fn can_access_scope( &self, security_config: &SecurityConfig, required_scope: &str, ) -> bool
Check if user can access a field based on role definitions.
Takes a required scope and checks if any of the user’s roles grant that scope.
§Arguments
security_config- Security config from compiled schema with role definitionsrequired_scope- Scope required to access the field (e.g., “read:User.email”)
§Returns
true if user’s roles grant the required scope, false otherwise.
§Example
// Requires: a SecurityConfig from a compiled schema.
// See: tests/integration/ for runnable examples.
let can_access = context.can_access_scope(&config, "read:User.email");Trait Implementations§
Source§impl Clone for SecurityContext
impl Clone for SecurityContext
Source§fn clone(&self) -> SecurityContext
fn clone(&self) -> SecurityContext
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more