Skip to main content

rws_server/sso/
mod.rs

1//! OAuth 2.0 / OIDC SSO client support.
2//!
3//! Enabled by the `sso` Cargo feature:
4//!
5//! ```toml
6//! rust-web-server = { version = "17", features = ["sso"] }
7//! ```
8//!
9//! # Quick start
10//!
11//! ```rust,no_run
12//! # extern crate rws_server as rust_web_server;
13//! use std::sync::Arc;
14//! use rust_web_server::app::App;
15//! use rust_web_server::core::New;
16//! use rust_web_server::session::SessionStore;
17//! use rust_web_server::sso::{OidcAuth, OidcConfig};
18//!
19//! let sessions = Arc::new(SessionStore::new(86_400));
20//! let config   = OidcConfig::google(
21//!     "my-client-id",
22//!     "my-client-secret",
23//!     "https://example.com/auth/callback",
24//! );
25//! let app = App::new().wrap(OidcAuth::new(config, sessions));
26//! ```
27//!
28//! # Module layout
29//!
30//! | Sub-module      | Purpose                                                |
31//! |-----------------|--------------------------------------------------------|
32//! | [`config`]      | [`OidcConfig`] + provider presets + `from_env()`       |
33//! | [`discovery`]   | [`OidcProvider`] endpoints + hardcoded presets         |
34//! | [`jwks`]        | [`JwksCache`], JWT RS256/ES256 verify, [`OidcClaims`]  |
35//! | [`pkce`]        | [`PkceVerifier`], [`PkceChallenge`], base64url         |
36//! | [`client`]      | [`OidcClient`]: auth URL, token exchange, user info    |
37//! | [`oidc_auth`]   | [`OidcAuth`] middleware                                |
38//! | [`server`]      | [`AuthServer`] — `rws` as its own OAuth 2.0 Authorization Server (`sso-server` feature) |
39//! | [`client_store`]| [`ClientStore`] / [`OAuthClient`] — clients registered with [`server::AuthServer`] (`sso-server` feature) |
40//! | [`saml`]        | [`saml::SamlSp`] — SAML 2.0 Service Provider (`sso-saml` feature) |
41
42#[cfg(test)]
43#[doc(hidden)]
44pub mod tests;
45
46pub mod client;
47pub mod config;
48pub mod discovery;
49pub mod jwks;
50pub mod oidc_auth;
51pub mod pkce;
52
53#[cfg(feature = "sso-server")]
54pub mod client_store;
55#[cfg(feature = "sso-server")]
56pub mod server;
57
58#[cfg(feature = "sso-saml")]
59pub mod saml;
60
61// ── public re-exports ─────────────────────────────────────────────────────────
62
63pub use client::{OidcClient, TokenResponse};
64pub use config::OidcConfig;
65pub use discovery::OidcProvider;
66pub use jwks::{JwksCache, OidcClaims, VerifyOptions};
67pub use oidc_auth::{OidcAuth, SignedIn};
68pub use pkce::{PkceChallenge, PkceVerifier};
69
70#[cfg(feature = "sso-server")]
71pub use client_store::{ClientStore, GrantType, OAuthClient};
72#[cfg(feature = "sso-server")]
73pub use server::{AuthServer, AuthServerConfig};
74
75// ── error type ────────────────────────────────────────────────────────────────
76
77/// An error produced by the SSO / OIDC flow.
78#[derive(Debug)]
79pub struct SsoError(pub String);
80
81impl std::fmt::Display for SsoError {
82    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83        write!(f, "SsoError: {}", self.0)
84    }
85}
86
87impl std::error::Error for SsoError {}
88
89impl From<String> for SsoError {
90    fn from(s: String) -> Self {
91        SsoError(s)
92    }
93}
94
95impl From<&str> for SsoError {
96    fn from(s: &str) -> Self {
97        SsoError(s.to_string())
98    }
99}