ó
‚¾^Yc           @   sC   d  Z  d d l Z d d l Z d d l Z d d „ Z d „  Z d S(   sm   
Utility functions for implementing Proof Key for Code Exchange (PKCE) by OAuth
Public Clients

See RFC7636.
iÿÿÿÿNi@   c         C   sk   t  j t j |  ƒ ƒ j d ƒ } t | ƒ d k  rB t d ƒ ‚ n% t | ƒ d k rc t d ƒ ‚ n | Sd S(   sŸ  
    Generates a 'code_verifier' as described in section 4.1 of RFC 7636.

    This is a 'high-entropy cryptographic random string' that will be
    impractical for an attacker to guess.

    Args:
        n_bytes: integer between 31 and 96, inclusive. default: 64
            number of bytes of entropy to include in verifier.

    Returns:
        Bytestring, representing urlsafe base64-encoded random data.
    t   =i+   s)   Verifier too short. n_bytes must be > 30.i€   s(   Verifier too long. n_bytes must be < 97.N(   t   base64t   urlsafe_b64encodet   ost   urandomt   rstript   lent
   ValueError(   t   n_bytest   verifier(    (    s8   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/_pkce.pyt   code_verifier   s    !c         C   s+   t  j |  ƒ j ƒ  } t j | ƒ j d ƒ S(   s“  
    Creates a 'code_challenge' as described in section 4.2 of RFC 7636
    by taking the sha256 hash of the verifier and then urlsafe
    base64-encoding it.

    Args:
        verifier: bytestring, representing a code_verifier as generated by
            code_verifier().

    Returns:
        Bytestring, representing a urlsafe base64-encoded sha256 hash digest,
            without '=' padding.
    R    (   t   hashlibt   sha256t   digestR   R   R   (   R	   R   (    (    s8   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/_pkce.pyt   code_challenge4   s    (   t   __doc__R   R   R   R
   R   (    (    (    s8   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/_pkce.pyt   <module>   s
   