U
    ù¹cB  ã                   @   sl   d dl mZmZ d dlZd dlZd dlZd dlZddlmZm	Z	m
Z
mZ dd„ Zdd„ Zd	d
„ Zdd„ ZdS )é    )Úabsolute_importÚprint_functionNé   )ÚDecodeErrorÚ__version__ÚdecodeÚencodec              	   C   sæ   | j d krtdƒ‚i }| jD ]¬}| dd¡\}}|dkrp|d dkrpt|ƒdkrpttt ¡ t|dd … ƒ ƒƒ}| ¡ r‚t|ƒ}n"zt	|ƒ}W n tk
r¢   Y nX ddd d	œ}||krÀ|| }|||< qt
|| j | jd
�}| d¡S )Nz4Key is required when encoding. See --help for usage.ú=r   Úexpr   ú+TF)ÚtrueÚfalseÚnull)ÚkeyÚ	algorithmúutf-8)r   Ú
ValueErrorÚpayloadÚsplitÚlenÚstrÚintÚtimeÚisdigitÚfloatr   r   r   )Úargsr   ÚargÚkÚvÚ	constantsÚtoken© r!   ú0/tmp/pip-unpacked-wheel-eh6jf6x0/jwt/__main__.pyÚencode_payload   s.    

  

ýr#   c              
   C   sŒ   zX| j r| j }n"tj ¡ r*tj ¡  ¡ }ntdƒ‚| d¡}t|| j	| j
d�}t |¡W S  tk
r† } ztd| ƒ‚W 5 d }~X Y nX d S )Nz*Cannot read from stdin: terminal not a TTYr   )r   Úverifyz)There was an error decoding the token: %s)r    ÚsysÚstdinÚisattyÚreadlineÚstripÚIOErrorr   r   r   r$   ÚjsonÚdumpsr   )r   r    ÚdataÚer!   r!   r"   Údecode_payload7   s    

r/   c                  C   sÒ   d} t jd| d�}|jddddt d� |jd	d
dd dd� |jdddddd� |jdddd�}|jddd�}d}|jdd|d� |jtd� |jdd d�}|jd!d"d#d$� |jd%d&d'd(d)d*d+� |jtd� |S ),Na  
    Encodes or decodes JSON Web Tokens based on input.

    %(prog)s [options] <command> [options] input

    Decoding examples:

    %(prog)s --key=secret decode json.web.token
    %(prog)s decode --no-verify json.web.token

    Encoding requires the key option and takes space separated key/value pairs
    separated by equals (=) as input. Examples:

    %(prog)s --key=secret encode iss=me exp=1302049071
    %(prog)s --key=secret encode foo=bar exp=+10

    The exp key is special and can take an offset to current Unix time.
    Zpyjwt)ÚprogÚusagez-vz	--versionÚversionz	%(prog)s )Úactionr2   z--keyr   ZKEYzset the secret key to sign with)ÚdestÚmetavarÚdefaultÚhelpz--algr   ZALGZHS256z0set crypto algorithm to sign with. default=HS256zPyJWT subcommandszvalid subcommandszadditional help)ÚtitleÚdescriptionr7   r   z use to encode a supplied payload)r7   zfPayload to encode. Must be a space separated list of key/value
    pairs separated by equals (=) sign.r   r   )Únargsr7   )Úfuncr   z'use to decode a supplied JSON web tokenr    zJSON web token to decode.ú?)r7   r:   z-nz--no-verifyÚstore_falser$   Tz2ignore signature and claims verification on decode)r3   r4   r6   r7   )	ÚargparseÚArgumentParserÚadd_argumentr   Úadd_subparsersÚ
add_parserÚset_defaultsr#   r/   )r1   Ú
arg_parserZ
subparsersZencode_parserZpayload_helpZdecode_parserr!   r!   r"   Úbuild_argparserJ   sd    þ ýûûýý ûrE   c               
   C   sj   t ƒ } z*|  tjdd … ¡}| |¡}t|ƒ W n4 tk
rd } ztd|ƒ |  ¡  W 5 d }~X Y nX d S )Nr   zThere was an unforseen error: )rE   Ú
parse_argsr%   Úargvr;   ÚprintÚ	ExceptionÚ
print_help)rD   Ú	argumentsÚoutputr.   r!   r!   r"   Úmain�   s    

rM   )Ú
__future__r   r   r>   r+   r%   r   Ú r   r   r   r   r#   r/   rE   rM   r!   r!   r!   r"   Ú<module>   s   *S