dummy.py 1.54 KB
Newer Older
1
"""
2
DummyBackend: A fake Third Party Auth provider for testing & development purposes.
3
"""
4
from social.backends.oauth import BaseOAuth2
5 6 7
from social.exceptions import AuthFailed


8
class DummyBackend(BaseOAuth2):  # pylint: disable=abstract-method
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    """
    python-social-auth backend that doesn't actually go to any third party site
    """
    name = "dummy"
    SUCCEED = True  # You can patch this during tests in order to control whether or not login works

    def auth_url(self):
        """ Get the URL to which we must redirect in order to authenticate the user """
        return self.redirect_uri

    def get_user_details(self, response):
        """ Get user details like full name, email, etc. from the third party """
        return {
            'fullname': "William Adama",
            'first_name': "Bill",
            'last_name': "Adama",
            'username': "Galactica1",
            'email': "adama@fleet.colonies.gov",
        }

    def get_user_id(self, details, response):
        """ Get the permanent ID for this user from the third party. """
        return '1234'

    def auth_complete(self, *args, **kwargs):
        """
        The user has been redirected back from the third party and we should now log them in, if
        everything checks out.
        """
        if not DummyBackend.SUCCEED:
            raise AuthFailed(self, 'Third Party login failed.')

        response = {
            'dummy': True,
        }

        kwargs.update({'response': response, 'backend': self})

        return self.strategy.authenticate(*args, **kwargs)