Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def generate_launch_request(
url,
body=None,
http_method="GET",
base_url="http://localhost",
roles="Instructor",
headers=None,
):
params = {}
if roles is not None:
params["roles"] = roles
urlparams = urlencode(params)
client = oauthlib.oauth1.Client(
"key",
client_secret="secret",
signature_method=oauthlib.oauth1.SIGNATURE_HMAC,
signature_type=oauthlib.oauth1.SIGNATURE_TYPE_QUERY,
)
signature = client.sign(
"{}{}?{}".format(base_url, url, urlparams),
body=body,
http_method=http_method,
headers=headers,
)
signed_url = signature[0]
new_url = signed_url[len(base_url) :]
return new_url
def test_invalid_oauth_and_XML(self):
# OAuth credentials are invalid and the body XML causes ParseError
req_xml = '''
'''
# OAuth1 signature and body hash for the HTTP request Authorization header
oauth_client = oauthlib.oauth1.Client(
client_key=self.lti_service.consumer_key,
client_secret='thewrongsecret',
signature_method=oauthlib.oauth1.SIGNATURE_HMAC,
signature_type=oauthlib.oauth1.SIGNATURE_TYPE_AUTH_HEADER,
)
oa_uri, oa_headers, oa_body = oauth_client.sign('http://aplus.local/api/v2/lti-outcomes',
http_method='POST',
body=req_xml,
headers={
'Content-Type': 'application/xml',
},
)
# make the test request
response = self.client.post(self.OUTCOMES_API_URL, data=req_xml, content_type='application/xml',
HTTP_AUTHORIZATION=oa_headers['Authorization'],
proc = subprocess.Popen(['python', 'server.py'], stdout=subprocess.PIPE)
stdout, stderr = proc.communicate()
if stderr:
exit(stderr)
client = oauthlib.oauth1.Client(self.ckey, client_secret=self.csecret,
resource_owner_key=oauth_token,
resource_owner_secret=oauth_secret,
verifier=stdout.strip())
uri, headers, body = client.sign("https://secure.splitwise.com/api/v3.0/get_access_token",
http_method='POST')
resp = requests.post(uri, headers=headers, data=body)
tokens = resp.text.split('&')
oauth_token = tokens[0].split('=')[1]
oauth_secret = tokens[1].split('=')[1]
client = oauthlib.oauth1.Client(self.ckey, client_secret=self.csecret,
resource_owner_key=oauth_token,
resource_owner_secret=oauth_secret,
verifier=stdout.strip())
with open('oauth_client.pkl', 'wb') as pkl:
pickle.dump(client, pkl)
self.client = client
def fetch_tweets(self, tweet_params):
oauth = oauth1.Client(
client_key=self.params['key'],
client_secret=self.params['secret'],
resource_owner_key=self.params['access_key'],
resource_owner_secret=self.params['access_secret'])
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'Gramex',
}
url, headers, data = oauth.sign(
self.url, 'POST', body=urlencode(tweet_params), headers=headers)
self.req = tornado.httpclient.HTTPRequest(
method='POST', url=url, body=data, headers=headers,
request_timeout=864000, # Keep request alive for 10 days
streaming_callback=self._stream,
header_callback=self.header_callback)
def oauth_headers(url, consumer_key, token_key, token_secret, consumer_secret,
timestamp=None):
try:
import oauthlib.oauth1 as oauth1
except ImportError:
raise NotImplementedError('oauth support is not available')
if timestamp:
timestamp = str(timestamp)
else:
timestamp = None
client = oauth1.Client(
consumer_key,
client_secret=consumer_secret,
resource_owner_key=token_key,
resource_owner_secret=token_secret,
signature_method=oauth1.SIGNATURE_PLAINTEXT,
timestamp=timestamp)
_uri, signed_headers, _body = client.sign(url)
return signed_headers
def make_client(self, token=None):
# request_token_url is for oauth1
if self.request_token_url:
# get params for client
params = self.get_oauth1_client_params(token)
client = oauthlib.oauth1.Client(
client_key=self.consumer_key,
client_secret=self.consumer_secret,
**params
)
else:
if token:
if isinstance(token, (tuple, list)):
token = {'access_token': token[0]}
elif isinstance(token, string_types):
token = {'access_token': token}
client = oauthlib.oauth2.WebApplicationClient(
self.consumer_key, token=token
)
return client
"""
Returns an OAuth token tuple that can be used with clients.ReaderClient.
:param base_url_template: Template for generating Readability API urls.
:param consumer_key: Readability consumer key, otherwise read from READABILITY_CONSUMER_KEY.
:param consumer_secret: Readability consumer secret, otherwise read from READABILITY_CONSUMER_SECRET.
:param username: A username, otherwise read from READABILITY_USERNAME.
:param password: A password, otherwise read from READABILITY_PASSWORD.
"""
consumer_key = xargs.get('consumer_key') or required_from_env('READABILITY_CONSUMER_KEY')
consumer_secret = xargs.get('consumer_secret') or required_from_env('READABILITY_CONSUMER_SECRET')
username = xargs.get('username') or required_from_env('READABILITY_USERNAME')
password = xargs.get('password') or required_from_env('READABILITY_PASSWORD')
client = Client(consumer_key, client_secret=consumer_secret, signature_type='BODY')
url = base_url_template.format(ACCESS_TOKEN_URL)
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
params = {
'x_auth_username': username,
'x_auth_password': password,
'x_auth_mode': 'client_auth'
}
uri, headers, body = client.sign(url,
http_method='POST',
body=urlencode(params),
headers=headers)
response = requests.post(uri, data=body)
logger.debug('POST to %s.', uri)
def __init__(self, consumer_key, consumer_secret, token=None, secret=None):
self.client = oauth1.Client(consumer_key, client_secret=consumer_secret)
self.store_token(token, secret)