o
     d                      @   s   d dl mZmZmZmZmZ d dlmZ d dlm	Z
 d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZmZ G d
d dZG dd deZG dd deZ	G dd de	ZG dd de	ZG dd dZdS )    )AnyDictListOptionalUnion)HTTPException)OAuth2)
OAuthFlows)Form)SecurityBase)get_authorization_scheme_param)Request)HTTP_401_UNAUTHORIZEDHTTP_403_FORBIDDENc                   @   sf   e Zd ZdZeddde e eddeddeddfdededed	ed
ee dee fddZdS )OAuth2PasswordRequestForma  
    This is a dependency class, use it like:

        @app.post("/login")
        def login(form_data: OAuth2PasswordRequestForm = Depends()):
            data = form_data.parse()
            print(data.username)
            print(data.password)
            for scope in data.scopes:
                print(scope)
            if data.client_id:
                print(data.client_id)
            if data.client_secret:
                print(data.client_secret)
            return data


    It creates the following Form request parameters in your endpoint:

    grant_type: the OAuth2 spec says it is required and MUST be the fixed string "password".
        Nevertheless, this dependency class is permissive and allows not passing it. If you want to enforce it,
        use instead the OAuth2PasswordRequestFormStrict dependency.
    username: username string. The OAuth2 spec requires the exact field name "username".
    password: password string. The OAuth2 spec requires the exact field name "password".
    scope: Optional string. Several scopes (each one a string) separated by spaces. E.g.
        "items:read items:write users:read profile openid"
    client_id: optional string. OAuth2 recommends sending the client_id and client_secret (if any)
        using HTTP Basic auth, as: client_id:client_secret
    client_secret: optional string. OAuth2 recommends sending the client_id and client_secret (if any)
        using HTTP Basic auth, as: client_id:client_secret
    Npassword)defaultregex r   
grant_typeusernamescope	client_idclient_secretc                 C   s,   || _ || _|| _| | _|| _|| _d S N)r   r   r   splitscopesr   r   selfr   r   r   r   r   r    r    B/usr/local/lib/python3.10/dist-packages/fastapi/security/oauth2.py__init__.   s   	

z"OAuth2PasswordRequestForm.__init__)__name__
__module____qualname____doc__r
   strr   r"   r    r    r    r!   r      s*    
"r   c                       sl   e Zd ZdZedde e eddeddeddfdededed	ed
ee dee f fddZ  ZS )OAuth2PasswordRequestFormStricta  
    This is a dependency class, use it like:

        @app.post("/login")
        def login(form_data: OAuth2PasswordRequestFormStrict = Depends()):
            data = form_data.parse()
            print(data.username)
            print(data.password)
            for scope in data.scopes:
                print(scope)
            if data.client_id:
                print(data.client_id)
            if data.client_secret:
                print(data.client_secret)
            return data


    It creates the following Form request parameters in your endpoint:

    grant_type: the OAuth2 spec says it is required and MUST be the fixed string "password".
        This dependency is strict about it. If you want to be permissive, use instead the
        OAuth2PasswordRequestForm dependency class.
    username: username string. The OAuth2 spec requires the exact field name "username".
    password: password string. The OAuth2 spec requires the exact field name "password".
    scope: Optional string. Several scopes (each one a string) separated by spaces. E.g.
        "items:read items:write users:read profile openid"
    client_id: optional string. OAuth2 recommends sending the client_id and client_secret (if any)
        using HTTP Basic auth, as: client_id:client_secret
    client_secret: optional string. OAuth2 recommends sending the client_id and client_secret (if any)
        using HTTP Basic auth, as: client_id:client_secret
    r   )r   r   r   Nr   r   r   r   r   c                    s   t  j||||||d d S )N)r   r   r   r   r   r   )superr"   r   	__class__r    r!   r"   `   s   	
z(OAuth2PasswordRequestFormStrict.__init__)	r#   r$   r%   r&   r
   r'   r   r"   __classcell__r    r    r*   r!   r(   ?   s*    "r(   c                	   @   sj   e Zd Ze dddddeeeeeeef f f dee dee de	fdd	Z
d
edee fddZdS )r   NTflowsscheme_namedescription
auto_errorr.   r/   r0   r1   c                C   s&   t ||d| _|p| jj| _|| _d S )N)r.   r0   )OAuth2Modelmodelr+   r#   r/   r1   )r   r.   r/   r0   r1   r    r    r!   r"   t   s   
zOAuth2.__init__requestreturnc                    s,   |j d}|s| jrttddd S |S )NAuthorizationNot authenticated)status_codedetail)headersgetr1   r   r   )r   r4   authorizationr    r    r!   __call__   s   zOAuth2.__call__)r#   r$   r%   OAuthFlowsModelr   r   r'   r   r   boolr"   r   r=   r    r    r    r!   r   s   s    
r   c                       sf   e Zd Z				ddedee deeeef  dee def
 fdd	Zd
edee fddZ	  Z
S )OAuth2PasswordBearerNTtokenUrlr/   r   r0   r1   c                    s0   |si }t ||dd}t j||||d d S )N)rA   r   )r   r-   r>   r)   r"   )r   rA   r/   r   r0   r1   r.   r*   r    r!   r"      s   
zOAuth2PasswordBearer.__init__r4   r5   c                    J   |j d}t|\}}|r| dkr#| jr!ttdddidd S |S Nr6   bearerr7   zWWW-AuthenticateBearer)r8   r9   r:   r:   r;   r   lowerr1   r   r   r   r4   r<   schemeparamr    r    r!   r=         zOAuth2PasswordBearer.__call__)NNNTr#   r$   r%   r'   r   r   r?   r"   r   r=   r,   r    r    r*   r!   r@      s"    r@   c                       st   e Zd Z					ddededee dee deeeef  dee d	ef fd
dZdedee fddZ	  Z
S )OAuth2AuthorizationCodeBearerNTauthorizationUrlrA   
refreshUrlr/   r   r0   r1   c           	         s4   |si }t ||||dd}t j||||d d S )N)rO   rA   rP   r   )authorizationCoder-   rB   )	r   rO   rA   rP   r/   r   r0   r1   r.   r*   r    r!   r"      s   

z&OAuth2AuthorizationCodeBearer.__init__r4   r5   c                    rC   rD   rG   rI   r    r    r!   r=      rL   z&OAuth2AuthorizationCodeBearer.__call__)NNNNTrM   r    r    r*   r!   rN      s,    rN   c                   @   s$   e Zd Zddeee  fddZdS )SecurityScopesNr   c                 C   s   |pg | _ d| j | _d S )N )r   join	scope_str)r   r   r    r    r!   r"      s   
zSecurityScopes.__init__r   )r#   r$   r%   r   r   r'   r"   r    r    r    r!   rR      s    rR   N)typingr   r   r   r   r   fastapi.exceptionsr   fastapi.openapi.modelsr   r2   r	   r>   fastapi.param_functionsr
   fastapi.security.baser   fastapi.security.utilsr   starlette.requestsr   starlette.statusr   r   r   r(   r@   rN   rR   r    r    r    r!   <module>   s    24"+