a
    xd}@                     @   s   d Z g dZddlmZ ddlZddlZdZdZdZdZ	d	Z
ed
ZdZdZG dd deZG dd deZee_G dd deZee_dS )zAn implementation of the OpenID Provider Authentication Policy
Extension 1.0, Draft 5

@see: http://openid.net/developers/specs/

@since: 2.1.0
)RequestResponsens_uriAUTH_PHISHING_RESISTANTAUTH_MULTI_FACTORAUTH_MULTI_FACTOR_PHYSICALLEVELS_NISTLEVELS_JISA    )	ExtensionNz+http://specs.openid.net/extensions/pape/1.0zEhttp://schemas.openid.net/pape/policies/2007/06/multi-factor-physicalz<http://schemas.openid.net/pape/policies/2007/06/multi-factorzBhttp://schemas.openid.net/pape/policies/2007/06/phishing-resistantz4http://schemas.openid.net/pape/policies/2007/06/nonez$^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\dZ$zDhttp://csrc.nist.gov/publications/nistpubs/800-63/SP800-63V1_0_2.pdfz*http://www.jisa.or.jp/spec/auth_level.htmlc                   @   s8   e Zd ZeedZdd ZdddZdd Zd	d
 Z	dS )PAPEExtension)ZnistZjisac                 C   s   | j  | _d S N)_default_auth_level_aliasescopyauth_level_aliasesself r   U/var/www/html/Ranjet/env/lib/python3.9/site-packages/openid/extensions/draft/pape5.py__init__/   s    zPAPEExtension.__init__Nc                 C   sl   |du r4z|  |}W q^ ty0   |  }Y q^0 n*| j|}|dur^||kr^td||||| j|< dS )a&  Add an auth level URI alias to this request.

        @param auth_level_uri: The auth level URI to send in the
            request.

        @param alias: The namespace alias to use for this auth level
            in this message. May be None if the alias is not
            important.
        Nz-Attempting to redefine alias %r from %r to %r)	_getAliasKeyError_generateAliasr   getr   auth_level_urialiasZexisting_urir   r   r   _addAuthLevelAlias2   s    
z PAPEExtension._addAuthLevelAliasc                 C   s6   t dD ] }d|f }|| jvr|  S qtddS )z!Return an unused auth level aliasi  zcust%dz,Could not find an unused alias (tried 1000!)N)ranger   RuntimeError)r   ir   r   r   r   r   I   s
    


zPAPEExtension._generateAliasc                 C   s0   | j  D ]\}}||kr
|  S q
t|dS )zmReturn the alias for the specified auth level URI.

        @raises KeyError: if no alias is defined
        N)r   itemsr   r   r   r   r   r   R   s    
zPAPEExtension._getAlias)N)
__name__
__module____qualname__r   r   r   r   r   r   r   r   r   r   r   r   )   s   
	r   c                       sj   e Zd ZdZdZd fdd	Zdd Zdd	 Zdd
dZdd Z	dd Z
ee
Z
dddZdd Z  ZS )r   aD  A Provider Authentication Policy request, sent from a relying
    party to a provider

    @ivar preferred_auth_policies: The authentication policies that
        the relying party prefers
    @type preferred_auth_policies: [str]

    @ivar max_auth_age: The maximum time, in seconds, that the relying
        party wants to allow to have elapsed before the user must
        re-authenticate
    @type max_auth_age: int or NoneType

    @ivar preferred_auth_level_types: Ordered list of authentication
        level namespace URIs

    @type preferred_auth_level_types: [str]
    papeNc                    sL   t t|   |d u rg }|| _|| _g | _|d urH|D ]}| | q8d S r   )superr   r   preferred_auth_policiesmax_auth_agepreferred_auth_level_typesaddAuthLevel)r   r&   r'   r(   Z
auth_level	__class__r   r   r   s   s    zRequest.__init__c                 C   s   t | jp| jd up| jS r   )boolr&   r'   r(   r   r   r   r   __bool__   s
    zRequest.__bool__c                 C   s   || j vr| j | dS )a  Add an acceptable authentication policy URI to this request

        This method is intended to be used by the relying party to add
        acceptable authentication types to the request.

        @param policy_uri: The identifier for the preferred type of
            authentication.
        @see: http://openid.net/specs/openid-provider-authentication-policy-extension-1_0-05.html#auth_policies
        N)r&   appendr   Z
policy_urir   r   r   addPolicyURI   s    

zRequest.addPolicyURIc                 C   s&   |  || || jvr"| j| d S r   )r   r(   r.   )r   r   r   r   r   r   r)      s    
zRequest.addAuthLevelc                 C   sr   dd | ji}| jdur(t| j|d< | jrng }| jD ]&}| |}||d|f < || q8d ||d< |S )/@see: C{L{Extension.getExtensionArgs}}
        r&    Nr'   auth_level.ns.%sr(   )joinr&   r'   strr(   r   r.   )r   ns_argsZpreferred_typesr   r   r   r   r   getExtensionArgs   s    


zRequest.getExtensionArgsc                 C   s:   |  }|j |j}|j  }|i kr*dS ||| |S )zaInstantiate a Request object from the arguments in a
        C{checkid_*} OpenID message
        N)messageZgetArgsr   	isOpenID1parseExtensionArgs)clsrequestr   args
is_openid1r   r   r   fromOpenIDRequest   s    
zRequest.fromOpenIDRequestFc              	   C   s"  g | _ |d}|rPt|tr*t|dd}|dD ]}|| j vr4| j | q4|d}d| _|rzt|| _W n t	y   |r Y n0 |d}|r|
  }|D ]p}	d|	f }
z||
 }W n( ty   |r| j|	}nd}Y n0 |du r|rt	d	|	f q| ||	 qdS )
a-  Set the state of this request to be that expressed in these
        PAPE arguments

        @param args: The PAPE arguments without a namespace

        @param strict: Whether to raise an exception if the input is
            out of spec or otherwise malformed. If strict is false,
            malformed input will be ignored.

        @param is_openid1: Whether the input should be treated as part
            of an OpenID1 request

        @rtype: None

        @raises ValueError: When the max_auth_age is not parseable as
            an integer
        r&   zutf-8)encodingr2   r'   Nr(   r3   z6preferred auth level %r is not defined in this message)r&   r   
isinstancebytesr5   splitr.   r'   int
ValueErrorstripr   r   r)   )r   r=   r>   strictpolicies_struriZmax_auth_age_strr(   aliasesr   keyr   r   r   r:      sB    







zRequest.parseExtensionArgsc                 C   s   t t| jj|S )a  Given a list of authentication policy URIs that a provider
        supports, this method returns the subsequence of those types
        that are preferred by the relying party.

        @param supported_types: A sequence of authentication policy
            type URIs that are supported by a provider

        @returns: The sub-sequence of the supported types that are
            preferred by the relying party. This list will be ordered
            in the order that the types appear in the supported_types
            sequence, and may be empty if the provider does not prefer
            any of the supported authentication types.

        @returntype: [str]
        )listfilterr&   __contains__)r   Zsupported_typesr   r   r   preferredTypes   s    zRequest.preferredTypes)NNN)N)F)r!   r"   r#   __doc__ns_aliasr   r-   r0   r)   r7   r?   classmethodr:   rO   __classcell__r   r   r*   r   r   ^   s      

?r   c                       sv   e Zd ZdZdZd fdd	ZdddZdd	 Zd
d Ze	eddZ
dd Zdd ZdddZeeZdd Z  ZS )r   zA Provider Authentication Policy response, sent from a provider
    to a relying party

    @ivar auth_policies: List of authentication policies conformed to
        by this OpenID assertion, represented as policy URIs
    r$   Nc                    sZ   t t|   |r|| _ng | _|| _i | _|d u r8i }| D ]\}}| || q@d S r   )r%   r   r   auth_policies	auth_timeauth_levelsr    setAuthLevel)r   rT   rU   rV   rI   levelr*   r   r   r      s    zResponse.__init__c                 C   s   |  || || j|< dS )a  Set the value for the given auth level type.

        @param level: string representation of an authentication level
            valid for level_uri

        @param alias: An optional namespace alias for the given auth
            level URI. May be omitted if the alias is not
            significant. The library will use a reasonable default for
            widely-used auth level types.
        N)r   rV   )r   	level_urirX   r   r   r   r   rW   0  s    zResponse.setAuthLevelc                 C   s
   | j | S )a  Return the auth level for the specified auth level
        identifier

        @returns: A string that should map to the auth levels defined
            for the auth level type

        @raises KeyError: If the auth level type is not present in
            this message
        )rV   )r   rY   r   r   r   getAuthLevel>  s    
zResponse.getAuthLevelc                 C   s*   zt | tW S  ty$   Y d S 0 d S r   )rD   rZ   r   r   r   r   r   r   _getNISTAuthLevelJ  s    zResponse._getNISTAuthLevelz7Backward-compatibility accessor for the NIST auth level)docc                 C   s*   |t krtd|| jvr&| j| dS )a  Add a authentication policy to this response

        This method is intended to be used by the provider to add a
        policy that the provider conformed to when authenticating the user.

        @param policy_uri: The identifier for the preferred type of
            authentication.
        @see: http://openid.net/specs/openid-provider-authentication-policy-extension-1_0-01.html#auth_policies
        z4To send no policies, do not set any on the response.N)	AUTH_NONEr   rT   r.   r/   r   r   r   r0   T  s    

zResponse.addPolicyURIc                 C   s:   |  }| |j}| }|dur2||| |S dS dS )a9  Create a C{L{Response}} object from a successful OpenID
        library response
        (C{L{openid.consumer.consumer.SuccessResponse}}) response
        message

        @param success_response: A SuccessResponse from consumer.complete()
        @type success_response: C{L{openid.consumer.consumer.SuccessResponse}}

        @rtype: Response or None
        @returns: A provider authentication policy response from the
            data that was supplied with the C{id_res} response or None
            if the provider sent no signed PAPE response arguments.
        N)ZgetSignedNSr   r9   r:   )r;   Zsuccess_responser   r=   r>   r   r   r   fromSuccessResponsee  s    zResponse.fromSuccessResponseFc              	   C   sb  | d}|r|d}n|r(tdng }t|dkrR|rRt|v rRtd|f d|v rzd}|rlt|ntj|dd	 d
d |D }|| _| D ]\}}|	dr|dd }	|		drqz|d|	f  }
W n( t
y   |r| j |	}
nd}
Y n0 |
du r|r*td|	f q| |
||	 q| d}|r^t|rP|| _n|r^tddS )a  Parse the provider authentication policy arguments into the
        internal state of this object

        @param args: unqualified provider authentication policy
            arguments

        @param strict: Whether to raise an exception when bad data is
            encountered

        @returns: None. The data is parsed into the internal fields of
            this object.
        rT   r2   zMissing auth_policies   z=Got some auth policies, as well as the special "none" URI: %rnonez0"none" used as a policy URI (see PAPE draft < 5)   )
stacklevelc                 S   s   g | ]}|d t fvr|qS )r`   )r]   ).0ur   r   r   
<listcomp>  s   z/Response.parseExtensionArgs.<locals>.<listcomp>zauth_level.   Nzns.r3   zUndefined auth level alias: %rrU   #auth_time must be in RFC3339 format)r   rC   rE   lenr]   warningswarnrT   r    
startswithr   r   rW   TIME_VALIDATORmatchrU   )r   r=   r>   rG   rH   rT   msgrK   valr   rI   rU   r   r   r   r:     sT    







zResponse.parseExtensionArgsc                 C   s   t | jdkrdti}ndd| ji}| j D ]2\}}| |}||d|f < t||d|f < q2| jdurt	
| jstd| j|d< |S )	r1   r	   rT   r2   r3   zauth_level.%sNrg   rU   )rh   rT   r]   r4   rV   r    r   r5   rU   rl   rm   rE   )r   r6   Z
level_typerX   r   r   r   r   r7     s    


zResponse.getExtensionArgs)NNN)N)F)r!   r"   r#   rP   rQ   r   rW   rZ   r[   propertyZnist_auth_levelr0   r^   r:   rR   r7   rS   r   r   r*   r   r     s   

Dr   )rP   __all__Zopenid.extensionr
   ri   rer   r   r   r   r]   compilerl   r   r   r   r   r   r   r   r   r   <module>   s.   
5 6 L