@@ -174,15 +174,15 @@ impl CatalystRBACTokenV1 {
174
174
175
175
/// Check if the token is young enough.
176
176
/// 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 {
178
178
// We check that the token is not too old or too skewed.
179
179
let now = SystemTime :: now ( ) ;
180
180
let token_age = self . ulid . datetime ( ) ;
181
181
182
182
// 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
184
184
// 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)
186
186
}
187
187
}
188
188
@@ -203,7 +203,7 @@ mod tests {
203
203
use ed25519_dalek:: SigningKey ;
204
204
use rand:: rngs:: OsRng ;
205
205
206
- use crate :: service :: common :: auth :: rbac :: token :: { CatalystRBACTokenV1 , Kid } ;
206
+ use super :: * ;
207
207
208
208
#[ test]
209
209
fn test_token_generation_and_decoding ( ) {
@@ -235,4 +235,35 @@ mod tests {
235
235
assert ! ( re_encoded_token. verify( & verifying_key) . is_ok( ) ) ;
236
236
assert ! ( re_encoded_token. verify( & verifying_key2) . is_err( ) ) ;
237
237
}
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
+ }
238
269
}
0 commit comments