| Crates.io | starberry_oauth |
| lib.rs | starberry_oauth |
| version | 0.6.4 |
| created_at | 2025-07-10 06:44:21.924282+00 |
| updated_at | 2025-07-10 06:44:21.924282+00 |
| description | OAuth and OIDC Middleware for Starberry |
| homepage | |
| repository | https://github.com/Redstone-D/starberry |
| max_upload_size | |
| id | 1745965 |
| size | 178,604 |
starberry_oauth is a flexible OAuth2 server and client library built on starberry_core.
tracing instrumentationoauth2 (default): pure OAuth2 coreopenid: OpenID Connect server support (discovery, JWKS, id_token, userinfo)social: Social login plugin (ExternalLoginProvider for upstream OAuth2/OIDC)Add to your Cargo.toml:
[dependencies]
starberry_oauth = { version = "0.6.4", features = ["openid", "social"] }
Use --no-default-features or selective features to enable only what you need:
cargo build --no-default-features # only core OAuth2
cargo build --features openid # core + OpenID Connect
cargo build --features social # core + Social login
cargo build --all-features # all plugins enabled
In your main.rs, configure the OAuth middleware and attach to your starberry_core application:
use std::sync::Arc;
use starberry_core::app::application::App;
use starberry_core::app::protocol::ProtocolHandlerBuilder;
use starberry_core::http::context::HttpReqCtx;
use starberry_oauth::{OAuthLayer, InMemoryClientStore, InMemoryTokenManager};
#[tokio::main]
async fn main() {
// Build OAuth2 middleware with in-memory stores
let oauth_layer = OAuthLayer::new()
.client_store(Arc::new(InMemoryClientStore::new(vec![])))
.token_manager(Arc::new(InMemoryTokenManager::new()));
// Attach middleware and run app
let app = App::new()
.single_protocol(
ProtocolHandlerBuilder::<HttpReqCtx>::new()
.append_middleware::<OAuthLayer>()
)
.build();
app.run().await;
}
The crate includes example programs under examples/:
minimal.rs — pure OAuth2 server exampleopenid.rs — OpenID Connect server example (--features openid)social.rs — Social login stub example (--features social)Run them with:
cargo run --example minimal
cargo run --example openid --features openid
cargo run --example social --features social
Run all tests, including integration, unit, doc, and feature-gated tests:
cargo test --all-features
To validate RFC compliance, run the OAuth2 conformance tests from oauth.net.
Add Rust tests under starberry_oauth/tests exercising:
Use reqwest or the in-memory HTTP client stub for simulating flows.
Use cargo fuzz to catch panics in token parsing and URL decoding:
cargo install cargo-fuzz
cd starberry_oauth
cargo fuzz init
# create fuzz_targets/token_parser.rs that calls `jsonwebtoken::decode` with random input
cargo fuzz run token_parser
Use k6 to simulate realistic auth-code and client-credentials traffic:
// load_tests/auth.js
import http from 'k6/http';
import { check, sleep } from 'k6';
export let options = { vus: 50, duration: '1m' };
export default function() {
let res = http.post('http://localhost:8080/oauth/token', {
grant_type: 'client_credentials',
client_id: __ENV.CLIENT_ID,
client_secret: __ENV.CLIENT_SECRET,
});
check(res, { 'status is 200': (r) => r.status == 200 });
sleep(1);
}
k6 run load_tests/auth.js
cargo audit)Contributions welcome! Please file issues or PRs on GitHub.