Skip to content

Commit 7562c93

Browse files
committed
Bug 2006638 - Add use_cache param to get_access_token
Removed the `ttl` param, since multiple optional positional args can get weird. I took a look at searchfox and firefox-ios and there didn't seem to be any consumers using the `ttl` param. Alternatively, we could input an `options` struct with multiple optional params. I didn't go this way because I didn't see anyone actually setting the `ttl` param, but maybe I'm missing something.
1 parent 6087f7c commit 7562c93

5 files changed

Lines changed: 28 additions & 17 deletions

File tree

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@
22

33
[Full Changelog](In progress)
44

5+
## ⚠️ Breaking Changes ⚠️
6+
7+
### Fxa Client
8+
- Removed the optional `ttl` paramater to `get_access_token`. In practice, no consumers were using this.
9+
10+
## ✨ What's New ✨
11+
512
### Ads Client
613
- Add agnostic telemetry support (compatible with Glean)
714

15+
### Fxa Client
16+
- Added optional `use_cache` paramater to `get_access_token`. Set this to `false` to force
17+
requesting a new token.
18+
819
# v147.0 (_2025-12-07_)
920

1021
### Relay

components/fxa-client/src/fxa_client.udl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ interface FirefoxAccount {
636636
/// - `scope` - the OAuth scope to be granted by the token.
637637
/// - This must be one of the scopes requested during the signin flow.
638638
/// - Only a single scope is supported; for multiple scopes request multiple tokens.
639-
/// - `ttl` - optionally, the time for which the token should be valid, in seconds.
639+
/// - `use_cache` - optionally set to false to force a new token request.
640640
///
641641
/// # Notes
642642
///
@@ -645,7 +645,7 @@ interface FirefoxAccount {
645645
/// before requesting a fresh token.
646646
///
647647
[Throws=FxaError]
648-
AccessTokenInfo get_access_token([ByRef] string scope, optional i64? ttl = null);
648+
AccessTokenInfo get_access_token([ByRef] string scope, optional boolean use_cache = true);
649649

650650
/// Get the session token for the user's account, if one is available.
651651
///

components/fxa-client/src/internal/oauth.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,20 @@ impl FirefoxAccount {
3838
/// using `begin_oauth_flow`.
3939
///
4040
/// * `scopes` - Space-separated list of requested scopes.
41-
/// * `ttl` - the ttl in seconds of the token requested from the server.
41+
/// * `use_cache` - optionally set to false to force a new token request.
4242
///
4343
/// **💾 This method may alter the persisted account state.**
44-
pub fn get_access_token(&mut self, scope: &str, ttl: Option<u64>) -> Result<AccessTokenInfo> {
44+
pub fn get_access_token(&mut self, scope: &str, use_cache: bool) -> Result<AccessTokenInfo> {
4545
if scope.contains(' ') {
4646
return Err(Error::MultipleScopesRequested);
4747
}
48-
if let Some(oauth_info) = self.state.get_cached_access_token(scope) {
49-
if oauth_info.expires_at > util::now_secs() + OAUTH_MIN_TIME_LEFT {
50-
// If the cached key is missing the required sync scoped key, try to fetch it again
51-
if oauth_info.check_missing_sync_scoped_key().is_ok() {
52-
return Ok(oauth_info.clone());
48+
if use_cache {
49+
if let Some(oauth_info) = self.state.get_cached_access_token(scope) {
50+
if oauth_info.expires_at > util::now_secs() + OAUTH_MIN_TIME_LEFT {
51+
// If the cached key is missing the required sync scoped key, try to fetch it again
52+
if oauth_info.check_missing_sync_scoped_key().is_ok() {
53+
return Ok(oauth_info.clone());
54+
}
5355
}
5456
}
5557
}
@@ -59,7 +61,7 @@ impl FirefoxAccount {
5961
self.client.create_access_token_using_refresh_token(
6062
self.state.config(),
6163
&refresh_token.token,
62-
ttl,
64+
None,
6365
&[scope],
6466
)?
6567
} else {

components/fxa-client/src/internal/profile.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl FirefoxAccount {
4545
}
4646
etag = Some(cached_profile.etag.clone());
4747
}
48-
let profile_access_token = self.get_access_token(scopes::PROFILE, None)?.token;
48+
let profile_access_token = self.get_access_token(scopes::PROFILE, true)?.token;
4949
match self
5050
.client
5151
.get_profile(self.state.config(), &profile_access_token, etag)?

components/fxa-client/src/token.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use crate::{ApiResult, Error, FirefoxAccount};
1919
use error_support::handle_error;
2020
use serde_derive::*;
21-
use std::convert::{TryFrom, TryInto};
21+
use std::convert::TryInto;
2222

2323
impl FirefoxAccount {
2424
/// Get a short-lived OAuth access token for the user's account.
@@ -37,20 +37,18 @@ impl FirefoxAccount {
3737
/// - `scope` - the OAuth scope to be granted by the token.
3838
/// - This must be one of the scopes requested during the signin flow.
3939
/// - Only a single scope is supported; for multiple scopes request multiple tokens.
40-
/// - `ttl` - optionally, the time for which the token should be valid, in seconds.
40+
/// - `use_cache` - optionally set to false to force a new token request.
4141
///
4242
/// # Notes
4343
///
4444
/// - If the application receives an authorization error when trying to use the resulting
4545
/// token, it should call [`clear_access_token_cache`](FirefoxAccount::clear_access_token_cache)
4646
/// before requesting a fresh token.
4747
#[handle_error(Error)]
48-
pub fn get_access_token(&self, scope: &str, ttl: Option<i64>) -> ApiResult<AccessTokenInfo> {
49-
// Signedness converstion for Kotlin compatibility :-/
50-
let ttl = ttl.map(|ttl| u64::try_from(ttl).unwrap_or_default());
48+
pub fn get_access_token(&self, scope: &str, use_cache: bool) -> ApiResult<AccessTokenInfo> {
5149
self.internal
5250
.lock()
53-
.get_access_token(scope, ttl)?
51+
.get_access_token(scope, use_cache)?
5452
.try_into()
5553
}
5654

0 commit comments

Comments
 (0)