omab / python-social-auth

Social auth made simple
http://psa.matiasaguirre.net
BSD 3-Clause "New" or "Revised" License
2.83k stars 1.09k forks source link

Multiple apps integration for the same provider #1041

Closed santhoshraju18 closed 7 years ago

santhoshraju18 commented 7 years ago

Hi Guys,

Is there a way to use multiple social apps to integrate with each backend? Like I have two apps for Facebook and I want to dynamically use those app credentials to authenticate social users instead of specifying them in Django settings. Can this be done @omab ?

Thanks.

omab commented 7 years ago

By itself, the app won't support that schema, but you can override it simply by defining your own Facebook backend that overrides the get_key_and_secret() method:

from social.backends.facebook import FacebookOAuth2

class CustomFacebook(FacebookOAuth2):
    def get_key_and_secret(self):
        key = get_facebook_app_key_based_on_some_rule()
        secret = get_facebook_app_secret_based_on_some_rule()
        return key, secret

Then use that backend by replacing social.backends.facebook.FacebookOAuth2 in the AUTHENTICATION_BACKENDS setting with the import path to your CustomFacebook one.

santhoshraju18 commented 7 years ago

Okay thanks for helping with the workaround @omab. But the real problem for us is to pass instance id as an argument to the get_key_and_secret() method so as to obtain the auth_key and auth_secret from a different model.

omab commented 7 years ago

Depends on your application, you can do it by:

  1. Overriding the python-social-auth views in order to specify the needed option
  2. Adding a parameter to the links querystring (which can then be retrieved in the backends by doing self.strategy.request_data())
  3. Adding a parameter to the session (which can be accessed in the backends with this.strategy.session
santhoshraju18 commented 7 years ago

Thanks @omab it really works using both steps 2 and 3 combined together. Closing this now.