Skip to content

Instantly share code, notes, and snippets.

@vladsf
Created July 20, 2015 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vladsf/fc2fde62bf77c51d1b57 to your computer and use it in GitHub Desktop.
Save vladsf/fc2fde62bf77c51d1b57 to your computer and use it in GitHub Desktop.
This script authenticates and saves cookies for further use with RBTools.
#!/usr/bin/env python
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
import cookielib
import urllib2
from six.moves.urllib.parse import urlencode, urlparse, urlunparse
sso_username = "someuser"
sso_password = "password"
sso_cookies_file = "my_sso_cookies.txt"
sso_host = "login.ssohost.com"
rb_uri = "http://rb.somehost.com/rb/"
cookies = cookielib.MozillaCookieJar()
handlers = [
urllib2.HTTPHandler(),
urllib2.HTTPSHandler(),
urllib2.HTTPCookieProcessor(cookies)
]
opener = urllib2.build_opener(*handlers)
def fetch(uri):
req = urllib2.Request(uri)
return opener.open(req)
def handle_sso(response):
url = urlparse(response.geturl())
if url.netloc == sso_host:
if not sso_username or not sso_password:
print "SSO login missing."
return response
print "Got SSO login, sending account info to " + url.netloc + ".."
response.close()
headers = {}
headers["Content-Type"] = "application/x-www-form-urlencoded"
token = url.query
data = urlencode({"ssousername": sso_username,
"password": sso_password}) + "&" + token
headers["Content-Length"] = str(len(data))
headers["User-Agent"] = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
req = urllib2.Request(url.scheme + "://" + url.netloc + "/sso/auth", data, headers)
return opener.open(req)
else:
print "SSO host changed? Hostname is not " + sso_host + ": " + url.netloc
return response
def dump():
print "Dumping cookies.."
for cookie in cookies:
print cookie.name, cookie.value
uri = rb_uri
res = fetch(uri)
res = handle_sso(res)
#dump()
# save cookies to disk. you can load them with cookies.load() as well.
print "Saving cookies to " + sso_cookies_file
cookies.save(sso_cookies_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment