Exemplo n.º 1
0
    def auth_method(self):

        if self.qr_code:
            payload = {
                'password': self.password,
                'username': self.username,
                'grant_type': 'password',
                'client_id': "c82SH0WZOsabOXGP2sxqcj34FxkvfnWRZBKlBjFS",
                'scope': 'internal',
                'device_token': self.device_token,
                'mfa_code': self.get_mfa_token(self.qr_code)
            }

            try:
                res = self.session.post(urls.login_url(),
                                        data=payload,
                                        timeout=15)
                data = res.json()

                if 'access_token' in data.keys(
                ) and 'refresh_token' in data.keys():
                    self.auth_token = data['access_token']
                    self.refresh_token = data['refresh_token']
                    self.headers['Authorization'] = 'Bearer ' + self.auth_token
                    return True

            except requests.exceptions.HTTPError:
                raise RH_exception.LoginFailed()

        else:
            payload = {
                'password': self.password,
                'username': self.username,
                'grant_type': 'password',
                'client_id': "c82SH0WZOsabOXGP2sxqcj34FxkvfnWRZBKlBjFS",
                'expires_in': '86400',
                'scope': 'internal',
                'device_token': self.device_token,
            }

            try:
                res = self.session.post(urls.login_url(),
                                        data=payload,
                                        timeout=15)
                res.raise_for_status()
                data = res.json()

                if 'access_token' in data.keys(
                ) and 'refresh_token' in data.keys():
                    self.auth_token = data['access_token']
                    self.refresh_token = data['refresh_token']
                    self.headers['Authorization'] = 'Bearer ' + self.auth_token
                    return True

            except requests.exceptions.HTTPError:
                raise RH_exception.LoginFailed()

        return False
Exemplo n.º 2
0
def login(username, password, expiresIn=86400, scope='internal'):
    """This function will effectivly log the user into robinhood by getting an
    authentication token and saving it to the session header.
    :param username: The username for your robinhood account. Usually your email.
    :type username: str
    :param password: The password for your robinhood account.
    :type password: str
    :param expiresIn: The time until your login session expires. This is in seconds.
    :type expiresIn: Optional[int]
    :param scope: Specifies the scope of the authentication.
    :type scope: Optional[str]
    :returns:  A dictionary with log in information.
    """
    if not username or not password:
        raise Exception('login must be called with a non-empty username and '
                        'password')

    url = urls.login_url()
    payload = {
        'client_id': 'c82SH0WZOsabOXGP2sxqcj34FxkvfnWRZBKlBjFS',
        'expires_in': expiresIn,
        'grant_type': 'password',
        'password': password,
        'scope': scope,
        'username': username,
        'challenge_type': 'sms',
        'device_token': GenerateDeviceToken()
    }
    data = helper.request_post(url, payload)
    token = 'Bearer {}'.format(data['access_token'])
    helper.update_session('Authorization', token)
    helper.set_login_state(True)
    return (data)
Exemplo n.º 3
0
    def login(self, username, password, qr_code=None):
        """Save and test login info for Robinhood accounts
        Args:
            username (str): username
            password (str): password
            qr_code (str): QR code that will be used to generate mfa_code (optional but recommended)
            To get QR code, set up 2FA in Security, get Authentication App, and click "Can't Scan It?"
        Returns:
            (bool): received valid auth token
        """
        self.username = username
        self.password = password

        if self.device_token == "":
            self.GenerateDeviceToken()

        if qr_code:
            self.qr_code = qr_code
            payload = {
                'password': self.password,
                'username': self.username,
                'grant_type': 'password',
                'client_id': "c82SH0WZOsabOXGP2sxqcj34FxkvfnWRZBKlBjFS",
                'scope': 'internal',
                'device_token': self.device_token,
                'mfa_code': self.get_mfa_token(self.qr_code)
            }

            try:
                res = self.session.post(urls.login_url(),
                                        data=payload,
                                        timeout=15)
                data = res.json()

                if 'access_token' in data.keys(
                ) and 'refresh_token' in data.keys():
                    self.auth_token = data['access_token']
                    self.refresh_token = data['refresh_token']
                    self.headers['Authorization'] = 'Bearer ' + self.auth_token
                    return True

            except requests.exceptions.HTTPError:
                raise RH_exception.LoginFailed()

        else:
            payload = {
                'password': self.password,
                'username': self.username,
                'grant_type': 'password',
                'client_id': "c82SH0WZOsabOXGP2sxqcj34FxkvfnWRZBKlBjFS",
                'expires_in': '86400',
                'scope': 'internal',
                'device_token': self.device_token,
                'challenge_type': 'sms'
            }

            try:
                res = self.session.post(urls.login_url(),
                                        data=payload,
                                        timeout=15)
                response_data = res.json()
                if self.challenge_id == "" and "challenge" in response_data.keys(
                ):
                    self.challenge_id = response_data["challenge"]["id"]
                self.headers[
                    "X-ROBINHOOD-CHALLENGE-RESPONSE-ID"] = self.challenge_id  # has to add this to stay logged in
                sms_challenge_endpoint = "https://api.robinhood.com/challenge/{}/respond/".format(
                    self.challenge_id)
                print("No 2FA Given")
                print("SMS Code:")
                self.sms_code = input()
                challenge_res = {"response": self.sms_code}
                res2 = self.session.post(sms_challenge_endpoint,
                                         data=challenge_res,
                                         timeout=15)
                res2.raise_for_status()
                # gets access token for final response to stay logged in
                res3 = self.session.post(urls.login_url(),
                                         data=payload,
                                         timeout=15)
                res3.raise_for_status()
                data = res3.json()

                if 'access_token' in data.keys(
                ) and 'refresh_token' in data.keys():
                    self.auth_token = data['access_token']
                    self.refresh_token = data['refresh_token']
                    self.headers['Authorization'] = 'Bearer ' + self.auth_token
                    return True

            except requests.exceptions.HTTPError:
                raise RH_exception.LoginFailed()

        return False