From: Stas Kravets Date: Wed, 19 Jan 2011 10:23:07 +0000 (+0300) Subject: Merge remote branch 'upstream/master' X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=0c4b22f2022c894e228c8f236455877eec0d8acd;p=django-social-auth.git Merge remote branch 'upstream/master' --- 0c4b22f2022c894e228c8f236455877eec0d8acd diff --cc example/app/views.py index 941c915,50f3b3e..16cdb96 --- a/example/app/views.py +++ b/example/app/views.py @@@ -4,9 -4,7 +4,6 @@@ from django.contrib.auth.decorators imp from django.template import RequestContext from django.shortcuts import render_to_response - from social_auth.vk import add_VK_Auth - add_VK_Auth() -- def home(request): """Home view, displays login mechanism""" if request.user.is_authenticated(): diff --cc example/local_settings.py.template index 300a597,e43eba8..0729e75 --- a/example/local_settings.py.template +++ b/example/local_settings.py.template @@@ -7,9 -7,6 +7,8 @@@ ORKUT_CONSUMER_SECRET = ' SOCIAL_AUTH_CREATE_USERS = True SOCIAL_AUTH_FORCE_RANDOM_USERNAME = False SOCIAL_AUTH_DEFAULT_USERNAME = 'socialauth_user' --SOCIAL_AUTH_COMPLETE_URL_NAME = 'social:complete' ++SOCIAL_AUTH_COMPLETE_URL_NAME = 'complete' LOGIN_ERROR_URL = '/login/error/' +VKONTAKTE_APP_ID = '' +VKONTAKTE_APP_SECRET = '' - VKONTAKTE_LOCAL_HTML = '' #SOCIAL_AUTH_USER_MODEL = 'app.CustomUser' diff --cc example/settings.py index 59fe422,f2e7d7c..ac731ea --- a/example/settings.py +++ b/example/settings.py @@@ -62,14 -62,13 +62,14 @@@ INSTALLED_APPS = ) AUTHENTICATION_BACKENDS = ( - 'social_auth.backends.TwitterBackend', - 'social_auth.backends.FacebookBackend', - 'social_auth.backends.GoogleOAuthBackend', - 'social_auth.backends.GoogleBackend', - 'social_auth.backends.YahooBackend', + 'social_auth.backends.twitter.TwitterBackend', + 'social_auth.backends.facebook.FacebookBackend', + 'social_auth.backends.google.GoogleOAuthBackend', + 'social_auth.backends.google.GoogleBackend', + 'social_auth.backends.yahoo.YahooBackend', 'social_auth.backends.OpenIDBackend', - 'social_auth.backends.LiveJournalBackend', - 'social_auth.vk.VKontakteBackend', + 'social_auth.backends.contrib.livejournal.LiveJournalBackend', ++ 'social_auth.backends.contrib.vkontakte.VKontakteBackend', 'django.contrib.auth.backends.ModelBackend', ) diff --cc example/templates/vkontakte.html index 0000000,0000000..137a105 new file mode 100644 --- /dev/null +++ b/example/templates/vkontakte.html @@@ -1,0 -1,0 +1,38 @@@ ++{% extends "base.html" %} ++ ++{% block heading %}VKontakte login{% endblock %} ++ ++{% block content %} ++ ++ ++ ++ ++ ++
Logging in, please wait...
++{% endblock %} diff --cc setup.py index 321d759,b1c2281..478f033 --- a/setup.py +++ b/setup.py @@@ -32,9 -32,12 +32,11 @@@ setup(name='django-social-auth' license='GPL', keywords='django, openid, oath, social auth, application', url='https://github.com/omab/django-social-auth', - packages=['social_auth'], + packages=['social_auth', + 'social_auth.backends', + 'social_auth.backends.contrib'], long_description=long_description(), - install_requires=['django>=1.2', - 'oauth>=1.0', + install_requires=['oauth>=1.0', 'python_openid>=2.2'], classifiers=['Framework :: Django', 'Development Status :: 4 - Beta', diff --cc social_auth/backends/contrib/vkontakte.py index 0000000,0000000..673ec41 new file mode 100644 --- /dev/null +++ b/social_auth/backends/contrib/vkontakte.py @@@ -1,0 -1,0 +1,81 @@@ ++""" ++VKontakte OpenAPI support. ++ ++This contribution adds support for VKontakte OpenAPI service in the form ++www.vkontakte.ru. Username is retrieved from the identity returned by server. ++""" ++ ++from django.conf import settings ++from django.contrib.auth import authenticate ++from urllib import unquote ++import md5 ++ ++from social_auth.backends import SocialAuthBackend, BaseAuth, USERNAME ++ ++VKONTAKTE_LOCAL_HTML = 'vkontakte.html' ++ ++class VKontakteBackend(SocialAuthBackend): ++ """VKontakte authentication backend""" ++ name = 'vkontakte' ++ ++ def get_user_id(self, details, response): ++ """Return user unique id provided by VKontakte""" ++ return int(response.GET['id']) ++ ++ def get_user_details(self, response): ++ """Return user details from VKontakte request""" ++ nickname = unquote(response.GET['nickname']) ++ values = { USERNAME: response.GET['id'] if len(nickname) == 0 else nickname, 'email': '', 'fullname': '', ++ 'first_name': unquote(response.GET['first_name']), 'last_name': unquote(response.GET['last_name'])} ++ return values ++ ++ ++class VKontakteAuth(BaseAuth): ++ """VKontakte OpenAPI authorization mechanism""" ++ AUTH_BACKEND = VKontakteBackend ++ APP_ID = settings.VKONTAKTE_APP_ID ++ ++ def auth_html(self): ++ """Returns local VK authentication page, not necessary for VK to authenticate """ ++ from django.core.urlresolvers import reverse ++ from django.template import RequestContext, loader ++ ++ dict = { 'VK_APP_ID' : self.APP_ID, ++ 'VK_COMPLETE_URL': reverse(settings.SOCIAL_AUTH_COMPLETE_URL_NAME, args=[VKontakteBackend.name]) } ++ ++ vk_template = loader.get_template(VKONTAKTE_LOCAL_HTML) ++ context = RequestContext(self.request, dict) ++ ++ return vk_template.render(context) ++ ++ def auth_complete(self, *args, **kwargs): ++ """Performs check of authentication in VKontakte, returns User if succeeded""" ++ app_cookie = 'vk_app_' + self.APP_ID ++ ++ if not 'id' in self.request.GET or not app_cookie in self.request.COOKIES: ++ raise ValueError('VKontakte authentication is not completed') ++ ++ cookie_dict = dict(item.split('=') for item in self.request.COOKIES[app_cookie].split('&')) ++ check_str = ''.join([item + '=' + cookie_dict[item] for item in ['expire', 'mid', 'secret', 'sid']]) ++ ++ hash = md5.new(check_str + settings.VKONTAKTE_APP_SECRET).hexdigest() ++ ++ if hash != cookie_dict['sig']: ++ raise ValueError('VKontakte authentication failed: invalid hash') ++ else: ++ kwargs.update({'response': self.request, self.AUTH_BACKEND.name: True}) ++ return authenticate(*args, **kwargs) ++ ++ @property ++ def uses_redirect(self): ++ """VKontakte does not require visiting server url in order ++ to do authentication, so auth_xxx methods are not needed to be called. ++ Their current implementation is just an example""" ++ return False ++ ++ ++# Backend definition ++BACKENDS = { ++ 'vkontakte': VKontakteAuth, ++} ++