Changes to token (#21)
All checks were successful
Release Tagging / release (push) Successful in 31s
Rust Build / Check (push) Successful in 43s
Rust Build / Clippy (push) Successful in 48s
Rust Build / build (push) Successful in 1m17s
Rust Build / Test Suite (push) Successful in 1m0s
Rust Build / Rustfmt (push) Successful in 27s
Rust Build / Check (pull_request) Successful in 42s
Rust Build / Test Suite (pull_request) Successful in 57s
Rust Build / Rustfmt (pull_request) Successful in 28s
Rust Build / Clippy (pull_request) Successful in 44s
Rust Build / build (pull_request) Successful in 1m11s
All checks were successful
Release Tagging / release (push) Successful in 31s
Rust Build / Check (push) Successful in 43s
Rust Build / Clippy (push) Successful in 48s
Rust Build / build (push) Successful in 1m17s
Rust Build / Test Suite (push) Successful in 1m0s
Rust Build / Rustfmt (push) Successful in 27s
Rust Build / Check (pull_request) Successful in 42s
Rust Build / Test Suite (pull_request) Successful in 57s
Rust Build / Rustfmt (pull_request) Successful in 28s
Rust Build / Clippy (pull_request) Successful in 44s
Rust Build / build (pull_request) Successful in 1m11s
Reviewed-on: #21 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
@@ -42,55 +42,48 @@ pub mod endpoint {
|
||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||
Json(payload): Json<request::Request>,
|
||||
) -> (StatusCode, Json<response::Response>) {
|
||||
let usr = icarus_models::user::User {
|
||||
username: payload.username,
|
||||
password: payload.password,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
// Check if user exists
|
||||
match repo::user::exists(&pool, &usr.username).await {
|
||||
Ok(exists) => {
|
||||
if !exists {
|
||||
return not_found("Not Found").await;
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
return not_found(&err.to_string()).await;
|
||||
}
|
||||
};
|
||||
match repo::user::get(&pool, &payload.username).await {
|
||||
Ok(user) => {
|
||||
let salt = repo::salt::get(&pool, &user.salt_id).await.unwrap();
|
||||
let salt_str = hashing::get_salt(&salt.salt).unwrap();
|
||||
let unhashed_password = payload.password;
|
||||
|
||||
let user = repo::user::get(&pool, &usr.username).await.unwrap();
|
||||
let salt = repo::salt::get(&pool, &user.salt_id).await.unwrap();
|
||||
let salt_str = hashing::get_salt(&salt.salt).unwrap();
|
||||
// Check if password is correct
|
||||
match hashing::hash_password(&unhashed_password, &salt_str) {
|
||||
Ok(hash_password) => {
|
||||
if hashing::verify_password(&unhashed_password, hash_password.clone())
|
||||
.unwrap()
|
||||
{
|
||||
// Create token
|
||||
let key = token_stuff::get_key().unwrap();
|
||||
let (token_literal, duration) =
|
||||
token_stuff::create_token(&key).unwrap();
|
||||
|
||||
// Check if password is correct
|
||||
match hashing::hash_password(&usr.password, &salt_str) {
|
||||
Ok(hash_password) => {
|
||||
if hashing::verify_password(&usr.password, hash_password.clone()).unwrap() {
|
||||
// Create token
|
||||
let key = token_stuff::get_key().unwrap();
|
||||
let (token_literal, duration) = token_stuff::create_token(&key).unwrap();
|
||||
|
||||
if token_stuff::verify_token(&key, &token_literal) {
|
||||
(
|
||||
StatusCode::OK,
|
||||
Json(response::Response {
|
||||
message: String::from("Successful"),
|
||||
data: vec![icarus_models::login_result::LoginResult {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
token: token_literal,
|
||||
token_type: String::from(token_stuff::TOKENTYPE),
|
||||
expiration: duration,
|
||||
}],
|
||||
}),
|
||||
)
|
||||
} else {
|
||||
return not_found("Could not verify password").await;
|
||||
if token_stuff::verify_token(&key, &token_literal) {
|
||||
(
|
||||
StatusCode::OK,
|
||||
Json(response::Response {
|
||||
message: String::from("Successful"),
|
||||
data: vec![icarus_models::login_result::LoginResult {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
token: token_literal,
|
||||
token_type: String::from(token_stuff::TOKENTYPE),
|
||||
expiration: duration,
|
||||
}],
|
||||
}),
|
||||
)
|
||||
} else {
|
||||
return not_found("Could not verify password").await;
|
||||
}
|
||||
} else {
|
||||
return not_found("Error Hashing").await;
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
return not_found(&err.to_string()).await;
|
||||
}
|
||||
} else {
|
||||
return not_found("Error Hashing").await;
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
|
@@ -18,11 +18,22 @@ pub fn get_key() -> Result<String, dotenvy::Error> {
|
||||
Ok(key)
|
||||
}
|
||||
|
||||
pub fn get_expiration() -> time::Result<time::Duration> {
|
||||
let now = time::OffsetDateTime::now_utc();
|
||||
let epoch = time::OffsetDateTime::UNIX_EPOCH;
|
||||
let since_the_epoch = now - epoch;
|
||||
Ok(since_the_epoch)
|
||||
pub fn get_issued() -> time::Result<time::OffsetDateTime> {
|
||||
Ok(time::OffsetDateTime::now_utc())
|
||||
}
|
||||
|
||||
pub fn get_expiration(issued: &time::OffsetDateTime) -> Result<time::OffsetDateTime, time::Error> {
|
||||
let duration_expire = time::Duration::hours(4);
|
||||
Ok(*issued + duration_expire)
|
||||
}
|
||||
|
||||
mod util {
|
||||
pub fn time_to_std_time(
|
||||
provided_time: &time::OffsetDateTime,
|
||||
) -> Result<std::time::SystemTime, std::time::SystemTimeError> {
|
||||
let converted = std::time::SystemTime::from(*provided_time);
|
||||
Ok(converted)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_token(provided_key: &String) -> Result<(String, i64), josekit::JoseError> {
|
||||
@@ -33,13 +44,11 @@ pub fn create_token(provided_key: &String) -> Result<(String, i64), josekit::Jos
|
||||
payload.set_subject(MESSAGE);
|
||||
payload.set_issuer(ISSUER);
|
||||
payload.set_audience(vec![AUDIENCE]);
|
||||
match get_expiration() {
|
||||
Ok(duration) => {
|
||||
let expire = duration.whole_seconds();
|
||||
let _ = payload.set_claim(
|
||||
"expiration",
|
||||
Some(serde_json::to_value(expire.to_string()).unwrap()),
|
||||
);
|
||||
match get_issued() {
|
||||
Ok(issued) => {
|
||||
let expire = get_expiration(&issued).unwrap();
|
||||
payload.set_issued_at(&util::time_to_std_time(&issued).unwrap());
|
||||
payload.set_expires_at(&util::time_to_std_time(&expire).unwrap());
|
||||
|
||||
let key: String = if provided_key.is_empty() {
|
||||
get_key().unwrap()
|
||||
@@ -50,7 +59,7 @@ pub fn create_token(provided_key: &String) -> Result<(String, i64), josekit::Jos
|
||||
let signer = Hs256.signer_from_bytes(key.as_bytes()).unwrap();
|
||||
Ok((
|
||||
josekit::jwt::encode_with_signer(&payload, &header, &signer).unwrap(),
|
||||
duration.whole_seconds(),
|
||||
(expire - time::OffsetDateTime::UNIX_EPOCH).whole_seconds(),
|
||||
))
|
||||
}
|
||||
Err(e) => Err(josekit::JoseError::InvalidClaim(e.into())),
|
||||
|
Reference in New Issue
Block a user