Package madrona :: Package openid :: Package utils
[hide private]

Source Code for Package madrona.openid.utils

  1  # -*- coding: utf-8 -*- 
  2  # Copyright 2007, 2008,2009 by Benoît Chesneau <benoitc@e-engura.org> 
  3  #  
  4  # Licensed under the Apache License, Version 2.0 (the "License"); 
  5  # you may not use this file except in compliance with the License. 
  6  # You may obtain a copy of the License at 
  7  # 
  8  #     http://www.apache.org/licenses/LICENSE-2.0 
  9  # 
 10  # Unless required by applicable law or agreed to in writing, software 
 11  # distributed under the License is distributed on an "AS IS" BASIS, 
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 13  # See the License for the specific language governing permissions and 
 14  # limitations under the License. 
 15  # 
 16  import time 
 17  import urllib 
 18   
 19  from django.conf import settings 
 20  from django.http import str_to_unicode, get_host 
 21  from django.utils.html import escape 
 22   
 23  from openid.consumer.discover import discover 
 24  from openid.extensions import sreg, ax 
 25  try: # needed for some linux distributions like debian 
 26      from openid.yadis import xri 
 27  except ImportError: 
 28      from yadis import xri 
 29   
 30   
31 -class OpenID(object):
32 - def __init__(self, openid_, issued, attrs=None, sreg_=None, ax_=None):
33 self.openid = openid_ 34 self.issued = issued 35 self.attrs = attrs or {} 36 self.sreg = sreg_ or {} 37 self.ax = ax_ or {} 38 self.is_iname = (xri.identifierScheme(openid_) == 'XRI')
39
40 - def __repr__(self):
41 return '<OpenID: %s>' % self.openid
42
43 - def __str__(self):
44 return self.openid
45
46 -def discover_extensions(openid_url):
47 service = discover(openid_url) 48 use_ax = False 49 use_sreg = False 50 for endpoint in service[1]: 51 if not use_sreg: 52 use_sreg = sreg.supportsSReg(endpoint) 53 if not use_ax: 54 use_ax = endpoint.usesExtension("http://openid.net/srv/ax/1.0") 55 if use_ax and use_sreg: 56 break 57 if not use_sreg and not use_ax: 58 use_sreg = True 59 return use_ax, use_sreg
60 61 62 DEFAULT_NEXT = getattr(settings, 'OPENID_REDIRECT_NEXT', '/')
63 -def clean_next(next):
64 if next is None: 65 return DEFAULT_NEXT 66 next = str_to_unicode(urllib.unquote(next), 'utf-8') 67 next = next.strip() 68 if next.startswith('/'): 69 return next 70 return DEFAULT_NEXT
71 72
73 -def from_openid_response(openid_response):
74 """ return openid object from response """ 75 issued = int(time.time()) 76 sreg_resp = sreg.SRegResponse.fromSuccessResponse(openid_response) \ 77 or [] 78 ax_resp = ax.FetchResponse.fromSuccessResponse(openid_response) 79 ax_args = {} 80 if ax_resp is not None: 81 ax_args = ax_resp.getExtensionArgs() 82 ax_resp.parseExtensionArgs(ax_args) 83 ax_args = ax_resp.data 84 85 return OpenID( 86 openid_response.identity_url, issued, openid_response.signed_fields, 87 dict(sreg_resp), ax_args 88 )
89
90 -def get_url_host(request):
91 if request.is_secure(): 92 protocol = 'https' 93 else: 94 protocol = 'http' 95 host = escape(get_host(request)) 96 return '%s://%s' % (protocol, host)
97
98 -def get_full_url(request):
99 return get_url_host(request) + request.get_full_path()
100