Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@W-16662200 - Fix auth params #52

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
name="sfdo-template-helpers", # Required
# https://www.python.org/dev/peps/pep-0440/
# https://packaging.python.org/en/latest/single_source_version.html
version="0.23.0", # Required
version="0.24.0", # Required
# https://packaging.python.org/specifications/core-metadata/#summary
description="A set of Django helpers and utils used by sfdo-template projects.",
# https://packaging.python.org/specifications/core-metadata/#description-optional
Expand Down
43 changes: 28 additions & 15 deletions sfdo_template_helpers/oauth2/salesforce/provider.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
from allauth.socialaccount.providers.salesforce.provider import SalesforceProvider


class SFDOSalesforceProvider(SalesforceProvider):
package = "sfdo_template_helpers.oauth2.salesforce"
# If you want this provider to replace the built-in "salesforce" provider, set:
id = "salesforce"
#
# If you want a *separate* provider alongside the built-in one, set a unique ID:
# id = "sfdo_salesforce"

def get_auth_params(self, request, action):
ret = super().get_auth_params(request, action)
# This will ensure that even if you're logged in to Salesforce,
# you'll be prompted to choose an identity to auth as:
ret["prompt"] = "login"
return ret
def get_auth_params_from_request(self, request, action):
# Call super() to retrieve existing params, then add/override as needed
params = super().get_auth_params_from_request(request, action)
# Force Salesforce to prompt a new login rather than reusing existing creds
params["prompt"] = "login"
return params

def extract_uid(self, data):
# The SalesforceProvider in allauth assumes that user_id is unique,
# but it can be the same between multiple sandboxes that were
# copied from the same production org. So we need to add the org id
# too to disambiguate.
return f"{data['organization_id']}/{data['user_id']}"
"""
The built-in SalesforceProvider uses data['user_id'] as the UID.
Here, we combine organization_id + user_id so that
multiple sandboxes (copied from the same production org)
won't share the same UID.
"""
org_id = data.get("organization_id", "")
user_id = data.get("user_id", "")
return f"{org_id}/{user_id}"

def extract_common_fields(self, data):
# Get fields used to populate the Django user.
return {"username": f"{data['organization_id']}_{data['user_id']}"}
"""
Map data returned from Salesforce to Django's User model fields.
This example sets 'username' to org_id_user_id.
"""
org_id = data.get("organization_id", "")
user_id = data.get("user_id", "")
return {"username": f"{org_id}_{user_id}"}


# Required by django-allauth to load custom providers
provider_classes = [SFDOSalesforceProvider]
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def dummy_app():
@pytest.mark.django_db
def test_get_auth_params(rf, dummy_app):
request = rf.get("/")
result = SFDOSalesforceProvider(request, dummy_app).get_auth_params(request, None)
result = SFDOSalesforceProvider(request, dummy_app).get_auth_params()
assert "prompt" in result and result["prompt"] == "login"


Expand Down
Loading