From 9aef39defa00cbe80ef9ab0aa25817a160511fdf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rafael=20L=C3=A1szl=C3=B3?= <rlacko99@gmail.com>
Date: Tue, 19 Jan 2021 02:02:15 +0100
Subject: [PATCH] added authsch lib and store username instead of external id
 in user

---
 requirements/base.in           |  1 -
 src/authsch/__init__.py        |  0
 src/authsch/apps.py            |  6 ++++++
 src/authsch/authentication.py  | 35 ++++++++++++++++++++++++++++++++++
 src/kszkepzes/settings/base.py |  1 -
 5 files changed, 41 insertions(+), 2 deletions(-)
 create mode 100644 src/authsch/__init__.py
 create mode 100644 src/authsch/apps.py
 create mode 100644 src/authsch/authentication.py

diff --git a/requirements/base.in b/requirements/base.in
index e319619..962a298 100644
--- a/requirements/base.in
+++ b/requirements/base.in
@@ -1,5 +1,4 @@
 Django==2.2.4
 djangorestframework==3.10.2
-django-social-authsch==0.1
 django-solo==1.1.3
 django-import-export==1.2.0
diff --git a/src/authsch/__init__.py b/src/authsch/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/src/authsch/apps.py b/src/authsch/apps.py
new file mode 100644
index 0000000..13882ba
--- /dev/null
+++ b/src/authsch/apps.py
@@ -0,0 +1,6 @@
+# Original: https://git.sch.bme.hu/kszk/devteam/django-authsch
+from django.apps import AppConfig
+
+
+class AuthschConfig(AppConfig):
+    name = 'authsch'
diff --git a/src/authsch/authentication.py b/src/authsch/authentication.py
new file mode 100644
index 0000000..b7e105a
--- /dev/null
+++ b/src/authsch/authentication.py
@@ -0,0 +1,35 @@
+# Original: https://git.sch.bme.hu/kszk/devteam/django-authsch
+# store schacc in username field
+from social_core.backends.oauth import BaseOAuth2
+
+
+class AuthSCHOAuth2(BaseOAuth2):
+    """AuthSCH OAuth2 authentication backend"""
+    name = 'authsch'
+    ID_KEY = 'internal_id'
+    AUTHORIZATION_URL = 'https://auth.sch.bme.hu/site/login'
+    ACCESS_TOKEN_URL = 'https://auth.sch.bme.hu/oauth2/token'
+    ACCESS_TOKEN_METHOD = 'POST'
+    REFRESH_TOKEN_URL = 'https://auth.sch.bme.hu/oauth2/token'
+    DEFAULT_SCOPE = ['basic', 'mail', 'givenName', 'sn', 'linkedAccounts']
+    EXTRA_DATA = [
+        ('internal_id', 'id'),
+        ('expires_in', 'expires'),
+        ('refresh_token', 'refresh_token'),
+    ]
+
+    def get_user_details(self, response):
+        """Return user details from AuthSCH account"""
+        return {
+            'username': response.get('linkedAccounts').get('schacc'), 
+            'email': response.get('mail'),
+            'first_name': response.get('givenName'),
+            'last_name': response.get('sn')
+        }
+
+    def user_data(self, access_token, *args, **kwargs):
+        """Loads user data from service"""
+        return self.get_json(
+            'https://auth.sch.bme.hu/api/profile/',
+            params={'access_token': access_token}
+        )
diff --git a/src/kszkepzes/settings/base.py b/src/kszkepzes/settings/base.py
index fa4afc6..1e96215 100644
--- a/src/kszkepzes/settings/base.py
+++ b/src/kszkepzes/settings/base.py
@@ -27,7 +27,6 @@ DEBUG = True
 
 ALLOWED_HOSTS = []
 
-
 # Application definition
 
 INSTALLED_APPS = [
-- 
GitLab