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 from google.appengine.api import urlfetch | 13 from google.appengine.api import urlfetch |
14 from google.appengine.ext import webapp | 14 from google.appengine.ext import webapp |
15 from google.appengine.ext.webapp import util | 15 from google.appengine.ext.webapp import util |
16 from google.appengine.ext.webapp.util import login_required | 16 from google.appengine.ext.webapp.util import login_required |
17 import auth | 17 import auth |
18 | 18 |
19 | 19 |
20 class GetXmppTokenHandler(webapp.RequestHandler): | |
21 """Retrieves the user's XMPP token.""" | |
22 @login_required | |
23 def get(self): | |
24 try: | |
25 self.response.headers['Content-Type'] = 'application/json' | |
26 self.response.out.write( | |
27 json.dumps({'xmpp_token': auth.GetXmppToken().token})) | |
28 except auth.NotAuthenticated: | |
29 self.response.out.write('User has not authenticated') | |
30 self.set_status(400) | |
31 | |
32 | |
33 class GetHostListHandler(webapp.RequestHandler): | 20 class GetHostListHandler(webapp.RequestHandler): |
34 """Proxies the host-list handlers on the Chromoting directory.""" | 21 """Proxies the host-list handlers on the Chromoting directory.""" |
35 @login_required | 22 @login_required |
36 def get(self): | 23 def get(self): |
37 if not auth.HasOAuth2Tokens(): | 24 if not auth.HasOAuth2Tokens(): |
38 self.response.set_status(403) | 25 self.response.set_status(403) |
39 self.response.headers['Content-Type'] = 'application/json' | 26 self.response.headers['Content-Type'] = 'application/json' |
40 self.response.out.write( | 27 self.response.out.write( |
41 '{"error": { "code": -1, "message": "No OAuth2 token" }}') | 28 '{"error": { "code": -1, "message": "No OAuth2 token" }}') |
42 return | 29 return |
43 result = urlfetch.fetch( | 30 result = urlfetch.fetch( |
44 url = 'https://www.googleapis.com/chromoting/v1/@me/hosts', | 31 url = 'https://www.googleapis.com/chromoting/v1/@me/hosts', |
45 method = urlfetch.GET, | 32 method = urlfetch.GET, |
46 headers = {'Authorization': 'OAuth ' + auth.GetAccessToken()}) | 33 headers = {'Authorization': 'OAuth ' + auth.GetOAuth2AccessToken()}) |
47 self.response.set_status(result.status_code) | 34 self.response.set_status(result.status_code) |
48 for i in result.headers: | 35 for i in result.headers: |
49 self.response.headers[i] = result.headers[i] | 36 self.response.headers[i] = result.headers[i] |
50 self.response.out.write(result.content) | 37 self.response.out.write(result.content) |
51 | 38 |
52 | 39 |
53 def main(): | 40 def main(): |
54 application = webapp.WSGIApplication( | 41 application = webapp.WSGIApplication( |
55 [ | 42 [ |
56 ('/api/get_xmpp_token', GetXmppTokenHandler), | |
57 ('/api/get_host_list', GetHostListHandler) | 43 ('/api/get_host_list', GetHostListHandler) |
58 ], | 44 ], |
59 debug=True) | 45 debug=True) |
60 util.run_wsgi_app(application) | 46 util.run_wsgi_app(application) |
61 | 47 |
62 | 48 |
63 if __name__ == '__main__': | 49 if __name__ == '__main__': |
64 main() | 50 main() |
OLD | NEW |