From 406c2eca191aed531dac74eaffafa70e956914b6 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Sun, 28 Jan 2024 11:21:51 -0800 Subject: Accept all OAuth2 client configuration through environment variables --- src/main.rs | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index ec91ec1..f979e2e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,15 +34,22 @@ struct Args { /// variable. #[arg(long = "secret")] client_secret: Option, + /// Scopes that must be present for the request to succeed. #[arg(long = "scope")] scope: Vec, /// The URL of the Authorization endpoint. - auth_url: Url, + /// This may also be provided through the OAUTH2_AUTHORIZATION_URL + /// environment variable. + #[arg(long = "authorization-url")] + auth_url: Option, /// The URL of the token introspection endpoint. - introspection_url: Url, + /// This may also be provided through the OAUTH2_INTROSPECTION_URL + /// environment variable. + #[arg(long = "introspection-url")] + introspection_url: Option, /// The command to run if authorized. command: String, @@ -81,13 +88,33 @@ fn handle_request() -> Result>, Error> { .or_else(|| env::var("OAUTH2_CLIENT_SECRET").ok()) .map(ClientSecret::new); + let auth_url = args + .auth_url + .or_else(|| { + env::var("OAUTH2_AUTHORIZATION_URL") + .ok() + .and_then(|url| url.parse().ok()) + }) + .map(AuthUrl::from_url) + .expect("Missing required argument --authorization-url"); + + let introspection_url = args + .introspection_url + .or_else(|| { + env::var("OAUTH2_INTROSPECTION_URL") + .ok() + .and_then(|url| url.parse().ok()) + }) + .map(IntrospectionUrl::from_url) + .expect("Missing required argument --introspection-url"); + let client = Client::new( client_id, client_secret, - AuthUrl::from_url(args.auth_url), + auth_url, None, ) - .set_introspection_uri(IntrospectionUrl::from_url(args.introspection_url)); + .set_introspection_uri(introspection_url); let access_token = env::var("HTTP_AUTHORIZATION") .ok() -- cgit v1.2.3