ngrok/config/
oauth.rs

1use crate::internals::proto::{
2    Oauth,
3    SecretString,
4};
5
6/// Oauth Options configuration
7///
8/// https://ngrok.com/docs/http/oauth/
9#[derive(Clone, Default)]
10pub struct OauthOptions {
11    /// The OAuth provider to use
12    provider: String,
13
14    /// The client ID, if a custom one is being used
15    client_id: String,
16    /// The client secret, if a custom one is being used
17    client_secret: SecretString,
18
19    /// Email addresses of users to authorize.
20    allow_emails: Vec<String>,
21    /// Email domains of users to authorize.
22    allow_domains: Vec<String>,
23    /// OAuth scopes to request from the provider.
24    scopes: Vec<String>,
25}
26
27impl OauthOptions {
28    /// Create a new [OauthOptions] for the given provider.
29    pub fn new(provider: impl Into<String>) -> Self {
30        OauthOptions {
31            provider: provider.into(),
32            ..Default::default()
33        }
34    }
35
36    /// Provide an OAuth client ID for custom apps.
37    pub fn client_id(&mut self, id: impl Into<String>) -> &mut Self {
38        self.client_id = id.into();
39        self
40    }
41
42    /// Provide an OAuth client secret for custom apps.
43    pub fn client_secret(&mut self, secret: impl Into<String>) -> &mut Self {
44        self.client_secret = SecretString::from(secret.into());
45        self
46    }
47
48    /// Append an email address to the list of allowed emails.
49    pub fn allow_email(&mut self, email: impl Into<String>) -> &mut Self {
50        self.allow_emails.push(email.into());
51        self
52    }
53    /// Append an email domain to the list of allowed domains.
54    pub fn allow_domain(&mut self, domain: impl Into<String>) -> &mut Self {
55        self.allow_domains.push(domain.into());
56        self
57    }
58    /// Append a scope to the list of scopes to request.
59    pub fn scope(&mut self, scope: impl Into<String>) -> &mut Self {
60        self.scopes.push(scope.into());
61        self
62    }
63}
64
65// transform into the wire protocol format
66impl From<OauthOptions> for Oauth {
67    fn from(o: OauthOptions) -> Self {
68        Oauth {
69            provider: o.provider,
70            client_id: o.client_id,
71            client_secret: o.client_secret,
72            sealed_client_secret: Default::default(), // unused in this context
73            allow_emails: o.allow_emails,
74            allow_domains: o.allow_domains,
75            scopes: o.scopes,
76        }
77    }
78}