o
    b                     @   s\   d dl Z d dlmZ d dlmZmZmZmZ ddlm	Z	 G dd de
ZG dd	 d	eZdS )
    N)OrderedDict)
json_dumpsto_bytes
to_unicodeurlsafe_b64encode   )InvalidUseErrorc                       s   e Zd ZdZdZg dZg dZg dZg Ze	Z
 fddZdd	 Zd
d Zdd Zdd Zdd Zd"ddZdd Zdd Zdd Zedd Zed#ddZed$d d!Z  ZS )%Keyz*This is the base class for a JSON Web Key._)usekey_opsalgkidZx5uZx5cZx5tzx5t#S256)signdecrypt	unwrapKey)verifyencryptwrapKeyc                    s    t t| | d| _d | _d S )Nsecret)superr	   __init__key_typeraw_key)selfpayload	__class__ Z/var/www/secure340b-portal/env/lib/python3.10/site-packages/authlib/jose/rfc7517/models.pyr      s   
zKey.__init__c                 C   s$   |  | || jv r|  S |  S )zGet the raw key for the given key_op. This method will also
        check if the given key_op is supported by this key.

        :param operation: key operation value, such as "sign", "encrypt".
        :return: raw key
        )check_key_opPUBLIC_KEY_OPSget_public_keyget_private_key)r   	operationr   r   r   
get_op_key%   s   

zKey.get_op_keyc                 C   s   | j dkr
| j S | jS Nprivate)r   r   Z
public_keyr   r   r   r   r"   1   s   

zKey.get_public_keyc                 C   s   | j dkr| jS d S r&   )r   r   r(   r   r   r   r#   6   s   
zKey.get_private_keyc                 C   s   |  d}|dur||vrtd||| jv r%| jdkr%td||  d}|rD|dv r9|dkr7t dS |d	v rF|d
krHt dS dS dS )zCheck if the given key_op is supported by this key.

        :param operation: key operation value, such as "sign", "encrypt".
        :raise: ValueError
        r   NzUnsupported key_op "{}"publicz"Invalid key_op "{}" for public keyr   )r   r   sig)r   r   r   r   enc)get
ValueErrorformatPRIVATE_KEY_OPSr   r   )r   r$   r   r   r   r   r   r    :   s"   

zKey.check_key_opc                 C   s   | j S )zRepresent this key as raw key.)r   r(   r   r   r   as_keyP      z
Key.as_keyFc                 C   s.   t | }| j|d< |rd|vr|  |d< |S )z1Represent this key as a dict of the JSON Web Key.ktyr   )dictr2   
thumbprint)r   Zadd_kidobjr   r   r   as_dictT   s
   
zKey.as_dictc                 C      |   }t|S )z$Represent this key as a JSON string.r6   r   r   r5   r   r   r   as_json\      zKey.as_jsonc                 C   s   t d)z+Represent this key as string in PEM format.zNot supported)RuntimeErrorr(   r   r   r   as_pema   s   z
Key.as_pemc                 C   sf   t | j}|d |  t }|  }|D ]}|| ||< qt|}tt	|
 }tt|S )z8Implementation of RFC7638 JSON Web Key (JWK) Thumbprint.r2   )listREQUIRED_JSON_FIELDSappendsortr   r6   r   hashlibsha256r   digestr   r   )r   fieldsdatar5   kZ	json_dataZdigest_datar   r   r   r4   e   s   

zKey.thumbprintc                 C   s&   | j D ]}||vrtd|qd S )NzMissing required field: "{}")r?   r-   r.   )clsrF   rG   r   r   r   check_required_fieldst   s
   
zKey.check_required_fieldsNc                 C      t  NNotImplementedError)rH   Zcrv_or_sizeoptions
is_privater   r   r   generate_keyz   r1   zKey.generate_keyc                 C   rJ   rK   rL   )rH   rawrN   r   r   r   
import_key~   r1   zKey.import_key)F)NFrK   )__name__
__module____qualname____doc__r2   ZALLOWED_PARAMSr/   r!   r?   bytesZRAW_KEY_CLSr   r%   r"   r#   r    r0   r6   r:   r=   r4   classmethodrI   rP   rR   __classcell__r   r   r   r   r	      s0    

r	   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )KeySetz)This class represents a JSON Web Key Set.c                 C   s
   || _ d S rK   keys)r   r\   r   r   r   r      s   
zKeySet.__init__c                 C   s   ddd | j D iS )z5Represent this key as a dict of the JSON Web Key Set.r\   c                 S   s   g | ]}| d qS )T)r6   ).0rG   r   r   r   
<listcomp>   s    z"KeySet.as_dict.<locals>.<listcomp>r[   r(   r   r   r   r6      s   zKeySet.as_dictc                 C   r7   )z(Represent this key set as a JSON string.r8   r9   r   r   r   r:      r;   zKeySet.as_jsonc                 C   s*   | j D ]}|d|kr|  S qtd)zFind the key matches the given kid value.

        :param kid: A string of kid
        :return: Key instance
        :raise: ValueError
        r   zInvalid JSON Web Key Set)r\   r,   r-   )r   r   rG   r   r   r   find_by_kid   s
   
zKeySet.find_by_kidN)rS   rT   rU   rV   r   r6   r:   r_   r   r   r   r   rZ      s    rZ   )rB   collectionsr   Zauthlib.common.encodingr   r   r   r   errorsr   r3   r	   objectrZ   r   r   r   r   <module>   s    w