comparison Code/python_oauth2-master/example/appengine_oauth.py @ 21:e68dbee1f6db

Modified code New datasets Updated report
author Paulo Chiliguano <p.e.chiilguano@se14.qmul.ac.uk>
date Tue, 11 Aug 2015 10:50:36 +0100
parents
children
comparison
equal deleted inserted replaced
20:1dbd24575d44 21:e68dbee1f6db
1 """
2 The MIT License
3
4 Copyright (c) 2010 Justin Plock
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 """
24
25 import os
26
27 from google.appengine.ext import webapp
28 from google.appengine.ext import db
29 from google.appengine.ext.webapp import util
30 import oauth2 as oauth # httplib2 is required for this to work on AppEngine
31
32 class Client(db.Model):
33 # oauth_key is the Model's key_name field
34 oauth_secret = db.StringProperty() # str(uuid.uuid4()) works well for this
35 first_name = db.StringProperty()
36 last_name = db.StringProperty()
37 email_address = db.EmailProperty(required=True)
38 password = db.StringProperty(required=True)
39
40 @property
41 def secret(self):
42 return self.oauth_secret
43
44 class OAuthHandler(webapp.RequestHandler):
45
46 def __init__(self):
47 self._server = oauth.Server()
48 self._server.add_signature_method(oauth.SignatureMethod_HMAC_SHA1())
49 self._server.add_signature_method(oauth.SignatureMethod_PLAINTEXT())
50
51 def get_oauth_request(self):
52 """Return an OAuth Request object for the current request."""
53
54 try:
55 method = os.environ['REQUEST_METHOD']
56 except:
57 method = 'GET'
58
59 postdata = None
60 if method in ('POST', 'PUT'):
61 postdata = self.request.body
62
63 return oauth.Request.from_request(method, self.request.uri,
64 headers=self.request.headers, query_string=postdata)
65
66 def get_client(self, request=None):
67 """Return the client from the OAuth parameters."""
68
69 if not isinstance(request, oauth.Request):
70 request = self.get_oauth_request()
71 client_key = request.get_parameter('oauth_consumer_key')
72 if not client_key:
73 raise Exception('Missing "oauth_consumer_key" parameter in ' \
74 'OAuth "Authorization" header')
75
76 client = models.Client.get_by_key_name(client_key)
77 if not client:
78 raise Exception('Client "%s" not found.' % client_key)
79
80 return client
81
82 def is_valid(self):
83 """Returns a Client object if this is a valid OAuth request."""
84
85 try:
86 request = self.get_oauth_request()
87 client = self.get_client(request)
88 params = self._server.verify_request(request, client, None)
89 except Exception, e:
90 raise e
91
92 return client
93
94 class SampleHandler(OAuthHandler):
95 def get(self):
96 try:
97 client = self.is_valid()
98 except Exception, e:
99 self.error(500)
100 self.response.out.write(e)
101
102 def main():
103 application = webapp.WSGIApplication([(r'/sample', SampleHandler)],
104 debug=False)
105 util.run_wsgi_app(application)
106
107 if __name__ == '__main__':
108 main()