Skip to content

Commit 2ee492c

Browse files
author
Nick van der Burgt
committed
make nbf optional, validate nbf if present
Signed-off-by: Nick van der Burgt <nvanderburgt@infiniot.nl>
1 parent cf03f03 commit 2ee492c

2 files changed

Lines changed: 16 additions & 14 deletions

File tree

openleadr-vtn/src/jwt.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,15 @@ struct EdKeys {
179179
#[derive(Debug, serde::Serialize, serde::Deserialize)]
180180
pub(crate) struct Claims {
181181
exp: usize,
182-
nbf: usize,
182+
nbf: Option<usize>,
183183
pub(crate) sub: String,
184184
pub(crate) roles: Vec<AuthRole>,
185185
}
186186

187187
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
188188
struct InitialClaims {
189189
exp: usize,
190-
nbf: usize,
190+
nbf: Option<usize>,
191191
sub: String,
192192
#[serde(default)]
193193
// Allow the roles claim to either contain the internal roles structure of OpenLEADR
@@ -333,7 +333,7 @@ impl Claims {
333333
pub(crate) fn new(roles: Vec<AuthRole>) -> Self {
334334
Self {
335335
exp: 0,
336-
nbf: 0,
336+
nbf: Some(0),
337337
sub: "".to_string(),
338338
roles,
339339
}
@@ -449,7 +449,7 @@ impl JwtManager {
449449

450450
let claims = Claims {
451451
exp: exp.timestamp() as usize,
452-
nbf: now.timestamp() as usize,
452+
nbf: Some(now.timestamp() as usize),
453453
sub: client_id,
454454
roles,
455455
};
@@ -711,7 +711,7 @@ mod tests {
711711
fn test_no_roles_no_scope_into_claims() {
712712
let initial = InitialClaims {
713713
exp: 10,
714-
nbf: 10,
714+
nbf: Some(10),
715715
sub: "test".to_string(),
716716
roles: None,
717717
scope: None,
@@ -725,7 +725,7 @@ mod tests {
725725
fn test_initial_roles_into_claims() {
726726
let initial = InitialClaims {
727727
exp: 10,
728-
nbf: 10,
728+
nbf: Some(10),
729729
sub: "test".to_string(),
730730
roles: Some(RolesOrScopes::AuthRoles(vec![
731731
AuthRole::AnyBusiness,
@@ -751,7 +751,7 @@ mod tests {
751751
fn test_scope_ignored_if_roles_present() {
752752
let initial = InitialClaims {
753753
exp: 10,
754-
nbf: 10,
754+
nbf: Some(10),
755755
sub: "test".to_string(),
756756
roles: Some(RolesOrScopes::AuthRoles(vec![AuthRole::AnyBusiness])),
757757
scope: Some(Scopes {
@@ -770,7 +770,7 @@ mod tests {
770770
fn test_scope_into_any_business_role() {
771771
let initial = InitialClaims {
772772
exp: 10,
773-
nbf: 10,
773+
nbf: Some(10),
774774
sub: "test".to_string(),
775775
roles: None,
776776
scope: Some(Scopes {
@@ -792,7 +792,7 @@ mod tests {
792792
fn test_scope_into_ven_manager_role() {
793793
let initial = InitialClaims {
794794
exp: 10,
795-
nbf: 10,
795+
nbf: Some(10),
796796
sub: "test".to_string(),
797797
roles: None,
798798
scope: Some(Scopes {
@@ -814,7 +814,7 @@ mod tests {
814814
fn test_scope_into_anonymous_ven_role() {
815815
let initial = InitialClaims {
816816
exp: 10,
817-
nbf: 10,
817+
nbf: Some(10),
818818
sub: "test".to_string(),
819819
roles: None,
820820
scope: Some(Scopes {
@@ -839,7 +839,7 @@ mod tests {
839839
fn test_scope_into_multiple_roles() {
840840
let initial = InitialClaims {
841841
exp: 10,
842-
nbf: 10,
842+
nbf: Some(10),
843843
sub: "test".to_string(),
844844
roles: None,
845845
scope: Some(Scopes {
@@ -869,7 +869,7 @@ mod tests {
869869
fn test_oadr_roles_into_any_business_role() {
870870
let initial = InitialClaims {
871871
exp: 10,
872-
nbf: 10,
872+
nbf: Some(10),
873873
sub: "test".to_string(),
874874
roles: Some(RolesOrScopes::Scopes(vec![
875875
Scope::ReadAll,
@@ -893,7 +893,7 @@ mod tests {
893893
fn test_oadr_roles_into_ven_manager_role() {
894894
let initial = InitialClaims {
895895
exp: 10,
896-
nbf: 10,
896+
nbf: Some(10),
897897
sub: "test".to_string(),
898898
roles: Some(RolesOrScopes::Scopes(vec![
899899
Scope::ReadAll,
@@ -916,7 +916,7 @@ mod tests {
916916
fn test_oadr_roles_into_anonymous_ven_role() {
917917
let initial = InitialClaims {
918918
exp: 10,
919-
nbf: 10,
919+
nbf: Some(10),
920920
sub: "test".to_string(),
921921
roles: Some(RolesOrScopes::Scopes(vec![
922922
Scope::ReadAll,

openleadr-vtn/src/state.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ fn internal_oauth_from_env(key_type: Option<OAuthKeyType>) -> JwtManager {
151151
});
152152

153153
let mut validation = Validation::default();
154+
validation.validate_nbf = true;
154155
validation.algorithms =
155156
signing_algorithms_from_key_type(&key_type.unwrap_or(OAuthKeyType::Hmac));
156157
validation.set_audience(&valid_audiences);
@@ -172,6 +173,7 @@ async fn external_oauth_from_env(key_type: Option<OAuthKeyType>) -> JwtManager {
172173
let mut validation = Validation::default();
173174
validation.algorithms = signing_algorithms_from_key_type(&key_type);
174175
validation.set_audience(&valid_audiences);
176+
validation.validate_nbf = true;
175177

176178
let oauth_jwks_location = env::var("OAUTH_JWKS_LOCATION");
177179
let oauth_keyfile = env::var("OAUTH_PEM");

0 commit comments

Comments
 (0)