o
    &zh                     @   s^   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eZ	G dd deZ
e
gZd	S )
    )HttpResponseRedirect)reverse)	urlencode)ProviderProviderAccountc                   @   s   e Zd ZdS )SAMLAccountN)__name__
__module____qualname__ r   r   h/var/www/html/kangema/venv/lib/python3.10/site-packages/allauth/socialaccount/providers/saml/provider.pyr      s    r   c                       s   e Zd ZdZdZdZeZdgddgdgdd	gd
gdgdZ fddZ	dd Z
dd Zdd Zdd Zdd ZdddZ  ZS )SAMLProvidersamlSAMLTz,urn:oasis:names:tc:SAML:attribute:subject-idz!urn:oid:0.9.2342.19200300.100.1.3zBhttp://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddressz'http://schemas.auth0.com/email_verifiedz?http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givennamezurn:oid:2.5.4.42zurn:oid:2.5.4.4z!http://schemas.auth0.com/nickname)uidemailemail_verified
first_name	last_nameusernamec                    s.   t  j|i | | jjp| jjp| j| _d S N)super__init__appname	client_id)selfargskwargs	__class__r   r   r   (   s   zSAMLProvider.__init__c                 K   s,   t dd| jjid}|r|d t| }|S )N
saml_loginorganization_slug)r   ?)r   r   r   r   )r   requestr   urlr   r   r   get_login_url,   s   zSAMLProvider.get_login_urlc                 C   s   |  S r   )get_attributes)r   datar   r   r   extract_extra_data2   s   zSAMLProvider.extract_extra_datac                 C   s$   |  |d}|du r| }|S )u  http://docs.oasis-open.org/security/saml-subject-id-attr/v1.0/csprd01/saml-subject-id-attr-v1.0-csprd01.html

        Quotes:

        "While the Attributes defined in this profile have as a goal the
        explicit replacement of the <saml:NameID> element as a means of subject
        identification, it is certainly possible to compose them with existing
        NameID usage provided the same subject is being identified. This can
        also serve as a migration strategy for existing applications."


        "SAML does not define an identifier that meets all of these
        requirements well. It does standardize a kind of NameID termed
        “persistent” that meets some of them in the particular case of so-called
        “pairwise” identification, where an identifier varies by relying
        party. It has seen minimal adoption outside of a few contexts, and fails
        at the “compact” and “simple to handle” criteria above, on top of the
        disadvantages inherent with all NameID usage."

        Overall, our strategy is to prefer a uid resulting from explicit
        attribute mappings, and only if there is no such uid fallback to the
        NameID.
        r   N)_extractget
get_nameid)r   r(   r   r   r   r   extract_uid5   s   zSAMLProvider.extract_uidc                 C   s   |  |}|dd  |S )Nr   )r*   pop)r   r(   retr   r   r   extract_common_fieldsR   s   
z"SAMLProvider.extract_common_fieldsc                 C   s   | j j}| }i }|d| j}| D ])\}}t|tr!|g}|D ]}||d }	|	d ur=t|	dkr=|	d ||<  nq#q|d}
|
rP|
	 dv }
|
|d< |dsg|
 dksa|ddrg| |d< |S )	Nattribute_mappingr   r   )true1tyyesr   z6urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddressuse_nameid_for_emailF)r   settingsr'   r+   default_attribute_mappingitems
isinstancestrlenlowerget_nameid_formatr,   )r   r(   provider_configraw_attributes
attributesr1   keyprovider_keysprovider_keyattribute_listr   r   r   r   r*   W   s6   



zSAMLProvider._extractNc           	      K   sJ   ddl m} ||| }|jdd}| j||||fd| i| t|S )Nr   )
build_auth )	return_tostate_id)*allauth.socialaccount.providers.saml.utilsrG   loginstash_redirect_stateget_last_request_idr   )	r   r$   processnext_urlr(   r   rG   authredirectr   r   r   rR   w   s   
zSAMLProvider.redirect)NN)r   r	   r
   idr   supports_redirectr   account_classr9   r   r&   r)   r-   r0   r*   rR   __classcell__r   r   r   r   r      s6     r   N)django.httpr   django.urlsr   django.utils.httpr   $allauth.socialaccount.providers.baser   r   r   r   provider_classesr   r   r   r   <module>   s    
}