]> git.parisson.com Git - django-social-auth.git/commitdiff
Yandex.ru support added; small change in default e-mail value in OpenIDBackend.get_us...
authorStas Kravets <skravets@internal-rfc1918.hn.nnov.stream.ru>
Wed, 19 Jan 2011 15:53:41 +0000 (18:53 +0300)
committerStas Kravets <skravets@internal-rfc1918.hn.nnov.stream.ru>
Wed, 19 Jan 2011 15:53:41 +0000 (18:53 +0300)
example/settings.py
example/templates/done.html
example/templates/home.html
social_auth/backends/__init__.py
social_auth/backends/contrib/yandex.py [new file with mode: 0644]

index ac731eac194b068e919f357942f9d70dd9d7d4ed..935afe843e6bcba2ceaec77ea03a973681c2936b 100644 (file)
@@ -70,6 +70,7 @@ AUTHENTICATION_BACKENDS = (
     'social_auth.backends.OpenIDBackend',
     'social_auth.backends.contrib.livejournal.LiveJournalBackend',
     'social_auth.backends.contrib.vkontakte.VKontakteBackend',
+    'social_auth.backends.contrib.yandex.YandexBackend',
     'django.contrib.auth.backends.ModelBackend',
 )
 
index 3b90b81e3129ccac8ff0347ad7cd5fc791b92c7c..fa4ff189b1927da634ec40e6c3f14bc5bc14c8f6 100644 (file)
         </div>
       </form>
     </li>
+    <li>
+      <form action="/login/yandex/" method="post">{% csrf_token %}
+        <div>
+          <label for="openid_ya_user">Yandex user
+            {% if yandex %} <span class="associated">(associated)</span>{% endif %}:
+          </label>
+          <input id="openid_ya_user" type="text" value="" name="openid_ya_user" />
+          <input type="submit" value="Login"/>
+        </div>
+      </form>
+    </li>
     <li>
       <form action="/associate/openid/" method="post">{% csrf_token %}
         <div>
index 317895bfe240398ef4b99f952d7ed6df85cff726..6126124963cf17b1becb19b4e931c3fb5ede2bbe 100644 (file)
         </div>
       </form>
     </li>
+    <li>
+      <form action="/login/yandex/" method="post">{% csrf_token %}
+        <div>
+          <label for="openid_ya_user">Yandex user:</label>
+          <input id="openid_ya_user" type="text" value="" name="openid_ya_user" />
+          <input type="submit" value="Login"/>
+        </div>
+      </form>
+    </li>
     <li>
       <form action="/login/openid/" method="post">{% csrf_token %}
         <div>
index eff0642da8100ce08198b44b94528974745f4b85..01f9aa57671d5e05e1f29d29e36dd367fa91d5f6 100644 (file)
@@ -233,6 +233,9 @@ class OpenIDBackend(SocialAuthBackend):
         first_name = values.get('first_name') or ''
         last_name = values.get('last_name') or ''
 
+        if not values['email']:
+            values['email'] = ''
+        
         if not fullname and first_name and last_name:
             fullname = first_name + ' ' + last_name
         elif fullname:
diff --git a/social_auth/backends/contrib/yandex.py b/social_auth/backends/contrib/yandex.py
new file mode 100644 (file)
index 0000000..39b5b1d
--- /dev/null
@@ -0,0 +1,46 @@
+"""
+Yandex OpenID support.
+
+This contribution adds support for Yandex.ru OpenID service in the form
+openid.yandex.ru/user. Username is retrieved from the identity url.
+"""
+import urlparse
+
+from social_auth.backends import OpenIDBackend, OpenIdAuth, USERNAME
+
+
+# Yandex conf
+YANDEX_URL = 'http://openid.yandex.ru/%s'
+YANDEX_USER_FIELD = 'openid_ya_user'
+
+
+class YandexBackend(OpenIDBackend):
+    """Yandex OpenID authentication backend"""
+    name = 'yandex'
+
+    def get_user_details(self, response):
+        """Generate username from identity url"""
+        values = super(YandexBackend, self).get_user_details(response)
+        values[USERNAME] = values.get(USERNAME) or \
+                           urlparse.urlsplit(response.identity_url)\
+                                   .path.strip('/')
+                                   
+        values['email'] = values.get('email') or values[USERNAME] + '@yandex.ru'
+        return values
+
+
+class YandexAuth(OpenIdAuth):
+    """Yandex OpenID authentication"""
+    AUTH_BACKEND = YandexBackend
+
+    def openid_url(self):
+        """Returns Yandex authentication URL"""
+        if self.request.method != 'POST' or \
+           not self.request.POST.get(YANDEX_USER_FIELD):
+            raise ValueError, 'Missing Yandex user identifier'
+        return YANDEX_URL % self.request.POST[YANDEX_USER_FIELD]
+    
+# Backend definition
+BACKENDS = {
+    'yandex': YandexAuth,
+}