Exemple #1
0
def set_password(client, pw):
    new_salt = client(account.GetPasswordRequest()).new_salt
    salt = new_salt + os.urandom(8)
    pw_hash = helpers.get_password_hash(pw, salt)
    client(
        account.UpdatePasswordSettingsRequest(
            current_password_hash=salt,
            new_settings=PasswordInputSettings(
                new_salt=salt,
                new_password_hash=pw_hash,
                hint='No hint',
            )))
Exemple #2
0
    def sign_in(self,
                phone_number=None,
                code=None,
                password=None,
                bot_token=None):
        """Completes the authorization of a phone number by providing the received code.

           If no phone or code is provided, then the sole password will be used. The password
           should be used after a normal authorization attempt has happened and an RPCError
           with `.password_required = True` was raised.

           To login as a bot, only `bot_token` should be provided. This should equal to the
           bot access hash provided by https://t.me/BotFather during your bot creation."""
        if phone_number and code:
            if phone_number not in self.phone_code_hashes:
                raise ValueError(
                    'Please make sure you have called send_code_request first.'
                )

            try:
                result = self.invoke(
                    SignInRequest(phone_number,
                                  self.phone_code_hashes[phone_number], code))

            except RPCError as error:
                if error.message.startswith('PHONE_CODE_'):
                    print(error)
                    return False
                else:
                    raise error
        elif password:
            salt = self.invoke(GetPasswordRequest()).current_salt
            result = self.invoke(
                CheckPasswordRequest(utils.get_password_hash(password, salt)))
        elif bot_token:
            result = self.invoke(
                ImportBotAuthorizationRequest(flags=0,
                                              api_id=self.api_id,
                                              api_hash=self.api_hash,
                                              bot_auth_token=bot_token))
        else:
            raise ValueError(
                'You must provide a phone_number and a code for the first time, '
                'and a password only if an RPCError was raised before.')

        # Result is an Auth.Authorization TLObject
        self.session.user = result.user
        self.session.save()

        # Now that we're authorized, set the signed_in flag
        # to True so update handlers can be added
        self.signed_in = True
        return True
Exemple #3
0
    def sign_in(self,
                phone_number=None,
                code=None,
                password=None,
                bot_token=None):
        if phone_number and code:
            if phone_number not in self.phone_code_hashes:
                raise ValueError(
                    "Please make sure you have called send_code_request first."
                )

            try:
                result = self.invoke(
                    SignInRequest(phone_number,
                                  self.phone_code_hashes[phone_number], code))
            except RPCError as error:
                if error.message.startswith("PHONE_CODE_"):
                    print(error)
                    return False
                else:
                    raise error

        elif password:
            salt = self.invoke(GetPasswordRequest()).current_salt
            result = self.invoke(
                CheckPasswordRequest(utils.get_password_hash(password, salt)))
        elif bot_token:
            result = self.invoke(
                ImportBotAuthorizationRequest(flags=0,
                                              api_id=self.api_id,
                                              api_hash=self.api_hash,
                                              bot_auth_token=bot_token))
        else:
            raise ValueError(
                "You must provide a phone_number and a code for the first time, "
                "and a password only if an RPCError was raised before.")

        self.session.user = result.user
        self.session.save()

        self.signed_in = True
        return True