Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/#31 jwt #36

Merged
merged 7 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions migrations/0_create-table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,6 @@ CREATE TABLE IF NOT EXISTS `users_passwords` (
`hashed_pass` VARCHAR(255) NOT NULL
);

CREATE TABLE IF NOT EXISTS `mail_verifications` (
`request_id` INT AUTO_INCREMENT PRIMARY KEY,
`email` VARCHAR(255) NOT NULL,
`token` VARCHAR(255) NOT NULL,
`created_at` DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS `put_mail_varifications` (
`request_id` INT AUTO_INCREMENT PRIMARY KEY,
`user_id` INT NOT NULL,
`email` VARCHAR(255) NOT NULL,
`token` VARCHAR(255) NOT NULL,
`created_at` DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS `reset_passwords` (
`request_id` INT AUTO_INCREMENT PRIMARY KEY,
`email` VARCHAR(255) NOT NULL,
`token` VARCHAR(255) NOT NULL,
`created_at` DATETIME DEFAULT CURRENT_TIMESTAMP
);


CREATE TABLE IF NOT EXISTS `normal_problems` (
`problem_id` INT AUTO_INCREMENT PRIMARY KEY,
`auther_id` INT NOT NULL,
Expand Down
2 changes: 1 addition & 1 deletion src/handler/authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub async fn sign_up_request(
.map_err(|_| StatusCode::BAD_REQUEST)?;

let jwt = state
.save_email_varifications(&body.email)
.encode_email_signup_jwt(&body.email)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;

Expand Down
3 changes: 1 addition & 2 deletions src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use sqlx::{
MySqlPool,
};

mod signup_jwt;
mod jwt;
pub mod users;
mod users_jwt;
mod users_session;

#[derive(Clone)]
Expand Down
77 changes: 77 additions & 0 deletions src/repository/jwt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use anyhow::Ok;
use async_session::chrono::{Duration, Utc};
use serde::{Deserialize, Serialize};

use super::Repository;

#[allow(non_camel_case_types)]
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
enum Action {
reset_password,
change_email,
register_email,
}

#[derive(Serialize, Deserialize)]
struct EmailToken {
exp: i64,
iat: i64,
nbf: i64,
user_id: Option<i64>,
email: String,
action: Action,
}

impl EmailToken {
fn to_jwt(&self) -> anyhow::Result<String> {
let encode_key: String = std::env::var("JWT_SECRET")?;

let jwt = jsonwebtoken::encode(
&jsonwebtoken::Header::default(),
&self,
&jsonwebtoken::EncodingKey::from_secret(encode_key.as_ref()),
)?;

Ok(jwt)
}
}

impl Repository {
pub async fn encode_email_update_jwt(
&self,
user_id: i64,
email: &str,
) -> anyhow::Result<String> {
let exp = (Utc::now() + Duration::minutes(60)).timestamp();
let iat = Utc::now().timestamp();
let nbf = Utc::now().timestamp();

let claims = EmailToken {
exp,
iat,
nbf,
user_id: Some(user_id),
email: email.to_owned(),
action: Action::change_email,
};

claims.to_jwt()
}

pub async fn encode_email_signup_jwt(&self, email: &str) -> anyhow::Result<String> {
let exp = (Utc::now() + Duration::minutes(60)).timestamp();
let iat = Utc::now().timestamp();
let nbf = Utc::now().timestamp();

let claims = EmailToken {
exp,
iat,
nbf,
user_id: None,
email: email.to_owned(),
action: Action::register_email,
};

claims.to_jwt()
}
}
42 changes: 0 additions & 42 deletions src/repository/signup_jwt.rs

This file was deleted.

36 changes: 0 additions & 36 deletions src/repository/users_jwt.rs

This file was deleted.