~gary/python-openid/python-openid-2.2.1-patched

« back to all changes in this revision

Viewing changes to examples/djopenid/consumer/views.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2007-11-30 02:46:28 UTC
  • mfrom: (1.1.1 pyopenid-2.0)
  • Revision ID: launchpad@pqm.canonical.com-20071130024628-qktwsew3383iawmq
[rs=SteveA] upgrade to python-openid-2.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
from djopenid import util
 
3
 
 
4
from django import http
 
5
from django.http import HttpResponseRedirect
 
6
 
 
7
from openid.consumer import consumer
 
8
from openid.consumer.discover import DiscoveryFailure
 
9
from openid import sreg
 
10
 
 
11
def getOpenIDStore():
 
12
    """
 
13
    Return an OpenID store object fit for the currently-chosen
 
14
    database backend, if any.
 
15
    """
 
16
    return util.getOpenIDStore('/tmp/djopenid_c_store', 'c_')
 
17
 
 
18
def getConsumer(request):
 
19
    """
 
20
    Get a Consumer object to perform OpenID authentication.
 
21
    """
 
22
    return consumer.Consumer(request.session, getOpenIDStore())
 
23
 
 
24
@util.sendResponse
 
25
def startOpenID(request):
 
26
    """
 
27
    Start the OpenID authentication process.  Renders an
 
28
    authentication form and accepts its POST.
 
29
 
 
30
    * Renders an error message if OpenID cannot be initiated
 
31
 
 
32
    * Requests some Simple Registration data using the OpenID
 
33
      library's Simple Registration machinery
 
34
 
 
35
    * Generates the appropriate trust root and return URL values for
 
36
      this application (tweak where appropriate)
 
37
 
 
38
    * Generates the appropriate redirect based on the OpenID protocol
 
39
      version.
 
40
    """
 
41
    if request.POST:
 
42
        # Start OpenID authentication.
 
43
        openid_url = request.POST['openid_url']
 
44
        c = getConsumer(request)
 
45
        error = None
 
46
 
 
47
        try:
 
48
            auth_request = c.begin(openid_url)
 
49
        except DiscoveryFailure, e:
 
50
            # Some other protocol-level failure occurred.
 
51
            error = "OpenID discovery error: %s" % (str(e),)
 
52
 
 
53
        if error:
 
54
            # Render the page with an error.
 
55
            return 'consumer/index.html', {'error': error}
 
56
 
 
57
        # Add Simple Registration request information.  Some fields
 
58
        # are optional, some are required.  It's possible that the
 
59
        # server doesn't support sreg or won't return any of the
 
60
        # fields.
 
61
        sreg_request = sreg.SRegRequest(optional=['email', 'nickname'],
 
62
                                        required=['dob'])
 
63
        auth_request.addExtension(sreg_request)
 
64
 
 
65
        # Compute the trust root and return URL values to build the
 
66
        # redirect information.
 
67
        trust_root = util.getTrustRoot(request)
 
68
        return_to = trust_root + 'consumer/finish/'
 
69
 
 
70
        # Send the browser to the server either by sending a redirect
 
71
        # URL or by generating a POST form.
 
72
        if auth_request.shouldSendRedirect():
 
73
            url = auth_request.redirectURL(trust_root, return_to)
 
74
            return HttpResponseRedirect(url)
 
75
        else:
 
76
            # Beware: this renders a template whose content is a form
 
77
            # and some javascript to submit it upon page load.  Non-JS
 
78
            # users will have to click the form submit button to
 
79
            # initiate OpenID authentication.
 
80
            form_id = 'openid_message'
 
81
            form_html = auth_request.formMarkup(trust_root, return_to,
 
82
                                                False, {'id': form_id})
 
83
            return 'consumer/request_form.html', {'html': form_html}
 
84
 
 
85
    return 'consumer/index.html', {}
 
86
 
 
87
@util.sendResponse
 
88
def finishOpenID(request):
 
89
    """
 
90
    Finish the OpenID authentication process.  Invoke the OpenID
 
91
    library with the response from the OpenID server and render a page
 
92
    detailing the result.
 
93
    """
 
94
    result = {}
 
95
 
 
96
    if request.GET:
 
97
        c = getConsumer(request)
 
98
 
 
99
        # Because the object containing the query parameters is a
 
100
        # MultiValueDict and the OpenID library doesn't allow that,
 
101
        # we'll convert it to a normal dict.
 
102
        GET_data = util.normalDict(request.GET)
 
103
 
 
104
        # Get a response object indicating the result of the OpenID
 
105
        # protocol.
 
106
        response = c.complete(GET_data)
 
107
 
 
108
        # Get a Simple Registration response object if response
 
109
        # information was included in the OpenID response.
 
110
        sreg_response = {}
 
111
        if response.status == consumer.SUCCESS:
 
112
            sreg_response = sreg.SRegResponse.fromSuccessResponse(response)
 
113
 
 
114
        # Map different consumer status codes to template contexts.
 
115
        results = {
 
116
            consumer.CANCEL:
 
117
            {'message': 'OpenID authentication cancelled.'},
 
118
 
 
119
            consumer.FAILURE:
 
120
            {'error': 'OpenID authentication failed.'},
 
121
 
 
122
            consumer.SUCCESS:
 
123
            {'url': response.identity_url,
 
124
             'sreg': sreg_response.items(),},
 
125
            }
 
126
 
 
127
        result = results[response.status]
 
128
 
 
129
        if isinstance(response, consumer.FailureResponse):
 
130
            # In a real application, this information should be
 
131
            # written to a log for debugging/tracking OpenID
 
132
            # authentication failures. In general, the messages are
 
133
            # not user-friendly, but intended for developers.
 
134
            result['failure_reason'] = response.message
 
135
 
 
136
    return 'consumer/index.html', result