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(any(test, feature = "test"))]
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		reqwest::Client,
29		store::{BrokerStore, MemoryStore},
30	};
31
32	/// Broker type alias used by reqwest-backed integration tests.
33	pub type ReqwestTestBroker = Broker<ReqwestHttpClient, ReqwestTransportErrorMapper>;
34
35	/// Builds a reqwest HTTP client that accepts the self-signed certificates produced by
36	/// `httpmock` during tests.
37	pub fn test_reqwest_http_client() -> ReqwestHttpClient {
38		let client = Client::builder()
39			.danger_accept_invalid_certs(true)
40			.danger_accept_invalid_hostnames(true)
41			.build()
42			.expect("Failed to build insecure Reqwest client for tests.");
43
44		ReqwestHttpClient::with_client(client)
45	}
46
47	/// Constructs a [`Broker`] backed by an in-memory store, default provider strategy, and the
48	/// reqwest transport used across integration tests.
49	pub fn build_reqwest_test_broker(
50		descriptor: ProviderDescriptor,
51		client_id: &str,
52		client_secret: &str,
53	) -> (ReqwestTestBroker, Arc<MemoryStore>) {
54		let store_backend = Arc::new(MemoryStore::default());
55		let store: Arc<dyn BrokerStore> = store_backend.clone();
56		let strategy: Arc<dyn ProviderStrategy> = Arc::new(DefaultProviderStrategy);
57		let http_client = test_reqwest_http_client();
58		let mapper = Arc::new(ReqwestTransportErrorMapper);
59		let broker =
60			Broker::with_http_client(store, descriptor, strategy, client_id, http_client, mapper)
61				.with_client_secret(client_secret);
62
63		(broker, store_backend)
64	}
65}
66
67#[cfg(test)] use {color_eyre as _, httpmock as _};
68
69mod _prelude {
70	pub use std::{
71		collections::{BTreeMap, HashMap, hash_map::DefaultHasher},
72		error::Error as StdError,
73		fmt::{Debug, Display, Formatter, Result as FmtResult},
74		future::Future,
75		hash::{Hash, Hasher},
76		pin::Pin,
77		str::FromStr,
78		sync::Arc,
79	};
80
81	pub use async_lock::Mutex as AsyncMutex;
82	pub use parking_lot::{Mutex, RwLock};
83	pub use reqwest::Error as ReqwestError;
84	pub use serde::{Deserialize, Serialize};
85	pub use thiserror::Error as ThisError;
86	pub use time::{Duration, OffsetDateTime};
87	pub use url::Url;
88
89	pub use crate::error::{Error, Result};
90}
91
92pub use reqwest;
93pub use url;