Skip to content

Commit

Permalink
feat: add mitxpro-social-auth (#376)
Browse files Browse the repository at this point in the history
* feat: add mitxpro-social-auth

* fix: github CI

* fix: test

* fix: precommit errors

* fix: Update and rename README.md to README.rst

* fix: issues

* fix: issues

* fix: issues

* fix: fixed install dependencies issue
  • Loading branch information
Anas12091101 authored Oct 3, 2024
1 parent 6abb6fe commit 3f54ed4
Show file tree
Hide file tree
Showing 14 changed files with 349 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
DIRECTORY="tutor"
DEV="tutor_dev"
fi
EDX_WORKSPACE=$PWD/.. docker-compose -f /home/runner/.local/share/$DIRECTORY/env/local/docker-compose.yml -f /home/runner/.local/share/$DIRECTORY/env/dev/docker-compose.yml --project-name $DEV run -v $PWD/../open-edx-plugins:/open-edx-plugins lms /open-edx-plugins/run_devstack_integration_tests.sh
EDX_WORKSPACE=$PWD/.. docker compose -f /home/runner/.local/share/$DIRECTORY/env/local/docker-compose.yml -f /home/runner/.local/share/$DIRECTORY/env/dev/docker-compose.yml --project-name $DEV run -v $PWD/../open-edx-plugins:/open-edx-plugins lms /open-edx-plugins/run_devstack_integration_tests.sh
- name: Upload coverage to CodeCov
uses: codecov/codecov-action@v4
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ repos:
- id: ruff
args: [--extend-ignore=D1, --fix]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.10.1
rev: v1.11.2
hooks:
- id: mypy
additional_dependencies:
- types-pytz
- types-requests
- types-pkg_resources
- types-python-dateutil
- types-setuptools
- repo: https://github.com/rhysd/actionlint
rev: v1.7.1
hooks:
Expand Down
91 changes: 90 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ gitpython = "^3.1.37"
python-json-logger = "^2.0.2"
sentry-sdk = "^2.0.0"
XBlock = "*"
social-auth-core = "^4.5.4"
opentelemetry-distro = "*"
opentelemetry-instrumentation-django = "*"
opentelemetry-exporter-richconsole = "*"
Expand Down
1 change: 1 addition & 0 deletions run_devstack_integration_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ cd /open-edx-plugins

# Installing test dependencies
pip install pytest-mock==3.14.0
pip install responses==0.25.3

# Plugins that may affect the tests of other plugins.
# e.g. openedx-companion-auth adds a redirect to the authentication
Expand Down
21 changes: 21 additions & 0 deletions src/ol_social_auth/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
python_sources(
name="ol_social_auth_source",
dependencies=["//:external_dependencies#social-auth-core"]
)

python_distribution(
name="ol_social_auth_package",
dependencies=[
":ol_social_auth_source",
],
provides=setup_py(
name="ol-social-auth",
version="0.1.0",
description="An Open edX plugin implementing MIT social auth backend",
license="BSD-3-Clause",
author="MIT Office of Digital Learning",
include_package_data=True,
zip_safe=False,
keywords="Python edx"
),
)
28 changes: 28 additions & 0 deletions src/ol_social_auth/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Copyright (C) 2022 MIT Open Learning

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 changes: 52 additions & 0 deletions src/ol_social_auth/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
Open Learning Social Auth
=======================

An Open edX plugin implementing MIT social auth backend

Version Compatibility
---------------------

Compatible with all edx releases

Installing The Plugin
---------------------

You can install this plugin into any Open edX instance by using any of the following methods:

**Option 1: Install from PyPI**

.. code-block::
# If running devstack in docker, first open a shell in LMS (make lms-shell)
pip install ol-social-auth
**Option 2: Build the package locally and install it**

Follow these steps in a terminal on your machine:

1. Navigate to ``open-edx-plugins`` directory
2. If you haven't done so already, run ``./pants build``
3. Run ``./pants package ::``. This will create a "dist" directory inside "open-edx-plugins" directory with ".whl" & ".tar.gz" format packages for all plugins in the src directory
4. Move/copy any of the ".whl" or ".tar.gz" files for this plugin that were generated in the above step to the machine/container running Open edX (NOTE: If running devstack via Docker, you can use ``docker cp`` to copy these files into your LMS or CMS containers)
5. Run a shell in the machine/container running Open edX, and install this plugin using pip


``Note``: In some cases you might need to restart edx-platform after installing the plugin to reflect the changes.

Configurations
--------------
This section outlines the steps for integrating your application with ol-social-auth for various deployment scenarios. Please refer to the corresponding documentation for detailed instructions.

* **MITxPRO:** To configure ol-social-auth with MITxPRO, follow the comprehensive guide available `here <https://github.com/mitodl/mitxpro/blob/master/docs/configure_open_edx.md>`_
* **MITxOnline:** For integration with MITxOnline, detailed instructions can be found in the official documentation `here <https://github.com/mitodl/mitxonline/blob/main/docs/source/configuration/open_edx.rst>`_
* **MITxOnline with Tutor:** If you're using MITxOnline with Tutor for development purposes, specific configuration steps are outlined in the `documentation <https://github.com/mitodl/mitxonline/blob/main/docs/source/configuration/tutor.rst>`_


How to use
----------
Make sure to properly configure the plugin following the links in the above "Configurations" section before use.
* Install the plugin in the lms following the installation steps above.
* Verify that you are not logged in on edx-platform.
* Create a new user in your MIT application and verify that a corresponding user is successfully created on the edX platform.
Empty file added src/ol_social_auth/__init__.py
Empty file.
63 changes: 63 additions & 0 deletions src/ol_social_auth/backends.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""MIT xPro social auth backend"""

from social_core.backends.oauth import BaseOAuth2


class MITxProOAuth2(BaseOAuth2):
"""MIT xPro social auth backend"""

name = "mitxpro-oauth2"

ID_KEY = "username"
REQUIRES_EMAIL_VALIDATION = False

ACCESS_TOKEN_METHOD = "POST" # noqa: S105

# at a minimum we need to be able to read the user
DEFAULT_SCOPE = ["user:read"]

def authorization_url(self):
"""Provides authorization_url from settings""" # noqa: D401
return self.setting("AUTHORIZATION_URL")

def access_token_url(self):
"""Provides access_token_url from settings""" # noqa: D401
return self.setting("ACCESS_TOKEN_URL")

def api_root(self):
"""Returns the API root as configured""" # noqa: D401
root = self.setting("API_ROOT")

if root and root[-1] != "/":
root = f"{root}/"

return root

def auth_html(self): # pragma: no cover
"""No html for this provider"""
# NOTE: this is only here to stop the pylint warning about this abstract
# method not being overridden without disabling it for the entire class
return ""

def api_url(self, path):
"""
Returns the full api url given a relative path
Args:
path (str): relative api path
""" # noqa: D401
return f"{self.api_root()}{path}"

def get_user_details(self, response):
"""Return user details from xPro account"""
return {
"username": response.get("username"),
"email": response.get("email", ""),
"name": response.get("name", ""),
}

def user_data(self, access_token, *args, **kwargs): # noqa: ARG002
"""Loads user data from xpro""" # noqa: D401
url = self.api_url("api/users/me")
headers = {"Authorization": f"Bearer {access_token}"}
return self.get_json(url, headers=headers)
Empty file.
Loading

0 comments on commit 3f54ed4

Please sign in to comment.