o
    Lf                     @   s   d dl 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 d d
lmZ ddlmZ dd Zdd Zdd Zdd Zdd ZdddZdd ZdS )    )	parse_qslurlparse)cache)ImproperlyConfigured)Http404)reverse	urlencode)OneLogin_Saml2_Constants) OneLogin_Saml2_IdPMetadataParser)get_adapter)	SocialApp   )SAMLProviderc                 C   s:   t  }z
|j| tj|dW S  tjy   td| w )N)provider	client_idz"no SocialApp found with client_id=)r   get_appr   idr   DoesNotExistr   )requestorganization_slugadapter r   k/var/www/html/humari/django-venv/lib/python3.10/site-packages/allauth/socialaccount/providers/saml/utils.pyget_app_or_404   s   r   c                 C   s8   |   rdnd| jd | jd | j | j d}|S )Nonoff	HTTP_HOST	PATH_INFO)https	http_hostscript_nameget_data	post_data)	is_secureMETAGETcopyPOST)r   resultr   r   r   prepare_django_request   s   r*   c                 C   s   |  td|gd}|  td|gd}|  td|gd}||tjd|tjdd}|di }|dd ur?|d |d< |d	rJ|d	 |d
< |dd urW|d |d< |dd urd|d |d< |S )Nsaml_acs)argssaml_slssaml_metadata)urlbinding)entityIdassertionConsumerServicesingleLogoutServiceadvancedx509certx509cert_newx509certNewprivate_key
privateKeyname_id_formatNameIDFormat)build_absolute_urir   r
   BINDING_HTTP_POSTBINDING_HTTP_REDIRECTget)r   provider_configorgacs_urlsls_urlmetadata_url	sp_configavdr   r   r   build_sp_config(   s*   
rG   c                 C   sd   | d }| d }d| d| }t |}|d u r0tj||| ddd}t ||| dd	 |S )
NrD   	entity_idzsaml.metadata..metadata_request_timeout
   )rH   timeoutmetadata_cache_timeouti@8  )r   r?   r   parse_remoteset)
idp_configrD   rH   	cache_keysaml_configr   r   r   fetch_metadata_url_configG   s    


rS   c                 C   s  | di }i d| ddd| dtjd| ddd	| d
dddd| dtjd| ddd| ddd| ddd| ddd| ddd| ddd| ddd| ddd| d dd!| d"dd#| d$d}| d%d|d&}| d'}|r||d(< | d)}|r||d)< | d*}|d u rtd+| d,}	|	rt|}
|
d* |d*< n|d- |d. d/|d0 id1|d*< | d2}|rd/|i|d* d3< t| |||d4< |S )5Nr4   authnRequestsSignedauthn_request_signedFdigestAlgorithmdigest_algorithmlogoutRequestSignedlogout_request_signedlogoutResponseSignedlogout_response_signedrequestedAuthnContextsignatureAlgorithmsignature_algorithmsignMetadatametadata_signedwantAssertionsEncryptedwant_assertion_encryptedwantAssertionsSignedwant_assertion_signedwantMessagesSignedwant_message_signednameIdEncryptedname_id_encryptedwantNameIdEncryptedwant_name_id_encryptedallowSingleLabelDomainsallow_single_label_domainsrejectDeprecatedAlgorithmreject_deprecated_algorithmT
wantNameIdwant_name_idwantAttributeStatementwant_attribute_statementallowRepeatAttributeNameallow_repeat_attribute_namestrict)ru   securitycontact_personcontactPersonorganizationidpz`idp` missingrD   rH   r5   r/   sso_url)r1   r5   singleSignOnServiceslo_urlr3   sp)r?   r
   SHA256
RSA_SHA256r   rS   rG   )r   r@   rA   rF   security_configrR   rw   ry   rz   rD   meta_configr}   r   r   r   build_saml_configZ   s~   	








r   Nc                 C   s$   i }| r| |d< |r||d< t |S )Nprocessnextr   )r   next_urlparamsr   r   r   encode_relay_state   s   r   c                 C   sJ   i }| r#t | }|js|js|jr|jdr| |d< |S tt| }|S )zAccording to the spec, RelayState need not be a URL, yet,
    ``onelogin.saml2` exposes it as ``return_to -- The target URL the user
    should be redirected to after login``. Also, for an IdP initiated login
    sometimes a URL is used.
    /r   )r   schemenetlocpath
startswithdictr   )relay_stateretpartsr   r   r   decode_relay_state   s   r   )NN)urllib.parser   r   django.core.cacher   django.core.exceptionsr   django.httpr   django.urlsr   django.utils.httpr	   onelogin.saml2.constantsr
   "onelogin.saml2.idp_metadata_parserr   allauth.socialaccount.adapterr   allauth.socialaccount.modelsr   r   r   r   r*   rG   rS   r   r   r   r   r   r   r   <module>   s$    

9	