root/trunk/djangoid/openidhandlers.py

Revision 23, 2.7 kB (checked in by nicolast, 5 years ago)

More doctest stuff

Line 
1 #Djangoid - Django-based OpenID server/provider
2 #Copyright (C) 2006  Nicolas Trangez <ikke nicolast be>
3 #
4 #This program is free software; you can redistribute it and/or modify
5 #it under the terms of the GNU General Public License as published by
6 #the Free Software Foundation; either version 2 of the License.
7 #
8 #This program is distributed in the hope that it will be useful,
9 #but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #GNU General Public License for more details.
12 #
13 #You should have received a copy of the GNU General Public License
14 #along with this program; if not, write to the Free Software
15 #Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16 #
17 #EOL
18 from openid.server import server
19 from djangoid.djangoidstore import DjangoidStore
20 from django.http import HttpResponse
21
22 #OpenID server instance, using a DjangoidStore object as container
23 _openidserver = server.Server(DjangoidStore())
24
25 def convertToHttpResponse(response):
26         #Convert an OpenID server response to a Django-compatible HttpResponse:
27         #copy HTTP headers, and payload
28         r = _openidserver.encodeResponse(response)
29         ret = HttpResponse(r.body)
30         for header, value in r.headers.iteritems():
31                 ret[header] = value
32         ret.status_code = r.code
33
34         return ret
35
36 def convertToOpenIDRequest(request):
37         #Copy over all query (GET and POST) key-value pairs, so we can pass them to out OpenID server.
38         #request.REQUEST.copy() seems not to work, as openidserver.decodeRequest seems to use some function
39         #on the passed object that's not implemented in the copied object.
40         query = {}
41         for i in request.REQUEST.items():
42                 query[i[0]] = i[1]
43         try:
44                 return _openidserver.decodeRequest(query)
45         except server.ProtocolError, why:
46                 raise
47
48 def checkYadisRequest(request):
49         """
50         Checks whether an incoming django.http.HttpRequest is a YADIS request
51
52         >>> class Request:
53         ...     def __init__(self, meta = {}):
54         ...             self.META = meta
55         ...
56         >>> request = Request()
57         >>> checkYadisRequest(request)
58         False
59         >>> request = Request({"HTTP_ACCEPT": "text/html"})
60         >>> checkYadisRequest(request)
61         False
62         >>> request = Request({"HTTP_ACCEPT": "application/xrds+xml"})
63         >>> checkYadisRequest(request)
64         True
65         """
66         if request.META.has_key("HTTP_ACCEPT"):
67                 ct = request.META["HTTP_ACCEPT"]
68                 if "application/xrds+xml" in ct:
69                         return True
70         return False
71
72 def handleOpenIDRequest(request):
73         return _openidserver.handleRequest(request)
Note: See TracBrowser for help on using the browser.