@@ -62,12 +62,12 @@ impl OAuthConfig {
62
62
}
63
63
64
64
/// 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. .
67
67
#[ derive( Debug ) ]
68
68
pub struct Authenticator {
69
69
/// Configuration
70
- pub config : OAuthConfig ,
70
+ config : OAuthConfig ,
71
71
client : MyOAuthClient ,
72
72
reqwest : reqwest:: Client ,
73
73
@@ -146,6 +146,11 @@ impl Authenticator {
146
146
Ok ( token_result)
147
147
}
148
148
149
+ /// Get a reference to the OAuth configuration.
150
+ pub fn config ( & self ) -> & OAuthConfig {
151
+ & self . config
152
+ }
153
+
149
154
/// Create a future that contains the periodic refresh loop.
150
155
async fn task_future ( self ) {
151
156
let interval = self . config . oauth_token_refresh_interval ;
@@ -212,6 +217,10 @@ impl SharedToken {
212
217
213
218
/// Wait for the token to be available, then get a reference to it.
214
219
///
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
+ ///
215
224
/// This is implemented using [`Receiver::wait_for`], and has the same
216
225
/// blocking, panics, errors, and cancel safety. Unlike [`Self::secret`]
217
226
/// it is NOT implemented using a clone, and will update the local view of
@@ -238,6 +247,10 @@ impl SharedToken {
238
247
/// Borrow the current token, if available. If called before the token is
239
248
/// set by the authentication task, this will return `None`.
240
249
///
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
+ ///
241
254
/// This is implemented using [`Receiver::borrow`].
242
255
///
243
256
/// [`Receiver::borrow`]: tokio::sync::watch::Receiver::borrow
@@ -247,8 +260,11 @@ impl SharedToken {
247
260
248
261
/// Check if the background task has produced an authentication token.
249
262
///
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`].
252
268
///
253
269
/// [`Receiver::borrow`]: tokio::sync::watch::Receiver::borrow
254
270
pub fn is_authenticated ( & self ) -> bool {
@@ -259,7 +275,9 @@ impl SharedToken {
259
275
/// A reference to token data, contained in a [`SharedToken`].
260
276
///
261
277
/// 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.
263
281
pub struct TokenRef < ' a > {
264
282
inner : Ref < ' a , Option < Token > > ,
265
283
}
@@ -277,26 +295,34 @@ impl fmt::Debug for TokenRef<'_> {
277
295
}
278
296
279
297
impl < ' a > TokenRef < ' a > {
298
+ /// Get a reference to the inner token.
280
299
pub fn inner ( & ' a self ) -> & ' a Token {
281
300
self . inner . as_ref ( ) . unwrap ( )
282
301
}
283
302
303
+ /// Get a reference to the [`AccessToken`] contained in the token.
284
304
pub fn access_token ( & self ) -> & AccessToken {
285
305
self . inner ( ) . access_token ( )
286
306
}
287
307
308
+ /// Get a reference to the [`TokenType`] instance contained in the token.
309
+ ///
310
+ /// [`TokenType`]: oauth2::TokenType
288
311
pub fn token_type ( & self ) -> & <Token as TokenResponse >:: TokenType {
289
312
self . inner ( ) . token_type ( )
290
313
}
291
314
315
+ /// Get a reference to the current token's expiration time, if it has one.
292
316
pub fn expires_in ( & self ) -> Option < std:: time:: Duration > {
293
317
self . inner ( ) . expires_in ( )
294
318
}
295
319
320
+ /// Get a reference to the refresh token, if it exists.
296
321
pub fn refresh_token ( & self ) -> Option < & RefreshToken > {
297
322
self . inner ( ) . refresh_token ( )
298
323
}
299
324
325
+ /// Get a reference to the scopes associated with the token, if any.
300
326
pub fn scopes ( & self ) -> Option < & Vec < Scope > > {
301
327
self . inner ( ) . scopes ( )
302
328
}
0 commit comments