Skip to content

Commit

Permalink
Merge pull request subzeroid#1533 from rickturner2001/auth-feature
Browse files Browse the repository at this point in the history
Changed behavior of login if username and password have already been …
  • Loading branch information
adw0rd authored Aug 13, 2023
2 parents 777a4a5 + 3975fb3 commit 28b2300
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
3 changes: 3 additions & 0 deletions instagrapi/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ class ProxyAddressIsBlocked(PrivateError):
class BadPassword(PrivateError):
pass

class BadCredentials(PrivateError):
pass


class PleaseWaitFewMinutes(PrivateError):
pass
Expand Down
19 changes: 13 additions & 6 deletions instagrapi/mixins/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
import time
import uuid
from pathlib import Path
from typing import Dict
from typing import Dict, Union
from uuid import uuid4

import requests
from pydantic import ValidationError

from instagrapi import config
from instagrapi.exceptions import (
BadCredentials,
ClientThrottledError,
PleaseWaitFewMinutes,
PrivateError,
Expand Down Expand Up @@ -372,8 +373,8 @@ def login_by_sessionid(self, sessionid: str) -> bool:

def login(
self,
username: str,
password: str,
username: Union[str, None] = None,
password: Union[str, None] = None,
relogin: bool = False,
verification_code: str = "",
) -> bool:
Expand All @@ -396,8 +397,14 @@ def login(
bool
A boolean value
"""
self.username = username
self.password = password

if not self.username or not self.password:
if username is None or password is None:
raise BadCredentials("Both username and password must be provided.")

self.username = username
self.password = password

if relogin:
self.authorization_data = {}
self.private.headers.pop("Authorization", None)
Expand All @@ -416,7 +423,7 @@ def login(
self.logger.warning("Ignore 429: Continue login")
# The instagram application ignores this error
# and continues to log in (repeat this behavior)
enc_password = self.password_encrypt(password)
enc_password = self.password_encrypt(self.password)
data = {
"jazoest": generate_jazoest(self.phone_id),
"country_codes": '[{"country_code":"%d","source":["default"]}]'
Expand Down
17 changes: 13 additions & 4 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pathlib import Path

from instagrapi import Client
from instagrapi.exceptions import DirectThreadNotFound
from instagrapi.exceptions import BadCredentials, DirectThreadNotFound, ProxyAddressIsBlocked, BadPassword
from instagrapi.story import StoryBuilder
from instagrapi.types import (
Account,
Expand Down Expand Up @@ -105,12 +105,21 @@ class FakeClientTestCase(BaseClientMixin, unittest.TestCase):

def test_login(self):
try:
self.cl.login(ACCOUNT_USERNAME, "fakepassword")
except Exception as e:
self.cl.login(ACCOUNT_USERNAME, ACCOUNT_PASSWORD)
except (ProxyAddressIsBlocked, BadPassword) as e:
self.assertEqual(
str(e), "The username you entered doesn't appear to belong to an account. Please check your username and try again."
str(e), "The password you entered is incorrect. Please try again. If you are sure that the password is correct, then change your IP address, because it is added to the blacklist of the Instagram Server"
)

try:
is_auth = self.cl.login()
self.assertEqual(is_auth, True)
except BadCredentials as e:
self.assertEqual(str(e), "Both username and password must be provided.")
except ProxyAddressIsBlocked as e:
self.assertEqual(
str(e), "Instagram has blocked your IP address, use a quality proxy provider (not free, not shared)"
)

class ClientPrivateTestCase(BaseClientMixin, unittest.TestCase):
cl = None
Expand Down

0 comments on commit 28b2300

Please sign in to comment.