rust/hedgewars-server/src/server/handlers/loggingin.rs
changeset 14700 216d39de1a44
parent 14698 6a2e13e36b7f
child 14701 8a45c90f4580
equal deleted inserted replaced
14699:25c564f77b7d 14700:216d39de1a44
    27         Ok(())
    27         Ok(())
    28     }
    28     }
    29 }
    29 }
    30 
    30 
    31 #[cfg(feature = "official-server")]
    31 #[cfg(feature = "official-server")]
    32 fn get_hash(client: &HWAnteClient, salt1: &str, salt2: &str) -> Sha1Digest {
    32 fn get_hash(protocol_number: u16, web_password: &str, salt1: &str, salt2: &str) -> Sha1Digest {
    33     let s = format!(
    33     let s = format!(
    34         "{}{}{}{}{}",
    34         "{}{}{}{}{}",
    35         salt1, salt2, client.web_password, client.protocol_number, "!hedgewars"
    35         salt1, salt2, web_password, protocol_number, "!hedgewars"
    36     );
    36     );
    37     Sha1Digest(sha1(s.as_bytes()))
    37     Sha1Digest(sha1(s.as_bytes()))
    38 }
    38 }
    39 
    39 
    40 pub enum LoginResult {
    40 pub enum LoginResult {
    95         }
    95         }
    96         #[cfg(feature = "official-server")]
    96         #[cfg(feature = "official-server")]
    97         HWProtocolMessage::Password(hash, salt) => {
    97         HWProtocolMessage::Password(hash, salt) => {
    98             let client = &anteroom.clients[client_id];
    98             let client = &anteroom.clients[client_id];
    99 
    99 
   100             let client_hash = get_hash(client, &salt, &client.server_salt);
   100             if let (Some(protocol), Some(password)) = (client.protocol_number, client.web_password.as_ref()) {
   101             let server_hash = get_hash(client, &client.server_salt, &salt);
   101                 let client_hash = get_hash(protocol.get(), &password, &salt, &client.server_salt);
   102             if client_hash == server_hash {
   102                 let server_hash = get_hash(protocol.get(), &password, &client.server_salt, &salt);
   103                 response.add(ServerAuth(format!("{:x}", server_hash)).send_self());
   103                 if client_hash == server_hash {
   104                 LoginResult::Complete
   104                     response.add(ServerAuth(format!("{:x}", server_hash)).send_self());
       
   105                     LoginResult::Complete
       
   106                 } else {
       
   107                     response.add(Bye("No protocol provided.".to_string()).send_self());
       
   108                     LoginResult::Unchanged
       
   109                 }
   105             } else {
   110             } else {
   106                 response.add(Bye("Authentication failed".to_string()).send_self());
   111                 response.add(Bye("Authentication failed.".to_string()).send_self());
   107                 LoginResult::Exit
   112                 LoginResult::Exit
   108             }
   113             }
   109         }
   114         }
   110         #[cfg(feature = "official-server")]
   115         #[cfg(feature = "official-server")]
   111         HWProtocolMessage::Checker(protocol, nick, password) => {
   116         HWProtocolMessage::Checker(protocol, nick, password) => {
   112             let client = &mut anteroom.clients[client_id];
   117             let client = &mut anteroom.clients[client_id];
   113             client.protocol_number = Some(protocol);
   118             client.protocol_number = NonZeroU16::new(protocol);
   114             client.nick = Some(nick);
   119             client.nick = Some(nick);
   115             client.web_password = password;
   120             client.web_password = Some(password);
   116             //client.set_is_checker(true);
   121             //client.set_is_checker(true);
   117             LoginResult::Complete
   122             LoginResult::Complete
   118         }
   123         }
   119         _ => {
   124         _ => {
   120             warn!("Incorrect command in logging-in state");
   125             warn!("Incorrect command in logging-in state");