Skip to content

Commit d0077f3

Browse files
committed
chore: misc cleanup and documentation
1 parent 270f8b4 commit d0077f3

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

src/perms/oauth.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ impl OAuthConfig {
6262
}
6363

6464
/// A self-refreshing, periodically fetching authenticator for the block
65-
/// builder. This task periodically fetches a new token, and stores it in a
66-
/// [`SharedToken`].
65+
/// builder. This task periodically fetches a new token, and sends it to all
66+
/// active [`SharedToken`]s via a [`tokio::sync::watch`] channel..
6767
#[derive(Debug)]
6868
pub struct Authenticator {
6969
/// Configuration
70-
pub config: OAuthConfig,
70+
config: OAuthConfig,
7171
client: MyOAuthClient,
7272
reqwest: reqwest::Client,
7373

@@ -146,6 +146,11 @@ impl Authenticator {
146146
Ok(token_result)
147147
}
148148

149+
/// Get a reference to the OAuth configuration.
150+
pub fn config(&self) -> &OAuthConfig {
151+
&self.config
152+
}
153+
149154
/// Create a future that contains the periodic refresh loop.
150155
async fn task_future(self) {
151156
let interval = self.config.oauth_token_refresh_interval;
@@ -212,6 +217,10 @@ impl SharedToken {
212217

213218
/// Wait for the token to be available, then get a reference to it.
214219
///
220+
/// Holding this reference will block the background task from updating
221+
/// the token until it is dropped, so it is recommended to drop this
222+
/// reference as soon as possible.
223+
///
215224
/// This is implemented using [`Receiver::wait_for`], and has the same
216225
/// blocking, panics, errors, and cancel safety. Unlike [`Self::secret`]
217226
/// it is NOT implemented using a clone, and will update the local view of
@@ -238,6 +247,10 @@ impl SharedToken {
238247
/// Borrow the current token, if available. If called before the token is
239248
/// set by the authentication task, this will return `None`.
240249
///
250+
/// Holding this reference will block the background task from updating
251+
/// the token until it is dropped, so it is recommended to drop this
252+
/// reference as soon as possible.
253+
///
241254
/// This is implemented using [`Receiver::borrow`].
242255
///
243256
/// [`Receiver::borrow`]: tokio::sync::watch::Receiver::borrow
@@ -247,8 +260,11 @@ impl SharedToken {
247260

248261
/// Check if the background task has produced an authentication token.
249262
///
250-
/// This is implemented using [`Receiver::borrow`], and checks if the
251-
/// borrowed token is `Some`.
263+
/// Holding this reference will block the background task from updating
264+
/// the token until it is dropped, so it is recommended to drop this
265+
/// reference as soon as possible.
266+
///
267+
/// This is implemented using [`Receiver::borrow`].
252268
///
253269
/// [`Receiver::borrow`]: tokio::sync::watch::Receiver::borrow
254270
pub fn is_authenticated(&self) -> bool {
@@ -259,7 +275,9 @@ impl SharedToken {
259275
/// A reference to token data, contained in a [`SharedToken`].
260276
///
261277
/// This is implemented using [`watch::Ref`], and as a result holds a lock on
262-
/// the token data. It is recommended that this be dropped
278+
/// the token data. Holding this reference will block the background task
279+
/// from updating the token until it is dropped, so it is recommended to drop
280+
/// this reference as soon as possible.
263281
pub struct TokenRef<'a> {
264282
inner: Ref<'a, Option<Token>>,
265283
}
@@ -277,26 +295,34 @@ impl fmt::Debug for TokenRef<'_> {
277295
}
278296

279297
impl<'a> TokenRef<'a> {
298+
/// Get a reference to the inner token.
280299
pub fn inner(&'a self) -> &'a Token {
281300
self.inner.as_ref().unwrap()
282301
}
283302

303+
/// Get a reference to the [`AccessToken`] contained in the token.
284304
pub fn access_token(&self) -> &AccessToken {
285305
self.inner().access_token()
286306
}
287307

308+
/// Get a reference to the [`TokenType`] instance contained in the token.
309+
///
310+
/// [`TokenType`]: oauth2::TokenType
288311
pub fn token_type(&self) -> &<Token as TokenResponse>::TokenType {
289312
self.inner().token_type()
290313
}
291314

315+
/// Get a reference to the current token's expiration time, if it has one.
292316
pub fn expires_in(&self) -> Option<std::time::Duration> {
293317
self.inner().expires_in()
294318
}
295319

320+
/// Get a reference to the refresh token, if it exists.
296321
pub fn refresh_token(&self) -> Option<&RefreshToken> {
297322
self.inner().refresh_token()
298323
}
299324

325+
/// Get a reference to the scopes associated with the token, if any.
300326
pub fn scopes(&self) -> Option<&Vec<Scope>> {
301327
self.inner().scopes()
302328
}

0 commit comments

Comments
 (0)