token_handler/
decode_token.rs1use serde::{Deserialize, Serialize};
2use jsonwebtoken::{decode, DecodingKey, Validation, Algorithm, TokenData};
3
4#[derive(Debug, Serialize, Deserialize)]
5pub struct DecodedTokenResponse {
6 pub application: String,
7 pub address: String,
8 pub iat: usize,
9 pub exp: usize,
10 pub iss: String,
11 }
13
14pub fn decode_token(token: &str, secret_key: &str) -> Result<DecodedTokenResponse, String> {
15 let key = DecodingKey::from_secret(secret_key.as_bytes());
16 match decode::<DecodedTokenResponse>(token, &key, &Validation::new(Algorithm::HS256)) {
17 Ok(TokenData { claims, .. }) => {
18 println!("claims.exp: {:?}", claims.exp);
19 let application = claims.application;
20 let address = claims.address;
21 let iat = claims.iat;
22 let exp = claims.exp;
23 let iss = claims.iss;
24 let response = DecodedTokenResponse { application, address, iat, exp, iss };
25 println!("Decoded JWT: {:?}", response);
26 Ok(response)
27 }
28 Err(err) => {
29 eprintln!("Failed to decode JWT: {:?}", err);
30 Err(format!("JWT decode error: {}", err))
31 }
32 }
33}