systemprompt-security
Security infrastructure module providing authentication, JWT handling, and token extraction.
Overview
This is a foundation-level infrastructure crate that provides security primitives for the SystemPrompt platform. It handles JWT token generation and validation, multi-method token extraction, and bot/scanner detection.
File Structure
src/
├── lib.rs # Module exports and public API
├── auth/
│ ├── mod.rs # Auth module re-exports
│ └── validation.rs # AuthValidationService, AuthMode, token validation
├── extraction/
│ ├── mod.rs # Extraction module re-exports
│ ├── token.rs # TokenExtractor with fallback chain
│ ├── header.rs # HeaderExtractor/HeaderInjector for context propagation
│ └── cookie.rs # CookieExtractor for cookie-based auth
├── jwt/
│ └── mod.rs # JwtService for admin token generation
├── services/
│ ├── mod.rs # Services module re-exports
│ └── scanner.rs # ScannerDetector for bot detection
└── session/
├── mod.rs # Session module re-exports
├── generator.rs # SessionGenerator for session token creation
└── claims.rs # ValidatedSessionClaims data structure
Modules
auth
Authentication validation service with configurable enforcement modes.
| Export | Type | Purpose |
|---|---|---|
AuthValidationService |
Struct | Validates JWT tokens and creates RequestContext |
AuthMode |
Enum | Required, Optional, Disabled enforcement levels |
extraction
Token extraction from multiple sources with fallback chain support.
| Export | Type | Purpose |
|---|---|---|
TokenExtractor |
Struct | Extracts tokens from headers/cookies with configurable fallback |
ExtractionMethod |
Enum | AuthorizationHeader, McpProxyHeader, Cookie |
TokenExtractionError |
Enum | Specific error types for extraction failures |
HeaderExtractor |
Struct | Extracts trace_id, context_id, agent_name from headers |
HeaderInjector |
Struct | Injects RequestContext fields into outgoing headers |
HeaderInjectionError |
Struct | Error type for header injection failures |
CookieExtractor |
Struct | Dedicated cookie-based token extraction |
CookieExtractionError |
Enum | Cookie-specific error types |
jwt
JWT token generation for administrative access.
| Export | Type | Purpose |
|---|---|---|
JwtService |
Struct | Generates admin JWT tokens with HS256 |
AdminTokenParams |
Struct | Configuration for admin token creation |
services
Security services for request classification.
| Export | Type | Purpose |
|---|---|---|
ScannerDetector |
Struct | Detects bot/scanner requests by path, user-agent, velocity |
session
Session token generation and claim validation.
| Export | Type | Purpose |
|---|---|---|
SessionGenerator |
Struct | Generates session-scoped JWT tokens |
SessionParams |
Struct | Configuration for session token creation |
ValidatedSessionClaims |
Struct | Extracted claims after JWT validation |
Dependencies
| Crate | Purpose |
|---|---|
systemprompt-models |
Shared models (JwtClaims, UserType, Permission) |
systemprompt-identifiers |
Typed identifiers (UserId, SessionId, TraceId) |
jsonwebtoken |
JWT encoding/decoding with HS256 |
axum |
HTTP types (HeaderMap, HeaderValue) |
chrono |
Timestamp handling |
tracing |
Debug logging for extraction errors |
Usage
Token Extraction
use ;
let extractor = standard;
let token = extractor.extract?;
let browser_extractor = browser_only
.with_cookie_name;
Authentication Validation
use ;
let service = new;
let context = service.validate_request?;
Admin Token Generation
use ;
let params = AdminTokenParams ;
let token = generate_admin_token?;
Session Token Generation
use ;
use ;
let generator = new;
let params = SessionParams ;
let token = generator.generate?;