Created
September 24, 2012 13:31
-
-
Save criccomini/3775967 to your computer and use it in GitHub Desktop.
App Engine and Facebook Connect - Install PyFacebook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def check_connect_session(self, request): | |
| """ | |
| For use in a facebook Connect application running in Google App Engine | |
| Takes a Google App Engine Request | |
| http://code.google.com/appengine/docs/webapp/requestclass.html | |
| and determines if the current user has a valid session | |
| """ | |
| # our session is stored in cookies - validate them | |
| params = self.validate_cookie(request.cookies) | |
| if not params: | |
| return False | |
| if params.get('expires'): | |
| self.session_key_expires = int(params['expires']) | |
| if 'session_key' in params and 'user' in params: | |
| self.session_key = params['session_key'] | |
| self.uid = params['user'] | |
| else: | |
| return False | |
| return True | |
| def validate_cookie(self, cookies): | |
| """ | |
| Validates parameters passed to a Facebook connect app through cookies | |
| """ | |
| # check for the hashed secret | |
| if self.api_key not in cookies: | |
| return None | |
| # create a dict of the elements that start with the api_key | |
| # the resultant dict removes the self.api_key from the beginning | |
| args = dict([(key[len(self.api_key) + 1:], value) | |
| for key, value in cookies.items() | |
| if key.startswith(self.api_key + "_")]) | |
| # check the hashes match before returning them | |
| if self._hash_args(args) == cookies[self.api_key]: | |
| return args | |
| return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment