<commit_before>"""
For managing API calls out to external resources such as sailpoint.
"""
# import urllib.request, urllib.parse, urllib.error
# import urllib.request, urllib.error, urllib.parse
# import json
from django.conf import settings
from AccountPickup.models import OAMStatusTracker
import logging
import requests

logger = logging.getLogger(__name__)


def iiq_auth():
    return settings.SAILPOINT_USERNAME, settings.SAILPOINT_PASSWORD


# Identify a user with an expired password.
def identify_oam_login(username, password):
    if settings.DEVELOPMENT is True:
        if username == "000000000":
            return None  # Provide 'known bad' stub for testing
        stub = {
            "PSU_UUID": username,
            "DISPLAY_NAME": "Development User",
            "SPRIDEN_ID": '123456789',
            "ODIN_NAME": 'jsmith5',
            "EMAIL_ADDRESS": "<EMAIL>",
            "PSU_PUBLISH": True,
        }

        return stub

    data = {
        'username': username,
        'password': password,
    }

    url = "https://{}/identityiq/rest/custom/login/password".format(
        settings.SAILPOINT_SERVER_URL,
    )

    r = requests.post(url, auth=(settings.SAILPOINT_USERNAME, settings.SAILPOINT_PASSWORD), verify=False, data=data)

    res = r.json()

    # Massage PSU_PUBLISH:
    if "PSU_PUBLISH" in res:
        if res["PSU_PUBLISH"] is not None and res["PSU_PUBLISH"].lower() == "yes":
            res["PSU_PUBLISH"] = True
        else:
            res["PSU_PUBLISH"] = False

    if "ERROR" in res:
        return None
    else:
        return res


# This function turns a PSU_UUID into the user's identity information like name.
def identity_from_psu_uuid(psu_uuid):
    if settings.DEVELOPMENT is True:
        return {
            "PSU_UUID": psu_uuid,
            "DISPLAY_NAME": "Development User - UUID",
            "SPRIDEN_ID": '123456789',
            "ODIN_NAME": 'jsmith5',
            "EMAIL_ADDRESS": "<EMAIL>",
            "PSU_PUBLISH": True,
        }

    data = {'PSU_UUID': psu_uuid}

    url = "https://{}/identityiq/rest/custom/login/uuid".format(
        settings.SAILPOINT_SERVER_URL,
    )

    r = requests.post(url, auth=(settings.SAILPOINT_USERNAME, settings.SAILPOINT_PASSWORD), verify=False, data=data)

    results = r.json()

    # Massage PSU_PUBLISH:
    if "PSU_PUBLISH" in results:
        if results["PSU_PUBLISH"] is not None and results["PSU_PUBLISH"].lower() == "yes":
            results["PSU_PUBLISH"] = True
        else:
            results["PSU_PUBLISH"] = False

    return results


# This function returns the appropriate password constraints based on an identity.
def passwordConstraintsFromIdentity(identity):  # is this function used? PEP8 says rename to lower_case
    # TODO: Stubbed return
    return {
        'letter_count': 1,
        'number_count': 1,
        'special_count': 1,
        'minimum_count': 8,
        'maximum_count': 30,
    }


# This function returns a list of potential odin names to choose from.
def truename_odin_names(identity):
    if settings.DEVELOPMENT is True:
        stub = [
            'Odin Name 1 - DEV',
            'Odin Name 2 - DEV',
            'Odin Name 3 - DEV',
            'Odin Name 4 - DEV',
        ]
        return stub

    url = "https://{}/identityiq/rest/custom/odinNames/{}".format(
        settings.SAILPOINT_SERVER_URL,
        identity['PSU_UUID'],
    )

    r = requests.post(url, auth=(settings.SAILPOINT_USERNAME, settings.SAILPOINT_PASSWORD), verify=False)
    return r.json()


# This function returns a list of potential email aliases to choose from.
def truename_email_aliases(identity):
    if settings.DEVELOPMENT is True:
        stub = [
            'Email Alias 1 - DEV',
            'Email Alias 2 - DEV',
            'Email Alias 3 - DEV',
            'Email Alias 4 - DEV',
        ]
        return stub

    url = "https://{}/identityiq/rest/custom/emailAliases/{}".format(
        settings.SAILPOINT_SERVER_URL,
        identity['PSU_UUID'],
    )

    r = requests.post(url, auth=(settings.SAILPOINT_USERNAME, settings.SAILPOINT_PASSWORD), verify=False)
    return r.json()


# This function calls out to sailpoint to begin a password update event.
def change_password(identity, new_password, old_password):
    if settings.DEVELOPMENT is True:
        return True, "Development password change."

    data = {'PSU_UUID': identity["PSU_UUID"],
            'password': <PASSWORD>,
            'old_password': <PASSWORD>,
            }

    url = "https://{}/identityiq/rest/custom/changePassword".format(
        settings.SAILPOINT_SERVER_URL,
    )

    r = requests.post(url, auth=(settings.SAILPOINT_USERNAME, settings.SAILPOINT_PASSWORD), data=data, verify=False)
    result = r.json()

    status = result["Status"]

    if status == "Success":
        return True, "Password changed successfully."<commit_msg>Add dev stubs in password change API call for Known Bad passwords<commit_after>"""
For managing API calls out to external resources such as sailpoint.
"""
# import urllib.request, urllib.parse, urllib.error
# import urllib.request, urllib.error, urllib.parse
# import json
from django.conf import settings
from AccountPickup.models import OAMStatusTracker
import logging
import requests

logger = logging.getLogger(__name__)


def iiq_auth():
    return settings.SAILPOINT_USERNAME, settings.SAILPOINT_PASSWORD


# Identify a user with an expired password.
def identify_oam_login(username, password):
    if settings.DEVELOPMENT is True:
        if username == "000000000":
            return None  # Provide 'known bad' stub for testing
        stub = {
            "PSU_UUID": username,
            "DISPLAY_NAME": "Development User",
            "SPRIDEN_ID": '123456789',
            "ODIN_NAME": 'jsmith5',
            "EMAIL_ADDRESS": "<EMAIL>",
            "PSU_PUBLISH": True,
        }

        return stub

    data = {
        'username': username,
        'password': password,
    }

    url = "https://{}/identityiq/rest/custom/login/password".format(
        settings.SAILPOINT_SERVER_URL,
    )

    r = requests.post(url, auth=(settings.SAILPOINT_USERNAME, settings.SAILPOINT_PASSWORD), verify=False, data=data)

    res = r.json()

    # Massage PSU_PUBLISH:
    if "PSU_PUBLISH" in res:
        if res["PSU_PUBLISH"] is not None and res["PSU_PUBLISH"].lower() == "yes":
            res["PSU_PUBLISH"] = True
        else:
            res["PSU_PUBLISH"] = False

    if "ERROR" in res:
        return None
    else:
        return res


# This function turns a PSU_UUID into the user's identity information like name.
def identity_from_psu_uuid(psu_uuid):
    if settings.DEVELOPMENT is True:
        return {
            "PSU_UUID": psu_uuid,
            "DISPLAY_NAME": "Development User - UUID",
            "SPRIDEN_ID": '123456789',
            "ODIN_NAME": 'jsmith5',
            "EMAIL_ADDRESS": "<EMAIL>",
            "PSU_PUBLISH": True,
        }

    data = {'PSU_UUID': psu_uuid}

    url = "https://{}/identityiq/rest/custom/login/uuid".format(
        settings.SAILPOINT_SERVER_URL,
    )

    r = requests.post(url, auth=(settings.SAILPOINT_USERNAME, settings.SAILPOINT_PASSWORD), verify=False, data=data)

    results = r.json()

    # Massage PSU_PUBLISH:
    if "PSU_PUBLISH" in results:
        if results["PSU_PUBLISH"] is not None and results["PSU_PUBLISH"].lower() == "yes":
            results["PSU_PUBLISH"] = True
        else:
            results["PSU_PUBLISH"] = False

    return results


# This function returns the appropriate password constraints based on an identity.
def passwordConstraintsFromIdentity(identity):  # is this function used? PEP8 says rename to lower_case
    # TODO: Stubbed return
    return {
        'letter_count': 1,
        'number_count': 1,
        'special_count': 1,
        'minimum_count': 8,
        'maximum_count': 30,
    }


# This function returns a list of potential odin names to choose from.
def truename_odin_names(identity):
    if settings.DEVELOPMENT is True:
        stub = [
            'Odin Name 1 - DEV',
            'Odin Name 2 - DEV',
            'Odin Name 3 - DEV',
            'Odin Name 4 - DEV',
        ]
        return stub

    url = "https://{}/identityiq/rest/custom/odinNames/{}".format(
        settings.SAILPOINT_SERVER_URL,
        identity['PSU_UUID'],
    )

    r = requests.post(url, auth=(settings.SAILPOINT_USERNAME, settings.SAILPOINT_PASSWORD), verify=False)
    return r.json()


# This function returns a list of potential email aliases to choose from.
def truename_email_aliases(identity):
    if settings.DEVELOPMENT is True:
        stub = [
            'Email Alias 1 - DEV',
            'Email Alias 2 - DEV',
            'Email Alias 3 - DEV',
            'Email Alias 4 - DEV',
        ]
        return stub

    url = "https://{}/identityiq/rest/custom/emailAliases/{}".format(
        settings.SAILPOINT_SERVER_URL,
        identity['PSU_UUID'],
    )

    r = requests.post(url, auth=(settings.SAILPOINT_USERNAME, settings.SAILPOINT_PASSWORD), verify=False)
    return r.json()


# This function calls out to sailpoint to begin a password update event.
def change_password(identity, new_password, old_password):
    if settings.DEVELOPMENT is True:
        if new_password == "<PASSWORD>":
            return False, ["Development rejecting new password."]
        elif old_password == "<PASSWORD>":
            return False, ["Development rejecting old password."]
        return True, "Development password change."

    data = {'PSU_UUID': identity["PSU_UUID"],
            'password': <PASSWORD>,
            'old_password': <PASSWORD>,
            }

    url = "https://{}/identityiq/rest/custom/changePassword".format(
        settings.SAILPOINT_SERVER_URL,
    )

    r = requests.post(url, auth=(settings.SAILPOINT_USERNAME, settings.SAILPOINT_PASSWORD), data=data, verify=False)
    result = r.json()

    status = result["Status"]

    if status == "Success":
        return True, "Password changed successfully."
{'fraction_non_alphanumeric': 0.11069082221779813, 'fraction_numerical': 0.00875970535536532, 'mean_word_length': 2.5856531049250537, 'pattern_counts': {'":': 34, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '572179', 'n_tokens_mistral': 3259, 'n_tokens_neox': 3030, 'n_words': 786}