Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3360442
D12766.id42323.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
D12766.id42323.diff
View Options
diff --git a/services/tunnelbroker/src/constants.rs b/services/tunnelbroker/src/constants.rs
--- a/services/tunnelbroker/src/constants.rs
+++ b/services/tunnelbroker/src/constants.rs
@@ -15,6 +15,7 @@
pub const ENV_FCM_CONFIG: &str = "FCM_CONFIG";
pub const LOG_LEVEL_ENV_VAR: &str =
tracing_subscriber::filter::EnvFilter::DEFAULT_ENV;
+pub const FCM_ACCESS_TOKEN_GENERATION_THRESHOLD: u64 = 5 * 60;
pub mod dynamodb {
// This table holds messages which could not be immediately delivered to
diff --git a/services/tunnelbroker/src/notifs/fcm/token.rs b/services/tunnelbroker/src/notifs/fcm/token.rs
--- a/services/tunnelbroker/src/notifs/fcm/token.rs
+++ b/services/tunnelbroker/src/notifs/fcm/token.rs
@@ -1,3 +1,4 @@
+use crate::constants::FCM_ACCESS_TOKEN_GENERATION_THRESHOLD;
use crate::notifs::fcm::config::FCMConfig;
use crate::notifs::fcm::error::Error;
use crate::notifs::fcm::error::Error::FCMTokenNotInitialized;
@@ -40,6 +41,10 @@
}
pub async fn get_auth_bearer(&self) -> Result<String, Error> {
+ if self.check_if_fcm_token_needs_generation().await {
+ self.generate_fcm_token().await?;
+ }
+
let bearer = self.token.read().await;
match &*bearer {
Some(token) => Ok(format!("{} {}", token.token_type, token.access_token)),
@@ -89,4 +94,39 @@
let access_token = response.json::<FCMAccessTokenResponse>().await?;
Ok(access_token)
}
+
+ async fn check_if_fcm_token_needs_generation(&self) -> bool {
+ let token = self.token.read().await;
+ match &*token {
+ None => true,
+ Some(token) => {
+ get_time() - FCM_ACCESS_TOKEN_GENERATION_THRESHOLD
+ >= token.expiration_time
+ }
+ }
+ }
+ async fn generate_fcm_token(&self) -> Result<(), Error> {
+ debug!("Generating FCM access token");
+ let mut token = self.token.write().await;
+
+ let created_at = get_time();
+ let new_jwt_token = self.get_jwt_token(created_at)?;
+ let access_token_response =
+ self.get_fcm_access_token(new_jwt_token).await?;
+
+ *token = Some(FCMAccessToken {
+ access_token: access_token_response.access_token,
+ token_type: access_token_response.token_type,
+ expiration_time: created_at + access_token_response.expires_in,
+ });
+
+ Ok(())
+ }
+}
+
+fn get_time() -> u64 {
+ SystemTime::now()
+ .duration_since(UNIX_EPOCH)
+ .unwrap()
+ .as_secs()
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Nov 25, 12:45 PM (20 h, 54 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2579833
Default Alt Text
D12766.id42323.diff (2 KB)
Attached To
Mode
D12766: [Tunnelbroker] implement FCM Access Token lifetime
Attached
Detach File
Event Timeline
Log In to Comment