Check if a token needs refresh (within refresh window of expiry). Returns `Some(remaining_secs)` if the token should be refreshed, `None` if still healthy.
(
token: &str,
config: &JwtConfig,
shared: &crate::control::state::SharedState,
)
| 82 | /// Returns `Some(remaining_secs)` if the token should be refreshed, |
| 83 | /// `None` if still healthy. |
| 84 | pub fn check_token_refresh_needed( |
| 85 | token: &str, |
| 86 | config: &JwtConfig, |
| 87 | shared: &crate::control::state::SharedState, |
| 88 | ) -> Option<u64> { |
| 89 | let token_refresh_window_secs = shared.tuning.network.token_refresh_window_secs; |
| 90 | // Re-validate to check if still valid. |
| 91 | let validator = JwtValidator::new(config.clone()); |
| 92 | match validator.validate(token) { |
| 93 | Ok(_) => { |
| 94 | let exp = extract_exp_from_token(token).unwrap_or(0); |
| 95 | if exp == 0 { |
| 96 | return None; // No expiry set. |
| 97 | } |
| 98 | let now = now_epoch_secs(); |
| 99 | let remaining = exp.saturating_sub(now); |
| 100 | if remaining <= token_refresh_window_secs { |
| 101 | Some(remaining) |
| 102 | } else { |
| 103 | None |
| 104 | } |
| 105 | } |
| 106 | Err(JwtError::Expired) => Some(0), // Already expired. |
| 107 | Err(_) => None, // Other errors — not a refresh issue. |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /// Extract the `exp` claim from a JWT without full validation. |
| 112 | fn extract_exp_from_token(token: &str) -> Option<u64> { |
nothing calls this directly
no test coverage detected