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//! - Google service-account credentials ([`google`]) — the RFC 7523 JWT-bearer
23//! exchange that turns a service-account key into a Google OAuth access
24//! token, with a process-wide token cache.
25//! - Bridge manifest signing ([`manifest_signing`]) with Ed25519 keys.
26//! - Lightweight scanner / bot detection ([`services`]).
27//! - Upstream-credential plane ([`credential`]) — a stored secret is parsed
28//! once into a `ProviderCredential`, which answers for its own scope
29//! (project, region, principal) and its own auth header; [`google`] is one
30//! implementation of that model, not a credential story of its own.
31//!
32//! All public fallible APIs return typed errors from [`error`] and
33//! [`credential::CredentialError`] — `anyhow` is not used in any public
34//! signature.
35//!
36//! # Feature flags
37//!
38//! This crate has no Cargo features; everything compiles by default.
39//!
40//! # Example
41//!
42//! ```no_run
43//! use systemprompt_models::auth::JwtAudience;
44//! use systemprompt_security::AuthValidationService;
45//!
46//! # fn demo(headers: &axum::http::HeaderMap) -> systemprompt_security::AuthResult<()> {
47//! let svc = AuthValidationService::new("systemprompt.io".to_string(), JwtAudience::standard());
48//! let _ctx = svc.validate_request(headers)?;
49//! # Ok(())
50//! # }
51//! ```
52//!
53//! Copyright (c) systemprompt.io — Business Source License 1.1.
54//! See <https://systemprompt.io> for licensing details.
55
56pub mod at_rest;
57pub mod auth;
58pub mod connector_grant;
59pub mod credential;
60pub mod error;
61pub mod extraction;
62pub mod google;
63pub mod jwt;
64pub mod keys;
65pub mod manifest_signing;
66pub mod oauth_wire;
67pub mod services;
68pub mod session;
69
70pub use at_rest::{AtRestHashError, hmac_sha256, hmac_sha256_hex};
71
72pub use auth::{AuthValidationService, HookTokenValidator, ValidatedHookClaims};
73pub use error::{
74 AuthError, AuthResult, JwtError, JwtResult, ManifestSigningError, ManifestSigningResult,
75};
76pub use extraction::{
77 CookieExtractionError, CookieExtractor, ExtractionMethod, HeaderExtractor,
78 HeaderInjectionError, HeaderInjector, TokenExtractionError, TokenExtractor,
79};
80pub use jwt::{AdminTokenParams, JwtService, JwtUserContext, extract_user_context};
81pub use services::ScannerDetector;
82pub use session::{SessionGenerator, SessionParams, ValidatedSessionClaims};