def access_token(): oauth_server, oauth_request = initialize_server_request(request) if oauth_server is None: return oauth_error_response(OAuthError('Invalid request parameters.')) try: # Create our access token token = oauth_server.fetch_access_token(oauth_request) if not token: return oauth_error_response(OAuthError("Cannot find corresponding access token.")) # Grab the mapping of access tokens to our identity providers oauth_map = OAuthMap.get_from_request_token(oauth_request.get_parameter("oauth_token")) if not oauth_map: return oauth_error_response(OAuthError("Cannot find oauth mapping for request token.")) oauth_map.access_token = token.key_ oauth_map.access_token_secret = token.secret oauth_map.put() # Flush the "apply phase" of the above put() to ensure that subsequent # retrievals of this OAuthmap returns fresh data. GAE's HRD can # otherwise take a second or two to propagate the data, and the # client may use the access token quicker than that. oauth_map = OAuthMap.get(oauth_map.key()) except OAuthError, e: return oauth_error_response(e)
def post(self): """POST submissions are for username/password based logins to acquire an OAuth access token. """ identifier = self.request_string('identifier') password = self.request_string('password') if not identifier or not password: self.render_login_page("Please enter your username and password.") return user_data = UserData.get_from_username_or_email(identifier.strip()) if not user_data or not user_data.validate_password(password): # TODO(benkomalo): IP-based throttling of failed logins? self.render_login_page("Your login or password is incorrect.") return # Successful login - convert to an OAuth access_token oauth_map_id = self.request_string("oauth_map_id", default="") oauth_map = OAuthMap.get_by_id_safe(oauth_map_id) if not oauth_map: self.render_login_page("Unable to find OAuthMap by id.") return # Mint the token and persist to the oauth_map oauth_map.khan_auth_token = AuthToken.for_user(user_data).value oauth_map.put() # Flush the "apply phase" of the above put() to ensure that subsequent # retrievals of this OAuthmap returns fresh data. GAE's HRD can # otherwise take a second or two to propagate the data, and the # following authorize endpoint redirect below could happen quicker # than that in some cases. oauth_map = OAuthMap.get(oauth_map.key()) # Need to redirect back to the http authorize endpoint return auth_util.authorize_token_redirect(oauth_map, force_http=True)