@@ -305,6 +305,7 @@ fn get_mac_deep_link_requests() -> Vec<String> {
305305}
306306
307307const PHOENIX_CRED_PREFIX : & str = "phcode_" ;
308+ const MIN_SEED_LENGTH : usize = 10 ; // Minimum required length for a valid TOTP seed
308309
309310fn get_username ( ) -> String {
310311 // Ensure a fallback username in case retrieval fails
@@ -324,6 +325,14 @@ fn store_credential(scope_name: String, session_id: String, otp_seed: String) ->
324325 let service = format ! ( "{}{}" , PHOENIX_CRED_PREFIX , scope_name) ; // Unique service name per scope
325326 let user = get_username ( ) ;
326327
328+ // Check if the seed is too short
329+ if otp_seed. len ( ) < MIN_SEED_LENGTH {
330+ return Err ( format ! (
331+ "SEED_TOO_SHORT: Seed length must be at least {} characters, but got {}." ,
332+ MIN_SEED_LENGTH , otp_seed. len( )
333+ ) ) ;
334+ }
335+
327336 // Combine sessionID and OTP seed into one stored value
328337 let credential_data = format ! ( "{}|{}" , session_id, otp_seed) ;
329338
@@ -370,8 +379,15 @@ fn get_credential_otp(scope_name: String) -> serde_json::Value {
370379 let session_id = parts[ 0 ] . to_string ( ) ;
371380 let otp_seed = parts[ 1 ] ;
372381
382+ if otp_seed. len ( ) < MIN_SEED_LENGTH {
383+ return json ! ( {
384+ "err_code" : "SEED_TOO_SHORT" ,
385+ "message" : format!( "Seed length must be at least {} characters, but got {}." , MIN_SEED_LENGTH , otp_seed. len( ) )
386+ } ) ;
387+ }
388+
373389 // Convert the OTP seed to Base32 (Required for TOTP)
374- let otp_seed_base32 = base32_encode ( Alphabet :: Rfc4648 { padding : false } , otp_seed. as_bytes ( ) ) ;
390+ let otp_seed_base32 = base32_encode ( Alphabet :: Rfc4648 { padding : true } , otp_seed. as_bytes ( ) ) ;
375391
376392 // Convert the Base32-encoded OTP seed into a Secret
377393 let secret = match Secret :: Encoded ( otp_seed_base32) . to_bytes ( ) {
0 commit comments