← Back
Editing: consumer.cpython-311.pyc
� QT�^�+ � �� � d Z ddlZddlZddlmZmZmZ ddlmZ ddl m Z mZmZm Z mZmZ ddlmZmZmZmZmZmZmZ ddlmZ ddlmZ dd lmZmZmZ dd lmZ ddl m!Z!m"Z# ddl$m%Z% dd lm&Z& g d�Z' ej( e)� � Z*d� Z+d� Z, G d� de-� � Z. G d� de-� � Z/ G d� de/� � Z0 G d� de-� � Z1 G d� de2� � Z3 G d� de4� � Z5 G d� de5� � Z6 G d� d e2� � Z7 G d!� d"e-� � Z8 G d#� d$e-� � Z9d%Z:d&Z;d'Z<d(Z= G d)� d*e-� � Z> G d+� d,e>� � Z? G d-� d.e>� � Z@ G d/� d0e>� � ZA G d1� d2e>� � ZBdS )3a� OpenID support for Relying Parties (aka Consumers). This module documents the main interface with the OpenID consumer library. The only part of the library which has to be used and isn't documented in full here is the store required to create an C{L{Consumer}} instance. More on the abstract store type and concrete implementations of it that are provided in the documentation for the C{L{__init__<Consumer.__init__>}} method of the C{L{Consumer}} class. OVERVIEW ======== The OpenID identity verification process most commonly uses the following steps, as visible to the user of this library: 1. The user enters their OpenID into a field on the consumer's site, and hits a login button. 2. The consumer site discovers the user's OpenID provider using the Yadis protocol. 3. The consumer site sends the browser a redirect to the OpenID provider. This is the authentication request as described in the OpenID specification. 4. The OpenID provider's site sends the browser a redirect back to the consumer site. This redirect contains the provider's response to the authentication request. The most important part of the flow to note is the consumer's site must handle two separate HTTP requests in order to perform the full identity check. LIBRARY DESIGN ============== This consumer library is designed with that flow in mind. The goal is to make it as easy as possible to perform the above steps securely. At a high level, there are two important parts in the consumer library. The first important part is this module, which contains the interface to actually use this library. The second is the C{L{openid.store.interface}} module, which describes the interface to use if you need to create a custom method for storing the state this library needs to maintain between requests. In general, the second part is less important for users of the library to know about, as several implementations are provided which cover a wide variety of situations in which consumers may use the library. This module contains a class, C{L{Consumer}}, with methods corresponding to the actions necessary in each of steps 2, 3, and 4 described in the overview. Use of this library should be as easy as creating an C{L{Consumer}} instance and calling the methods appropriate for the action the site wants to take. SESSIONS, STORES, AND STATELESS MODE ==================================== The C{L{Consumer}} object keeps track of two types of state: 1. State of the user's current authentication attempt. Things like the identity URL, the list of endpoints discovered for that URL, and in case where some endpoints are unreachable, the list of endpoints already tried. This state needs to be held from Consumer.begin() to Consumer.complete(), but it is only applicable to a single session with a single user agent, and at the end of the authentication process (i.e. when an OP replies with either C{id_res} or C{cancel}) it may be discarded. 2. State of relationships with servers, i.e. shared secrets (associations) with servers and nonces seen on signed messages. This information should persist from one session to the next and should not be bound to a particular user-agent. These two types of storage are reflected in the first two arguments of Consumer's constructor, C{session} and C{store}. C{session} is a dict-like object and we hope your web framework provides you with one of these bound to the user agent. C{store} is an instance of L{openid.store.interface.OpenIDStore}. Since the store does hold secrets shared between your application and the OpenID provider, you should be careful about how you use it in a shared hosting environment. If the filesystem or database permissions of your web host allow strangers to read from them, do not store your data there! If you have no safe place to store your data, construct your consumer with C{None} for the store, and it will operate only in stateless mode. Stateless mode may be slower, put more load on the OpenID provider, and trusts the provider to keep you safe from replay attacks. Several store implementation are provided, and the interface is fully documented so that custom stores can be used as well. See the documentation for the C{L{Consumer}} class for more information on the interface for stores. The implementations that are provided allow the consumer site to store the necessary data in several different ways, including several SQL databases and normal files on disk. IMMEDIATE MODE ============== In the flow described above, the user may need to confirm to the OpenID provider that it's ok to disclose his or her identity. The provider may draw pages asking for information from the user before it redirects the browser back to the consumer's site. This is generally transparent to the consumer site, so it is typically ignored as an implementation detail. There can be times, however, where the consumer site wants to get a response immediately. When this is the case, the consumer can put the library in immediate mode. In immediate mode, there is an extra response possible from the server, which is essentially the server reporting that it doesn't have enough information to answer the question yet. USING THIS LIBRARY ================== Integrating this library into an application is usually a relatively straightforward process. The process should basically follow this plan: Add an OpenID login field somewhere on your site. When an OpenID is entered in that field and the form is submitted, it should make a request to your site which includes that OpenID URL. First, the application should L{instantiate a Consumer<Consumer.__init__>} with a session for per-user state and store for shared state. using the store of choice. Next, the application should call the 'C{L{begin<Consumer.begin>}}' method on the C{L{Consumer}} instance. This method takes the OpenID URL. The C{L{begin<Consumer.begin>}} method returns an C{L{AuthRequest}} object. Next, the application should call the C{L{redirectURL<AuthRequest.redirectURL>}} method on the C{L{AuthRequest}} object. The parameter C{return_to} is the URL that the OpenID server will send the user back to after attempting to verify his or her identity. The C{realm} parameter is the URL (or URL pattern) that identifies your web site to the user when he or she is authorizing it. Send a redirect to the resulting URL to the user's browser. That's the first half of the authentication process. The second half of the process is done after the user's OpenID Provider sends the user's browser a redirect back to your site to complete their login. When that happens, the user will contact your site at the URL given as the C{return_to} URL to the C{L{redirectURL<AuthRequest.redirectURL>}} call made above. The request will have several query parameters added to the URL by the OpenID provider as the information necessary to finish the request. Get a C{L{Consumer}} instance with the same session and store as before and call its C{L{complete<Consumer.complete>}} method, passing in all the received query arguments. There are multiple possible return types possible from that method. These indicate whether or not the login was successful, and include any additional information appropriate for their type. @var SUCCESS: constant used as the status for L{SuccessResponse<openid.consumer.consumer.SuccessResponse>} objects. @var FAILURE: constant used as the status for L{FailureResponse<openid.consumer.consumer.FailureResponse>} objects. @var CANCEL: constant used as the status for L{CancelResponse<openid.consumer.consumer.CancelResponse>} objects. @var SETUP_NEEDED: constant used as the status for L{SetupNeededResponse<openid.consumer.consumer.SetupNeededResponse>} objects. � N)�urlparse� urldefrag� parse_qsl)�fetchers)�discover�OpenIDServiceEndpoint�DiscoveryFailure�OPENID_1_0_TYPE�OPENID_1_1_TYPE�OPENID_2_0_TYPE)�Message� OPENID_NS� OPENID2_NS� OPENID1_NS�IDENTIFIER_SELECT� no_default�BARE_NS)� cryptutil)�oidutil)�Association�default_negotiator�SessionNegotiator)� DiffieHellman)�mkNonce�split)� Discovery)�urinorm) �AuthRequest�Consumer�SuccessResponse�SetupNeededResponse�CancelResponse�FailureResponse�SUCCESS�FAILURE�CANCEL�SETUP_NEEDEDc �r � t j || � � � �� � }t ||� � S )z�Make a Direct Request to an OpenID Provider and return the result as a Message object. @raises openid.fetchers.HTTPFetchingError: if an error is encountered in making the HTTP post. @rtype: L{openid.message.Message} )�body)r �fetch�toURLEncoded�_httpResponseToMessage)�request_message� server_url�resps �:/usr/lib/python3/dist-packages/openid/consumer/consumer.py� makeKVPostr1 � s6 � � �>�*�?�+G�+G�+I�+I�J�J�J�D� "�$� �3�3�3� c �� � t j | j � � }| j dk rt � |� � �| j dvr"d}||| j fz }t j |� � �|S )au Adapt a POST response to a Message. @type response: L{openid.fetchers.HTTPResponse} @param response: Result of a POST to an OpenID endpoint. @rtype: L{openid.message.Message} @raises openid.fetchers.HTTPFetchingError: if the server returned a status of other than 200 or 400. @raises ServerError: if the server returned an OpenID error. i� )�� �� z"bad status code from server %s: %s)r � fromKVFormr) �status�ServerError�fromMessager �HTTPFetchingError)�responser. �response_message�fmt� error_messages r0 r, r, � ss � � �)�(�-�8�8����#����%�%�&6�7�7�7� �� � *� *�2���z�8�?�;�;� ��(��7�7�7��r2 c �T � e Zd ZdZdZdZ ee� � Zdd�Z dd�Z dd�Zd � Zd � Z dS ) r a� An OpenID consumer implementation that performs discovery and does session management. @ivar consumer: an instance of an object implementing the OpenID protocol, but doing no discovery or session management. @type consumer: GenericConsumer @ivar session: A dictionary-like object representing the user's session data. This is used for keeping state of the OpenID transaction when the user is redirected to the server. @cvar session_key_prefix: A string that is prepended to session keys to ensure that they are unique. This variable may be changed to suit your application. �_openid_consumer_� last_tokenNc �n � || _ |�t } ||� � | _ | j | j z | _ dS )ax Initialize a Consumer instance. You should create a new instance of the Consumer object with every HTTP request that handles OpenID transactions. @param session: See L{the session instance variable<openid.consumer.consumer.Consumer.session>} @param store: an object that implements the interface in C{L{openid.store.interface.OpenIDStore}}. Several implementations are provided, to cover common database environments. @type store: C{L{openid.store.interface.OpenIDStore}} @see: L{openid.store.interface} @see: L{openid.store} N)�session�GenericConsumer�consumer�session_key_prefix�_token� _token_key)�selfrC �store�consumer_classs r0 �__init__zConsumer.__init__! s<