Esempio n. 1
0
 def test_case_insensitivity_for_provider(self):
     """
     Test that provider information form user can be case insesitive,
     to static standard strings that  they will be checked against
     """
     assert is_valid_provider("opensrp", "OpenSRP")
     assert is_valid_provider("OnaData", 'onadata')
     assert is_valid_provider("OpenlMis", "openlmis")
     assert not is_valid_provider("oensrp", "OpenSrp")
Esempio n. 2
0
    def oauth_user_info(self, provider, response=None):
        """Get user info"""

        # dirty hack.  We need to load the app from here because at the top
        # of the file superset is not yet initialized with an app property
        from superset import app

        # this is used for provider's whose users do not have an email address
        # superset requires an email address and so we need to provide it
        # the email base should be a string e.g. [email protected]
        # we then use it to construct email addresses for the user logging in
        # that look like [email protected] (if the base was as set
        # above)
        email_base = app.config.get("PATCHUP_EMAIL_BASE")

        if is_valid_provider(provider, "onadata"):
            user = (self.appbuilder.sm.oauth_remotes[provider].get(
                "api/v1/user.json").data)

            user_data = (self.appbuilder.sm.oauth_remotes[provider].get(
                f"api/v1/profiles/{user['username']}.json").data)

            return {
                "name": user_data["name"],
                "email": user_data["email"],
                "id": user_data["id"],
                "username": user["username"],
                "first_name": user_data["first_name"],
                "last_name": user_data["last_name"],
            }

        if is_valid_provider(provider, "OpenSRP"):
            user_object = (self.appbuilder.sm.oauth_remotes[provider].get(
                "user-details").data)
            username = user_object["userName"]

            result = {"username": username}

            if user_object.get("preferredName"):
                result["name"] = user_object.get("preferredName")

            if email_base:
                # change emails from [email protected] to [email protected]
                result["email"] = email_base.replace("@", f"+{username}@")

            return result

        if is_valid_provider(provider, "openlmis"):
            # get access token
            my_token = self.oauth_tokengetter()[0]
            # get referenceDataUserId
            reference_user = self.appbuilder.sm.oauth_remotes[provider].post(
                "oauth/check_token", data={"token": my_token})
            reference_data_user_id = reference_user.data["referenceDataUserId"]
            # get user details
            endpoint = f"users/{reference_data_user_id}"
            user_data = self.appbuilder.sm.oauth_remotes[provider].get(
                endpoint).data
            # get email
            email_endpoint = f"userContactDetails/{reference_data_user_id}"
            email = self.appbuilder.sm.oauth_remotes[provider].get(
                email_endpoint).data
            return {
                "name": user_data["username"],
                "email": email["emailDetails"]["email"],
                "id": user_data["id"],
                "username": user_data["username"],
                "first_name": user_data["firstName"],
                "last_name": user_data["lastName"],
            }

        return None