Skip to content

Commit a4b2c8b

Browse files
committed
Update dependencies + refactor code
1 parent 34fa6d6 commit a4b2c8b

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

Cargo.toml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,20 @@ edition = "2018"
77
[dependencies]
88
actix-web = "2.0.0"
99
actix-rt = "1.0.0"
10-
actix-service = "1.0.1"
10+
actix-service = "1.0.5"
1111
actix-cors = "0.2.0"
1212
log = "0.4.8"
1313
env_logger = "0.7.1"
1414
diesel_migrations = "1.4.0"
1515
serde = "1.0.104"
1616
serde_derive = "1.0.104"
17-
serde_json = "1.0.44"
17+
serde_json = "1.0.48"
1818
dotenv = "0.15.0"
19-
futures = "0.3.1"
19+
futures = "0.3.4"
2020
failure = "0.1.6"
21-
derive_more = "0.99.2"
22-
jsonwebtoken = "6.0.1"
21+
derive_more = "0.99.3"
22+
jsonwebtoken = "7.1.0"
2323
bcrypt = "0.6.1"
24-
time = "0.2.1"
2524

2625
[dependencies.diesel]
2726
version = "1.4.3"

src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ extern crate derive_more;
2323
extern crate jsonwebtoken;
2424
extern crate uuid;
2525
extern crate bcrypt;
26-
extern crate time;
2726

2827
mod api;
2928
mod config;

src/models/user_token.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use crate::models::user::LoginInfoDTO;
2-
use jsonwebtoken::Header;
3-
use time::PrimitiveDateTime;
2+
use chrono::Utc;
3+
use jsonwebtoken::{
4+
EncodingKey,
5+
Header
6+
};
47

58
pub static KEY: [u8; 16] = *include_bytes!("../secret.key");
6-
static ONE_WEEK: i64 = 60 * 60 * 24 * 7;
9+
static ONE_WEEK: i64 = 60 * 60 * 24 * 7; // in seconds
710

811
#[derive(Serialize, Deserialize)]
912
pub struct UserToken {
@@ -18,14 +21,14 @@ pub struct UserToken {
1821

1922
impl UserToken {
2023
pub fn generate_token(login: LoginInfoDTO) -> String {
21-
let now = PrimitiveDateTime::now().timestamp();
24+
let now = Utc::now().timestamp_nanos() / 1_000_000_000; // nanosecond -> second
2225
let payload = UserToken {
2326
iat: now,
2427
exp: now + ONE_WEEK,
2528
user: login.username,
2629
login_session: login.login_session,
2730
};
2831

29-
jsonwebtoken::encode(&Header::default(), &payload, &KEY).unwrap()
32+
jsonwebtoken::encode(&Header::default(), &payload, &EncodingKey::from_secret(&KEY)).unwrap()
3033
}
3134
}

src/utils/token_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use crate::{
66
},
77
};
88
use actix_web::web;
9-
use jsonwebtoken::{TokenData, Validation};
9+
use jsonwebtoken::{DecodingKey, TokenData, Validation};
1010

1111
pub fn decode_token(token: String) -> jsonwebtoken::errors::Result<TokenData<UserToken>> {
12-
jsonwebtoken::decode::<UserToken>(&token, &KEY, &Validation::default())
12+
jsonwebtoken::decode::<UserToken>(&token, &DecodingKey::from_secret(&KEY), &Validation::default())
1313
}
1414

1515
pub fn verify_token(token_data: &TokenData<UserToken>, pool: &web::Data<Pool>) -> Result<String, String> {

0 commit comments

Comments
 (0)