Commits

offline  committed d91699d

new implementation of PROFILE_MAPPING

  • Participants
  • Parent commits 7b9df12

Comments (0)

Files changed (7)

File pipreq.txt

--e svn+http://oauth.googlecode.com/svn/code/python/oauth/@1137#egg=oauth
-

File publicauth/backends/__init__.py

         keys from profile mapping settings.
         """
         data = {}
-        for field in self.PROFILE_MAPPING:
-            data.update(self.extract_data(extra, field))
+        if extra:
+            for backend_field, form_field in self.PROFILE_MAPPING.items():
+                data.update(self.extract_data(extra, backend_field, form_field))
 
-        form = str_to_class(settings.EXTRA_FORM)(data, fields=self.PROFILE_MAPPING)
+        form = str_to_class(settings.EXTRA_FORM)(data)
         if form.is_valid():
             form.save(self.identity, self.provider)
             self.login_user(request)
         else:
             return data
 
-    def extract_data(self, extra, field):
+    def extract_data(self, extra, backend_field, form_field):
         """
         If extra isnt standart python dictionary
         you need to implement this method to retrive
         values from this object.
         """
-        try:
-            return {self.PROFILE_MAPPING[field][0]: extra.get(field, '')}
-        except AttributeError:
-            return {self.PROFILE_MAPPING[field][0]: ''}
+        return {form_field: extra.get(backend_field, '')}
 
 

File publicauth/backends/google.py

     def get_extra_data(self, resp):
         return FetchResponse.fromSuccessResponse(resp)
 
-    def extract_data(self, extra, field):
-        try:
-            return {self.PROFILE_MAPPING[field][0]: extra.getSingle(settings.AX_URIS[field], '')}
-        except:
-            return {self.PROFILE_MAPPING[field][0]: ''}
+    def extract_data(self, extra, backend_field, form_field):
+        return {form_field: extra.getSingle(settings.AX_URIS[backend_field], '')}
 

File publicauth/backends/oauth.py

 from oauth.oauth import OAuthConsumer, OAuthToken, OAuthRequest, OAuthSignatureMethod_HMAC_SHA1
 
 from django.conf import settings as global_settings
+from django.utils.datastructures import MultiValueDictKeyError
 from django.core.urlresolvers import reverse
+from django.contrib import messages
 
 from annoying.exceptions import Redirect
 
 from publicauth.backends import BaseBackend
+from publicauth import lang
 
 
 class OAuthBackend(BaseBackend):
     def validate(self, request, data):
         signature_method = OAuthSignatureMethod_HMAC_SHA1()
         consumer = OAuthConsumer(self.CONSUMER_KEY, self.CONSUMER_SECRET)
-        oauth_token = data['oauth_token']
-        oauth_verifier = data['oauth_verifier']
+        try:
+            oauth_token = data['oauth_token']
+            oauth_verifier = data['oauth_verifier']
+        except MultiValueDictKeyError:
+            messages.add_message(request, messages.ERROR, lang.BACKEND_ERROR)
+            raise Redirect('publicauth-login')
         oauth_req = OAuthRequest.from_consumer_and_token(consumer, http_url=self.ACCESS_TOKEN_URL)
         oauth_req.set_parameter('oauth_token', oauth_token)
         oauth_req.set_parameter('oauth_verifier', oauth_verifier)
     def get_extra_data(self, response):
         return urlparse.parse_qs(response, keep_blank_values=False)
 
-    def extract_data(self, extra, field):
-        try:
-            return {self.PROFILE_MAPPING[field][0]: extra.get(field, '')[0]}
-        except IndexError:
-            return {self.PROFILE_MAPPING[field][0]: ''}
+    def extract_data(self, extra, backend_field, form_field):
+        return {form_field: extra.get(backend_field, '')[0]}
             
 

File publicauth/backends/openid.py

         try:
             openid_url = data['openid_url'].strip()
         except KeyError:
-            messages.add_message(request, messages.ERROR, "Please fill openid url field")
+            messages.add_message(request, messages.ERROR, lang.FILL_OPENID_URL)
 
         # allow user to type openid provider without http:// prefix
         if not openid_url.startswith("http"):

File publicauth/forms.py

 
 
 class ExtraForm(forms.Form):
-    def __init__(self, *args, **kwargs):
-        """
-        Form with fields from backend PROFILE_MAPPINGS setting.
-        """
-        fields = kwargs.pop("fields", {})
-        for key, value in fields.values():
-            self.base_fields[key] = value
-        super(ExtraForm, self).__init__(*args, **kwargs)
+    
+    username = forms.CharField(min_length=3, max_length=25, label="Display Name")
 
     def clean_username(self):
         if get_object_or_None(User, username=self.cleaned_data['username']):

File publicauth/lang.py

 from django.utils.translation import ugettext as _
 from django.conf import settings
 
+
 REGISTRATION_DISABLED = getattr(settings, "PUBLICAUTH_REGISTRATION_DISABLED", _('We are sorry, but registration is disabled. Come back later'))
+FILL_OPENID_URL = getattr(settings, "PUBLICAUTH_FILL_OPENID_URL", _('Please fill openid url field'))
+BACKEND_ERROR = getattr(settings, "PUBLICAUTH_BACKEND_ERROR", _('Your authentication provider returned bad response, please try again'))
 OPENID_CANCELED = getattr(settings, "PUBLICAUTH_OPENID_CANCELED", _('You have cancelled OpenID authentication'))
 OPENID_FAILED = getattr(settings, "PUBLICAUTH_OPENID_FAILED", _('OpenID authentication failed. Reason: %s'))
 SUCCESS_LOGOUT = getattr(settings, "PUBLICAUTH_SUCCESS_LOGOUT", _('You have successfully logged out'))