diff options
author | niliara-edu <nil.jimeno@estudiant.fjaverianas.com> | 2025-01-23 23:28:01 +0100 |
---|---|---|
committer | niliara-edu <nil.jimeno@estudiant.fjaverianas.com> | 2025-01-23 23:28:01 +0100 |
commit | 8c4de2ddac066a072f376e9f30409b114aa9978c (patch) | |
tree | c1dc334904206f1b796d753293cb0b5c838f35d8 /src/auth.rs | |
parent | 6a7a49fb3804d0d27bbaee08b6feb26b4973b4bc (diff) |
finished jws tutorial
Diffstat (limited to 'src/auth.rs')
-rw-r--r-- | src/auth.rs | 68 |
1 files changed, 51 insertions, 17 deletions
diff --git a/src/auth.rs b/src/auth.rs index 8bf29f7..e0c8ae9 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1,9 +1,13 @@ -use actix_web::{web, Scope, HttpResponse}; -use serde::{Serialize, Deserialize}; -use chrono::{Utc, Duration}; -use jsonwebtoken::{encode, EncodingKey, Header}; - +use actix_web::{web, HttpResponse, Scope}; +use chrono::{Duration, Utc}; +use jsonwebtoken::{ + decode, encode, errors::Error as JwtError, Algorithm, DecodingKey, EncodingKey, Header, + TokenData, Validation, +}; +use serde::{Deserialize, Serialize}; use crate::AppState; +use crate::extractors::auth_token::AuthenticationToken; + pub fn auth_scope() -> Scope { web::scope("/auth") @@ -13,41 +17,71 @@ pub fn auth_scope() -> Scope { } #[derive(Serialize, Deserialize)] -struct Claims{ - id: usize, - exp: usize, +pub struct Claims { + pub id: usize, + pub exp: usize, +} + +#[derive(Serialize, Deserialize)] +struct Response { + message: String, } #[derive(Serialize, Deserialize)] -struct Response{ +struct EncodeResponse { message: String, + token: String, } #[derive(Serialize, Deserialize)] -struct EncodeResponse{ +struct DecodeResponse { message: String, + id: usize, +} + +#[derive(Serialize, Deserialize)] +struct DecodeBody { token: String, } async fn encode_token(path: web::Path<usize>, data: web::Data<AppState>) -> HttpResponse { let id: usize = path.into_inner(); let exp: usize = (Utc::now() + Duration::days(365)).timestamp() as usize; - let claims: Claims = Claims {id, exp}; - let token: String = encode( + let claims: Claims = Claims { id, exp }; + let token: String = match encode( &Header::default(), &claims, &EncodingKey::from_secret(data.secret.as_str().as_ref()), - ).unwrap(); + ) { + Ok(res) => res, + Err(_) => return HttpResponse::Ok().body("Token encoding didn't work\n"), + }; + HttpResponse::Ok().json(EncodeResponse { message: "success".to_owned(), token: token.to_owned(), }) } -async fn decode_token() -> HttpResponse { - HttpResponse::Ok().body("decode_token\n".to_owned()) +async fn decode_token(body: web::Json<DecodeBody>, data: web::Data<AppState>) -> HttpResponse { + let decoded: Result<TokenData<Claims>, JwtError> = decode::<Claims>( + &body.token, + &DecodingKey::from_secret(data.secret.as_str().as_ref()), + &Validation::new(Algorithm::HS256), + ); + + match decoded { + Ok(token) => HttpResponse::Ok().json(DecodeResponse { + message: "Authorized".to_string(), + id: token.claims.id, + }), + Err(e) => HttpResponse::BadRequest().json(Response { + message: e.to_string(), + }), + } } -async fn protected() -> HttpResponse { - HttpResponse::Ok().body("protected\n".to_owned()) +async fn protected(auth_token: AuthenticationToken) -> HttpResponse { + println!("{:#?}", auth_token); + HttpResponse::Ok().json(Response { message: "Authorized".to_owned() }) } |