Skip to content

Instantly share code, notes, and snippets.

@hanleybrand
Created November 7, 2012 22:15
Show Gist options
  • Save hanleybrand/4034866 to your computer and use it in GitHub Desktop.
Save hanleybrand/4034866 to your computer and use it in GitHub Desktop.
Script that generates an html file that will log a credentialled user into iTunesU (obvs you supply your own credentials)
#!/usr/bin/python
import urllib
import urllib2
import time
import hashlib
import hmac
# I was fixing iTunesU.py as per
# http://www.maclearning.org/articles/40/fixing-apple-s-i-tunes-u-python-s
# and I decided to try to make it a tiny bit more pythonic, as in
# no semi-colons terminating lines and removing the antipythonic main() method
# no, really
# Define your site's information. Replace these
# values with ones appropriate for your site.
siteurl = "https://deimos.apple.com/WebObjects/Core.woa/Browse/example.edu"
debugsuffix = "/abc123"
sharedsecret = "STRINGOFTHIRTYTWOLETTERSORDIGITS"
administratorcredential = "Administrator@urn:mace:itunesu.com:sites:example.edu"
# Define the user information. Replace the credentials with the
# credentials you want to grant to that user, and the optional
# identity information with the identity of the current user.
# For initial testing and site setup, use the singe administrator
# credential defined when your iTunes U site was created. Once
# you have access to your iTunes U site, you will be able to define
# additional credentials and the iTunes U access they provide.
credentialarray = [administratorcredential]
displayname = "Jane Doe"
emailaddress = "janedoe@example.edu"
username = "jdoe"
useridentifier = "42"
# Append your site's debug suffix to the destination if you
# want to receive an HTML page providing information about
# the transmission of credentials and identity between this
# program and iTunes U. Remove this code after initial
# testing to instead receive the destination page requested.
# uncomment the line below to access debug information from the
# iTunes U server.
# siteurl = siteurl + debugsuffix
def get_identity_string(displayname, emailaddress, username, useridentifier):
# Combine user identity information into an appropriately formatted string.
# take the arguments passed into the function copy them to variables
return '"%s" <%s> (%s) [%s]' % (displayname, emailaddress, username, useridentifier)
def get_credentials_string(credentialarray):
# Combine individual credentials into a semicolon delimited string
credentialString = ""
i = 0
while(i < len(credentialarray)):
if (i == 0):
credentialString = credentialarray[i]
else:
credentialString = "%s;%s" % (credentialString,credentialarray[i])
i += 1
return credentialString
def get_authorization_token(identity, credentials, currenttime, sharedsecret):
# create the token that contains the necessary elements to authorize the user
# signature = "";
# buffer = ""
dict = {"credentials" : credentials,
"identity" : identity,
"time" : currenttime
}
encoded_parms = urllib.urlencode(dict)
h = hmac.new(sharedsecret, encoded_parms, hashlib.sha256)
dict['signature'] = h.hexdigest()
return urllib.urlencode(dict)
def invoke_action(siteurl, token, username):
# create the request and pass it to the iTunes U server.
req = urllib2.Request(siteurl, token)
req.add_data(token)
handle = urllib2.urlopen(req)
the_page = handle.read()
filename = 'openITU-' + username + '.html'
f = open( filename , 'w' )
f.write("Content-type: text/html\n\n")
f.write(the_page)
f.close()
identity = get_identity_string(displayname, emailaddress, username, useridentifier)
credentials = get_credentials_string(credentialarray)
# time.time() returns a float, so we cast to an int
currenttime = int(time.time())
token = get_authorization_token(identity, credentials, currenttime, sharedsecret)
invoke_action(siteurl, token, username)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment