summaryrefslogtreecommitdiff
path: root/wowarmopy/auth.py
diff options
context:
space:
mode:
Diffstat (limited to 'wowarmopy/auth.py')
-rw-r--r--wowarmopy/auth.py131
1 files changed, 131 insertions, 0 deletions
diff --git a/wowarmopy/auth.py b/wowarmopy/auth.py
new file mode 100644
index 0000000..97a6981
--- /dev/null
+++ b/wowarmopy/auth.py
@@ -0,0 +1,131 @@
+import re
+import httplib, urllib, urlparse
+
+import constants
+
+def login_http (url, cookies = None, data = None, post = False):
+ parsed_url = urlparse.urlsplit (url)
+ scheme, domain, path, query, fragment = parsed_url
+ if scheme == "https":
+ h = httplib.HTTPSConnection (domain)
+ else:
+ h = httplib.HTTPConnection (domain)
+ headers = {"User-Agent": "Mozilla/5.0 Gecko/20070219 Firefox/2.0.0.2", # ensure returns XML
+ "Cookie": "cookieMenu=all; cookies=true; "}
+ if data:
+ headers["Content-type"] = "application/x-www-form-urlencoded"
+ if cookies:
+ headers["Cookie"] += " ".join (["%s=%s;" % cookie for cookie in cookies.items ()])
+ if post:
+ params = urllib.urlencode (data)
+ method = "POST"
+ else:
+ params = None
+ method = "GET"
+
+ h.request (method, "%s?%s#%s" % (path, query, fragment), params, headers)
+ return h.getresponse ()
+
+def login_final_bounce (url):
+ # Let's bounce to our page that will give us our short term cookie, URL has Kerbrose style ticket.
+ finalstage = login_http (url)
+
+ # Did we get a 200?
+ if finalstage.status == 200:
+ # Get the short term cookie at last
+ short_cookie = None
+ match = re.search ("%s=(.*?);" % constants.temporary_cookie,
+ finalstage.getheader ("set-cookie"))
+ if match:
+ short_cookie = match.groups ()[0]
+ return short_cookie
+
+ # Finally we didn't get 200?
+ raise RuntimeError, "Login broken"
+
+class ArmoryAuth (object):
+
+ short_cookie = None
+ long_cookie = None
+
+ def __init__ (self, armory):
+ self.armory = armory
+
+ def login (self, username, password):
+ # Create the base URL we will be POSTing to.
+ url = self.armory.get_base_url (login = True, secure = True) \
+ + constants.login_url \
+ + "?app=armory"
+
+ # Ensure we add the correct bounce point.
+ if self.armory.locale == "us":
+ url += "&ref=http://www.wowarmory.com/index.xml"
+ else:
+ url += "&ref=http://%s.wowarmory.com/index.xml" % self.armory.locale
+
+ # Ensure we have no final stage.
+ redirectstage = None
+
+ # Post the first stage
+ stage1 = login_http (url, None, { 'accountName': username, 'password': password }, True)
+
+ # Check what happened.
+ if stage1.status == 200:
+ raise RuntimeError, "Could not login, authenticators unsupported"
+ elif stage1.status == 302:
+ redirectstage = stage1
+
+ # We should have been redirected by now.
+ if not redirectstage:
+ raise RuntimeError, "Login broken"
+
+ # Time to obtain our next URL and our long term cookie.
+ long_cookie = None
+ #print redirectstage.getheaders ()
+ match = re.search ("%s=(.*?);" % constants.persistant_cookie,
+ redirectstage.getheader ("set-cookie"))
+ if match:
+ long_cookie = match.groups ()[0]
+
+ # Let's bounce to our page that will give us our short term cookie, URL has Kerbrose style ticket.
+ short_cookie = login_final_bounce (redirectstage.getheader ("location"))
+
+ self.short_cookie = short_cookie
+ self.long_cookie = long_cookie
+
+ #print self.short_cookie, self.long_cookie
+
+ def get_cookie (self):
+ if self.short_cookie:
+ return "%s=%s; " % (constants.temporary_cookie, self.short_cookie)
+ else:
+ return ""
+
+ # FIXME : doesn't work, meh
+ def refresh_login (self):
+ # Create the base URL we will be POSTing to.
+ url = self.armory.get_base_url (login = True, secure = True) \
+ + constants.login_url \
+ + "?app=armory"
+
+ # Ensure we add the correct bounce point.
+ if self.armory.locale == "us":
+ url += "&ref=http://www.wowarmory.com/index.xml"
+ else:
+ url += "&ref=http://%s.wowarmory.com/index.xml" % self.armory.locale
+
+ # All we need to do is goto the armory login page passing our long life cookie, we should get 302 instantly.
+ stage1 = login_http (url, { constants.persistant_cookie: self.long_cookie })
+
+ # Let's see
+ if stage1.status == 200:
+ # It's no good, our cookie doesn't work anymore.
+ raise RuntimeError, "Invalid login details"
+ elif stage1.status == 302:
+ # Let's bounce to our page that will give us our short term cookie, URL has Kerbrose style ticket.
+ short_cookie = login_final_bounce (stage1.getheader ("location"))
+ self.short_cookie = short_cookie
+
+ # Finally we didn't get 302 or 200?
+ raise RuntimeError, "Login broken"
+