Mercurial > universal-identity-relyer
comparison oauth_experiment.py @ 12:63ea847bfa75
added more stuff
author | Atul Varma <avarma@mozilla.com> |
---|---|
date | Sat, 12 Jun 2010 22:37:20 -0700 |
parents | 42fe50c20cc8 |
children |
comparison
equal
deleted
inserted
replaced
11:727b82d4b596 | 12:63ea847bfa75 |
---|---|
1 import os | 1 import os |
2 import json | 2 import json |
3 import oauth2 as oauth | 3 import oauth2 as oauth |
4 import wsgiref.util | |
5 | |
4 import twitter_client | 6 import twitter_client |
7 import file_storage | |
8 from static_file_serving import StaticFileApp | |
9 | |
10 class MyTwitterApp(object): | |
11 def __init__(self, default, access_tokens): | |
12 self.default = default | |
13 self.twitter = None | |
14 self.access_tokens = access_tokens | |
15 | |
16 def onsuccess(self, environ, start_response): | |
17 access_token = environ['oauth.access_token'] | |
18 tokid = access_token['oauth_token'] | |
19 if tokid not in self.access_tokens: | |
20 self.access_tokens[tokid] = access_token | |
21 start_response('200 OK', [('Content-Type', 'text/html')]) | |
22 js = 'window.opener.onLogin(window, "%s");' % tokid | |
23 return ['<script>%s</script>' % js] | |
24 | |
25 def __call__(self, environ, start_response): | |
26 path = environ['PATH_INFO'] | |
27 | |
28 if path.startswith('/twitter/') and self.twitter is not None: | |
29 wsgiref.util.shift_path_info(environ) | |
30 return self.twitter(environ, start_response) | |
31 | |
32 return self.default(environ, start_response) | |
33 | |
34 def ensure_dirs(*paths): | |
35 for path in paths: | |
36 if not os.path.exists(path): | |
37 os.mkdir(path) | |
38 | |
39 mydir = os.getcwd() | |
40 staticfilesdir = os.path.join(mydir, 'static-files') | |
41 storagedir = os.path.join(mydir, 'storage') | |
42 reqdir = os.path.join(storagedir, 'request-tokens') | |
43 accdir = os.path.join(storagedir, 'access-tokens') | |
44 | |
45 ensure_dirs(storagedir, reqdir, accdir) | |
46 | |
47 static_files = StaticFileApp(staticfilesdir) | |
48 | |
49 app = MyTwitterApp( | |
50 default=static_files, | |
51 access_tokens=file_storage.FileStorage(accdir) | |
52 ) | |
5 | 53 |
6 config = json.loads(open("config.json").read()) | 54 config = json.loads(open("config.json").read()) |
7 | 55 |
8 consumer = oauth.Consumer(config['consumer_key'], | 56 consumer = oauth.Consumer(config['consumer_key'], |
9 config['consumer_secret']) | 57 config['consumer_secret']) |
10 | 58 |
11 class Storage(object): | 59 twitter = twitter_client.TwitterOauthClientApp( |
12 def __filename(self, name): | |
13 return "__REQUEST_TOKEN_%s.json" % name | |
14 | |
15 def __contains__(self, name): | |
16 return os.path.exists(self.__filename(name)) | |
17 | |
18 def __delitem__(self, name): | |
19 if not name in self: | |
20 raise KeyError(name) | |
21 os.remove(self.__filename(name)) | |
22 | |
23 def __getitem__(self, name): | |
24 if not name in self: | |
25 raise KeyError(name) | |
26 return json.loads(open(self.__filename(name)).read()) | |
27 | |
28 def __setitem__(self, name, value): | |
29 open(self.__filename(name), 'w').write(json.dumps(value)) | |
30 | |
31 def success_app(environ, start_response): | |
32 start_response('200 OK', [('Content-Type', 'text/plain')]) | |
33 return ['woot %s' % repr(environ['oauth.access_token'])] | |
34 | |
35 app = twitter_client.TwitterOauthClientApp( | |
36 consumer=consumer, | 60 consumer=consumer, |
37 oauth=oauth, | 61 oauth=oauth, |
38 request_tokens=Storage(), | 62 request_tokens=file_storage.FileStorage(reqdir), |
39 onsuccess=success_app | 63 onsuccess=app.onsuccess |
40 ) | 64 ) |
65 | |
66 app.twitter = twitter |