Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

account.UpdatePasswordSettings INPUT_FETCH_ERROR #7

Closed
1pyxa1 opened this issue Jan 18, 2018 · 6 comments
Closed

account.UpdatePasswordSettings INPUT_FETCH_ERROR #7

1pyxa1 opened this issue Jan 18, 2018 · 6 comments

Comments

@1pyxa1
Copy link
Contributor

1pyxa1 commented Jan 18, 2018

Hi there!
Trying to use functions.account.UpdatePasswordSettings with an empty current password and have an error

pyrogram.api.errors.error.UnknownError: [520 Unknown error]: {
    "_": "types.RpcError",
    "error_code": 400,
    "error_message": "INPUT_FETCH_ERROR"
}

here is my function:

def update_password_settings(client, new_password, current_password=None, email=None):                                           
      r = client.send(functions.account.GetPassword())                                                                             
      if current_password:                                                            
          current_password = r.current_salt + current_password.encode() + \           
              r.current_salt                                                          
          current_password_hash = sha256(current_password).digest()                   
      else:                                                                           
        current_password_hash = bytearray()                                         
      new_password = r.new_salt + new_password.encode('utf-8') + r.new_salt           
      new_password_hash = sha256(new_password).digest()                               
      if email:                                                                       
          new_settings = types.account.PasswordInputSettings(                         
              new_salt=r.new_salt,                                                    
              new_password_hash=new_password_hash,                                    
              email=email)                                                            
      else:                                                                           
          new_settings = types.account.PasswordInputSettings(                         
              new_salt=r.new_salt,                                                    
              new_password_hash=new_password_hash)                                    
      client.send(functions.account.UpdatePasswordSettings(
           current_password_hash, new_settings=new_settings))

Would be grateful for any advice!

@delivrance
Copy link
Member

@1pyxa1 What are you trying to do exactly? Remove the password? Change it? ...?

@1pyxa1
Copy link
Contributor Author

1pyxa1 commented Jan 18, 2018

@delivrance Trying to set new password first time.

@1pyxa1
Copy link
Contributor Author

1pyxa1 commented Jan 18, 2018

I found the solution. Tested it for setting new password first time and for changing it. Works great.
Here is my version:

def update_password_settings(client,                                                
                             new_password,                                          
                             current_password=None,                                 
                             email=None,                                            
                             hint='hint'):                                          
    r = client.send(functions.account.GetPassword())                                
    salt_random = os.urandom(8)                                                     
    salt = r.new_salt + salt_random                                                 
    if current_password:                                                            
        current_password = r.current_salt + current_password.encode() + \           
            r.current_salt                                                          
        current_password_hash = sha256(current_password).digest()                   
    else:                                                                           
        current_password_hash = salt                                                
    new_password = salt + new_password.encode('utf-8') + salt                       
    new_password_hash = sha256(new_password).digest()                               
    if email:                                                                       
        new_settings = types.account.PasswordInputSettings(                         
            new_salt=salt,                                                          
            new_password_hash=new_password_hash,                                    
            email=email,                                                            
            hint=hint)                                                              
    else:                                                                           
        new_settings = types.account.PasswordInputSettings(                         
            new_salt=salt,                                                          
            new_password_hash=new_password_hash,                                    
            hint=hint)                                                              
                                                                                 
    return client.send(functions.account.UpdatePasswordSettings(                        
        current_password_hash, new_settings=new_settings))    

Maybe it would be useful to add it to pyrogram/client/client.py for other users, what do you think?

@delivrance
Copy link
Member

@1pyxa1

Maybe it would be useful to add it to pyrogram/client/client.py for other users, what do you think?

Definitely! Being a pretty young project there's a lot to be added and suggestions are very welcome.

I was thinking about adding three different methods to create, change and remove the password, or maybe a single one that does everything? What do you think?

@1pyxa1
Copy link
Contributor Author

1pyxa1 commented Jan 19, 2018

Personally I would prefer a single one that does everything. Maybe put there little more explanation. Because the code for three different methods will be pretty much the same. But of course it's up to you. And i think it's not so important which way to choose.

Here is my final tested version with removing a password also:

def update_password_settings(client,                                             
                             new_password=None,                                  
                             current_password=None,                              
                             email=None,                                         
                             hint='hint'):                                       
                                                                                 
    if new_password is None and current_password is None:                        
        logging.warning("new_password is None and current_password is None.\     
                        Nothing to do.")                                         
        return False                                                             
                                                                                 
    r = client.send(functions.account.GetPassword())                             
    salt_random = os.urandom(8)                                                  
    salt = r.new_salt + salt_random                                              
                                                                                 
    if current_password:                                                         
        current_password = r.current_salt + current_password.encode() + \        
            r.current_salt                                                       
        current_password_hash = sha256(current_password).digest()                
    else:                                                                        
        current_password_hash = salt                                             
                                                                                 
    if new_password:                                                             
        new_password = salt + new_password.encode('utf-8') + salt                
        new_password_hash = sha256(new_password).digest()                        
    else:                                                                        
        return client.send(functions.account.UpdatePasswordSettings(             
            current_password_hash,                                               
            new_settings=types.account.PasswordInputSettings(                    
                new_salt=bytearray(),                                            
                new_password_hash=bytearray(),                                   
                hint=hint)))                                                     
                                                                                 
    if email:                                                                    
        new_settings = types.account.PasswordInputSettings(                      
            new_salt=salt,                                                       
            new_password_hash=new_password_hash,                                 
            email=email,                                                         
            hint=hint)                                                           
    else:                                                                        
        new_settings = types.account.PasswordInputSettings(                      
            new_salt=salt,                                                       
            new_password_hash=new_password_hash,                                 
            hint=hint)                                                           
                                                                                 
    return client.send(functions.account.UpdatePasswordSettings(                 
        current_password_hash, new_settings=new_settings))       

@delivrance
Copy link
Member

I've opted for three distinct methods, I think it's simpler to use this way: 8cc0fe1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants