Available on crate features
sso-server and sso only.Expand description
AuthServer — a minimal OAuth 2.0 Authorization Server / token issuer.
Lets rws be the identity provider for downstream services or
single-page apps, rather than always delegating to an external OIDC
provider (that’s super::oidc_auth::OidcAuth’s job — this is the
reverse role).
§Usage
use std::sync::Arc;
use std::time::Duration;
use rust_web_server::app::App;
use rust_web_server::core::New;
use rust_web_server::session::SessionStore;
use rust_web_server::sso::server::{AuthServer, AuthServerConfig};
use rust_web_server::sso::client_store::{ClientStore, OAuthClient, GrantType};
let auth_server = AuthServer::new(AuthServerConfig {
issuer: "https://myapp.com".into(),
signing_secret: std::env::var("RWS_AUTH_SIGNING_SECRET").unwrap(),
access_token_ttl: Duration::from_secs(3600),
refresh_token_ttl: Duration::from_secs(86_400 * 30),
clients: ClientStore::new().add(OAuthClient {
client_id: "backend-service".into(),
client_secret: Some("s3cr3t".into()),
redirect_uris: vec![],
grants: vec![GrantType::ClientCredentials],
scopes: vec!["api:read".into()],
}),
sessions: Arc::new(SessionStore::new(86_400)),
});
let app = App::new().wrap(auth_server);
// Registers, and intercepts: POST /oauth/token, GET /oauth/authorize,
// GET /.well-known/openid-configuration, GET /.well-known/jwks.json§Deviations from a full OAuth 2.0 Authorization Server
- HS256, not RSA/EC. Tokens are signed with the existing
crate::auth::build_jwt/crate::auth::verify_jwtHS256 machinery (authfeature) rather than an RSA/EC private key loaded from PEM. This crate has no PEM/DER private-key parser; adding one — plus the signing-side counterpart tosuper::jwks::JwksCache’s verification-only RSA/ES code — would be a large addition for a feature that already has a fully adequate, already-tested symmetric alternative when the issuer and every resource server share one secret (the common case for “rws issues its own tokens”). ConsequentlyGET /.well-known/jwks.jsonalways returns an empty key set — there is no public key to publish for a symmetric algorithm. Resource servers must be configured with the samesigning_secretdirectly (e.g.auth::JwtLayerorauth::verify_jwt). /oauth/authorizetrusts an existing application session, not a built-in login page. The spec for this feature says “redirect to login page or IdP, then back with code” but building a username/ password login form is out of scope (the same boundarysuper::oidc_auth::OidcAuthdraws around needing an external IdP to already exist).AuthServerinstead checks the browser’s session (via the existingcrate::session::SessionStore) for a configurable key (default"user_id", override withAuthServer::subject_session_key) — if present, that value becomes the issued token’ssubclaim and a code is minted immediately; if absent, the browser is redirected to a configurablelogin_url(default/login, override withAuthServer::login_url) with?return_to=<original authorize URL>. Populating that session is the embedding application’s own responsibility.- No
ClientStore::from_env()/::from_db(). Client secrets belong in code or a real secret store, not flat env vars — and unlikesuper::config::OidcConfig(one client per process), aClientStoreis a list of clients with no natural flat-env-var encoding. The builder (ClientStore::new().add(...)) already covers registering clients from wherever the caller wants to load them.
Structs§
- Auth
Server - A minimal OAuth 2.0 Authorization Server — see the module docs.
- Auth
Server Config - Configuration for
AuthServer.