11use crate :: { debug_log, get_config_content} ;
2- use hmac:: { Hmac , Mac } ;
3- use jwt:: { Header , SignWithKey , Token , VerifyWithKey } ;
4- use sha2:: Sha256 ;
2+ use jsonwebtoken:: { decode, encode, DecodingKey , EncodingKey , Header , Validation } ;
3+ use serde:: { Deserialize , Serialize } ;
54use std:: collections:: BTreeMap ;
65use toml:: value:: Table ;
6+
7+ #[ derive( Debug , Serialize , Deserialize ) ]
8+ struct Claims {
9+ email : String ,
10+ }
11+
712fn verify_with_key_str (
813 token_str : & str ,
914 key_str : & str ,
10- ) -> Result < BTreeMap < String , String > , jwt :: Error > {
11- let key: Hmac < Sha256 > = Hmac :: new_from_slice ( key_str. as_bytes ( ) ) ? ;
12- let token : Token < Header , BTreeMap < String , String > , _ > = token_str . verify_with_key ( & key ) ? ;
13- let claims = token . claims ( ) ;
14- if claims . get ( "email" ) . is_none ( ) {
15- debug_log ! ( "email not found" ) ;
16- return Err ( jwt :: Error :: InvalidSignature ) ;
17- }
18- Ok ( claims. clone ( ) )
15+ ) -> Result < BTreeMap < String , String > , jsonwebtoken :: errors :: Error > {
16+ let key = DecodingKey :: from_secret ( key_str. as_bytes ( ) ) ;
17+ let mut validation = Validation :: default ( ) ;
18+ validation . required_spec_claims . clear ( ) ;
19+ validation . validate_exp = false ;
20+ let token_data = decode :: < Claims > ( token_str , & key , & validation ) ? ;
21+ let mut claims = BTreeMap :: new ( ) ;
22+ claims . insert ( "email" . to_string ( ) , token_data . claims . email ) ;
23+ Ok ( claims)
1924}
2025
21- pub fn verify_jwt_token ( token_str : & str ) -> Result < BTreeMap < String , String > , jwt:: Error > {
26+ pub fn verify_jwt_token (
27+ token_str : & str ,
28+ ) -> Result < BTreeMap < String , String > , jsonwebtoken:: errors:: Error > {
2229 let toml_cfg = get_config_content ( ) ;
2330 let parsed_toml = toml_cfg. parse :: < Table > ( ) . unwrap ( ) ;
2431
@@ -49,7 +56,7 @@ pub fn verify_jwt_token(token_str: &str) -> Result<BTreeMap<String, String>, jwt
4956 }
5057 }
5158
52- Err ( jwt :: Error :: InvalidSignature )
59+ Err ( jsonwebtoken :: errors :: ErrorKind :: InvalidSignature . into ( ) )
5360}
5461
5562pub fn generate_jwt_secret ( ) {
@@ -64,7 +71,7 @@ pub fn generate_jwt_secret() {
6471 debug_log ! ( "jwt_secret=\" {}\" " , secret) ;
6572}
6673
67- pub fn generate_jwt_token ( email : & str ) -> Result < String , jwt :: Error > {
74+ pub fn generate_jwt_token ( email : & str ) -> Result < String , jsonwebtoken :: errors :: Error > {
6875 let toml_cfg = get_config_content ( ) ;
6976 let parsed_toml = toml_cfg. parse :: < Table > ( ) . unwrap ( ) ;
7077 // For token generation, prefer jwt_secret, fall back to unified_secret
@@ -73,9 +80,9 @@ pub fn generate_jwt_token(email: &str) -> Result<String, jwt::Error> {
7380 . or_else ( || parsed_toml. get ( "unified_secret" ) )
7481 . and_then ( |v| v. as_str ( ) )
7582 . expect ( "config must define jwt_secret or unified_secret" ) ;
76- let key: Hmac < Sha256 > = Hmac :: new_from_slice ( key_str. as_bytes ( ) ) ? ;
77- let mut claims = BTreeMap :: new ( ) ;
78- claims . insert ( " email" . to_string ( ) , email. to_string ( ) ) ;
79- let token_str = claims . sign_with_key ( & key ) ? ;
80- Ok ( token_str )
83+ let key = EncodingKey :: from_secret ( key_str. as_bytes ( ) ) ;
84+ let claims = Claims {
85+ email : email. to_string ( ) ,
86+ } ;
87+ encode ( & Header :: default ( ) , & claims , & key )
8188}
0 commit comments