Skip to content

Commit 27a4405

Browse files
committed
chore: example for oauth
1 parent f77a964 commit 27a4405

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ reqwest = { version = "0.12.15", optional = true }
5454
[dev-dependencies]
5555
ajj = "0.3.1"
5656
axum = "0.8.1"
57+
eyre = "0.6.12"
5758
serial_test = "3.2.0"
5859
signal-hook = "0.3.17"
5960
tokio = { version = "1.43.0", features = ["macros"] }

examples/oauth.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use init4_bin_base::{perms::OAuthConfig, utils::from_env::FromEnv};
2+
3+
#[tokio::main]
4+
async fn main() -> eyre::Result<()> {
5+
let cfg = OAuthConfig::from_env()?;
6+
let authenticator = cfg.authenticator();
7+
let token = authenticator.token();
8+
9+
let _jh = authenticator.spawn();
10+
11+
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
12+
dbg!(token.read());
13+
14+
Ok(())
15+
}

src/perms/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ pub(crate) mod config;
55
pub use config::{SlotAuthzConfig, SlotAuthzConfigEnvError};
66

77
pub(crate) mod oauth;
8-
pub use oauth::{Authenticator, SharedToken};
8+
pub use oauth::{Authenticator, OAuthConfig, SharedToken};

src/perms/oauth.rs

+7
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ pub struct OAuthConfig {
5050
pub oauth_token_refresh_interval: u64,
5151
}
5252

53+
impl OAuthConfig {
54+
/// Create a new [`Authenticator`] from the provided config.
55+
pub fn authenticator(&self) -> Authenticator {
56+
Authenticator::new(self)
57+
}
58+
}
59+
5360
/// A shared token that can be read and written to by multiple threads.
5461
#[derive(Debug, Clone, Default)]
5562
pub struct SharedToken(Arc<Mutex<Option<Token>>>);

0 commit comments

Comments
 (0)