[Python-modules-commits] [python-social-auth] 85/322: Added backend for Coursera

Wolfgang Borgert debacle at moszumanska.debian.org
Sat Dec 24 15:12:52 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 9173ace70150e3e04ad07fc15ddd165dbe4d03ea
Author: Adam Babik <adam.babik at 10clouds.com>
Date:   Fri Jan 23 15:33:22 2015 +0100

    Added backend for Coursera
---
 social/backends/coursera.py            | 39 ++++++++++++++++++++++++++++++++++
 social/tests/backends/test_coursera.py | 37 ++++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+)

diff --git a/social/backends/coursera.py b/social/backends/coursera.py
new file mode 100644
index 0000000..9507edb
--- /dev/null
+++ b/social/backends/coursera.py
@@ -0,0 +1,39 @@
+"""
+Coursera OAuth2 backend, docs at:
+    https://tech.coursera.org/app-platform/oauth2/
+"""
+from social.backends.oauth import BaseOAuth2
+
+
+class CourseraOAuth2(BaseOAuth2):
+    """Coursera OAuth2 authentication backend"""
+    name = 'coursera'
+    ID_KEY = 'username'
+    AUTHORIZATION_URL = 'https://accounts.coursera.org/oauth2/v1/auth'
+    ACCESS_TOKEN_URL = 'https://accounts.coursera.org/oauth2/v1/token'
+    ACCESS_TOKEN_METHOD = 'POST'
+    REDIRECT_STATE = False
+    SCOPE_SEPARATOR = ','
+    DEFAULT_SCOPE = ['view_profile']
+
+    def _get_username_from_response(self, response):
+        elements = response.get('elements', [])
+        for element in elements:
+            if 'id' in element:
+                return element.get('id')
+
+        return None
+
+    def get_user_details(self, response):
+        """Return user details from Coursera account"""
+        return {'username': self._get_username_from_response(response)}
+
+    def user_data(self, access_token, *args, **kwargs):
+        """Load user data from the service"""
+        return self.get_json(
+            'https://api.coursera.org/api/externalBasicProfiles.v1?q=me',
+            headers=self.get_auth_header(access_token)
+        )
+
+    def get_auth_header(self, access_token):
+        return {'Authorization': 'Bearer {0}'.format(access_token)}
diff --git a/social/tests/backends/test_coursera.py b/social/tests/backends/test_coursera.py
new file mode 100644
index 0000000..ad6836e
--- /dev/null
+++ b/social/tests/backends/test_coursera.py
@@ -0,0 +1,37 @@
+import json
+
+from social.tests.backends.oauth import OAuth2Test
+
+
+class CourseraOAuth2Test(OAuth2Test):
+    backend_path = 'social.backends.coursera.CourseraOAuth2'
+    user_data_url = 'https://api.coursera.org/api/externalBasicProfiles.v1?q=me'
+    expected_username = '560e7ed2076e0d589e88bd74b6aad4b7'
+    access_token_body = json.dumps({
+        'access_token': 'foobar',
+        'token_type': 'Bearer',
+        'expires_in': 1795
+    })
+    request_token_body = json.dumps({
+        'code': 'foobar-code',
+        'client_id': 'foobar-client-id',
+        'client_secret': 'foobar-client-secret',
+        'redirect_uri': 'http://localhost:8000/accounts/coursera/',
+        'grant_type': 'authorization_code'
+    })
+    user_data_body = json.dumps({
+        'token_type': 'Bearer',
+        'paging': None,
+        'elements': [{
+            'id': '560e7ed2076e0d589e88bd74b6aad4b7'
+        }],
+        'access_token': 'foobar',
+        'expires_in': 1800,
+        'linked': None
+    })
+
+    def test_login(self):
+        self.do_login()
+
+    def test_partial_pipeline(self):
+        self.do_partial_pipeline()

-- 
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