o
    ߭h~$                     @   s  d dl mZ d dlZd dlmZmZmZmZmZm	Z	 d dl
mZ d dlmZmZ d dlmZ d dlmZmZ d dlmZmZmZ d d	lmZ d d
lmZmZmZ d dlmZm Z  d dl!m"Z"m#Z# dZ$G dd deZ%	d&de&e'e'f dede(de'fddZ)			d'dedeej*ej+f de ej*ej+f dedee' de,de,defdd Z-		d(ded!eej*ej+f de ej*ej+f d"e.ej/ dedee' d#e,defd$d%Z0dS ))    )OptionalN)	APIRouterDependsHTTPExceptionQueryRequeststatus)OAuth2AuthorizeCallback)
BaseOAuth2OAuth2Token)	BaseModel)modelsschemas)AuthenticationBackendAuthenticatorStrategy)UserAlreadyExists)
SecretType
decode_jwtgenerate_jwt)BaseUserManagerUserManagerDependency)	ErrorCode
ErrorModelzfastapi-users:oauth-statec                   @   s   e Zd ZU eed< dS )OAuth2AuthorizeResponseauthorization_urlN)__name__
__module____qualname__str__annotations__ r!   r!   _/var/www/html/stock_analysis/be/venv/lib/python3.10/site-packages/fastapi_users/router/oauth.pyr      s   
 r     datasecretlifetime_secondsreturnc                 C   s   t | d< t| ||S )Naud)STATE_TOKEN_AUDIENCEr   )r$   r%   r&   r!   r!   r"   generate_state_token   s   r*   Foauth_clientbackendget_user_managerstate_secretredirect_urlassociate_by_emailis_verified_by_defaultc                    s.  t  }dj dj ddurtd}ntd}|jddj dj dtd	tdfd
tdtt dtffdd}	|jddt	j
tddddddtjddtjidiiididt|t|tjfd
tdtttf dttjtjf dttjtjf f fdd}
|S )z(Generate a router with the OAuth routes.zoauth:.	.callbackNr/   
route_name
/authorize
.authorizenameresponse_modelrequestscopesr'   c                    sJ   d ur}nt |  }i }t|}|||I d H }t|dS )Nr   )r   url_forr*   get_authorization_urlr   )r<   r=   authorize_redirect_url
state_datastater   callback_route_namer+   r/   r.   r!   r"   	authorize6   s   


z#get_oauth_router.<locals>.authorize	/callback=The response varies based on the authentication backend used.application/jsonexamplesINVALID_STATE_TOKENInvalid state token.summaryvaluezUser is inactive.detailmodelcontent)r:   description	responsesaccess_token_stateuser_managerstrategyc           
         s  |\}} |d I d H \}}|d u rttjtjdz	t|tg W n tj	y4   ttjdw z|j
j|d |||d|d|  d	I d H }W n ty`   ttjtjdw |jslttjtjd||I d H }	||| |	I d H  |	S )Naccess_tokenstatus_coderP   r[   
expires_atrefresh_token)r0   r1   )get_id_emailr   r   HTTP_400_BAD_REQUESTr   OAUTH_NOT_AVAILABLE_EMAILr   r)   jwtDecodeErroroauth_callbackr:   getr   OAUTH_USER_ALREADY_EXISTS	is_activeLOGIN_BAD_CREDENTIALSloginon_after_login)
r<   rV   rW   rX   tokenrC   
account_idaccount_emailuserresponse)r0   r,   r1   r+   r.   r!   r"   callbackM   sR    z"get_oauth_router.<locals>.callback)r   r:   r	   re   r   r   r   listr   r   r`   r   r   rh   r   get_strategytupler   r   r   UPIDr   )r+   r,   r-   r.   r/   r0   r1   routeroauth2_authorize_callbackrF   rp   r!   )r0   r,   rE   r1   r+   r/   r.   r"   get_oauth_router   sz   

5rx   authenticatoruser_schemarequires_verificationc                    s  t  }|jd|d}dj d durtd}	nt d}	|jddj d	td
tdt|fdtdt	t
 dtjdtf fdd}
|jd dtjtddddddiiididt|t|	t|fdtdtjdttt
f dttjtjf ffdd}|S )zKGenerate a router with the OAuth routes to associate an authenticated user.T)activeverifiedzoauth-associate:r3   Nr4   r5   r7   r8   r9   r<   r=   rn   r'   c                    sT   d ur}nt |  }dt |ji}t|}|||I d H }t|dS )Nsubr>   )r   r?   idr*   r@   r   )r<   r=   rn   rA   rB   rC   r   rD   r!   r"   rF      s   



z-get_oauth_associate_router.<locals>.authorizerG   rH   rI   rJ   rK   rL   rM   rQ   )r;   r:   rT   rU   rV   rW   c           	         s   |\}}  |d I d H \}}|d u rttjtjdz	t|tg}W n tj	y4   ttjdw |d t
|jkrDttjd|| j|d |||d|d| I d H }t|S )NrY   rZ   r\   r~   r]   r^   )r_   r   r   r`   r   ra   r   r)   rb   rc   r   r   oauth_associate_callbackr:   re   r   model_validate)	r<   rn   rV   rW   rk   rC   rl   rm   rB   )r+   r.   rz   r!   r"   rp      s8   
z,get_oauth_associate_router.<locals>.callback)r   current_userr:   r	   re   r   r   r   r   rq   r   r   rt   r   r`   r   rs   r   r   ru   )r+   ry   r-   rz   r.   r/   r{   rv   get_current_active_userrw   rF   rp   r!   )rE   r+   r/   r.   rz   r"   get_oauth_associate_router   s   

(r   )r#   )NFF)NF)1typingr   rb   fastapir   r   r   r   r   r    httpx_oauth.integrations.fastapir	   httpx_oauth.oauth2r
   r   pydanticr   fastapi_usersr   r   fastapi_users.authenticationr   r   r   fastapi_users.exceptionsr   fastapi_users.jwtr   r   r   fastapi_users.managerr   r   fastapi_users.router.commonr   r   r)   r   dictr   intr*   rt   ru   boolrx   typeUr   r!   r!   r!   r"   <module>   s     


 