<div align="center">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://systemprompt.io/files/images/logo.svg">
<source media="(prefers-color-scheme: light)" srcset="https://systemprompt.io/files/images/logo-dark.svg">
<img src="https://systemprompt.io/files/images/logo.svg" alt="systemprompt.io" width="180">
</picture>
### Production infrastructure for AI agents
[**Website**](https://systemprompt.io) · [**Documentation**](https://systemprompt.io/documentation/) · [**Guides**](https://systemprompt.io/guides) · [**Core**](https://github.com/systempromptio/systemprompt-core) · [**Template**](https://github.com/systempromptio/systemprompt-template) · [**Discord**](https://discord.gg/wkAbSuPWpr)
</div>
---
# systemprompt-security
<div align="center">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/systempromptio/systemprompt-core/main/assets/readme/terminals/dark/00-overview.svg">
<source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/systempromptio/systemprompt-core/main/assets/readme/terminals/light/00-overview.svg">
<img alt="systemprompt-security — systemprompt-core workspace" src="https://raw.githubusercontent.com/systempromptio/systemprompt-core/main/assets/readme/terminals/dark/00-overview.svg" width="100%">
</picture>
</div>
[](https://crates.io/crates/systemprompt-security)
[](https://docs.rs/systemprompt-security)
[](https://github.com/systempromptio/systemprompt-core/blob/main/LICENSE)
Security infrastructure for systemprompt.io AI governance: JWT, OAuth2 token extraction, scope enforcement, ChaCha20-Poly1305 secret encryption, and the four-layer tool-call governance pipeline. Handles JWT token generation and validation, multi-method token extraction, and bot/scanner detection.
**Layer**: Infra — infrastructure primitives (database, security, events, etc.) consumed by domain crates. Part of the [systemprompt-core](https://github.com/systempromptio/systemprompt-core) workspace.
## Overview
This crate provides security primitives for the systemprompt.io platform. It handles JWT token generation and validation, multi-method token extraction, and bot/scanner detection.
## Architecture
```
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
```
### `auth`
Authentication validation service with configurable enforcement modes.
| `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.
| `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.
| `JwtService` | Struct | Generates admin JWT tokens with HS256 |
| `AdminTokenParams` | Struct | Configuration for admin token creation |
### `services`
Security services for request classification.
| `ScannerDetector` | Struct | Detects bot/scanner requests by path, user-agent, velocity |
### `session`
Session token generation and claim validation.
| `SessionGenerator` | Struct | Generates session-scoped JWT tokens |
| `SessionParams` | Struct | Configuration for session token creation |
| `ValidatedSessionClaims` | Struct | Extracted claims after JWT validation |
## Usage
```toml
[dependencies]
systemprompt-security = "0.2.1"
```
### Token Extraction
```rust
use systemprompt_security::{TokenExtractor, ExtractionMethod};
let extractor = TokenExtractor::standard();
let token = extractor.extract(&headers)?;
let browser_extractor = TokenExtractor::browser_only()
.with_cookie_name("auth_token".to_string());
```
### Authentication Validation
```rust
use systemprompt_security::{AuthValidationService, AuthMode};
let service = AuthValidationService::new(secret, issuer, audiences);
let context = service.validate_request(&headers, AuthMode::Required)?;
```
### Admin Token Generation
```rust
use systemprompt_security::{JwtService, AdminTokenParams};
let params = AdminTokenParams {
user_id: &user_id,
session_id: &session_id,
email: "admin@example.com",
jwt_secret: &secret,
issuer: "systemprompt",
duration: Duration::days(365),
};
let token = JwtService::generate_admin_token(¶ms)?;
```
### Session Token Generation
```rust
use systemprompt_security::{SessionGenerator, SessionParams};
use systemprompt_models::auth::{Permission, RateLimitTier, UserType};
let generator = SessionGenerator::new(jwt_secret, "systemprompt");
let params = SessionParams {
user_id: &user_id,
session_id: &session_id,
email: "user@example.com",
duration: Duration::hours(24),
user_type: UserType::User,
permissions: vec![Permission::Read, Permission::Write],
roles: vec!["user".to_string()],
rate_limit_tier: RateLimitTier::Standard,
};
let token = generator.generate(¶ms)?;
```
## Dependencies
| `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 |
## License
BSL-1.1 (Business Source License). Source-available for evaluation, testing, and non-production use. Production use requires a commercial license. Each version converts to Apache 2.0 four years after publication. See [LICENSE](https://github.com/systempromptio/systemprompt-core/blob/main/LICENSE).
---
<div align="center">
**[systemprompt.io](https://systemprompt.io)** · **[Documentation](https://systemprompt.io/documentation/)** · **[Guides](https://systemprompt.io/guides)** · **[Live Demo](https://systemprompt.io/features/demo)** · **[Template](https://github.com/systempromptio/systemprompt-template)** · **[crates.io](https://crates.io/crates/systemprompt-security)** · **[docs.rs](https://docs.rs/systemprompt-security)** · **[Discord](https://discord.gg/wkAbSuPWpr)**
<sub>Infra layer · Own how your organization uses AI.</sub>
</div>