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"
|
dotenvy = "0.15"
|
||||||
bcrypt = "0.19"
|
bcrypt = "0.19"
|
||||||
uuid = {version = "1.23", features=["serde", "v4"]}
|
uuid = {version = "1.23", features=["serde", "v4"]}
|
||||||
jwt = "0.16.0"
|
jsonwebtoken = { version = "11", default-features = false, features = ["aws_lc_rs"] }
|
||||||
hmac = "0.12"
|
|
||||||
sha2 = "0.10"
|
|
||||||
log = "0.4.32"
|
log = "0.4.32"
|
||||||
env_logger = "0.11"
|
env_logger = "0.11"
|
||||||
scraper = "0.27"
|
scraper = "0.29"
|
||||||
actix-cors = "0.7"
|
actix-cors = "0.7"
|
||||||
chrono = { version = "0.4.45", features = ["serde"] }
|
chrono = { version = "0.4.45", features = ["serde"] }
|
||||||
dateparser = "0.3"
|
dateparser = "0.3"
|
||||||
|
|||||||
+49
-38
@@ -1,16 +1,12 @@
|
|||||||
extern crate hmac;
|
|
||||||
extern crate jwt;
|
|
||||||
extern crate sha2;
|
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
use actix_web::HttpRequest;
|
use actix_web::HttpRequest;
|
||||||
use chrono::{Duration, Utc};
|
use chrono::{Duration, Utc};
|
||||||
use dotenvy::dotenv;
|
use dotenvy::dotenv;
|
||||||
use hmac::{Hmac, Mac};
|
use jsonwebtoken::{
|
||||||
use jwt::{Header, SignWithKey, Token, VerifyWithKey};
|
decode, encode, errors::ErrorKind, Algorithm, DecodingKey, EncodingKey, Header, Validation,
|
||||||
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sha2::Sha256;
|
|
||||||
|
|
||||||
/// How long a freshly issued token remains valid for.
|
/// How long a freshly issued token remains valid for.
|
||||||
const TOKEN_LIFETIME_HOURS: i64 = 730;
|
const TOKEN_LIFETIME_HOURS: i64 = 730;
|
||||||
@@ -30,45 +26,42 @@ struct Claims {
|
|||||||
exp: i64,
|
exp: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
type HmacSha256 = Hmac<Sha256>;
|
fn jwt_secret() -> String {
|
||||||
|
|
||||||
fn signing_key() -> HmacSha256 {
|
|
||||||
dotenv().ok();
|
dotenv().ok();
|
||||||
let secret = env::var("JWT_SECRET").expect("JWT_SECRET must be set");
|
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")
|
|
||||||
|
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 {
|
impl JwtToken {
|
||||||
pub fn encode(user_id: i32, token_version: i32) -> String {
|
pub fn encode(user_id: i32, token_version: i32) -> String {
|
||||||
let key: HmacSha256 = signing_key();
|
|
||||||
let claims = Claims {
|
let claims = Claims {
|
||||||
user_id,
|
user_id,
|
||||||
tv: token_version,
|
tv: token_version,
|
||||||
exp: (Utc::now() + Duration::hours(TOKEN_LIFETIME_HOURS)).timestamp(),
|
exp: (Utc::now() + Duration::hours(TOKEN_LIFETIME_HOURS)).timestamp(),
|
||||||
};
|
};
|
||||||
// Signing claims with a valid HMAC key cannot fail.
|
encode(&Header::new(Algorithm::HS256), &claims, &encoding_key())
|
||||||
claims
|
|
||||||
.sign_with_key(&key)
|
|
||||||
.expect("signing claims with a valid HMAC key cannot fail")
|
.expect("signing claims with a valid HMAC key cannot fail")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn decode(encoded_token: String) -> Result<JwtToken, &'static str> {
|
pub fn decode(encoded_token: String) -> Result<JwtToken, &'static str> {
|
||||||
let key: HmacSha256 = signing_key();
|
let key = DecodingKey::from_secret(jwt_secret().as_bytes());
|
||||||
let token_str: &str = encoded_token.as_str();
|
match decode::<Claims>(&encoded_token, &key, &validation()) {
|
||||||
let token: Result<Token<Header, Claims, jwt::Verified>, jwt::Error> =
|
Ok(token) => Ok(JwtToken {
|
||||||
VerifyWithKey::verify_with_key(token_str, &key);
|
user_id: token.claims.user_id,
|
||||||
|
token_version: token.claims.tv,
|
||||||
match token {
|
}),
|
||||||
Ok(token) => {
|
Err(err) if matches!(err.kind(), ErrorKind::ExpiredSignature) => {
|
||||||
let claims = token.claims();
|
Err("token has expired")
|
||||||
if claims.exp < Utc::now().timestamp() {
|
|
||||||
return Err("token has expired");
|
|
||||||
}
|
|
||||||
Ok(JwtToken {
|
|
||||||
user_id: claims.user_id,
|
|
||||||
token_version: claims.tv,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
Err(_err) => Err("could not decode token"),
|
Err(_err) => Err("could not decode token"),
|
||||||
}
|
}
|
||||||
@@ -90,11 +83,9 @@ impl JwtToken {
|
|||||||
mod jwt_test {
|
mod jwt_test {
|
||||||
use actix_web::{http::header, test};
|
use actix_web::{http::header, test};
|
||||||
use chrono::{Duration, Utc};
|
use chrono::{Duration, Utc};
|
||||||
use hmac::Hmac;
|
use jsonwebtoken::{encode, Algorithm, EncodingKey, Header};
|
||||||
use jwt::SignWithKey;
|
|
||||||
use sha2::Sha256;
|
|
||||||
|
|
||||||
use super::{Claims, JwtToken};
|
use super::{encoding_key, Claims, JwtToken};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
async fn encode_decode() {
|
async fn encode_decode() {
|
||||||
@@ -116,13 +107,13 @@ mod jwt_test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
async fn decode_expired_token() {
|
async fn decode_expired_token() {
|
||||||
let key: Hmac<Sha256> = super::signing_key();
|
|
||||||
let claims = Claims {
|
let claims = Claims {
|
||||||
user_id: 32,
|
user_id: 32,
|
||||||
tv: 0,
|
tv: 0,
|
||||||
exp: (Utc::now() - Duration::hours(1)).timestamp(),
|
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) {
|
match JwtToken::decode(expired_token) {
|
||||||
Err(message) => assert_eq!(message, "token has expired"),
|
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]
|
#[actix_web::test]
|
||||||
async fn decode_from_request_with_correct_token() {
|
async fn decode_from_request_with_correct_token() {
|
||||||
let encoded_token: String = JwtToken::encode(32, 0);
|
let encoded_token: String = JwtToken::encode(32, 0);
|
||||||
|
|||||||
Reference in new issue
Block a user