Upgrade Rust dependencies and replace jwt with jsonwebtoken
Update all crates to their latest compatible releases, bump scraper to 0.29, and replace the unmaintained jwt crate (RUSTSEC-2016-0007) with jsonwebtoken 11 on the aws-lc-rs backend, which also drops the direct hmac/sha2 dependencies. Tokens stay HS256 with zero expiry leeway, so tokens issued before the switch remain valid.
This commit is contained in:
1 parent
e20b41649f
commit
c7b1dd4658
3 files changed
+321
-449
No files matched your search
Generated
+270
-407
File diff suppressed because it is too large.
Load diff
+2
-4
@@ -26,12 +26,10 @@ diesel_migrations = "2.3"
|
||||
dotenvy = "0.15"
|
||||
bcrypt = "0.19"
|
||||
uuid = {version = "1.23", features=["serde", "v4"]}
|
||||
jwt = "0.16.0"
|
||||
hmac = "0.12"
|
||||
sha2 = "0.10"
|
||||
jsonwebtoken = { version = "11", default-features = false, features = ["aws_lc_rs"] }
|
||||
log = "0.4.32"
|
||||
env_logger = "0.11"
|
||||
scraper = "0.27"
|
||||
scraper = "0.29"
|
||||
actix-cors = "0.7"
|
||||
chrono = { version = "0.4.45", features = ["serde"] }
|
||||
dateparser = "0.3"
|
||||
|
||||
+49
-38
@@ -1,16 +1,12 @@
|
||||
extern crate hmac;
|
||||
extern crate jwt;
|
||||
extern crate sha2;
|
||||
|
||||
use std::env;
|
||||
|
||||
use actix_web::HttpRequest;
|
||||
use chrono::{Duration, Utc};
|
||||
use dotenvy::dotenv;
|
||||
use hmac::{Hmac, Mac};
|
||||
use jwt::{Header, SignWithKey, Token, VerifyWithKey};
|
||||
use jsonwebtoken::{
|
||||
decode, encode, errors::ErrorKind, Algorithm, DecodingKey, EncodingKey, Header, Validation,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::Sha256;
|
||||
|
||||
/// How long a freshly issued token remains valid for.
|
||||
const TOKEN_LIFETIME_HOURS: i64 = 730;
|
||||
@@ -30,45 +26,42 @@ struct Claims {
|
||||
exp: i64,
|
||||
}
|
||||
|
||||
type HmacSha256 = Hmac<Sha256>;
|
||||
|
||||
fn signing_key() -> HmacSha256 {
|
||||
fn jwt_secret() -> String {
|
||||
dotenv().ok();
|
||||
let secret = env::var("JWT_SECRET").expect("JWT_SECRET must be set");
|
||||
// HMAC-SHA256 accepts a key of any length, so this cannot fail.
|
||||
HmacSha256::new_from_slice(secret.as_bytes()).expect("HMAC accepts a key of any length")
|
||||
env::var("JWT_SECRET").expect("JWT_SECRET must be set")
|
||||
}
|
||||
|
||||
fn encoding_key() -> EncodingKey {
|
||||
EncodingKey::from_secret(jwt_secret().as_bytes())
|
||||
}
|
||||
|
||||
fn validation() -> Validation {
|
||||
let mut validation = Validation::new(Algorithm::HS256);
|
||||
// Reject a token the moment `exp` passes, matching the previous hand-rolled check.
|
||||
validation.leeway = 0;
|
||||
validation
|
||||
}
|
||||
|
||||
impl JwtToken {
|
||||
pub fn encode(user_id: i32, token_version: i32) -> String {
|
||||
let key: HmacSha256 = signing_key();
|
||||
let claims = Claims {
|
||||
user_id,
|
||||
tv: token_version,
|
||||
exp: (Utc::now() + Duration::hours(TOKEN_LIFETIME_HOURS)).timestamp(),
|
||||
};
|
||||
// Signing claims with a valid HMAC key cannot fail.
|
||||
claims
|
||||
.sign_with_key(&key)
|
||||
encode(&Header::new(Algorithm::HS256), &claims, &encoding_key())
|
||||
.expect("signing claims with a valid HMAC key cannot fail")
|
||||
}
|
||||
|
||||
pub fn decode(encoded_token: String) -> Result<JwtToken, &'static str> {
|
||||
let key: HmacSha256 = signing_key();
|
||||
let token_str: &str = encoded_token.as_str();
|
||||
let token: Result<Token<Header, Claims, jwt::Verified>, jwt::Error> =
|
||||
VerifyWithKey::verify_with_key(token_str, &key);
|
||||
|
||||
match token {
|
||||
Ok(token) => {
|
||||
let claims = token.claims();
|
||||
if claims.exp < Utc::now().timestamp() {
|
||||
return Err("token has expired");
|
||||
}
|
||||
Ok(JwtToken {
|
||||
user_id: claims.user_id,
|
||||
token_version: claims.tv,
|
||||
})
|
||||
let key = DecodingKey::from_secret(jwt_secret().as_bytes());
|
||||
match decode::<Claims>(&encoded_token, &key, &validation()) {
|
||||
Ok(token) => Ok(JwtToken {
|
||||
user_id: token.claims.user_id,
|
||||
token_version: token.claims.tv,
|
||||
}),
|
||||
Err(err) if matches!(err.kind(), ErrorKind::ExpiredSignature) => {
|
||||
Err("token has expired")
|
||||
}
|
||||
Err(_err) => Err("could not decode token"),
|
||||
}
|
||||
@@ -90,11 +83,9 @@ impl JwtToken {
|
||||
mod jwt_test {
|
||||
use actix_web::{http::header, test};
|
||||
use chrono::{Duration, Utc};
|
||||
use hmac::Hmac;
|
||||
use jwt::SignWithKey;
|
||||
use sha2::Sha256;
|
||||
use jsonwebtoken::{encode, Algorithm, EncodingKey, Header};
|
||||
|
||||
use super::{Claims, JwtToken};
|
||||
use super::{encoding_key, Claims, JwtToken};
|
||||
|
||||
#[test]
|
||||
async fn encode_decode() {
|
||||
@@ -116,13 +107,13 @@ mod jwt_test {
|
||||
|
||||
#[test]
|
||||
async fn decode_expired_token() {
|
||||
let key: Hmac<Sha256> = super::signing_key();
|
||||
let claims = Claims {
|
||||
user_id: 32,
|
||||
tv: 0,
|
||||
exp: (Utc::now() - Duration::hours(1)).timestamp(),
|
||||
};
|
||||
let expired_token: String = claims.sign_with_key(&key).unwrap();
|
||||
let expired_token: String =
|
||||
encode(&Header::new(Algorithm::HS256), &claims, &encoding_key()).unwrap();
|
||||
|
||||
match JwtToken::decode(expired_token) {
|
||||
Err(message) => assert_eq!(message, "token has expired"),
|
||||
@@ -130,6 +121,26 @@ mod jwt_test {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
async fn decode_rejects_token_signed_with_another_secret() {
|
||||
let claims = Claims {
|
||||
user_id: 32,
|
||||
tv: 0,
|
||||
exp: (Utc::now() + Duration::hours(1)).timestamp(),
|
||||
};
|
||||
let forged_token: String = encode(
|
||||
&Header::new(Algorithm::HS256),
|
||||
&claims,
|
||||
&EncodingKey::from_secret(b"not-the-real-secret"),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
match JwtToken::decode(forged_token) {
|
||||
Err(message) => assert_eq!(message, "could not decode token"),
|
||||
_ => panic!("A token signed with a different secret must be rejected."),
|
||||
}
|
||||
}
|
||||
|
||||
#[actix_web::test]
|
||||
async fn decode_from_request_with_correct_token() {
|
||||
let encoded_token: String = JwtToken::encode(32, 0);
|
||||
|
||||
Reference in new issue
Block a user