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   }| rt|}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