Load the HubiC form, submit it and return an authorization token.

        This will load the HTML form to accept if the application can access
        the user account and submit the form using the user's credentials.

        :raises keystoneclient.exceptions.AuthorizationFailure: if something
                                                                goes wrong.

        :returns: The (short lived) authorization code to use to get the
                  refresh token.
        :rtype: string

def _get_authorization_token(self, session):
        """Load the HubiC form, submit it and return an authorization token.

        This will load the HTML form to accept if the application can access
        the user account and submit the form using the user's credentials.

        :raises keystoneclient.exceptions.AuthorizationFailure: if something
                                                                goes wrong.

        :returns: The (short lived) authorization code to use to get the
                  refresh token.
        :rtype: string
        """

        request_scope = 'account.r,credentials.r'

        params = {
            'client_id': self.client_id,
            'redirect_uri': self.redirect_uri,
            'response_type': 'code',
            'scope': request_scope,
            'state': random_str_generator(),
        }

        r = session.get("https://api.hubic.com/oauth/auth",
                        params=params,
                        authenticated=False)
        if r.status_code != 200:
            raise AuthorizationFailure()

        oauth_match = re.search(r'name="oauth" value="([0-9]+)"', r.text)
        if oauth_match is None:
            raise AuthorizationFailure()

        oauth_value = oauth_match.group(1)
        if oauth_value is None:
            AuthorizationFailure()

        payload = {
            'oauth': oauth_value,
            'action': 'accepted',
            'account': 'r',
            'credentials': 'r',
            'login': self.email,
            'user_pwd': self.password,
        }

        # this is necessary because the API will return a 509 error
        # (bandwidth exceeded) if we don't wait a little
        time.sleep(2)

        headers = {
            'Referer': r.url,
            'Content-Type': 'application/x-www-form-urlencoded',
        }

        r = session.post("https://api.hubic.com/oauth/auth",
                         headers=headers,
                         data=payload,
                         redirect=False,
                         authenticated=False)
        if r.status_code != 302:
            raise AuthorizationFailure()

        # location looks like this, and we need the code:
        # http://localhost/?code=...&scope=account.r&state=randomstring
        location_info = dict(
            map(lambda item: item.split('='),
                r.headers['location'].split('?')[1].split('&')
            )
        )
        assert (
            'code'  in location_info and
            'scope' in location_info and location_info['scope'] == request_scope and
            'state' in location_info and location_info['state'] == params['state']
        )

        return location_info['code']