From: Ben Darnell Date: Sat, 8 Jul 2023 00:31:18 +0000 (-0400) Subject: auth: Copy google mixin comment to top-of-file X-Git-Tag: v6.4.0b1~24^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b62902466bf6c48d4b74d1d342e294e455204556;p=thirdparty%2Ftornado.git auth: Copy google mixin comment to top-of-file --- diff --git a/tornado/auth.py b/tornado/auth.py index e22397370..c857387be 100644 --- a/tornado/auth.py +++ b/tornado/auth.py @@ -36,17 +36,28 @@ Example usage for Google OAuth: .. testcode:: class GoogleOAuth2LoginHandler(tornado.web.RequestHandler, - tornado.auth.GoogleOAuth2Mixin): + tornado.auth.GoogleOAuth2Mixin): + # Google requires an exact match for redirect_uri, so it's + # best to get it from your app configuration instead of from + # self.request.full_uri(). + redirect_uri = urllib.parse.urljoin(self.application.settings['redirect_base_uri'], + self.reverse_url('google_oauth')) async def get(self): if self.get_argument('code', False): - user = await self.get_authenticated_user( - redirect_uri='http://your.site.com/auth/google', + access = await self.get_authenticated_user( + redirect_uri=redirect_uri, code=self.get_argument('code')) - # Save the user with e.g. set_signed_cookie + user = await self.oauth2_request( + "https://www.googleapis.com/oauth2/v1/userinfo", + access_token=access["access_token"]) + # Save the user and access token. For example: + user_cookie = dict(id=user["id"], access_token=access["access_token"]) + self.set_signed_cookie("user", json.dumps(user_cookie)) + self.redirect("/") else: self.authorize_redirect( - redirect_uri='http://your.site.com/auth/google', - client_id=self.settings['google_oauth']['key'], + redirect_uri=redirect_uri, + client_id=self.get_google_oauth_settings()['key'], scope=['profile', 'email'], response_type='code', extra_params={'approval_prompt': 'auto'})