Skip to main content

systemprompt_security/
lib.rs

1//! Security infrastructure for systemprompt.io.
2//!
3//! Houses the request-level authentication primitives shared by the HTTP
4//! API and the runtime layer:
5//!
6//! - Asymmetric signing key plane ([`keys`]) — the in-process `TokenAuthority`
7//!   holds the active RSA keypair, exposes the public set for
8//!   `/.well-known/jwks.json`, and caches federated JWKS documents under a
9//!   bounded LRU with an HTTPS allowlist.
10//! - JWT minting ([`jwt`]) for admin tokens and ([`session`]) for
11//!   session-scoped tokens. Tokens are signed RS256 via `TokenAuthority` and
12//!   carry a `kid` header; HS256 is rejected on validation.
13//! - Token extraction ([`extraction`]) from `Authorization` headers, MCP proxy
14//!   headers, and cookies.
15//! - Request validation ([`auth`]) that turns those tokens into a
16//!   [`systemprompt_models::execution::context::RequestContext`], resolving
17//!   non-self-issued tokens against `profile.security.trusted_issuers` and
18//!   propagating the RFC 8693 `act_chain` onto the per-request context.
19//! - At-rest hashing ([`at_rest`]) — `hmac_sha256` / `hmac_sha256_hex` under
20//!   the deployment `oauth_at_rest_pepper`, used to store refresh-token ids and
21//!   authorisation codes as digests rather than plaintext.
22//! - Bridge manifest signing ([`manifest_signing`]) with Ed25519 keys.
23//! - Lightweight scanner / bot detection ([`services`]).
24//! - Authorization decision plane ([`authz`]) — deny-overrides resolver,
25//!   `access_control_rules` repository, and `AuthzDecisionHook` extension
26//!   surface shared by the gateway and MCP enforcement sites.
27//! - Governed-call plane ([`policy`]) — the `GovernanceEngine` policy chain
28//!   (secret scan, scope check, blocklist, rate limit + extension-registered
29//!   policies) with per-entry audit tracing into `governance_decisions`.
30//!
31//! All public fallible APIs return typed errors from [`error`] — `anyhow`
32//! is not used in any public signature.
33//!
34//! # Feature flags
35//!
36//! This crate has no Cargo features; everything compiles by default.
37//!
38//! # Example
39//!
40//! ```no_run
41//! use systemprompt_models::auth::JwtAudience;
42//! use systemprompt_security::AuthValidationService;
43//!
44//! # fn demo(headers: &axum::http::HeaderMap) -> systemprompt_security::AuthResult<()> {
45//! let svc = AuthValidationService::new("systemprompt.io".to_string(), JwtAudience::standard());
46//! let _ctx = svc.validate_request(headers)?;
47//! # Ok(())
48//! # }
49//! ```
50//!
51//! Copyright (c) systemprompt.io — Business Source License 1.1.
52//! See <https://systemprompt.io> for licensing details.
53
54pub mod at_rest;
55pub mod auth;
56pub mod authz;
57pub mod error;
58pub mod extraction;
59pub mod jwt;
60pub mod keys;
61pub mod manifest_signing;
62pub mod policy;
63pub mod services;
64pub mod session;
65
66pub use at_rest::{hmac_sha256, hmac_sha256_hex};
67
68pub use auth::{AuthValidationService, HookTokenValidator, ValidatedHookClaims};
69pub use authz::CompositeAuthzHook;
70pub use error::{
71    AuthError, AuthResult, JwtError, JwtResult, ManifestSigningError, ManifestSigningResult,
72};
73pub use extraction::{
74    CookieExtractionError, CookieExtractor, ExtractionMethod, HeaderExtractor,
75    HeaderInjectionError, HeaderInjector, TokenExtractionError, TokenExtractor,
76};
77pub use jwt::{AdminTokenParams, JwtService, JwtUserContext, extract_user_context};
78pub use services::ScannerDetector;
79pub use session::{SessionGenerator, SessionParams, ValidatedSessionClaims};