-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from onaio/support-opensrp-fhir-core-oauth
Support Opensrp Fhir Core oAuth
- Loading branch information
Showing
6 changed files
with
27 additions
and
57 deletions.
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
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
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 |
---|---|---|
|
@@ -305,16 +305,16 @@ def oauth_user_info(self, provider, response=None): | |
if is_valid_provider(provider, "OpenSRP"): | ||
user_object = ( | ||
self.appbuilder.sm.oauth_remotes[provider] | ||
.get("user-details", token=response) | ||
.get("userinfo", token=response) | ||
.json() | ||
) | ||
|
||
username = user_object.get("username") or user_object.get("userName") | ||
username = user_object.get("preferred_username") | ||
|
||
result = {"username": username} | ||
|
||
if user_object.get("preferredName"): | ||
result["name"] = user_object.get("preferredName") | ||
if user_object.get("name"): | ||
result["name"] = user_object.get("name") | ||
|
||
if email_base: | ||
# change emails from [email protected] to [email protected] | ||
|
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
"""Version goes here - to avoid cyclic dependencies :-(""" | ||
VERSION = (0, 4, 0) | ||
VERSION = (0, 5, 0) | ||
__version__ = ".".join(str(v) for v in VERSION) |
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 |
---|---|---|
|
@@ -188,60 +188,27 @@ def test_oauth_user_info_opensrp_provider(self): # pylint: disable=R0201 | |
# set test configs | ||
app.config["PATCHUP_EMAIL_BASE"] = "[email protected]" | ||
|
||
# Sample data returned OpenSRP | ||
data = {"userName": "tlv1", "roles": ["Privilege Level: Full"]} | ||
# Sample data returned KeyCloak | ||
data = { | ||
"sub": "296fad33-ad11-4030-86e5-405025149224", | ||
"email_verified": False, | ||
"preferred_username": "test-user", | ||
"enabled": True | ||
} | ||
|
||
# Expected result | ||
result_info = {"email": "noreply+tlv1@example.com", "username": "tlv1"} | ||
result_info = {"email": "noreply+test-user@example.com", "username": "test-user"} | ||
|
||
appbuilder = MagicMock() | ||
user_mock = MagicMock() | ||
user_mock.json.return_value = data | ||
appbuilder.sm.oauth_remotes["OpenSRP"].get = MagicMock(side_effect=[user_mock]) | ||
appbuilder.sm.oauth_remotes["OpenSRP"].get = MagicMock( | ||
side_effect=[user_mock] | ||
) | ||
csm = CustomSecurityManager(appbuilder=appbuilder) | ||
user_info = csm.oauth_user_info(provider="OpenSRP") | ||
assert user_info == result_info | ||
|
||
# Sample data returned OpenSRP with preferredName | ||
data2 = { | ||
"preferredName": "mosh", | ||
"userName": "mosh", | ||
"roles": ["Privilege Level: Full"], | ||
} | ||
|
||
# Expected result | ||
result_info2 = { | ||
"email": "[email protected]", | ||
"name": "mosh", | ||
"username": "mosh", | ||
} | ||
|
||
appbuilder2 = MagicMock() | ||
user_mock2 = MagicMock() | ||
request_mock = MagicMock(side_effect=[user_mock2]) | ||
user_mock2.json.return_value = data2 | ||
appbuilder2.sm.oauth_remotes["OpenSRP"].get = request_mock | ||
csm2 = CustomSecurityManager(appbuilder=appbuilder2) | ||
user_info2 = csm2.oauth_user_info(provider="OpenSRP") | ||
request_mock.assert_called_once_with("user-details", token=None) | ||
assert user_info2 == result_info2 | ||
|
||
# Sample data returned OpenSRP v2 | ||
data3 = {"username": "mosh", "roles": ["Privilege Level: Full"]} | ||
|
||
# Expected result | ||
result_info3 = {"email": "[email protected]", "username": "mosh"} | ||
|
||
appbuilder3 = MagicMock() | ||
user_mock3 = MagicMock() | ||
user_mock3.json.return_value = data3 | ||
appbuilder3.sm.oauth_remotes["OpenSRP"].get = MagicMock( | ||
side_effect=[user_mock3] | ||
) | ||
csm3 = CustomSecurityManager(appbuilder=appbuilder3) | ||
user_info3 = csm3.oauth_user_info(provider="OpenSRP") | ||
assert user_info3 == result_info3 | ||
|
||
def test_oauth_user_info_no_provider(self): # pylint: disable=R0201 | ||
""" | ||
Test that when no provider is provided | ||
|
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