U
    Tºc   ã                   @   sf   d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	 ddl
mZ e e¡ZG dd„ deƒZdS )	zŸ
oauthlib.openid.connect.core.endpoints.userinfo
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This module is an implementation of userinfo endpoint.
é    N)ÚRequest)Úerrors)ÚBaseEndpointÚcatch_errors_and_unavailability)ÚBearerTokenc                   @   s.   e Zd ZdZdd„ Zed
dd„ƒZdd	„ ZdS )ÚUserInfoEndpointz,Authorizes access to userinfo resource.
    c                 C   s$   t |d d d ƒ| _|| _t | ¡ d S )N)r   ÚbearerÚrequest_validatorr   Ú__init__)Úselfr	   © r   úS/tmp/pip-unpacked-wheel-dp7l75bz/oauthlib/openid/connect/core/endpoints/userinfo.pyr
      s    zUserInfoEndpoint.__init__ÚGETNc                 C   sÐ   t ||||ƒ}dg|_|  |¡ | j |¡}|dkrLt d|¡ tjdd‚t	|t
ƒrŠddi}d|kr~t d	|¡ tjdd‚t |¡}n0t	|tƒr¢dd
i}|}nt d|¡ tjdd‚t d|¡ ||dfS )ap  Validate BearerToken and return userinfo from RequestValidator

        The UserInfo Endpoint MUST return a
        content-type header to indicate which format is being returned. The
        content-type of the HTTP response MUST be application/json if the
        response body is a text JSON object; the response body SHOULD be encoded
        using UTF-8.
        ÚopenidNz!Userinfo MUST have claims for %r.iô  )Ústatus_codezContent-Typezapplication/jsonÚsubz Userinfo MUST have "sub" for %r.zapplication/jwtz(Userinfo return unknown response for %r.zUserinfo access valid for %r.éÈ   )r   ÚscopesÚvalidate_userinfo_requestr	   Zget_userinfo_claimsÚlogÚerrorr   ZServerErrorÚ
isinstanceÚdictÚjsonÚdumpsÚstrÚdebug)r   ÚuriZhttp_methodÚbodyÚheadersÚrequestZclaimsZresp_headersr   r   r   Úcreate_userinfo_response   s0    


 ÿ
 ÿz)UserInfoEndpoint.create_userinfo_responsec                 C   s*   | j  |¡st ¡ ‚d|jkr&t ¡ ‚dS )a»  Ensure the request is valid.

        5.3.1.  UserInfo Request
        The Client sends the UserInfo Request using either HTTP GET or HTTP
        POST. The Access Token obtained from an OpenID Connect Authentication
        Request MUST be sent as a Bearer Token, per `Section 2`_ of OAuth 2.0
        Bearer Token Usage [RFC6750].

        It is RECOMMENDED that the request use the HTTP GET method and the
        Access Token be sent using the Authorization header field.

        The following is a non-normative example of a UserInfo Request:

        .. code-block:: http

            GET /userinfo HTTP/1.1
            Host: server.example.com
            Authorization: Bearer SlAV32hkKG

        5.3.3. UserInfo Error Response
        When an error condition occurs, the UserInfo Endpoint returns an Error
        Response as defined in `Section 3`_ of OAuth 2.0 Bearer Token Usage
        [RFC6750]. (HTTP errors unrelated to RFC 6750 are returned to the User
        Agent using the appropriate HTTP status code.)

        The following is a non-normative example of a UserInfo Error Response:

        .. code-block:: http

            HTTP/1.1 401 Unauthorized
            WWW-Authenticate: Bearer error="invalid_token",
                error_description="The Access Token expired"

        .. _`Section 2`: https://datatracker.ietf.org/doc/html/rfc6750#section-2
        .. _`Section 3`: https://datatracker.ietf.org/doc/html/rfc6750#section-3
        r   N)r   Zvalidate_requestr   ZInvalidTokenErrorr   ZInsufficientScopeError)r   r    r   r   r   r   B   s    %
z*UserInfoEndpoint.validate_userinfo_request)r   NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   r!   r   r   r   r   r   r      s
   %r   )r%   r   ÚloggingZoauthlib.commonr   Zoauthlib.oauth2.rfc6749r   Z&oauthlib.oauth2.rfc6749.endpoints.baser   r   Zoauthlib.oauth2.rfc6749.tokensr   Ú	getLoggerr"   r   r   r   r   r   r   Ú<module>   s   
