Как пройти двухфакторную аутентификацию с помощью instagramapi- php? - PullRequest
0 голосов
/ 10 апреля 2020

Я пытаюсь создать страницу входа, чтобы получить доступ к некоторым API и использовать их, у меня проблема с аутентификацией IG буксировки по фактору. При входе в систему с учетной записью без 2FA все работает нормально. Но когда IG аккаунт с 2FA вошел в систему, мой код go до l oop, и данные уничтожаются.

Может ли какой-либо орган помочь мне проверить и исправить мой код?!

TNX

public function login()
    {

        $user_session = $this->session->all_userdata();

        $do_login = $this->input->post('do_login', true);
        $req = $this->input->get('req', true);

        if (!empty($_SESSION['_temp']['user']) and !empty($_SESSION['_temp']['pass'])) {

            $username = $_SESSION['_temp']['user'];
            $password = $_SESSION['_temp']['pass'];
        } else {
            $username = $this->input->post('username', true);
            $password = $this->input->post('password', true);

            $session_temp = array(
                'user' => $username,
                'pass' => $password
            );

            $_SESSION['_temp'] = $session_temp;
        }



        InstagramAPI\Instagram::$allowDangerousWebUsageAtMyOwnRisk = true;

        $ig = new ExtendedInstagram(false, false);

        if (isset($req) and !empty($req)) {

            switch ($req) {
                case 'twoFactorAuthentication':


                    try {

                        if(!empty($_SESSION['verificationCode'])) {
                            $verificationCode = $_SESSION['verificationCode'];
                        } else {
                            $verificationCode = $this->input->post('tfa_code', true);
                            $_SESSION['verificationCode'] = $verificationCode;
                        }

                        $ig->finishTwoFactorLogin($username, $password, $_SESSION['twoFactorIdentifier'], $verificationCode);
                    } catch (InstagramException $e) {
                        try {

                            $verification_method = $this->input->post('verification_method', true);
                            $verificationMethod = '0';
                            $verification_code = $this->input->post('verification_code', true);


                            /** @noinspection PhpUndefinedClassInspection */
                            if ((class_exists('InstagramAPI\\Exception\\ChallengeRequiredException') && $e instanceof InstagramAPI\Exception\ChallengeRequiredException) || (class_exists('InstagramAPI\Exception\Checkpoint\ChallengeRequiredException') && $e instanceof InstagramAPI\Exception\Checkpoint\ChallengeRequiredException)) {

                                $response = $e->getResponse();

                                if (isset($verification_method) and $verification_method == true) {


                                    $ig->webChallengePublic($response, $verificationMethod);
                                    $this->load->view('instagram/verify_code');

                                } elseif (isset($verification_code) and $verification_code == true) {

                                    //print_r('Work and verificationMethod: ' . $verificationMethod);

                                    $verify_code = $this->input->post('verify_code', true);

                                    if ($ig->checkWebChallengePublic($response, $username, $password, $verify_code)) {

                                        if ($ig->isMaybeLoggedIn) {

                                            $user = json_decode($ig->account->getCurrentUser()->getUser()->asJson(), true);

                                            $user_session = array(
                                                'username' => $username,
                                                'password' => $password,
                                                'is_ig_login' => TRUE,
                                                'user' => $user
                                            );

                                            $this->session->set_userdata($user_session);

                                            redirect("insta/");
                                        } else {
                                            redirect("insta/login");

                                        }

                                    } else {
                                        print_r($ig->checkWebChallengePublic($response, $username, $password, $verify_code));
                                        exit();
                                    }

                                } else {
                                    $ig->webChallengePublic($response, '0');
                                    $this->load->view('instagram/2fa/verify_code');
                                }

                            } else {
                                print_r("Error while logging into Instagram: " . $e->getMessage());
                                exit(1);
                            }
                        } catch (LazyJsonMapperException $mapperException) {
                            print_r("Error while decoding challenge response: " . $e->getMessage());
                            exit(1);
                        }
                    }

                    break;

                default:
                    redirect('insta/logout');
            }


        } elseif (isset($do_login) and $do_login == true) {

            if (!isset($username) or empty($username) or !isset($password) or empty($password)) {
                print "IG username & password required!";
                exit();
            }

            try {
                $loginResponse = $ig->login($username, $password);
                $_SESSION['login_log'] = $loginResponse;

                if ($loginResponse !== null && $loginResponse->isTwoFactorRequired()) {
                    $twoFactorIdentifier = $loginResponse->getTwoFactorInfo()->getTwoFactorIdentifier();
                    $_SESSION['twoFactorIdentifier'] = $twoFactorIdentifier;

                    $this->load->view('instagram/2fa');

                }
            } catch (InstagramException $e) {
                try {

                    $verification_method = $this->input->post('verification_method', true);
                    $verificationMethod = $this->input->post('verificationMethod', true);
                    $verification_code = $this->input->post('verification_code', true);


                    /** @noinspection PhpUndefinedClassInspection */
                    if ((class_exists('InstagramAPI\\Exception\\ChallengeRequiredException') && $e instanceof InstagramAPI\Exception\ChallengeRequiredException) || (class_exists('InstagramAPI\Exception\Checkpoint\ChallengeRequiredException') && $e instanceof InstagramAPI\Exception\Checkpoint\ChallengeRequiredException)) {

                        $response = $e->getResponse();

                        if (isset($verification_method) and $verification_method == true) {


                            $ig->webChallengePublic($response, $verificationMethod);
                            $this->load->view('instagram/verify_code');

                        } elseif (isset($verification_code) and $verification_code == true) {

                            //print_r('Work and verificationMethod: ' . $verificationMethod);

                            $verify_code = $this->input->post('verify_code', true);

                            if ($ig->checkWebChallengePublic($response, $username, $password, $verify_code)) {

                                if ($ig->isMaybeLoggedIn) {

                                    $user = json_decode($ig->account->getCurrentUser()->getUser()->asJson(), true);

                                    $user_session = array(
                                        'username' => $username,
                                        'password' => $password,
                                        'is_ig_login' => TRUE,
                                        'user' => $user
                                    );

                                    $this->session->set_userdata($user_session);

                                    redirect("insta/");
                                } else {
                                    redirect("insta/login");

                                }

                            } else {
                                print_r($ig->checkWebChallengePublic($response, $username, $password, $verify_code));
                                exit();
                            }

                        } else {
                            $this->load->view('instagram/verifying');
                        }

                    } else {
                        print_r("Error while logging into Instagram: " . $e->getMessage());
                        exit(1);
                    }
                } catch (LazyJsonMapperException $mapperException) {
                    print_r("Error while decoding challenge response: " . $e->getMessage());
                    exit(1);
                }
            }

            if ($ig->isMaybeLoggedIn) {

                $user = json_decode($ig->account->getCurrentUser()->getUser()->asJson(), true);

                $user_session = array(
                    'username' => $username,
                    'password' => $password,
                    'is_ig_login' => TRUE,
                    'user' => $user
                );

                $this->session->set_userdata($user_session);

                redirect("insta/");
            }

        } else {
            if (isset($_SESSION)) {
                unset($_SESSION);
                $this->session->sess_destroy();
            }

            $this->load->view('instagram/login');
        }

    }
...