-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_https.rs
More file actions
218 lines (199 loc) · 8.01 KB
/
Copy pathauth_https.rs
File metadata and controls
218 lines (199 loc) · 8.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#![expect(
clippy::print_stdout,
clippy::exit,
clippy::expect_used,
reason = "CLI examples can be more lax"
)]
//! HTTPS provider token acquisition example using locally-trusted certificates.
//!
//! This example demonstrates two approaches for OAuth providers that require
//! HTTPS redirect URIs (e.g. Slack):
//!
//! - **Managed mode** (recommended): Set `LOOPAUTH_TLS_DIR` and loopauth
//! generates certs automatically via `mkcert` on first run.
//! - **Manual mode**: Set `LOOPAUTH_CERT_FILE` and `LOOPAUTH_KEY_FILE` to
//! load pre-existing PEM files.
//!
//! # One-time setup (both modes)
//!
//! Install [`mkcert`](https://github.com/FiloSottile/mkcert) and trust the
//! local CA:
//!
//! ```sh
//! brew install mkcert # macOS; see mkcert docs for Linux/Windows
//! mkcert -install # one-time, may prompt for password
//! ```
//!
//! For managed mode, that's all you need. Certs are generated on first run.
//!
//! For manual mode, also generate the cert files:
//!
//! ```sh
//! mkcert -cert-file localhost-cert.pem -key-file localhost-key.pem localhost 127.0.0.1
//! ```
//!
//! Print the full guide from code:
//!
//! ```sh
//! cargo run --example auth_https -- --setup-guide
//! ```
//!
//! # Required environment variables
//!
//! | Variable | Description |
//! |----------------------|---------------------------------------|
//! | `LOOPAUTH_CLIENT_ID` | `OAuth 2.0` client ID from your provider |
//! | `LOOPAUTH_AUTH_URL` | Authorization endpoint URL |
//! | `LOOPAUTH_TOKEN_URL` | Token endpoint URL |
//!
//! Plus one of the following certificate configurations:
//!
//! | Variable | Description |
//! |----------------------|-------------------------------------------------|
//! | `LOOPAUTH_TLS_DIR` | Directory for managed certs (recommended) |
//! | `LOOPAUTH_CERT_FILE` + `LOOPAUTH_KEY_FILE` | Paths to PEM files (manual) |
//!
//! # Optional environment variables
//!
//! | Variable | Description | Default |
//! |--------------------------|------------------------------------|------------------------|
//! | `LOOPAUTH_CLIENT_SECRET` | Client secret | - |
//! | `LOOPAUTH_SCOPES` | Comma-separated scopes | `openid,email,profile` |
//! | `LOOPAUTH_PORT` | Port hint for the loopback server | OS-assigned |
//!
//! # Provider quick-start
//!
//! **Slack (managed mode)**
//! ```sh
//! LOOPAUTH_CLIENT_ID=... \
//! LOOPAUTH_CLIENT_SECRET=... \
//! LOOPAUTH_AUTH_URL=https://slack.com/oauth/v2/authorize \
//! LOOPAUTH_TOKEN_URL=https://slack.com/api/oauth.v2.access \
//! LOOPAUTH_TLS_DIR=~/.config/my-cli/tls \
//! LOOPAUTH_SCOPES=openid,email,profile \
//! LOOPAUTH_PORT=8443 \
//! cargo run --example auth_https
//! ```
//! Add `https://127.0.0.1:8443/callback` to your Slack app's Redirect URLs.
//! Use `LOOPAUTH_PORT` to pin the port so it matches the registered redirect URL.
use loopauth::{CliTokenClient, RequestScope, TlsCertificate};
const DEFAULT_SCOPES: &str = "email,profile";
const FAILURE_EXIT_CODE: i32 = 1;
const SIGINT_EXIT_CODE: i32 = 130;
#[tokio::main]
async fn main() {
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
)
.init();
// Print setup guide and exit if requested
if std::env::args().any(|a| a == "--setup-guide") {
println!("{}", TlsCertificate::SETUP_GUIDE);
return;
}
let client_id = require_env("LOOPAUTH_CLIENT_ID");
let auth_url = url::Url::parse(&require_env("LOOPAUTH_AUTH_URL"))
.expect("LOOPAUTH_AUTH_URL must be a valid URL");
let token_url = url::Url::parse(&require_env("LOOPAUTH_TOKEN_URL"))
.expect("LOOPAUTH_TOKEN_URL must be a valid URL");
let client_secret = std::env::var("LOOPAUTH_CLIENT_SECRET").ok();
let scopes = parse_scopes(
&std::env::var("LOOPAUTH_SCOPES").unwrap_or_else(|_| DEFAULT_SCOPES.to_string()),
);
let port_hint = std::env::var("LOOPAUTH_PORT")
.ok()
.and_then(|p| p.parse::<u16>().ok());
// Load TLS certificate: managed mode (LOOPAUTH_TLS_DIR) or manual mode (CERT_FILE + KEY_FILE)
let certificate = load_certificate();
// Enter OIDC mode explicitly (adds the openid scope and enables id_token processing).
// We skip JWKS signature verification here for simplicity; production code should
// use .jwks_validator() or .with_open_id_configuration_jwks_validator() instead.
let mut builder = CliTokenClient::builder()
.client_id(client_id)
.auth_url(auth_url)
.token_url(token_url)
.with_openid_scope()
.without_jwks_validation()
.use_https_with(certificate)
.add_scopes(scopes)
.on_url(|url| {
tracing::info!("opening: {url}");
tracing::info!("waiting for browser callback... (Ctrl+C to cancel)");
});
if let Some(secret) = client_secret {
builder = builder.client_secret(secret);
}
if let Some(port) = port_hint {
builder = builder.port_hint(port);
}
let auth = builder.build();
tracing::info!("starting HTTPS authorization flow");
match auth.run_authorization_flow().await {
Ok(tokens) => {
println!("\n=== Authentication successful ===");
println!("access_token : {}", tokens.access_token());
if let Some(rt) = tokens.refresh_token() {
println!("refresh_token: {rt}");
}
if let Some(oidc) = tokens.oidc() {
println!("\n=== OIDC claims ===");
println!("sub : {}", oidc.claims().sub());
if let Some(email) = oidc.claims().email() {
println!("email : {email}");
}
if let Some(name) = oidc.claims().name() {
println!("name : {name}");
}
}
}
Err(loopauth::AuthError::Cancelled) => {
tracing::info!("cancelled");
std::process::exit(SIGINT_EXIT_CODE);
}
Err(e) => {
tracing::error!("authentication failed: {e}");
std::process::exit(FAILURE_EXIT_CODE);
}
}
}
fn load_certificate() -> TlsCertificate {
// Prefer managed mode (LOOPAUTH_TLS_DIR) over manual mode (CERT_FILE + KEY_FILE)
if let Ok(tls_dir) = std::env::var("LOOPAUTH_TLS_DIR") {
tracing::info!("using managed TLS certificates in {tls_dir}");
return TlsCertificate::ensure_localhost(&tls_dir).unwrap_or_else(|e| {
tracing::error!("TLS certificate setup failed: {e}");
if matches!(e, loopauth::TlsCertificateError::MkcertNotFound) {
println!("\n{}", TlsCertificate::SETUP_GUIDE_MANAGED);
}
std::process::exit(FAILURE_EXIT_CODE);
});
}
if let (Ok(cert_file), Ok(key_file)) = (
std::env::var("LOOPAUTH_CERT_FILE"),
std::env::var("LOOPAUTH_KEY_FILE"),
) {
tracing::info!("loading TLS certificate from {cert_file} + {key_file}");
return TlsCertificate::from_pem_files(&cert_file, &key_file).unwrap_or_else(|e| {
tracing::error!("failed to load TLS certificate: {e}");
tracing::info!("run with --setup-guide for certificate setup instructions");
std::process::exit(FAILURE_EXIT_CODE);
});
}
tracing::error!("set LOOPAUTH_TLS_DIR (recommended) or LOOPAUTH_CERT_FILE + LOOPAUTH_KEY_FILE");
std::process::exit(FAILURE_EXIT_CODE);
}
fn require_env(name: &str) -> String {
std::env::var(name).unwrap_or_else(|_| {
tracing::error!("{name} ENV var not set");
std::process::exit(FAILURE_EXIT_CODE);
})
}
fn parse_scopes(s: &str) -> Vec<RequestScope> {
s.split(',')
.map(str::trim)
.filter(|s| !s.is_empty())
.map(RequestScope::from)
.collect()
}