[Python-modules-commits] [python-social-auth] 205/322: Added NaszaKlasa OAuth2 support

Wolfgang Borgert debacle at moszumanska.debian.org
Sat Dec 24 15:13:09 UTC 2016


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

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

commit 3d182459b8556178584a184450cdd278e71cea97
Author: Krzysztof Hoffmann <krzysiekpl at gmail.com>
Date:   Mon Mar 30 16:48:53 2015 +0200

    Added NaszaKlasa OAuth2 support
---
 README.rst                   |  2 ++
 docs/backends/naszaklasa.rst | 26 ++++++++++++++++
 docs/intro.rst               |  2 ++
 social/backends/nk.py        | 70 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 100 insertions(+)

diff --git a/README.rst b/README.rst
index 72b61c1..5817f67 100644
--- a/README.rst
+++ b/README.rst
@@ -90,6 +90,7 @@ or current ones extended):
     * Mendeley_ OAuth1 http://mendeley.com
     * Mixcloud_ OAuth2
     * `Mozilla Persona`_
+    * NaszaKlasa_ OAuth2
     * Odnoklassniki_ OAuth2 and Application Auth
     * OpenId_
     * OpenStreetMap_ OAuth1 http://wiki.openstreetmap.org/wiki/OAuth
@@ -259,6 +260,7 @@ check `django-social-auth LICENSE`_ for details:
 .. _MapMyFitness: http://www.mapmyfitness.com/
 .. _Mixcloud: https://www.mixcloud.com
 .. _Mozilla Persona: http://www.mozilla.org/persona/
+.. _NaszaKlasa: https://developers.nk.pl/
 .. _Odnoklassniki: http://www.odnoklassniki.ru
 .. _Pocket: http://getpocket.com
 .. _Podio: https://podio.com
diff --git a/docs/backends/naszaklasa.rst b/docs/backends/naszaklasa.rst
new file mode 100644
index 0000000..01fe78e
--- /dev/null
+++ b/docs/backends/naszaklasa.rst
@@ -0,0 +1,26 @@
+NationBuilder
+=============
+
+`NaszaKlasa supports OAuth2`_ as their authentication mechanism. Follow these
+steps in order to use it:
+
+- Register a new application at your `NK Developers`_ (define the `Callback
+  URL` to ``http://example.com/complete/nk/`` where ``example.com``
+  is your domain).
+
+- Fill the ``Client ID`` and ``Client Secret`` values from the newly created
+  application::
+
+    SOCIAL_AUTH_NK_KEY = ''
+    SOCIAL_AUTH_NK_SECRET = ''
+
+- Enable the backend in ``AUTHENTICATION_BACKENDS`` setting::
+
+    AUTHENTICATION_BACKENDS = (
+        ...
+        'social.backends.nk.NKOAuth2',
+        ...
+    )
+
+.. _NaszaKlasa supports OAuth2: https://developers.nk.pl
+.. _NK Developers: https://developers.nk.pl/developers/oauth2client/form
\ No newline at end of file
diff --git a/docs/intro.rst b/docs/intro.rst
index 712f6db..b835961 100644
--- a/docs/intro.rst
+++ b/docs/intro.rst
@@ -60,6 +60,7 @@ or extend current one):
     * MineID_ OAuth2
     * Mixcloud_ OAuth2
     * `Mozilla Persona`_
+    * NaszaKlasa_ OAuth2
     * Odnoklassniki_ OAuth2 and Application Auth
     * OpenId_
     * Podio_ OAuth2
@@ -139,6 +140,7 @@ section.
 .. _MineID: https://www.mineid.org
 .. _Mixcloud: https://www.mixcloud.com
 .. _Mozilla Persona: http://www.mozilla.org/persona/
+.. _NaszaKlasa: https://developers.nk.pl/
 .. _Odnoklassniki: http://www.odnoklassniki.ru
 .. _Podio: https://podio.com
 .. _Shopify: http://shopify.com
diff --git a/social/backends/nk.py b/social/backends/nk.py
new file mode 100644
index 0000000..0ad3813
--- /dev/null
+++ b/social/backends/nk.py
@@ -0,0 +1,70 @@
+import json
+from urllib import urlencode, urlopen
+from requests_oauthlib import OAuth1
+
+from social.backends.oauth import BaseOAuth2
+import six
+
+class NKOAuth2(BaseOAuth2):
+    """NK OAuth authentication backend"""
+    name = 'nk'
+    AUTHORIZATION_URL = 'https://nk.pl/oauth2/login'
+    ACCESS_TOKEN_URL = 'https://nk.pl/oauth2/token'
+    SCOPE_SEPARATOR = ','
+    ACCESS_TOKEN_METHOD = 'POST'
+    SIGNATURE_TYPE_AUTH_HEADER = 'AUTH_HEADER'
+    EXTRA_DATA = [
+        ('id', 'id'),
+    ]
+
+    def get_user_details(self, response):
+        """Return user details from NK account"""
+        entry = response['entry']
+        return {'username': entry.get('displayName'),
+                'email': entry['emails'][0]['value'],
+                'first_name': entry.get('displayName').split(" ")[0],
+                'id':entry.get('id')}
+
+    def auth_complete_params(self, state=None):
+        client_id, client_secret = self.get_key_and_secret()
+        return {
+            'grant_type': 'authorization_code',  # request auth code
+            'code': self.data.get('code', ''),  # server response code
+            'client_id': client_id,
+            'client_secret': client_secret,
+            'redirect_uri': self.get_redirect_uri(state),
+            'scope':self.get_scope_argument()
+        }
+
+    def get_user_id(self, details, response):
+        """Return a unique ID for the current user, by default from server
+        response."""
+        return details.get(self.ID_KEY)
+
+    def user_data(self, access_token, *args, **kwargs):
+        """Loads user data from service"""
+        url = 'http://opensocial.nk-net.pl/v09/social/rest/people/@me?' + urlencode({
+            'nk_token': access_token,
+            'fields': 'name,surname,avatar,localization,age,gender,emails,birthdate'
+        })
+        return self.get_json(
+            url,
+            auth=self.oauth_auth(access_token)
+        )
+
+    def oauth_auth(self, token=None, oauth_verifier=None,
+                   signature_type=SIGNATURE_TYPE_AUTH_HEADER):
+        key, secret = self.get_key_and_secret()
+        oauth_verifier = oauth_verifier or self.data.get('oauth_verifier')
+        token = token or {}
+        # decoding='utf-8' produces errors with python-requests on Python3
+        # since the final URL will be of type bytes
+        decoding = None if six.PY3 else 'utf-8'
+        state = self.get_or_create_state()
+        return OAuth1(key, secret,
+                      resource_owner_key=None,
+                      resource_owner_secret=None,
+                      callback_uri=self.get_redirect_uri(state),
+                      verifier=oauth_verifier,
+                      signature_type=signature_type,
+                      decoding=decoding)
\ 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