U
    b[B                     @   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   Zexpr   +TF)trueZfalseZnull)key	algorithmutf-8)r   
ValueErrorpayloadsplitlenstrinttimeisdigitfloatr   r   r   )argsr   argkvZ	constantstoken r   ./usr/lib/python3/dist-packages/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 )actionr.   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)titleZdescriptionr3   r   z use to encode a supplied payload)r3   zfPayload to encode. Must be a space separated list of key/value
    pairs separated by equals (=) sign.r   r
   )nargsr3   )funcr   z'use to decode a supplied JSON web tokenr   zJSON web token to decode.?)r3   r5   z-nz--no-verifyZstore_falser    Tz2ignore signature and claims verification on decode)r/   r0   r2   r3   )	argparseArgumentParseradd_argumentr   Zadd_subparsersZ
add_parserZset_defaultsr   r+   )r-   
arg_parserZ
subparsersZencode_parserZpayload_helpZdecode_parserr   r   r   build_argparserJ   sd      r<   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: )r<   
parse_argsr!   argvr6   print	ExceptionZ
print_help)r;   Z	argumentsoutputr*   r   r   r   main   s    

rB   )Z
__future__r   r   r8   r'   r!   r    r   r   r   r   r   r+   r<   rB   r   r   r   r   <module>   s   *S