Skip to content

Commit cc5819c

Browse files
committed
Switch to insecure_decode to avoid constructing invalid key
1 parent 62c883d commit cc5819c

File tree

2 files changed

+6
-26
lines changed

2 files changed

+6
-26
lines changed

foundation/auth/src/token_source/compute_identity_source.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use async_trait::async_trait;
2-
use jsonwebtoken::Validation;
32
use serde::Deserialize;
43
use time::OffsetDateTime;
54
use urlencoding::encode;
@@ -20,8 +19,6 @@ use crate::token_source::{default_http_client, TokenSource};
2019
pub struct ComputeIdentitySource {
2120
token_url: String,
2221
client: reqwest::Client,
23-
decoding_key: jsonwebtoken::DecodingKey,
24-
validation: jsonwebtoken::Validation,
2522
}
2623

2724
impl std::fmt::Debug for ComputeIdentitySource {
@@ -39,21 +36,13 @@ impl ComputeIdentitySource {
3936
Err(_e) => METADATA_IP.to_string(),
4037
};
4138

42-
// Only used to extract the expiry without checking the signature.
43-
let mut validation = Validation::default();
44-
validation.insecure_disable_signature_validation();
45-
validation.set_audience(&[audience]);
46-
let decoding_key = jsonwebtoken::DecodingKey::from_secret(b"");
47-
4839
Ok(ComputeIdentitySource {
4940
token_url: format!(
5041
"http://{}/computeMetadata/v1/instance/service-accounts/default/identity?audience={}&format=full",
5142
host,
5243
encode(audience)
5344
),
5445
client: default_http_client(),
55-
decoding_key,
56-
validation,
5746
})
5847
}
5948
}
@@ -75,14 +64,11 @@ impl TokenSource for ComputeIdentitySource {
7564
.text()
7665
.await?;
7766

78-
let exp = jsonwebtoken::decode::<ExpClaim>(&jwt, &self.decoding_key, &self.validation)?
79-
.claims
80-
.exp;
81-
67+
let token = jsonwebtoken::dangerous::insecure_decode::<ExpClaim>(jwt.as_bytes())?;
8268
Ok(Token {
8369
access_token: jwt,
8470
token_type: "Bearer".into(),
85-
expiry: OffsetDateTime::from_unix_timestamp(exp).ok(),
71+
expiry: OffsetDateTime::from_unix_timestamp(token.claims.exp).ok(),
8672
})
8773
}
8874
}

foundation/auth/src/token_source/mod.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,10 @@ impl InternalIdToken {
6666
})
6767
}
6868

69-
fn get_exp(&self, audience: &str) -> Result<i64, Error> {
70-
let mut validation = jsonwebtoken::Validation::default();
71-
validation.insecure_disable_signature_validation();
72-
validation.set_audience(&[audience]);
73-
let decoding_key = jsonwebtoken::DecodingKey::from_secret(b"");
74-
Ok(
75-
jsonwebtoken::decode::<ExpClaim>(self.id_token.as_str(), &decoding_key, &validation)?
76-
.claims
77-
.exp,
78-
)
69+
fn get_exp(&self, _audience: &str) -> Result<i64, Error> {
70+
//skips all checks, so audience has to be manually checked if necessary
71+
let token = jsonwebtoken::dangerous::insecure_decode::<ExpClaim>(self.id_token.as_bytes())?;
72+
Ok(token.claims.exp)
7973
}
8074
}
8175

0 commit comments

Comments
 (0)