File: //home/arjun/projects/env/lib/python3.10/site-packages/tornado/__pycache__/auth.cpython-310.pyc
o
we� � @ s� d Z ddlZddlZddlZddlZddlZddlZddlZddl Z ddl
mZ ddl
mZ ddl
mZ ddlmZ ddlmZ ddlmZmZmZmZmZmZmZ G d d
� d
e�ZG dd� de�ZG d
d� de�ZG dd� de�Z G dd� de�Z!G dd� de �Z"G dd� de �Z#i dfdee$ef de$de$dee$ef deee$ef de%fdd�Z&i dfdee$ef de$de$dee$ef deee$ef de%fdd �Z'd!ee$e%f de$fd"d#�Z(d$e%dee$ef fd%d&�Z)dS )'a� This module contains implementations of various third-party
authentication schemes.
All the classes in this file are class mixins designed to be used with
the `tornado.web.RequestHandler` class. They are used in two ways:
* On a login handler, use methods such as ``authenticate_redirect()``,
``authorize_redirect()``, and ``get_authenticated_user()`` to
establish the user's identity and store authentication tokens to your
database and/or cookies.
* In non-login handlers, use methods such as ``facebook_request()``
or ``twitter_request()`` to use the authentication tokens to make
requests to the respective services.
They all take slightly different arguments due to the fact all these
services implement authentication and authorization slightly differently.
See the individual service classes below for complete documentation.
Example usage for Google OAuth:
.. testsetup::
import urllib
.. testcode::
class GoogleOAuth2LoginHandler(tornado.web.RequestHandler,
tornado.auth.GoogleOAuth2Mixin):
async def get(self):
# 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):
access = await self.get_authenticated_user(
redirect_uri=redirect_uri,
code=self.get_argument('code'))
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=redirect_uri,
client_id=self.get_google_oauth_settings()['key'],
scope=['profile', 'email'],
response_type='code',
extra_params={'approval_prompt': 'auto'})
.. testoutput::
:hide:
� N)�
httpclient)�escape)�
url_concat)�unicode_type)�RequestHandler)�List�Any�Dict�cast�Iterable�Union�Optionalc @ s e Zd ZdS )� AuthErrorN)�__name__�
__module__�__qualname__� r r �E/home/arjun/projects/env/lib/python3.10/site-packages/tornado/auth.pyr ] s r c @ s� e Zd ZdZdg d�fdee dee ddfdd�Z dd eej de
eef fd
d�Zg dfdede
e dee de
eef fd
d�Zdejde
eef fdd�Zdej fdd�ZdS )�OpenIdMixinz�Abstract implementation of OpenID and Attribute Exchange.
Class attributes:
* ``_OPENID_ENDPOINT``: the identity provider's URI.
N)�name�email�language�username�callback_uri�ax_attrs�returnc C sT t t| �}|p
|jj}|dusJ �| j||d�}| j}|�|d tj� |� � dS )a� Redirects to the authentication URL for this service.
After authentication, the service will redirect back to the given
callback URI with additional parameters including ``openid.mode``.
We request the given attributes for the authenticated user by
default (name, email, language, and username). If you don't need
all those attributes for your app, you can request fewer with
the ax_attrs keyword argument.
.. versionchanged:: 6.0
The ``callback`` argument was removed and this method no
longer returns an awaitable object. It is now an ordinary
synchronous function.
N)r �?)
r
r �request�uri�_openid_args�_OPENID_ENDPOINT�redirect�urllib�parse� urlencode)�selfr r �handler�args�endpointr r r �authenticate_redirecti s
z!OpenIdMixin.authenticate_redirect�http_clientc � sl �t t| �}tdd� |jj�� D ��}d|d<