Python oauth2.SignatureMethod_HMAC_SHA1() Examples

The following are 11 code examples of oauth2.SignatureMethod_HMAC_SHA1(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module oauth2 , or try the search function .
Example #1
Source File: cartodb.py    From qgis-cartodb with GNU General Public License v2.0 6 votes vote down vote up
def __init__(self, key, secret, email, password, cartodb_domain, host='carto.com', protocol='https', proxy_info=None, *args, **kwargs):
        super(CartoDBOAuth, self).__init__(cartodb_domain, host, protocol, *args, **kwargs)

        self.consumer_key = key
        self.consumer_secret = secret
        consumer = oauth.Consumer(self.consumer_key, self.consumer_secret)

        client = oauth.Client(consumer, proxy_info=proxy_info)
        client.set_signature_method = oauth.SignatureMethod_HMAC_SHA1()

        params = {}
        params["x_auth_username"] = email
        params["x_auth_password"] = password
        params["x_auth_mode"] = 'client_auth'

        # Get Access Token
        access_token_url = ACCESS_TOKEN_URL % {'user': cartodb_domain, 'domain': host, 'protocol': protocol}
        resp, token = client.request(access_token_url, method="POST", body=urllib.urlencode(params))
        access_token = dict(urlparse.parse_qsl(token))
        token = oauth.Token(access_token['oauth_token'], access_token['oauth_token_secret'])

        # prepare client
        self.client = oauth.Client(consumer, token) 
Example #2
Source File: oauth7digital.py    From MusicGenreClassification with MIT License 6 votes vote down vote up
def signed_request(url, access_token=None):
    ''' return a signed request. Usually not used, save for specific
        functions like deriving a preview-clip URL.
    '''
    consumer = _consumer()

    req = oauth.Request.from_consumer_and_token(
                consumer,
                http_url=url,
                is_form_encoded=True,
                parameters={'country':api_settings.country})

    signing_method = oauth.SignatureMethod_HMAC_SHA1()
    req.sign_request(signing_method, consumer, access_token)

    return req 
Example #3
Source File: oauth7digital.py    From MusicGenreClassification with MIT License 6 votes vote down vote up
def signed_request(url, access_token=None):
    ''' return a signed request. Usually not used, save for specific
        functions like deriving a preview-clip URL.
    '''
    consumer = _consumer()

    req = oauth.Request.from_consumer_and_token(
                consumer,
                http_url=url,
                is_form_encoded=True,
                parameters={'country':api_settings.country})

    signing_method = oauth.SignatureMethod_HMAC_SHA1()
    req.sign_request(signing_method, consumer, access_token)

    return req 
Example #4
Source File: demo4_use_access_token.py    From coursys with GNU General Public License v3.0 5 votes vote down vote up
def use_access_token(oauth_token, oauth_secret):
    consumer = oauth.Consumer(key='9fdce0e111a1489eb5a42eab7a84306b', secret='liqutXmqJpKmetfs')
    token = oauth.Token(oauth_token, oauth_secret)
    oauth_request = oauth.Request.from_consumer_and_token(consumer, http_url=RESOURCE_URL)
    oauth_request.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer, token)

    # get the resource
    response = requests.get(RESOURCE_URL, headers=oauth_request.to_header())
    return response.text 
Example #5
Source File: demo2_get_request_token.py    From coursys with GNU General Public License v3.0 5 votes vote down vote up
def get_request_token(with_callback=True):
    consumer = oauth.Consumer(key='9fdce0e111a1489eb5a42eab7a84306b', secret='liqutXmqJpKmetfs')
    if with_callback:
        callback = CALLBACK_URL
    else:
        callback = 'oob'
    oauth_request = oauth.Request.from_consumer_and_token(consumer, http_url=REQUEST_TOKEN_URL, parameters={'oauth_callback': callback})
    oauth_request.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer, None)

    response = requests.get(REQUEST_TOKEN_URL, headers=oauth_request.to_header())
    request_token = dict(urllib.parse.parse_qsl(response.content.decode('utf8')))
    return request_token 
Example #6
Source File: demo3_get_access_token.py    From coursys with GNU General Public License v3.0 5 votes vote down vote up
def get_access_token(oauth_token, oauth_secret, oauth_verifier):
    consumer = oauth.Consumer(key='9fdce0e111a1489eb5a42eab7a84306b', secret='liqutXmqJpKmetfs')
    token = oauth.Token(oauth_token, oauth_secret)
    token.set_verifier(oauth_verifier)
    oauth_request = oauth.Request.from_consumer_and_token(consumer, token, http_url=ACCESS_TOKEN_URL)
    oauth_request.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer, token)
    response = requests.get(ACCESS_TOKEN_URL, headers=oauth_request.to_header())
    access_token = dict(urllib.parse.parse_qsl(response.content.decode('utf8')))

    return access_token 
Example #7
Source File: request.py    From Python-Programming-Blueprints with MIT License 5 votes vote down vote up
def prepare_request(url, url_params):
    reqconfig = read_reqauth()
    config = read_config()

    token = oauth.Token(
        key=reqconfig.oauth_token,
        secret=reqconfig.oauth_token_secret)

    consumer = oauth.Consumer(
        key=config.consumer_key,
        secret=config.consumer_secret)

    params = {
        'oauth_version': "1.0",
        'oauth_nonce': oauth.generate_nonce(),
        'oauth_timestamp': str(int(time.time()))
    }

    params['oauth_token'] = token.key
    params['oauth_consumer_key'] = consumer.key

    params.update(url_params)

    req = oauth.Request(method="GET", url=url, parameters=params)

    signature_method = oauth.SignatureMethod_HMAC_SHA1()
    req.sign_request(signature_method, consumer, token)

    return req.to_url() 
Example #8
Source File: request_validator.py    From ontask_b with MIT License 5 votes vote down vote up
def __init__(self):
        super().__init__()

        self.oauth_server = oauth2.Server()
        signature_method = oauth2.SignatureMethod_HMAC_SHA1()
        self.oauth_server.add_signature_method(signature_method)
        self.oauth_consumer = oauth2.Consumer(
            self.consumer_key, self.consumer_secret) 
Example #9
Source File: rest.py    From allura with Apache License 2.0 5 votes vote down vote up
def server(self):
        result = oauth.Server()
        result.add_signature_method(oauth.SignatureMethod_PLAINTEXT())
        result.add_signature_method(oauth.SignatureMethod_HMAC_SHA1())
        return result 
Example #10
Source File: tests.py    From coursys with GNU General Public License v3.0 4 votes vote down vote up
def test_oauth_workflow(self):
        request_token_url = 'http://testserver' + reverse('api:oauth_request_token')
        authorize_token_url = 'http://testserver' + reverse('api:oauth_user_authorization')

        # create consumer for tests
        c = Client()
        c.login_user('ggbaker')
        c.logout()
        consumer = Consumer(name='Test Consumer', description='Consumer to do some tests with', status=ACCEPTED,
                     user=User.objects.get(username='ggbaker'), xauth_allowed=False)
        consumer.generate_random_codes()
        consumer.save()
        ci = ConsumerInfo(consumer=consumer)
        ci.admin_contact = 'the_developer@example.com'
        ci.permissions = ['courses', 'grades']
        ci.save()

        # generate request token
        oauth_request = oauth.Request.from_consumer_and_token(consumer, http_url=request_token_url,
                                                              parameters={'oauth_callback': 'oob'})
        oauth_request.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer, None)

        resp = c.get(request_token_url, **oauth_request.to_header())
        self.assertEqual(resp.status_code, 200)
        request_token = dict(urllib.parse.parse_qsl(resp.content.decode('utf8')))

        # get auth verifier
        c.login_user('ggbaker')
        resp = c.get(authorize_token_url, {'oauth_token': request_token['oauth_token']})
        self.assertEqual(resp.status_code, 200)
        resp = c.post(authorize_token_url, {'oauth_token': request_token['oauth_token'], 'authorize_access': 'on'})
        self.assertEqual(resp.status_code, 200)
        parser = etree.HTMLParser()
        root = etree.fromstring(resp.content, parser=parser)
        verifier_elt = root.xpath('//*[@id="verifier"]')[0]
        oauth_verifier = verifier_elt.text.strip()
        c.logout()

        # get access token
        token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
        token.set_verifier(oauth_verifier)
        oauth_request = oauth.Request.from_consumer_and_token(consumer, token, http_url=authorize_token_url)
        oauth_request.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer, token)

        resp = c.get(authorize_token_url, **oauth_request.to_header())
        # TODO: this is failing for some reason, but succeeds in the demo scripts
        #self.assertEqual(resp.status_code, 200)
        #access_token = dict(urllib.parse.parse_qsl(response.content.decode('utf8'))) 
Example #11
Source File: tool_consumer.py    From ontask_b with MIT License 4 votes vote down vote up
def generate_launch_data(self):
        # Validate params
        if not self.has_required_params():
            raise InvalidLTIConfigError('ToolConsumer does not have all required attributes: consumer_key = %s, consumer_secret = %s, resource_link_id = %s, launch_url = %s' % (
                self.consumer_key, self.consumer_secret, self.resource_link_id, self.launch_url))

        params = self.to_params()
        params['lti_version'] = 'LTI-1.0'
        params['lti_message_type'] = 'basic-lti-launch-request'

        # Get new OAuth consumer
        consumer = oauth2.Consumer(key=self.consumer_key,
                                   secret=self.consumer_secret)

        params.update({
            'oauth_nonce': str(generate_identifier()),
            'oauth_timestamp': str(int(time.time())),
            'oauth_scheme': 'body',
            'oauth_consumer_key': consumer.key
        })

        uri = urlparse(self.launch_url)
        if uri.query != '':
            for param in uri.query.split('&'):
                key, val = param.split('=')
                if params.get(key) == None:
                    params[key] = str(val)

        request = oauth2.Request(method='POST',
                                 url=self.launch_url,
                                 parameters=params)

        signature_method = oauth2.SignatureMethod_HMAC_SHA1()
        request.sign_request(signature_method, consumer, None)

        # Request was made by an HTML form in the user's browser.
        # Return the dict of post parameters ready for embedding
        # in an html view.
        return_params = {}
        for key in request:
            if request[key] == None:
                return_params[key] = None
            elif isinstance(request[key], list):
                return_params[key] = request.get_parameter(key)
            else:
                return_params[key] = unquote(request.get_parameter(key))
        return return_params