pub struct AuthContext {Show 18 fields
pub sub: String,
pub iss: Option<String>,
pub aud: Option<String>,
pub exp: Option<u64>,
pub iat: Option<u64>,
pub nbf: Option<u64>,
pub jti: Option<String>,
pub user: UserInfo,
pub roles: Vec<String>,
pub permissions: Vec<String>,
pub scopes: Vec<String>,
pub request_id: Option<String>,
pub authenticated_at: SystemTime,
pub expires_at: Option<SystemTime>,
pub token: Option<TokenInfo>,
pub provider: String,
pub dpop_jkt: Option<String>,
pub metadata: HashMap<String, Value>,
}Expand description
Unified authentication context containing user identity, claims, and session metadata.
This type serves as both:
- The internal authentication representation
- The JWT claims structure (via
to_jwt_claims/from_jwt_claims)
§Standard JWT Claims (RFC 7519)
sub: Subject (user ID)iss: Issuer (who issued the token)aud: Audience (who the token is for)exp: Expiration time (Unix timestamp)iat: Issued at (Unix timestamp)nbf: Not before (Unix timestamp)jti: JWT ID (unique identifier)
§Extended Claims
user: Full user informationroles: RBAC rolespermissions: Fine-grained permissionsscopes: OAuth scopesrequest_id: Request identifier for replay protection (NOT session-based)provider: Auth provider identifiermetadata: Custom claims
§Example
use turbomcp_auth::context::{AuthContext, AuthContextBuilder};
let ctx = AuthContext::builder()
.subject("user123")
.user(user_info)
.roles(vec!["admin".into(), "user".into()])
.permissions(vec!["read:posts".into(), "write:posts".into()])
.build();
// Check authorization
if ctx.has_role("admin") && ctx.has_permission("write:posts") {
// Allow action
}Fields§
§sub: StringSubject (typically user ID)
iss: Option<String>Issuer (who issued this token)
aud: Option<String>Audience (who this token is for)
exp: Option<u64>Expiration time (Unix timestamp)
iat: Option<u64>Issued at (Unix timestamp)
nbf: Option<u64>Not before (Unix timestamp)
jti: Option<String>JWT ID (unique identifier)
user: UserInfoFull user information
roles: Vec<String>RBAC roles (e.g., [“admin”, “user”])
permissions: Vec<String>Fine-grained permissions (e.g., [“read:posts”, “write:posts”])
scopes: Vec<String>OAuth scopes (e.g., [“openid”, “email”, “profile”])
request_id: Option<String>Request ID for nonce/replay protection (MCP compliant - NOT session-based)
Per MCP security requirements, servers MUST NOT use sessions for authentication. This field is for request-level binding (DPoP nonces, one-time tokens, etc.), not session management. Each request must include valid credentials.
authenticated_at: SystemTimeWhen authentication occurred
expires_at: Option<SystemTime>When this context expires (may differ from JWT exp)
token: Option<TokenInfo>Token information (access + refresh tokens)
provider: StringAuth provider (e.g., “oauth2:google”, “api_key”, “jwt:internal”)
dpop_jkt: Option<String>DPoP JWK thumbprint for token binding
metadata: HashMap<String, Value>Custom metadata (tenant_id, org_id, etc.)
Implementations§
Source§impl AuthContext
impl AuthContext
Sourcepub fn builder() -> AuthContextBuilder
pub fn builder() -> AuthContextBuilder
Create builder for constructing auth context
Sourcepub fn to_jwt_claims(&self) -> Value
pub fn to_jwt_claims(&self) -> Value
Sourcepub fn from_jwt_claims(claims: Value) -> Result<Self, AuthError>
pub fn from_jwt_claims(claims: Value) -> Result<Self, AuthError>
Create from JWT claims (after validation)
Deserializes a validated JWT claims object into an AuthContext.
§Errors
Returns error if:
- Required fields are missing (sub, user, provider)
- Field types don’t match expected types
- Invalid timestamps
Sourcepub fn is_expired(&self) -> bool
pub fn is_expired(&self) -> bool
Check if token is expired
Uses expires_at field if present, otherwise falls back to exp claim.
Sourcepub fn validate(&self, config: &ValidationConfig) -> Result<(), AuthError>
pub fn validate(&self, config: &ValidationConfig) -> Result<(), AuthError>
Validate all fields (exp, nbf, aud, iss)
Performs comprehensive validation according to RFC 7519.
§Errors
Returns error if:
- Token is expired (with leeway)
- Token not yet valid (nbf with leeway)
- Audience mismatch
- Issuer mismatch
Sourcepub fn has_any_role(&self, roles: &[&str]) -> bool
pub fn has_any_role(&self, roles: &[&str]) -> bool
Check if user has any of the roles
Sourcepub fn has_all_roles(&self, roles: &[&str]) -> bool
pub fn has_all_roles(&self, roles: &[&str]) -> bool
Check if user has all of the roles
Sourcepub fn has_permission(&self, perm: &str) -> bool
pub fn has_permission(&self, perm: &str) -> bool
Check if user has specific permission
Sourcepub fn has_any_permission(&self, perms: &[&str]) -> bool
pub fn has_any_permission(&self, perms: &[&str]) -> bool
Check if user has any of the permissions
Sourcepub fn has_all_permissions(&self, perms: &[&str]) -> bool
pub fn has_all_permissions(&self, perms: &[&str]) -> bool
Check if user has all of the permissions
Sourcepub fn has_any_scope(&self, scopes: &[&str]) -> bool
pub fn has_any_scope(&self, scopes: &[&str]) -> bool
Check if token has any of the scopes
Sourcepub fn has_all_scopes(&self, scopes: &[&str]) -> bool
pub fn has_all_scopes(&self, scopes: &[&str]) -> bool
Check if token has all of the scopes
Sourcepub fn get_metadata<T: DeserializeOwned>(&self, key: &str) -> Option<T>
pub fn get_metadata<T: DeserializeOwned>(&self, key: &str) -> Option<T>
Trait Implementations§
Source§impl Clone for AuthContext
impl Clone for AuthContext
Source§fn clone(&self) -> AuthContext
fn clone(&self) -> AuthContext
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more