MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / check_token_refresh_needed

Function check_token_refresh_needed

nodedb/src/control/server/sync/security.rs:84–109  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

82/// Returns `Some(remaining_secs)` if the token should be refreshed,
83/// `None` if still healthy.
84pub 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.
112fn extract_exp_from_token(token: &str) -> Option<u64> {

Callers

nothing calls this directly

Calls 4

extract_exp_from_tokenFunction · 0.85
now_epoch_secsFunction · 0.70
cloneMethod · 0.45
validateMethod · 0.45

Tested by

no test coverage detected