-
-
Notifications
You must be signed in to change notification settings - Fork 178
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
Issues/523 saml auth create email address objects #544
Open
kaushikaryan04
wants to merge
5
commits into
openwisp:master
Choose a base branch
from
kaushikaryan04:issues/523-SAML-auth-create-email-address-objects
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cf028b2
[change] Saml-Authentication-creates-EmailAddress-Objects #523
kaushikaryan04 5935051
Merge branch 'openwisp:master' into issues/523-SAML-auth-create-email…
kaushikaryan04 5d9faeb
Merge branch 'master' into issues/523-SAML-auth-create-email-address-…
nemesifier 124db52
[change] Implemented-suggested-changes #523
kaushikaryan04 fe7e794
Merge branch 'master' into issues/523-SAML-auth-create-email-address-…
nemesifier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -2,6 +2,7 @@ | |||
from urllib.parse import parse_qs, urlparse | ||||
|
||||
import swapper | ||||
from allauth.account.models import EmailAddress | ||||
from django.conf import settings | ||||
from django.contrib.auth import logout | ||||
from django.core.exceptions import ObjectDoesNotExist, PermissionDenied | ||||
|
@@ -66,6 +67,22 @@ def post_login_hook(self, request, user, session_info): | |||
try: | ||||
user.registered_user | ||||
except ObjectDoesNotExist: | ||||
email = None | ||||
uid_is_email = 'email' in getattr( | ||||
settings, 'SAML_ATTRIBUTE_MAPPING', {} | ||||
).get('uid', ()) | ||||
if uid_is_email: | ||||
email = session_info['name_id'].text | ||||
if email is None: | ||||
email = session_info['ava'].get('email', [None])[0] | ||||
if email: | ||||
user.email = email | ||||
user.save() | ||||
email_address = EmailAddress.objects.create( | ||||
user=user, email=email, verified=True, primary=True | ||||
) | ||||
email_address.save() | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove (it's redundant,
Suggested change
|
||||
|
||||
registered_user = RegisteredUser( | ||||
user=user, method='saml', is_verified=app_settings.SAML_IS_VERIFIED | ||||
) | ||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
from urllib.parse import parse_qs, urlparse | ||
|
||
import swapper | ||
from allauth.account.models import EmailAddress | ||
from django.contrib.auth import SESSION_KEY, get_user_model | ||
from django.test import TestCase, override_settings | ||
from django.urls import reverse | ||
|
@@ -69,6 +70,8 @@ def _post_successful_auth_assertions(self, query_params, org_slug): | |
self.assertEqual(User.objects.count(), 1) | ||
user_id = self.client.session[SESSION_KEY] | ||
user = User.objects.get(id=user_id) | ||
email = EmailAddress.objects.filter(user=user) | ||
self.assertEqual(email.count(), 1) | ||
self.assertEqual(user.username, '[email protected]') | ||
self.assertEqual(OrganizationUser.objects.count(), 1) | ||
org_user = OrganizationUser.objects.get(user_id=user_id) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Saving this information without validating it is dangerous.
Here we need to run
full_clean()
and catch any possible exception in case the email we get from the identity provider is invalid for some reason. If the email is invalid wont save/create, log a warning and skip, eg:Please add a new test for this error case, you can use
mock
or any other way to pass an invalid email and catch the validation error, there's similar tests in this and other modules you can base your test on, look forValidationError
in the test suites of the codebase.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem.The new unified documentation looks great. I will make the suggested changes.