subspace_github_auth/
config.rs1use std::net::IpAddr;
4
5use subspace_core::Secret;
6
7pub const SCOPES: &str = "repo read:org";
9pub const OAUTH_URL: &str = "https://github.com";
10pub const API_URL: &str = "https://api.github.com";
11pub const OAUTH_URL_ENV: &str = "SUBSPACE_GITHUB_OAUTH_URL";
14pub const API_URL_ENV: &str = "SUBSPACE_GITHUB_API_URL";
17
18#[derive(Debug, Clone, PartialEq, Eq)]
21pub struct OAuthClient {
22 pub id: String,
23 pub secret: Option<Secret>,
24}
25
26impl OAuthClient {
27 pub fn new(id: &str, secret: Option<&str>) -> Option<Self> {
30 let id = id.trim();
31 (!id.is_empty()).then(|| Self {
32 id: id.to_owned(),
33 secret: secret
34 .map(str::trim)
35 .filter(|secret| !secret.is_empty())
36 .map(Secret::new),
37 })
38 }
39}
40
41#[derive(Debug, Clone, PartialEq, Eq)]
43pub struct Endpoints {
44 pub oauth: String,
45 pub api: String,
46}
47
48impl Endpoints {
49 pub fn github() -> Self {
50 Self {
51 oauth: OAUTH_URL.to_owned(),
52 api: API_URL.to_owned(),
53 }
54 }
55
56 pub fn from_build() -> Self {
58 let read = |name| std::env::var(name).ok();
59 Self::with_overrides(
60 cfg!(debug_assertions),
61 read(OAUTH_URL_ENV).as_deref(),
62 read(API_URL_ENV).as_deref(),
63 )
64 }
65
66 pub fn with_overrides(debug: bool, oauth: Option<&str>, api: Option<&str>) -> Self {
69 let github = Self::github();
70 let pick = |value: Option<&str>, default: String| {
71 value
72 .filter(|_| debug)
73 .and_then(loopback_base)
74 .unwrap_or(default)
75 };
76 Self {
77 oauth: pick(oauth, github.oauth),
78 api: pick(api, github.api),
79 }
80 }
81}
82
83fn loopback_base(value: &str) -> Option<String> {
85 let url = url::Url::parse(value.trim()).ok()?;
86 let loopback = match url.host()? {
87 url::Host::Ipv4(address) => IpAddr::V4(address).is_loopback(),
88 url::Host::Ipv6(address) => IpAddr::V6(address).is_loopback(),
89 url::Host::Domain(_) => false,
90 };
91 (url.scheme() == "http" && loopback && url.query().is_none())
92 .then(|| value.trim().trim_end_matches('/').to_owned())
93}
94
95#[cfg(test)]
96mod tests {
97 use subspace_core::Secret;
98
99 use super::{API_URL, Endpoints, OAUTH_URL, OAuthClient};
100
101 #[test]
102 fn a_blank_client_id_is_no_oauth_app_and_a_blank_secret_is_none() {
103 assert_eq!(OAuthClient::new(" ", Some("secret")), None);
104 let client = OAuthClient::new(" Iv1.example ", Some(" ")).unwrap();
105 assert_eq!(client.id, "Iv1.example");
106 assert_eq!(client.secret, None);
107 let client = OAuthClient::new("Iv1.example", Some(" fixture-secret\n")).unwrap();
108 assert_eq!(client.secret, Some(Secret::new("fixture-secret")));
109 }
110
111 #[test]
112 fn only_a_debug_build_accepts_numeric_loopback_overrides() {
113 let fixture = Endpoints::with_overrides(
114 true,
115 Some("http://127.0.0.1:4100/"),
116 Some("http://[::1]:4101"),
117 );
118 assert_eq!(fixture.oauth, "http://127.0.0.1:4100");
119 assert_eq!(fixture.api, "http://[::1]:4101");
120 let release = Endpoints::with_overrides(false, Some("http://127.0.0.1:4100"), None);
121 assert_eq!(release.oauth, OAUTH_URL);
122 for refused in [
123 "http://localhost:4100",
124 "https://127.0.0.1:4100",
125 "http://10.0.0.1:4100",
126 "http://example.com",
127 "not a url",
128 ] {
129 let endpoints = Endpoints::with_overrides(true, Some(refused), Some(refused));
130 assert_eq!(endpoints.oauth, OAUTH_URL, "{refused}");
131 assert_eq!(endpoints.api, API_URL, "{refused}");
132 }
133 }
134}