PK!social_auth_mitxpro/__init__.pyPK!4MMsocial_auth_mitxpro/backends.py"""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" # 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""" return self.setting("AUTHORIZATION_URL") def access_token_url(self): """Provides access_token_url from settings""" return self.setting("ACCESS_TOKEN_URL") def api_root(self): """Returns the API root as configured""" root = self.setting("API_ROOT") if root and root[-1] != "/": root = "{}/".format(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 """ return "{}{}".format(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): """Loads user data from xpro""" url = self.api_url("api/users/me") headers = {"Authorization": "Bearer {}".format(access_token)} return self.get_json(url, headers=headers) PK!r$social_auth_mitxpro/backends_test.py"""Tests for our backend""" import pytest from social_auth_mitxpro.backends import MITxProOAuth2 # pylint: disable=redefined-outer-name @pytest.fixture def strategy(mocker): """Mock strategy""" return mocker.Mock() @pytest.fixture def backend(strategy): """MITxProOAuth2 backend fixture""" return MITxProOAuth2(strategy) @pytest.mark.parametrize( "response, expected", [ ( {"username": "abc123", "email": "user@example.com", "name": "Jane Doe"}, {"username": "abc123", "email": "user@example.com", "name": "Jane Doe"}, ), ({"username": "abc123"}, {"username": "abc123", "email": "", "name": ""}), ], ) def test_get_user_details(backend, response, expected): """Test that get_user_details produces expected results""" assert backend.get_user_details(response) == expected @pytest.mark.parametrize( "api_root", ["http://xpro.example.com/", "http://xpro.example.com"] ) def test_user_data(backend, strategy, mocked_responses, api_root): """Tests that the backend makes a correct appropriate request""" access_token = "user_token" response = {"username": "abc123", "email": "user@example.com", "name": "Jane Doe"} mocked_responses.add( mocked_responses.GET, "http://xpro.example.com/api/users/me", json=response ) strategy.setting.return_value = api_root assert backend.user_data(access_token) request, _ = mocked_responses.calls[0] assert request.headers["Authorization"] == "Bearer user_token" strategy.setting.assert_any_call("API_ROOT", default=None, backend=backend) def test_authorization_url(backend, strategy): """Test authorization_url()""" strategy.setting.return_value = "abc" assert backend.authorization_url() == "abc" strategy.setting.assert_called_once_with( "AUTHORIZATION_URL", default=None, backend=backend ) def test_access_token_url(backend, strategy): """Test access_token_url()""" strategy.setting.return_value = "abc" assert backend.access_token_url() == "abc" strategy.setting.assert_called_once_with( "ACCESS_TOKEN_URL", default=None, backend=backend ) PK!yL#social_auth_mitxpro/conftest.py"""Common test configuration""" import pytest import responses @pytest.fixture def mocked_responses(): """Mock requests responses""" with responses.RequestsMock() as rsps: yield rsps PK!)social_auth_mitxpro-0.2.dist-info/LICENSEBSD 3-Clause License Copyright (c) 2017, MIT Office of Digital 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. PK!H|n-WY'social_auth_mitxpro-0.2.dist-info/WHEEL A н#Z;/" bFF]xzwK;<*mTֻ0*Ri.4Vm0[H, JPK!H}q}[ *social_auth_mitxpro-0.2.dist-info/METADATAVmOF_1}(D mr*"A=w}}gwmCH-fyf6why-6Bc5&`T&x֖q-sU4yn.+e[a0C7 ߥIfkS+fvv(Py.H?r%-,p\K!^'ƚ*w1љaF+ &'|Φ);ap8t84~fƈ@Jhse܋LܶWpeRu,*a==¨%|:Ā#Q={ vC՝gg8}>g/}S`ڦQRɓҏ6|1@~; m[^{ S)u2 s,u.SYEG 6Vf:$`}c Qحq[Q\&By"eQr )7դdV쐂=aJ[3Kz4ryw&kGJs}vطkg*G'9qޞB؇`! ?PVl2ڲqJά1q\.E6e&*cdr]<зV)!aK @HS_8>aǥL;q W5h+uaɳxw;يa_iSu_}+?OQ*N<Ф{vʌ- zL˥G,bKo#aCd PK!social_auth_mitxpro/__init__.pyPK!4MM=social_auth_mitxpro/backends.pyPK!r$social_auth_mitxpro/backends_test.pyPK!yL#social_auth_mitxpro/conftest.pyPK!)social_auth_mitxpro-0.2.dist-info/LICENSEPK!H|n-WY'social_auth_mitxpro-0.2.dist-info/WHEELPK!H}q}[ *usocial_auth_mitxpro-0.2.dist-info/METADATAPK!Hg(social_auth_mitxpro-0.2.dist-info/RECORDPK