djangosalesforcedjango-allauthdjango-rest-authjsforce

Trouble getting Salesforce login working with dj-rest-auth + django-allauth


My app has a Django 3.1 backend with django-allauth and dj-rest-auth (actively supported fork of django-rest-auth).

My mobile and web frontends can already sign in using Facebook and Google via REST. I'm now trying to add Salesforce as a 3rd REST social login method, but am running into issues.

I've followed the django-allauth instructions for Salesforce:

I've been using client-side JSforce to kick off the Salesforce auth request in the frontend, but I'm open to other methods if they are simpler/better/etc.

Running jsforce.browser.login() in my clients' JS code opens a Salesforce login popup. After entering Salesforce login credentials, the Salesforce system redirected to my defined callback URL, resulting in a page that shows the following text:

Social Network Login Failure
An error occurred while attempting to login via your social network account.

The URL in the address bar on that page looks something like this:

https://www.mywebdomain.com/accounts/salesforce/login/callback/#access_token=00D3t000004QWRm%21ARwAQPfHWiM6jdB43dlyW6qjEw._34mjzGi_Jv6YCXp0QssT.9F9lCge5_YaH8gqTy3Od6SywCs8X9zOGv145SyviBVeGdn0&instance_url=https%3A%2F%2Fna123.salesforce.com&id=https%3A%2F%2Flogin.salesforce.com%2Fid%2F00D3t000004QWRmEAO%2F0053t000008QBetAAG&issued_at=1606802917608&signature=KvxAX0WBCFQYY%2BO25id9%2FXxpbh2q2d2vWdQ%2FFV5FCBw%3D&state=jsforce0.popup.c0ockgct29g&scope=id+api+web+refresh_token+openid&token_type=Bearer

I tried to debug and print the error in my backend, but both auth_error.code and auth_error.exception were blank/empty.

I also tried sending the access_token from that URL's hash to my Salesforce API endpoint (see below), but that resulted in a 400 error ("Incorrect value").

Here is how I've defined my SocialLoginViews in my views.py, based on dj-rest-auth's social auth documentation:

from dj_rest_auth.registration.views import SocialLoginView
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
from allauth.socialaccount.providers.google.views import GoogleOAuth2Adapter
from allauth.socialaccount.providers.salesforce.views import SalesforceOAuth2Adapter


class FacebookLogin(SocialLoginView):
    adapter_class = FacebookOAuth2Adapter


class GoogleLogin(SocialLoginView):
    adapter_class = GoogleOAuth2Adapter


class SalesforceLogin(SocialLoginView):
    adapter_class = SalesforceOAuth2Adapter

My urls.py:

from .views import FacebookLogin, GoogleLogin, SalesforceLogin

urlpatterns = [
    ...
    # Sending access_token to the Facebook and Google REST endpoints works,
    # but doing the same for the Salesforce REST endpoint does not (400 error: "Incorrect value")
    url(r'^api/rest-auth/facebook/$', FacebookLogin.as_view(), name='fb_login'),
    url(r'^api/rest-auth/google/$', GoogleLogin.as_view(), name='google_login'),
    url(r'^api/rest-auth/salesforce/$', SalesforceLogin.as_view(), name='salesforce_login'),
    ...
]

How can Salesforce social auth be made to work in this app?


Solution

  • I figured it out and got it working: when posting to my dj-rest-auth Salesforce API endpoint, I was only including access_token in my POST body. I actually need both access_token and key, where key is the Salesforce login URL ("https://login.salesforce.com").

    It was indeed in the django-allauth instructions for Salesforce, but I misinterpreted the wording. I now know that it says to require both access_token and key in the POST body.