Skip to content

Commit ad56383

Browse files
Fix CatalystRBACTokenV1::is_young implementation (#1329)
Co-authored-by: Alex Pozhylenkov <[email protected]>
1 parent 5a42e7c commit ad56383

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

catalyst-gateway/bin/src/service/common/auth/rbac/scheme.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ async fn checker_api_catalyst_auth(
113113
};
114114

115115
// Check if the token is young enough.
116-
if !token.young(MAX_TOKEN_AGE, MAX_TOKEN_SKEW) {
116+
if !token.is_young(MAX_TOKEN_AGE, MAX_TOKEN_SKEW) {
117117
// Token is too old or too far in the future.
118118
error!("Auth token expired: {:?}", token);
119119
Err(AuthTokenAccessViolation(vec!["EXPIRED".to_string()]))?;

catalyst-gateway/bin/src/service/common/auth/rbac/token.rs

+35-4
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,15 @@ impl CatalystRBACTokenV1 {
174174

175175
/// Check if the token is young enough.
176176
/// Old tokens are no longer valid.
177-
pub(crate) fn young(&self, max_age: Duration, max_skew: Duration) -> bool {
177+
pub(crate) fn is_young(&self, max_age: Duration, max_skew: Duration) -> bool {
178178
// We check that the token is not too old or too skewed.
179179
let now = SystemTime::now();
180180
let token_age = self.ulid.datetime();
181181

182182
// The token is considered old if it was issued more than max_age ago.
183-
// Or newer than an allowed clock skew value
183+
// And newer than an allowed clock skew value
184184
// This is a safety measure to avoid replay attacks.
185-
((now - max_age) > token_age) && ((now + max_skew) < token_age)
185+
((now - max_age) < token_age) && ((now + max_skew) > token_age)
186186
}
187187
}
188188

@@ -203,7 +203,7 @@ mod tests {
203203
use ed25519_dalek::SigningKey;
204204
use rand::rngs::OsRng;
205205

206-
use crate::service::common::auth::rbac::token::{CatalystRBACTokenV1, Kid};
206+
use super::*;
207207

208208
#[test]
209209
fn test_token_generation_and_decoding() {
@@ -235,4 +235,35 @@ mod tests {
235235
assert!(re_encoded_token.verify(&verifying_key).is_ok());
236236
assert!(re_encoded_token.verify(&verifying_key2).is_err());
237237
}
238+
239+
#[test]
240+
fn is_young() {
241+
let mut random_seed = OsRng;
242+
let key = SigningKey::generate(&mut random_seed);
243+
let mut token = CatalystRBACTokenV1::new(&key);
244+
245+
// Update the token timestamp to be two seconds in the past.
246+
let now = SystemTime::now();
247+
token.ulid = Ulid::from_datetime(now - Duration::from_secs(2));
248+
249+
// Check that the token ISN'T young if max_age is one second.
250+
let max_age = Duration::from_secs(1);
251+
let max_skew = Duration::from_secs(1);
252+
assert!(!token.is_young(max_age, max_skew));
253+
254+
// Check that the token IS young if max_age is three seconds.
255+
let max_age = Duration::from_secs(3);
256+
assert!(token.is_young(max_age, max_skew));
257+
258+
// Update the token timestamp to be two seconds in the future.
259+
token.ulid = Ulid::from_datetime(now + Duration::from_secs(2));
260+
261+
// Check that the token IS too new if max_skew is one seconds.
262+
let max_skew = Duration::from_secs(1);
263+
assert!(!token.is_young(max_age, max_skew));
264+
265+
// Check that the token ISN'T too new if max_skew is three seconds.
266+
let max_skew = Duration::from_secs(3);
267+
assert!(token.is_young(max_age, max_skew));
268+
}
238269
}

0 commit comments

Comments
 (0)