o
    Df"                     @   sL   d dl Z d dlmZmZmZmZ G dd deZG dd deZdd Z	dS )	    N)MissingClaimErrorInvalidClaimErrorExpiredTokenErrorInvalidTokenErrorc                       sF   e Zd ZdZg Zd fdd	Zdd Zdd Zd	d
 Zdd Z	  Z
S )
BaseClaimsa  Payload claims for JWT, which contains a validate interface.

    :param payload: the payload dict of JWT
    :param header: the header dict of JWT
    :param options: validate options
    :param params: other params

    An example on ``options`` parameter, the format is inspired by
    `OpenID Connect Claims`_::

        {
            "iss": {
                "essential": True,
                "values": ["https://example.com", "https://example.org"]
            },
            "sub": {
                "essential": True
                "value": "248289761001"
            },
            "jti": {
                "validate": validate_jti
            }
        }

    .. _`OpenID Connect Claims`:
        http://openid.net/specs/openid-connect-core-1_0.html#IndividualClaimsRequests
    Nc                    s*   t  | || _|pi | _|pi | _d S N)super__init__headeroptionsparams)selfpayloadr
   r   r   	__class__ T/home/ubuntu/webapp/venv/lib/python3.10/site-packages/authlib/jose/rfc7519/claims.pyr	   (   s   
zBaseClaims.__init__c              
   C   sL   zt | |W S  ty% } z|| jv r| |W  Y d }~S |d }~ww r   )object__getattribute__AttributeErrorREGISTERED_CLAIMSget)r   keyerrorr   r   r   __getattr__.   s   
zBaseClaims.__getattr__c                 C   sB   | j D ]}| j | dr|| vrt|| |st|qd S )N	essential)r   r   r   r   )r   kr   r   r   _validate_essential_claims6   s   

z%BaseClaims._validate_essential_claimsc                 C   s   | j |}|s
d S | |}|d}|r||krt||d}|r-||vr-t||d}|r=|| |s?t|d S d S )Nvaluevaluesvalidate)r   r   r   )r   
claim_nameoptionr   option_valueoption_valuesr    r   r   r   _validate_claim_value>   s   



z BaseClaims._validate_claim_valuec                 C   s(   i }| j D ]}|| v r| | ||< q|S r   )r   )r   rvr   r   r   r   get_registered_claimsP   s   
z BaseClaims.get_registered_claims)NN)__name__
__module____qualname____doc__r   r	   r   r   r%   r'   __classcell__r   r   r   r   r   
   s    r   c                   @   sV   e Zd Zg dZdddZdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd ZdS )	JWTClaims)isssubaudexpnbfiatjtiNr   c                 C   s   |    |du rtt }|   |   |   | || | || | || | 	  | j
 D ]}|| jvrA| | q5dS )z&Validate everything in claims payload.N)r   inttimevalidate_issvalidate_subvalidate_audvalidate_expvalidate_nbfvalidate_iatvalidate_jtir   keysr   r%   )r   nowleewayr   r   r   r   r    [   s   

zJWTClaims.validatec                 C      |  d dS )a  The "iss" (issuer) claim identifies the principal that issued the
        JWT.  The processing of this claim is generally application specific.
        The "iss" value is a case-sensitive string containing a StringOrURI
        value.  Use of this claim is OPTIONAL.
        r.   Nr%   r   r   r   r   r7   o   s   zJWTClaims.validate_issc                 C   rA   )a  The "sub" (subject) claim identifies the principal that is the
        subject of the JWT.  The claims in a JWT are normally statements
        about the subject.  The subject value MUST either be scoped to be
        locally unique in the context of the issuer or be globally unique.
        The processing of this claim is generally application specific.  The
        "sub" value is a case-sensitive string containing a StringOrURI
        value.  Use of this claim is OPTIONAL.
        r/   NrB   rC   r   r   r   r8   w   s   	zJWTClaims.validate_subc                    s   | j d}| d}|r|sdS |d}|s"|d}|r"|g}|s&dS t| d tr2| d  n| d g t fdd|D sFtddS )a  The "aud" (audience) claim identifies the recipients that the JWT is
        intended for.  Each principal intended to process the JWT MUST
        identify itself with a value in the audience claim.  If the principal
        processing the claim does not identify itself with a value in the
        "aud" claim when this claim is present, then the JWT MUST be
        rejected.  In the general case, the "aud" value is an array of case-
        sensitive strings, each containing a StringOrURI value.  In the
        special case when the JWT has one audience, the "aud" value MAY be a
        single case-sensitive string containing a StringOrURI value.  The
        interpretation of audience values is generally application specific.
        Use of this claim is OPTIONAL.
        r0   Nr   r   c                    s   g | ]}| v qS r   r   ).0vaud_listr   r   
<listcomp>   s    z*JWTClaims.validate_aud.<locals>.<listcomp>)r   r   
isinstancelistanyr   )r   
aud_optionr0   
aud_values	aud_valuer   rF   r   r9      s"   




zJWTClaims.validate_audc                 C   s:   d| v r| d }t |std||| k rt dS dS )a  The "exp" (expiration time) claim identifies the expiration time on
        or after which the JWT MUST NOT be accepted for processing.  The
        processing of the "exp" claim requires that the current date/time
        MUST be before the expiration date/time listed in the "exp" claim.
        Implementers MAY provide for some small leeway, usually no more than
        a few minutes, to account for clock skew.  Its value MUST be a number
        containing a NumericDate value.  Use of this claim is OPTIONAL.
        r1   N)_validate_numeric_timer   r   )r   r?   r@   r1   r   r   r   r:         	zJWTClaims.validate_expc                 C   s:   d| v r| d }t |std||| krt dS dS )a  The "nbf" (not before) claim identifies the time before which the JWT
        MUST NOT be accepted for processing.  The processing of the "nbf"
        claim requires that the current date/time MUST be after or equal to
        the not-before date/time listed in the "nbf" claim.  Implementers MAY
        provide for some small leeway, usually no more than a few minutes, to
        account for clock skew.  Its value MUST be a number containing a
        NumericDate value.  Use of this claim is OPTIONAL.
        r2   NrO   r   r   )r   r?   r@   r2   r   r   r   r;      rP   zJWTClaims.validate_nbfc                 C   s>   d| v r| d }t |std||| krtdddS dS )ax  The "iat" (issued at) claim identifies the time at which the JWT was
        issued.  This claim can be used to determine the age of the JWT.
        Implementers MAY provide for some small leeway, usually no more
        than a few minutes, to account for clock skew. Its value MUST be a
        number containing a NumericDate value.  Use of this claim is OPTIONAL.
        r3   z5The token is not valid as it was issued in the future)descriptionNrQ   )r   r?   r@   r3   r   r   r   r<      s   zJWTClaims.validate_iatc                 C   rA   )aE  The "jti" (JWT ID) claim provides a unique identifier for the JWT.
        The identifier value MUST be assigned in a manner that ensures that
        there is a negligible probability that the same value will be
        accidentally assigned to a different data object; if the application
        uses multiple issuers, collisions MUST be prevented among values
        produced by different issuers as well.  The "jti" claim can be used
        to prevent the JWT from being replayed.  The "jti" value is a case-
        sensitive string.  Use of this claim is OPTIONAL.
        r4   NrB   rC   r   r   r   r=      s   
zJWTClaims.validate_jti)Nr   )r(   r)   r*   r   r    r7   r8   r9   r:   r;   r<   r=   r   r   r   r   r-   X   s    
#r-   c                 C   s   t | ttfS r   )rI   r5   float)sr   r   r   rO      s   rO   )
r6   authlib.jose.errorsr   r   r   r   dictr   r-   rO   r   r   r   r   <module>   s    N 