o
    gi,                     @   s   d Z ddlZddlZddlZddlZddlZddlZddl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 eeZG dd	 d	Zed
krYejdeddd dS dS )a  
A license (or licence) is an official permission or permit to do, use, or own something
(as well as the document of that permission or permit). A license is granted by a party (licensor) to another party
(licensee) as an element of an agreement between those parties. In the case of a license issued by a government,
the license is obtained by applying for it. In the case of a private party, it is by a specific agreement,
usually in writing (such as a lease or other contract).

The simplest definition is "A license is a promise not to sue," because a license usually either permits
the licensed party to engage in an activity which is illegal, and subject to prosecution,
without the license (e.g. fishing, driving an automobile, or operating a broadcast radio or television station),
or it permits the licensed party to do something that would violate the rights of the licensing party
(e.g. make copies of a copyrighted work), which, without the license,
the licensed party could be sued, civilly, criminally, or both.
    N)hashes)serialization)padding)rsa)UUIDc                   @   s  e Zd ZU dZdZeje ed< dZ	eje ed< dZ
eje ed< dZeje ed< eeejd eejd fd	ejd
ejfddZedeejd ede eej  d eej  d d fdedejd	ejdedededdfddZedeje deje fddZedeje fddZedeje fdd Zedeje fd!d"Zedeje fd#d$Zed%ejdefd&d'ZdS )(Licensea  
    A license (or licence) is an official permission or permit to do, use, or own something
    (as well as the document of that permission or permit). A license is granted by a party (licensor) to another party
    (licensee) as an element of an agreement between those parties. In the case of a license issued by a government,
    the license is obtained by applying for it. In the case of a private party, it is by a specific agreement,
    usually in writing (such as a lease or other contract).
    N_LICENSE_COMPANY_LICENSE_USER_ID_LICENSE_VALID_FROM_IN_MS_LICENSE_VALID_UNTIL_IN_MSzprivate_key.pemzpublic_key.pemprivate_key_filepublic_key_filec                 C   s   t jddd}t| d}||jtjjtjj	t
 d W d    n1 s(w   Y  | }t|d}||jtjjtjjd W d    d S 1 sPw   Y  d S )Ni  i   )public_exponentkey_sizewb)encodingformatencryption_algorithm)r   r   )r   generate_private_keyopenwriteprivate_bytesr   EncodingPEMPrivateFormatPKCS8NoEncryption
public_keypublic_bytesPublicFormatSubjectPublicKeyInfo)r   r   private_keyfr    r#   H/home/ubuntu/.local/lib/python3.10/site-packages/borb/license/license.py_create_key_pair;   s,   
"zLicense._create_key_pair zlicense_key.jsonzJ/home/joris/Code/borb-license-key-private/borb-license-key-private-001.pem  i $companyoutput_fileuser_idvalid_from_in_msvalid_until_in_msreturnc                 C   s  || t j |d dt j |d dd}tj|dd }t|  }t	|d}	t
j|	 d d}
W d    n1 sDw   Y  |
|t t }t|}| |d< t	|d	}|tj|dd W d    d S 1 szw   Y  d S )
Nr'   %Y-%m-%d %H:%M:%S)anonymous_user_idr(   valid_from_as_strvalid_until_as_str   indentrb)passwordlicense_keyw)datetimefromtimestampstrftimejsondumpsencodehashlibsha256	hexdigestr   r   load_pem_private_keyreadsignr   PKCS1v15r   SHA256base64	b64encodedecoder   )r(   r)   r   r*   r+   r,   license_dictlicense_byteslicense_bytes_unsigned_hashr"   r!   license_bytes_signed_hash base64_license_bytes_signed_hashfhr#   r#   r$   _create_license\   s&   	
"zLicense._create_licensesc                 C   sH   | d u rd S zt tj| d d W S    tdt|   Y d S )Nr.   r'   z"Unable to convert %s to a datetime)intr9   strptime	timestamploggerwarningstr)rQ   r#   r#   r$   _datetime_str_to_ms   s   zLicense._datetime_str_to_msc                   C      t jS )z
        This function returns the company to which this License was granted
        :return:    the company to which this License was granted
        )r   r   r#   r#   r#   r$   get_company      zLicense.get_companyc                   C   rY   )z
        This function returns the user_id to which this License was granted
        :return:    the user_id to which this License was granted
        )r   r	   r#   r#   r#   r$   get_user_id   r[   zLicense.get_user_idc                   C   rY   )z
        This function returns the start of the validity period of this License
        :return:    the start of the validity period of this License
        )r   r
   r#   r#   r#   r$   get_valid_from_in_ms   r[   zLicense.get_valid_from_in_msc                   C   rY   )z
        This function returns the end of the validity period of this License
        :return:    the end of the validity period of this License
        )r   r   r#   r#   r#   r$   get_valid_until_in_ms   r[   zLicense.get_valid_until_in_mspath_to_license_filec              	   C   sv  |   stdt|   dS d}zt| d}t| }W d   n1 s)w   Y  W n   tdt|   Y dS |du rEdS |dd	 }|
d tj|dd		 }t| 	 }ttjd
 }|  srJ | sxJ |d fD ]}t|d}t| }	W d   n1 sw   Y  t|}
z|	|
|t t  |ddt_|ddt_t |ddt_!t |ddt_"t#t$j$% & d }tj!durtj!|krtdt| |ddf  dt_dt_dt_!dt_"W  dS tj"dur0tj"|k r0tdt| |ddf  dt_dt_dt_!dt_"W  dS W  dS    Y q}dS )z
        This function (attempts to) register a license (specified as a Path)
        :param path_to_license_file:    the license to register
        :return:                        True if the license is valid, False otherwise
        zFile %s does not existFNrz(Unable to convert contents of %s to JSONr7   r&   r2   r3   public_keyszpublic_key_001.pemr5   r(   r/   r0   r1   r'   zLicense %s is valid FROM %szN.A.zLicense %s is valid UNTIL %sT)'existsrU   rV   rW   r   r<   loadsrC   getr>   popr=   r?   r@   rA   pathlibPath__file__parentis_dirr   load_pem_public_keyrG   	b64decodeverifyr   rE   r   rF   r   r   r	   rX   r
   r   rR   r9   nowrT   )r_   	json_dictrO   license_hash_0license_bytes1license_hash_1key_dirr   r"   r   encrypted_message	now_in_msr#   r#   r$   register   st   	

zLicense.register) __name__
__module____qualname____doc__r   typingOptionalrW   __annotations__r	   r
   rR   r   staticmethodrf   rg   rh   ri   r%   r   rd   r9   rn   rT   rP   rX   rZ   r\   r]   r^   boolrv   r#   r#   r#   r$   r   #   sp   
 	

 
. r   __main__z	borb (EZ)zT/home/joris/Code/borb-dev/tests/license/artifacts_test_register_license/license.jsonzJoris Schellekens)r(   r)   r*   )rz   rG   r9   r<   loggingr{   r?   rf   cryptography.hazmat.primitivesr   r   )cryptography.hazmat.primitives.asymmetricr   r   borb.license.uuidr   	getLoggerrw   rU   r   rP   rg   r#   r#   r#   r$   <module>   s.   
 }
