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)
oauth_map.google_access_token_secret = google_token.secret return oauth_map @route("/api/auth/google_token_callback", methods=["GET"]) @decorators.manual_access_checking def google_token_callback(): oauth_map = OAuthMap.get_by_id_safe(request.values.get("oauth_map_id")) if not oauth_map: return oauth_error_response( OAuthError("Unable to find OAuthMap by id.")) if oauth_map.google_verification_code: return oauth_error_response( OAuthError("Request token already has google verification code.")) oauth_map.google_verification_code = request.values.get("oauth_verifier") try: oauth_map = retrieve_google_access_token(oauth_map) except OAuthBadRequestError, e: return pretty_error_response('Unable to log in with Google.') except OAuthError, e: return oauth_error_response(e) oauth_map.put() return authorize_token_redirect(oauth_map)
google_client = GoogleOAuthClient() google_token = google_client.fetch_access_token(oauth_map) except Exception, e: raise OAuthError(e.message) oauth_map.google_access_token = google_token.key oauth_map.google_access_token_secret = google_token.secret return oauth_map @route("/api/auth/google_token_callback", methods=["GET"]) def google_token_callback(): oauth_map = OAuthMap.get_by_id_safe(request.values.get("oauth_map_id")) if not oauth_map: return oauth_error_response(OAuthError("Unable to find OAuthMap by id.")) if oauth_map.google_verification_code: return oauth_error_response(OAuthError("Request token already has google verification code.")) oauth_map.google_verification_code = request.values.get("oauth_verifier") try: oauth_map = retrieve_google_access_token(oauth_map) except OAuthError, e: return oauth_error_response(e) oauth_map.put() return authorize_token_redirect(oauth_map)