Skip to content

Commit 9055552

Browse files
authored
Backport token verification from upstream (#25)
* Update user.rs Backport upstream backend PR torrust#58 * Update mailer.rs Backport upstream backend PR torrust/torrust-index#58
1 parent dee6dbc commit 9055552

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

backend/src/handlers/user.rs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ pub fn init_routes(cfg: &mut web::ServiceConfig) {
2626
.route(web::post().to(login)))
2727
.service(web::resource("/ban/{user}")
2828
.route(web::delete().to(ban_user)))
29-
.service(web::resource("/verify/{token}")
30-
.route(web::get().to(verify_user)))
29+
.service(web::resource("/token/verify")
30+
.route(web::post().to(verify_token)))
31+
.service(web::resource("/token/renew")
32+
.route(web::post().to(renew_token)))
33+
.service(web::resource("/email/verify/{token}")
34+
.route(web::get().to(verify_email)))
3135
);
3236
}
3337

@@ -45,6 +49,11 @@ pub struct Login {
4549
pub password: String,
4650
}
4751

52+
#[derive(Clone, Debug, Deserialize, Serialize)]
53+
pub struct Token {
54+
pub token: String,
55+
}
56+
4857
pub async fn register(req: HttpRequest, mut payload: web::Json<Register>, app_data: WebAppData) -> ServiceResult<impl Responder> {
4958
let settings = app_data.cfg.settings.read().await;
5059

@@ -178,8 +187,39 @@ pub async fn login(payload: web::Json<Login>, app_data: WebAppData) -> ServiceRe
178187
None => Err(ServiceError::WrongPasswordOrUsername)
179188
}
180189
}
190+
pub async fn verify_token(payload: web::Json<Token>, app_data: WebAppData) -> ServiceResult<impl Responder> {
191+
// verify if token is valid
192+
let _claims = app_data.auth.verify_jwt(&payload.token).await?;
193+
194+
Ok(HttpResponse::Ok().json(OkResponse {
195+
data: format!("Token is valid.")
196+
}))
197+
}
198+
199+
pub async fn renew_token(payload: web::Json<Token>, app_data: WebAppData) -> ServiceResult<impl Responder> {
200+
// verify if token is valid
201+
let claims = app_data.auth.verify_jwt(&payload.token).await?;
202+
203+
let user_compact = app_data.database.get_user_compact_from_id(claims.user.user_id).await?;
204+
205+
const ONE_WEEK_IN_SECONDS: u64 = 604_800;
206+
207+
// renew token if it is valid for less than one week
208+
let token = match claims.exp - current_time() {
209+
x if x < ONE_WEEK_IN_SECONDS => app_data.auth.sign_jwt(user_compact.clone()).await,
210+
_ => payload.token.clone()
211+
};
212+
213+
Ok(HttpResponse::Ok().json(OkResponse {
214+
data: TokenResponse {
215+
token,
216+
username: user_compact.username,
217+
admin: user_compact.administrator
218+
}
219+
}))
220+
}
181221

182-
pub async fn verify_user(req: HttpRequest, app_data: WebAppData) -> String {
222+
pub async fn verify_email(req: HttpRequest, app_data: WebAppData) -> String {
183223
let settings = app_data.cfg.settings.read().await;
184224
let token = req.match_info().get("token").unwrap();
185225

backend/src/mailer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ If this account wasn't made by you, you can ignore this email.
137137
base_url = cfg_base_url;
138138
}
139139

140-
format!("{}/user/verify/{}", base_url, token)
140+
format!("{}/user/email/verify/{}", base_url, token)
141141
}
142142
}
143143

0 commit comments

Comments
 (0)