oauth2_broker/
lib.rs

1//! Rust’s turnkey OAuth 2.0 broker—spin up multi-tenant flows, CAS-smart token stores, and
2//! transport-aware observability in one crate built for production.
3
4#![deny(clippy::all, missing_docs, unused_crate_dependencies)]
5
6pub mod auth;
7pub mod error;
8pub mod ext;
9pub mod flows;
10pub mod http;
11pub mod oauth;
12pub mod obs;
13pub mod provider;
14pub mod store;
15#[cfg(all(any(test, feature = "test"), feature = "reqwest"))]
16pub mod _preludet {
17	//! Convenience re-exports and helpers for integration tests; enabled via `cfg(test)` or the
18	//! `test` crate feature.
19
20	pub use crate::_prelude::*;
21
22	// self
23	use crate::{
24		flows::Broker,
25		http::ReqwestHttpClient,
26		oauth::ReqwestTransportErrorMapper,
27		provider::{DefaultProviderStrategy, ProviderDescriptor, ProviderStrategy},
28		store::{BrokerStore, MemoryStore},
29	};
30
31	/// Broker type alias used by reqwest-backed integration tests.
32	pub type ReqwestTestBroker = Broker<ReqwestHttpClient, ReqwestTransportErrorMapper>;
33
34	/// Builds a reqwest HTTP client that accepts the self-signed certificates produced by
35	/// `httpmock` during tests.
36	pub fn test_reqwest_http_client() -> ReqwestHttpClient {
37		let client = ReqwestClient::builder()
38			.danger_accept_invalid_certs(true)
39			.danger_accept_invalid_hostnames(true)
40			.build()
41			.expect("Failed to build insecure Reqwest client for tests.");
42
43		ReqwestHttpClient::with_client(client)
44	}
45
46	/// Constructs a [`Broker`] backed by an in-memory store, default provider strategy, and the
47	/// reqwest transport used across integration tests.
48	pub fn build_reqwest_test_broker(
49		descriptor: ProviderDescriptor,
50		client_id: &str,
51		client_secret: &str,
52	) -> (ReqwestTestBroker, Arc<MemoryStore>) {
53		let store_backend = Arc::new(MemoryStore::default());
54		let store: Arc<dyn BrokerStore> = store_backend.clone();
55		let strategy: Arc<dyn ProviderStrategy> = Arc::new(DefaultProviderStrategy);
56		let http_client = test_reqwest_http_client();
57		let mapper = Arc::new(ReqwestTransportErrorMapper);
58		let broker =
59			Broker::with_http_client(store, descriptor, strategy, client_id, http_client, mapper)
60				.with_client_secret(client_secret);
61
62		(broker, store_backend)
63	}
64}
65
66mod _prelude {
67	pub use std::{
68		collections::{BTreeMap, HashMap, hash_map::DefaultHasher},
69		error::Error as StdError,
70		fmt::{Debug, Display, Formatter, Result as FmtResult},
71		future::Future,
72		hash::{Hash, Hasher},
73		pin::Pin,
74		str::FromStr,
75		sync::Arc,
76	};
77
78	pub use async_lock::Mutex as AsyncMutex;
79	pub use parking_lot::{Mutex, RwLock};
80	#[cfg(feature = "reqwest")]
81	pub use reqwest::{Client as ReqwestClient, Error as ReqwestError};
82	pub use serde::{Deserialize, Serialize};
83	pub use thiserror::Error as ThisError;
84	pub use time::{Duration, OffsetDateTime};
85	pub use url::Url;
86
87	pub use crate::error::{Error, Result};
88}
89
90#[cfg(feature = "reqwest")] pub use reqwest;
91pub use url;
92#[cfg(all(test, feature = "reqwest"))] use {color_eyre as _, httpmock as _};