From 00e282e32b46bb4b6040dc3810599c693306c0ec Mon Sep 17 00:00:00 2001 From: David Croft Date: Thu, 24 Mar 2022 11:09:14 +0000 Subject: [PATCH] add uffd oauth2 backend --- social_core/backends/uffd.py | 51 ++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 social_core/backends/uffd.py diff --git a/social_core/backends/uffd.py b/social_core/backends/uffd.py new file mode 100644 index 00000000..fb8ffb62 --- /dev/null +++ b/social_core/backends/uffd.py @@ -0,0 +1,51 @@ +from urllib.parse import urlencode + +from .oauth import BaseOAuth2 + + +class UffdOAuth2(BaseOAuth2): + """Uffd OAuth2 authentication backend + + You need to set the following config: + SOCIAL_AUTH_UFFD_KEY - client id + SOCIAL_AUTH_UFFD_SECRET - client secret + SOCIAL_AUTH_UFFD_BASE_URL - base url to uffd installation + """ + + name = 'uffd' + ACCESS_TOKEN_METHOD = 'POST' + REFRESH_TOKEN_METHOD = 'POST' + SCOPE_SEPARATOR = ' ' + STATE_PARAMETER = True + REDIRECT_STATE = False + EXTRA_DATA = [ + ('id', 'id'), + ] + + def get_user_details(self, response): + """Return user details from a Uffd account""" + fullname, first_name, last_name = self.get_user_names(fullname=response.get('name')) + return { + 'username': response.get('nickname'), + 'email': response.get('email') or '', + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name, + } + + def user_data(self, access_token, *args, **kwargs): + """Loads user data from service""" + url = self.userinfo_url() + '?' + urlencode({'access_token': access_token}) + try: + return self.get_json(url) + except ValueError: + return None + + def authorization_url(self): + return self.setting('BASE_URL') + '/oauth2/authorize' + + def access_token_url(self): + return self.setting('BASE_URL') + '/oauth2/token' + + def userinfo_url(self): + return self.setting('BASE_URL') + '/oauth2/userinfo' -- 2.38.1