Beispiel #1
0
def migrate(consumer_key, consumer_secret, access_token, access_secret, auth_client, scopes):
    """Migrates OAuth1 tokens to OAuth2 tokens
    
    :param consumer_key: OAuth1 Consumer Key
    :param consumer_secret: OAuth1 Consumer Secret
    :param access_token: OAuth1 Access Token
    :param access_secret: OAuth1 Access Secret
    :param auth_client: AuthClient for OAuth2 specs
    :type auth_client: `intuitlib.client.AuthClient`
    :param scopes: list of `intuitlib.enum.Scopes`
    :raises AuthClientError: if response status != 200
    """

    if auth_client.environment.lower() == 'production':
        migration_url = MIGRATION_URL['production']
    else:
        migration_url = MIGRATION_URL['sandbox']
    
    auth_header = OAuth1(consumer_key, consumer_secret, access_token, access_secret)

    headers = {
        'Content-Type': 'application/json',
    }

    body = {
        'scope': scopes_to_string(scopes),
        'redirect_uri': auth_client.redirect_uri,
        'client_id': auth_client.client_id,
        'client_secret': auth_client.client_secret
    }
    
    send_request('POST', migration_url, headers, auth_client, body=json.dumps(body), oauth1_header=auth_header)
    def get_authorization_url(self, scopes, state_token=None):
        """Generates authorization url using scopes specified where user is redirected to
        
        :param scopes: Scopes for OAuth/OpenId flow
        :type scopes: list of enum, `intuitlib.enums.Scopes`
        :param state_token: CSRF token, defaults to None
        :return: Authorization url
        """

        state = state_token or self.state_token
        if state is None:
            state = generate_token()
        self.state_token = state

        url_params = {
            'client_id': self.client_id,
            'response_type': 'code',
            'scope': scopes_to_string(scopes),
            'redirect_uri': self.redirect_uri,
            'state': self.state_token
        }

        return '?'.join([self.auth_endpoint, urlencode(url_params)])
Beispiel #3
0
 def test_scopes_to_string_input_correct(self):
     scope = scopes_to_string([Scopes.OPENID, Scopes.EMAIL])  
     
     assert scope == 'openid email'
Beispiel #4
0
 def test_scopes_to_string_input_list(self):
     with pytest.raises(TypeError):
         scopes_to_string(['openid'])