diff options
Diffstat (limited to 'exchange/authenticators.py')
-rw-r--r-- | exchange/authenticators.py | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/exchange/authenticators.py b/exchange/authenticators.py new file mode 100644 index 0000000..33db4c7 --- /dev/null +++ b/exchange/authenticators.py | |||
@@ -0,0 +1,79 @@ | |||
1 | # -*- coding: utf-8 -*- | ||
2 | """ | ||
3 | Exchange Server Authenticators | ||
4 | |||
5 | @author: Mike Crute (mcrute@gmail.com) | ||
6 | @organization: SoftGroup Interactive, Inc. | ||
7 | @date: April 26, 2009 | ||
8 | @version: $Rev$ | ||
9 | |||
10 | $Id$ | ||
11 | """ | ||
12 | import urllib | ||
13 | |||
14 | from copy import copy | ||
15 | from httplib import HTTPSConnection | ||
16 | from Cookie import SimpleCookie | ||
17 | |||
18 | |||
19 | class ExchangeAuthenticator(object): | ||
20 | |||
21 | _auth_cookie = None | ||
22 | authenticated = False | ||
23 | username = None | ||
24 | |||
25 | def __init__(self, web_server): | ||
26 | self.web_server = web_server | ||
27 | |||
28 | def authenticate(self, username, password): | ||
29 | """ | ||
30 | Authenticate the user and cache the authentication so we aren't | ||
31 | hammering the auth server. Should hanlde expiration eventually. | ||
32 | """ | ||
33 | self.username = username | ||
34 | |||
35 | if self._auth_cookie: | ||
36 | return self._auth_cookie | ||
37 | |||
38 | self._auth_cookie = self._do_authentication(username, password) | ||
39 | return self._auth_cookie | ||
40 | |||
41 | def _do_authentication(self, username, password): | ||
42 | raise NotImplemented | ||
43 | |||
44 | def patch_headers(self, headers): | ||
45 | raise NotImplemented | ||
46 | |||
47 | |||
48 | class CookieAuthenticator(ExchangeAuthenticator): | ||
49 | |||
50 | AUTH_DLL = "/exchweb/bin/auth/owaauth.dll" | ||
51 | |||
52 | def _do_authentication(self, username, password): | ||
53 | """ | ||
54 | Does a post to the authentication DLL to fetch a cookie for the session | ||
55 | this can then be passed back to the exchange API for servers that don't | ||
56 | support basicc HTTP auth. | ||
57 | """ | ||
58 | params = urllib.urlencode({ "destination": "https://%s/exchange" % (self.web_server), | ||
59 | "flags": "0", | ||
60 | "username": username, | ||
61 | "password": password, | ||
62 | "SubmitCreds": "Log On", | ||
63 | "trusted": "4" | ||
64 | }) | ||
65 | |||
66 | conn = HTTPSConnection(self.web_server) | ||
67 | conn.request("POST", self.AUTH_DLL, params) | ||
68 | response = conn.getresponse() | ||
69 | |||
70 | cookie = SimpleCookie(response.getheader("set-cookie")) | ||
71 | cookie = ("sessionid=%s" % cookie["sessionid"].value, "cadata=%s" % cookie["cadata"].value) | ||
72 | |||
73 | self.authenticated = True | ||
74 | return "; ".join(cookie) | ||
75 | |||
76 | def patch_headers(self, headers): | ||
77 | out_headers = copy(headers) | ||
78 | out_headers["Cookie"] = self._auth_cookie | ||
79 | return out_headers | ||