[Python-modules-commits] [python-social-auth] 46/71: Add naver backends

Wolfgang Borgert debacle at moszumanska.debian.org
Sat Dec 24 15:14:28 UTC 2016


This is an automated email from the git hooks/post-receive script.

debacle pushed a commit to tag v0.2.14
in repository python-social-auth.

commit ce2e3a32dcca5fad603bd46a183b66a856006984
Author: SeokJun Hong <hong921122 at gmail.com>
Date:   Mon Nov 16 09:37:11 2015 -0500

    Add naver backends
---
 docs/backends/naver.rst             | 25 +++++++++++++++++
 social/backends/naver.py            | 53 +++++++++++++++++++++++++++++++++++++
 social/tests/backends/test_naver.py | 35 ++++++++++++++++++++++++
 3 files changed, 113 insertions(+)

diff --git a/docs/backends/naver.rst b/docs/backends/naver.rst
new file mode 100644
index 0000000..18bd99c
--- /dev/null
+++ b/docs/backends/naver.rst
@@ -0,0 +1,25 @@
+Naver
+========
+
+Naver uses OAuth v2 for Authentication.
+
+- Register a new application at the `Naver API`_, and
+
+- add naver oauth backend to your settings page:
+
+    SOCIAL_AUTH_AUTHENTICATION_BACKENDS = (
+        ...
+        'social.backends.naver.NaverOAuth2',
+        ...
+    )
+
+- fill ``Client ID`` and ``Client Secret`` from developer.naver.com values in the settings::
+
+	SOCIAL_AUTH_NAVER_KEY = ''
+	SOCIAL_AUTH_NAVER_SECRET = ''
+
+- you can get EXTRA_DATA: 
+	
+	SOCIAL_AUTH_NAVER_EXTRA_DATA = ['nickname', 'gender', 'age', 'birthday', 'profile_image']
+
+.. _Naver API: https://nid.naver.com/devcenter/docs.nhn?menu=API
diff --git a/social/backends/naver.py b/social/backends/naver.py
new file mode 100644
index 0000000..df67586
--- /dev/null
+++ b/social/backends/naver.py
@@ -0,0 +1,53 @@
+
+from xml.dom import minidom
+
+from social.backends.oauth import BaseOAuth2
+
+class NaverOAuth2(BaseOAuth2):
+    """Naver OAuth authentication backend"""
+    name = 'naver'
+    AUTHORIZATION_URL = 'https://nid.naver.com/oauth2.0/authorize'
+    ACCESS_TOKEN_URL = 'https://nid.naver.com/oauth2.0/token'
+    ACCESS_TOKEN_METHOD = 'POST'
+    EXTRA_DATA = [
+        ('id', 'id'),
+    ]
+
+    def get_user_id(self, details, response):
+        return response.get('id')
+
+    def get_user_details(self, response):
+        """Return user details from Naver account"""
+        return {
+            'username': response.get('username'),
+            'email': response.get('email'),
+            'fullname': response.get('username'),
+        }
+
+    def user_data(self, access_token, *args, **kwargs):
+        """Loads user data from service"""
+        dom = minidom.parseString(self.request(
+            'https://openapi.naver.com/v1/nid/getUserProfile.xml', 
+            method='POST',
+            headers={'Authorization': 'Bearer {0}'.format(access_token)}
+            ).text.encode('utf-8').strip())
+
+        return {
+            'id': dom.getElementsByTagName('id')[0].childNodes[0].data,
+            'email': dom.getElementsByTagName('email')[0].childNodes[0].data,
+            'username': dom.getElementsByTagName('name')[0].childNodes[0].data,
+            'nickname': dom.getElementsByTagName('nickname')[0].childNodes[0].data,
+            'gender': dom.getElementsByTagName('gender')[0].childNodes[0].data,
+            'age': dom.getElementsByTagName('age')[0].childNodes[0].data,
+            'birthday': dom.getElementsByTagName('birthday')[0].childNodes[0].data,
+            'profile_image': dom.getElementsByTagName('profile_image')[0].childNodes[0].data,
+        }
+
+    def auth_headers(self):
+        client_id, client_secret = self.get_key_and_secret()
+        return {
+            'grant_type': 'authorization_code',
+            'code': self.data.get('code'),
+            'client_id': client_id,
+            'client_secret': client_secret,
+        }
diff --git a/social/tests/backends/test_naver.py b/social/tests/backends/test_naver.py
new file mode 100644
index 0000000..d625046
--- /dev/null
+++ b/social/tests/backends/test_naver.py
@@ -0,0 +1,35 @@
+import json
+from social.tests.backends.oauth import OAuth2Test
+
+class NaverOAuth2Test(OAuth2Test):
+    backend_path = 'social.backends.naver.NaverOAuth2'
+    user_data_url = 'https://openapi.naver.com/v1/nid/getUserProfile.xml'
+    expected_username = 'foobar'
+    access_token_body = json.dumps({
+        'access_token': 'foobar',
+        'token_type': 'bearer',
+    })
+
+    user_data_content_type = 'text/xml'
+    user_data_body = \
+    '<data>' \
+        '<result>' \
+            '<resultcode>00</resultcode>' \
+            '<message>success</message>' \
+        '</result>' \
+        '<response>' \
+            '<nickname>naverIDLogin</nickname>' \
+            '<name>userName<name>' \
+            '<id>123456</id>' \
+            '<gender>M</gender>' \
+            '<age>40-49</age>' \
+            '<birthday>01-01</birthday>' \
+            '<profile_image>http://naver.com/image.url.jpg</profile_image>' \
+        '</response>' \
+    '</data>'
+
+    def test_login(self):
+        self.do_login()
+
+    def test_partial_pipeline(self):
+        self.do_partial_pipeline()
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-social-auth.git



More information about the Python-modules-commits mailing list