revert subclass

This commit is contained in:
lpm0073 2022-10-04 09:21:54 -05:00
parent bab30abe34
commit a8f81db1f4
3 changed files with 35 additions and 39 deletions

View File

@ -26,7 +26,7 @@ include this repo in your project's requiremets.txt, or install it from the comm
ADDL_INSTALLED_APPS: ADDL_INSTALLED_APPS:
- "wp_oauth_backend" - "wp_oauth_backend"
THIRD_PARTY_AUTH_BACKENDS: THIRD_PARTY_AUTH_BACKENDS:
- "wp_oauth_backend.wp_oauth.StepwiseMathOAuth2" - "wp_oauth_backend.wp_oauth.StepwiseMathWPOAuth2"
ENABLE_REQUIRE_THIRD_PARTY_AUTH: true ENABLE_REQUIRE_THIRD_PARTY_AUTH: true
add these settings to django.conf: add these settings to django.conf:

View File

@ -1,5 +0,0 @@
"""
An OAuth backend for WordPress.
It's mostly used for Open edX but can be used elsewhere.
"""

View File

@ -18,7 +18,7 @@ from logging import getLogger
logger = getLogger(__name__) logger = getLogger(__name__)
VERBOSE_LOGGING = True VERBOSE_LOGGING = True
class WPOpenedxOAuth2AbstractClass(BaseOAuth2): class StepwiseMathWPOAuth2(BaseOAuth2):
""" """
WP OAuth authentication backend customized for Open edX WP OAuth authentication backend customized for Open edX
""" """
@ -27,30 +27,50 @@ class WPOpenedxOAuth2AbstractClass(BaseOAuth2):
SOCIAL_AUTH_SANITIZE_REDIRECTS = True # requires redirect domain to match the original initiating domain. SOCIAL_AUTH_SANITIZE_REDIRECTS = True # requires redirect domain to match the original initiating domain.
ACCESS_TOKEN_METHOD = 'POST' ACCESS_TOKEN_METHOD = 'POST'
@abstractmethod # This is the string value that will appear in the LMS Django Admin
# Third Party Authentication / Provider Configuration (OAuth)
# setup page drop-down box titled, "Backend name:", just above
# the "Client ID:" and "Client Secret:" fields.
def name(self): def name(self):
raise NotImplementedError("Subclasses should implement this property.") return 'stepwisemath-oauth'
@abstractmethod # note: no slash at the end of the base url. Python Social Auth
# might clean this up for you, but i'm not 100% certain of that.
def BASE_URL(self): def BASE_URL(self):
raise NotImplementedError("Subclasses should implement this property.") return "https://stepwisemath.ai"
@abstractmethod # the value of the scope separator is user-defined. Check the
# scopes field value for your oauth client in your wordpress host.
# the wp-oauth default value for scopes is 'basic' but can be
# changed to a list. example 'basic, email, profile'. This
# list can be delimited with commas, spaces, whatever.
def SCOPE_SEPARATOR(self): def SCOPE_SEPARATOR(self):
raise NotImplementedError("Subclasses should implement this property.") return ","
@property @property
def base_url(self) -> str: def base_url(self) -> str:
return self.BASE_URL return self.BASE_URL
# override AUTHORIZATION_URL in parent class
# see https://wp-oauth.com/docs/general/endpoints/
@property @property
def AUTHORIZATION_URL(self) -> str: def AUTHORIZATION_URL(self) -> str:
return f"{self.base_url}/oauth/authorize" return f"{self.base_url}/oauth/authorize"
# overrides ACCESS_TOKEN_URL from parent class
@property @property
# see https://wp-oauth.com/docs/general/endpoints/
def ACCESS_TOKEN_URL(self) -> str: def ACCESS_TOKEN_URL(self) -> str:
return f"{self.base_url}/oauth/token" return f"{self.base_url}/oauth/token"
# overrides USER_QUERY from parent class
# see https://wp-oauth.com/docs/general/endpoints/
@property
def USER_QUERY(self) -> str:
return f"{self.base_url}/oauth/me"
# overrides EXTRA_DATA from parent class
# see https://python-social-auth.readthedocs.io/en/latest/backends/implementation.html
@property @property
def EXTRA_DATA(self) -> list: def EXTRA_DATA(self) -> list:
return [ return [
@ -65,10 +85,8 @@ class WPOpenedxOAuth2AbstractClass(BaseOAuth2):
('date_joined', 'date_joined'), ('date_joined', 'date_joined'),
] ]
@property # implementation of get_user_details()
def USER_QUERY(self) -> str: # see https://python-social-auth.readthedocs.io/en/latest/backends/implementation.html
return f"{self.base_url}/oauth/me"
def get_user_details(self, response) -> dict: def get_user_details(self, response) -> dict:
"""Return user details from the WP account""" """Return user details from the WP account"""
@ -118,6 +136,11 @@ class WPOpenedxOAuth2AbstractClass(BaseOAuth2):
)) ))
return user_details return user_details
# implementation of user_data()
# note that in the case of wp oauth, the response object returned by self.USER_QUERY
# is the same as the response object passed to get_user_details().
#
# see https://python-social-auth.readthedocs.io/en/latest/backends/implementation.html
def user_data(self, access_token, *args, **kwargs) -> dict: def user_data(self, access_token, *args, **kwargs) -> dict:
"""Loads user data from service""" """Loads user data from service"""
@ -139,25 +162,3 @@ class WPOpenedxOAuth2AbstractClass(BaseOAuth2):
# utility function. not part of psa. # utility function. not part of psa.
def urlopen(self, url): def urlopen(self, url):
return urlopen(url).read().decode("utf-8") return urlopen(url).read().decode("utf-8")
class StepwiseMathOAuth2 (WPOpenedxOAuth2AbstractClass):
# This is the string value that will appear in the LMS Django Admin
# Third Party Authentication / Provider Configuration (OAuth)
# setup page drop-down box titled, "Backend name:", just above
# the "Client ID:" and "Client Secret:" fields.
def name(self):
return 'wp-oauth'
# note: no slash at the end of the base url. Python Social Auth
# might clean this up for you, but i'm not 100% certain of that.
def BASE_URL(self):
return "https://stepwisemath.ai"
# the value of the scope separator is user-defined. Check the
# scopes field value for your oauth client in your wordpress host.
# the wp-oauth default value for scopes is 'basic' but can be
# changed to a list. example 'basic, email, profile'. This
# list can be delimited with commas, spaces, whatever.
def SCOPE_SEPARATOR(self):
return ","