Last active
December 14, 2015 13:18
-
-
Save mvliet/e6d908c99191a750dd83 to your computer and use it in GitHub Desktop.
Simple command line script that creates an LTI signed url based on input params.
Useful for generating utls to test an LTI provider.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import argparse | |
import requests | |
from oauth2 import Consumer | |
from oauth2 import Request | |
from oauth2 import SignatureMethod_HMAC_SHA1 | |
from oauth2 import generate_nonce | |
from time import time | |
KEY = u'12345' | |
SECRET = u'secret' | |
BASE_URL = 'http://localhost:8080/' | |
def generate_signed_lti_request(url, key, secret, params={}, method="POST"): | |
""" | |
Emulate a tool consumer by creating signed LTI postdata. | |
:param url: url endpoint (in mediacore) for the request. | |
:param key: oauth consumer key | |
:param secret: oauth secret key | |
:param params: (optional) extra post params for the reqest | |
""" | |
request_params = dict( | |
oauth_version = '1.0', | |
oauth_nonce = generate_nonce(), | |
oauth_timestamp = int(time()), | |
oauth_consumer_key = key, | |
lti_message_type = 'basic-lti-launch-request', | |
lti_version = 'LTI-1p0', | |
) | |
request_params.update(params) | |
if not request_params.get('resource_link_id', None): | |
request_params['resource_link_id'] = 'abc-defghi-j' | |
consumer = Consumer(key=key, secret=secret) | |
req = Request(method=method, url=url, parameters=request_params) | |
sig_method = SignatureMethod_HMAC_SHA1() | |
req.sign_request(sig_method, consumer, None) | |
return req | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-u', '--url', default=BASE_URL) | |
parser.add_argument('-k', '--key', default=KEY) | |
parser.add_argument('-s', '--secret', default=KEY) | |
parser.add_argument('-m', '--method', default="POST") | |
parser.add_argument('-p', '--post-data', action='store_true') | |
parser.add_argument('--url-out', action='store_true') | |
parser.add_argument('params', nargs='*') | |
args = parser.parse_args() | |
d_params = {} | |
for p in args.params: | |
s = p.split('=') | |
d_params[s[0]] = s[1] | |
req = generate_signed_lti_request( | |
args.url, | |
args.key, | |
args.secret, | |
d_params, | |
args.method | |
) | |
if args.url_out: | |
sys.stdout.write(args.url + '?' + req.to_postdata()) | |
sys.exit(0) | |
if args.post_data: | |
sys.stdout.write(req.to_postdata()) | |
sys.exit(0) | |
if args.method == 'POST': | |
resp = requests.post(args.url, req) | |
elif args.method == 'GET': | |
resp = requests.get(args.url, req) | |
print resp.text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated the script with some output options.