Skip to content

Commit

Permalink
Refactor auth to merge token creating methods into one
Browse files Browse the repository at this point in the history
  • Loading branch information
sabotack committed Nov 14, 2023
1 parent 9adcea0 commit 28d39a3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
65 changes: 35 additions & 30 deletions src/api/auth.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use chrono::Utc;
use chrono::{Utc, Duration};
use jsonwebtoken::{
decode, encode,
errors::{Error, ErrorKind},
Expand All @@ -14,37 +14,38 @@ pub struct Claims {
exp: usize,
}

pub fn create_access_token(uid: &str) -> Result<String, Error> {
let secret = env::var("ACCESS_TOKEN_HS512_SECRET")
.expect("Expected ACCESS_TOKEN_HS512_SECRET to be set.");

let expiration = Utc::now()
.checked_add_signed(chrono::Duration::minutes(20))
.expect("valid timestamp")
.timestamp();

let claims = Claims {
sub: uid.to_owned(),
exp: expiration as usize,
};

let header = Header::new(Algorithm::HS512);
encode(
&header,
&claims,
&EncodingKey::from_secret(secret.as_bytes()),
)
.map_err(|_| ErrorKind::InvalidToken.into())
pub enum TokenType {
AccessToken,
RefreshToken,
}

pub fn create_refresh_token(uid: &str) -> Result<String, Error> {
let secret = env::var("REFRESH_TOKEN_HS512_SECRET")
.expect("Expected REFRESH_TOKEN_HS512_SECRET to be set.");

let expiration = Utc::now()
.checked_add_signed(chrono::Duration::days(90))
.expect("valid timestamp")
.timestamp();
pub fn create_token(token_type: TokenType, uid: &str) -> Result<String, Error> {
const ACCESS_TOKEN_DURATION_MINS: i64 = 20;
const REFRESH_TOKEN_DURATION_DAYS: i64 = 90;

let secret: String;
let expiration: i64;

match token_type {
TokenType::AccessToken => {
secret = env::var("ACCESS_TOKEN_HS512_SECRET")
.expect("Expected ACCESS_TOKEN_HS512_SECRET to be set.");

expiration = Utc::now()
.checked_add_signed(Duration::minutes(ACCESS_TOKEN_DURATION_MINS))
.expect("valid timestamp")
.timestamp();
},
TokenType::RefreshToken => {
secret = env::var("REFRESH_TOKEN_HS512_SECRET")
.expect("Expected REFRESH_TOKEN_HS512_SECRET to be set.");

expiration = Utc::now()
.checked_add_signed(Duration::days(REFRESH_TOKEN_DURATION_DAYS))
.expect("valid timestamp")
.timestamp();
},
};

let claims = Claims {
sub: uid.to_owned(),
Expand Down Expand Up @@ -125,3 +126,7 @@ pub fn validate_token(token: String, is_refresh_token: bool) -> Result<TokenData
},
}
}

#[cfg(test)]
#[path = "../tests/api/auth.rs"]
mod tests;

Check failure on line 132 in src/api/auth.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

couldn't read /home/runner/work/Ecdar-API/Ecdar-API/src/api/../tests/api/auth.rs: No such file or directory (os error 2)
4 changes: 2 additions & 2 deletions src/api/ecdar_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,11 @@ impl EcdarApiAuth for ConcreteEcdarApi {
}

// Create new access and refresh token with user id
let access_token = match auth::create_access_token(&uid) {
let access_token = match auth::create_token(auth::TokenType::AccessToken, &uid) {
Ok(token) => token.to_owned(),
Err(e) => return Err(Status::new(Code::Internal, e.to_string())),
};
let refresh_token = match auth::create_refresh_token(&uid) {
let refresh_token = match auth::create_token(auth::TokenType::RefreshToken, &uid) {
Ok(token) => token.to_owned(),
Err(e) => return Err(Status::new(Code::Internal, e.to_string())),
};
Expand Down

0 comments on commit 28d39a3

Please sign in to comment.