OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """API endpoints to get around javascript's single-origin restriction.""" | 7 """API endpoints to get around javascript's single-origin restriction.""" |
8 | 8 |
9 import logging | 9 import logging |
10 | 10 |
11 from django.utils import simplejson as json | 11 from django.utils import simplejson as json |
12 | 12 |
13 import gdata.client | 13 from google.appengine.api import urlfetch |
14 | |
15 from google.appengine.ext import webapp | 14 from google.appengine.ext import webapp |
16 from google.appengine.ext.webapp import util | 15 from google.appengine.ext.webapp import util |
17 from google.appengine.ext.webapp.util import login_required | 16 from google.appengine.ext.webapp.util import login_required |
18 import auth | 17 import auth |
19 | 18 |
20 | 19 |
21 class GetXmppTokenHandler(webapp.RequestHandler): | 20 class GetXmppTokenHandler(webapp.RequestHandler): |
22 """Retrieves the user's XMPP token.""" | 21 """Retrieves the user's XMPP token.""" |
23 @login_required | 22 @login_required |
24 def get(self): | 23 def get(self): |
25 try: | 24 try: |
26 self.response.headers['Content-Type'] = 'application/json' | 25 self.response.headers['Content-Type'] = 'application/json' |
27 self.response.out.write( | 26 self.response.out.write( |
28 json.dumps({'xmpp_token': auth.GetXmppToken().token})) | 27 json.dumps({'xmpp_token': auth.GetXmppToken().token})) |
29 except auth.NotAuthenticated: | 28 except auth.NotAuthenticated: |
30 self.response.out.write('User has not authenticated') | 29 self.response.out.write('User has not authenticated') |
31 self.set_status(400) | 30 self.set_status(400) |
32 | 31 |
| 32 |
33 class GetHostListHandler(webapp.RequestHandler): | 33 class GetHostListHandler(webapp.RequestHandler): |
34 """Proxies the host-list handlers on the Chromoting directory.""" | 34 """Proxies the host-list handlers on the Chromoting directory.""" |
35 @login_required | 35 @login_required |
36 def get(self): | 36 def get(self): |
37 try: | 37 if not auth.HasOAuth2Tokens(): |
38 client = gdata.client.GDClient() | 38 self.response.set_status(403) |
39 host_list_json = client.Request( | |
40 method='GET', | |
41 uri="https://www.googleapis.com/chromoting/v1/@me/hosts", | |
42 converter=None, | |
43 desired_class=None, | |
44 auth_token=auth.GetChromotingToken()) | |
45 self.response.headers['Content-Type'] = 'application/json' | 39 self.response.headers['Content-Type'] = 'application/json' |
46 self.response.out.write(host_list_json.read()) | 40 self.response.out.write( |
47 except auth.NotAuthenticated: | 41 '{"error": { "code": -1, "message": "No OAuth2 token" }}') |
48 self.response.out.write('User has not authenticated') | 42 return |
49 self.response.set_status(400) | 43 result = urlfetch.fetch( |
50 except (gdata.client.Unauthorized, gdata.client.RequestError), inst: | 44 url = 'https://www.googleapis.com/chromoting/v1/@me/hosts', |
51 self.response.out.write(inst.reason) | 45 method = urlfetch.GET, |
52 self.response.set_status(inst.status) | 46 headers = {'Authorization': 'OAuth ' + auth.GetAccessToken()}) |
| 47 self.response.set_status(result.status_code) |
| 48 for i in result.headers: |
| 49 self.response.headers[i] = result.headers[i] |
| 50 self.response.out.write(result.content) |
| 51 |
53 | 52 |
54 def main(): | 53 def main(): |
55 application = webapp.WSGIApplication( | 54 application = webapp.WSGIApplication( |
56 [ | 55 [ |
57 ('/api/get_xmpp_token', GetXmppTokenHandler), | 56 ('/api/get_xmpp_token', GetXmppTokenHandler), |
58 ('/api/get_host_list', GetHostListHandler) | 57 ('/api/get_host_list', GetHostListHandler) |
59 ], | 58 ], |
60 debug=True) | 59 debug=True) |
61 util.run_wsgi_app(application) | 60 util.run_wsgi_app(application) |
62 | 61 |
63 | 62 |
64 if __name__ == '__main__': | 63 if __name__ == '__main__': |
65 main() | 64 main() |
OLD | NEW |