Fix: Consistently enforce session expiration (#3686)

This commit is contained in:
Gerard Guillemas Martos 2024-03-13 10:06:16 +01:00 committed by GitHub
parent 3c92765fad
commit 9e2a0c75ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 29 additions and 12 deletions

View file

@ -244,7 +244,8 @@ pub async fn db(
session.tk = Some(val.into()); session.tk = Some(val.into());
session.ns = Some(ns.to_owned()); session.ns = Some(ns.to_owned());
session.db = Some(db.to_owned()); session.db = Some(db.to_owned());
session.exp = exp; // TODO(gguillemas): Enforce expiration once session lifetime can be customized.
session.exp = None;
session.au = Arc::new((&u, Level::Database(ns.to_owned(), db.to_owned())).into()); session.au = Arc::new((&u, Level::Database(ns.to_owned(), db.to_owned())).into());
// Check the authentication token // Check the authentication token
match enc { match enc {
@ -296,7 +297,8 @@ pub async fn ns(
// Set the authentication on the session // Set the authentication on the session
session.tk = Some(val.into()); session.tk = Some(val.into());
session.ns = Some(ns.to_owned()); session.ns = Some(ns.to_owned());
session.exp = exp; // TODO(gguillemas): Enforce expiration once session lifetime can be customized.
session.exp = None;
session.au = Arc::new((&u, Level::Namespace(ns.to_owned())).into()); session.au = Arc::new((&u, Level::Namespace(ns.to_owned())).into());
// Check the authentication token // Check the authentication token
match enc { match enc {
@ -346,7 +348,8 @@ pub async fn root(
let enc = encode(&HEADER, &val, &key); let enc = encode(&HEADER, &val, &key);
// Set the authentication on the session // Set the authentication on the session
session.tk = Some(val.into()); session.tk = Some(val.into());
session.exp = exp; // TODO(gguillemas): Enforce expiration once session lifetime can be customized.
session.exp = None;
session.au = Arc::new((&u, Level::Root).into()); session.au = Arc::new((&u, Level::Root).into());
// Check the authentication token // Check the authentication token
match enc { match enc {

View file

@ -70,20 +70,33 @@ pub async fn sc(
// Create the authentication key // Create the authentication key
let key = EncodingKey::from_secret(sv.code.as_ref()); let key = EncodingKey::from_secret(sv.code.as_ref());
// Create the authentication claim // Create the authentication claim
let exp = Some(
match sv.session {
Some(v) => {
// The defined session duration must be valid
match Duration::from_std(v.0) {
// The resulting session expiration must be valid
Ok(d) => match Utc::now().checked_add_signed(d) {
Some(exp) => exp,
None => {
return Err(Error::InvalidSessionExpiration)
}
},
Err(_) => {
return Err(Error::InvalidSessionDuration)
}
}
}
_ => Utc::now() + Duration::hours(1),
}
.timestamp(),
);
let val = Claims { let val = Claims {
iss: Some(SERVER_NAME.to_owned()), iss: Some(SERVER_NAME.to_owned()),
iat: Some(Utc::now().timestamp()), iat: Some(Utc::now().timestamp()),
nbf: Some(Utc::now().timestamp()), nbf: Some(Utc::now().timestamp()),
jti: Some(Uuid::new_v4().to_string()), jti: Some(Uuid::new_v4().to_string()),
exp: Some( exp,
match sv.session {
Some(v) => {
Utc::now() + Duration::from_std(v.0).unwrap()
}
_ => Utc::now() + Duration::hours(1),
}
.timestamp(),
),
ns: Some(ns.to_owned()), ns: Some(ns.to_owned()),
db: Some(db.to_owned()), db: Some(db.to_owned()),
sc: Some(sc.to_owned()), sc: Some(sc.to_owned()),
@ -100,6 +113,7 @@ pub async fn sc(
session.db = Some(db.to_owned()); session.db = Some(db.to_owned());
session.sc = Some(sc.to_owned()); session.sc = Some(sc.to_owned());
session.sd = Some(Value::from(rid.to_owned())); session.sd = Some(Value::from(rid.to_owned()));
session.exp = exp;
session.au = Arc::new(Auth::new(Actor::new( session.au = Arc::new(Auth::new(Actor::new(
rid.to_string(), rid.to_string(),
Default::default(), Default::default(),