@transactional_session
def add_identity(identity, type, email, password=None, session=None):
    '\n    Creates a user identity.\n\n    :param identity: The identity key name. For example x509 DN, or a username.\n    :param type: The type of the authentication (x509, gss, userpass, ssh, saml)\n    :param email: The Email address associated with the identity.\n    :param password: If type==userpass, this sets the password.\n    :param session: The database session in use.\n    '
    if ((type == IdentityType.USERPASS) and (password is None)):
        raise exception.IdentityError('You must provide a password!')
    new_id = models.Identity()
    new_id.update({'identity': identity, 'identity_type': type, 'email': email})
    if ((type == IdentityType.USERPASS) and (password is not None)):
        salt = os.urandom(255)
        if six.PY3:
            decoded_salt = b64encode(salt).decode()
            salted_password = ('%s%s' % (decoded_salt, password)).encode()
        else:
            salted_password = ('%s%s' % (salt, str(password)))
        password = hashlib.sha256(salted_password).hexdigest()
        new_id.update({'salt': salt, 'password': password, 'email': email})
    try:
        new_id.save(session=session)
    except IntegrityError as e:
        if match('.*IntegrityError.*1062.*Duplicate entry.*for key.*', e.args[0]):
            raise exception.Duplicate(("Identity pair '%s','%s' already exists!" % (identity, type)))
        raise exception.DatabaseException(str(e))
Creates a user identity.

:param identity: The identity key name. For example x509 DN, or a username.
:param type: The type of the authentication (x509, gss, userpass, ssh, saml)
:param email: The Email address associated with the identity.
:param password: If type==userpass, this sets the password.
:param session: The database session in use.
@transactional_session
def add_identity(identity, type, email, password=None, session=None):
    '\n    Creates a user identity.\n\n    :param identity: The identity key name. For example x509 DN, or a username.\n    :param type: The type of the authentication (x509, gss, userpass, ssh, saml)\n    :param email: The Email address associated with the identity.\n    :param password: If type==userpass, this sets the password.\n    :param session: The database session in use.\n    '
    if ((type == IdentityType.USERPASS) and (password is None)):
        raise exception.IdentityError('You must provide a password!')
    new_id = models.Identity()
    new_id.update({'identity': identity, 'identity_type': type, 'email': email})
    if ((type == IdentityType.USERPASS) and (password is not None)):
        salt = os.urandom(255)
        if six.PY3:
            decoded_salt = b64encode(salt).decode()
            salted_password = ('%s%s' % (decoded_salt, password)).encode()
        else:
            salted_password = ('%s%s' % (salt, str(password)))
        password = hashlib.sha256(salted_password).hexdigest()
        new_id.update({'salt': salt, 'password': password, 'email': email})
    try:
        new_id.save(session=session)
    except IntegrityError as e:
        if match('.*IntegrityError.*1062.*Duplicate entry.*for key.*', e.args[0]):
            raise exception.Duplicate(("Identity pair '%s','%s' already exists!" % (identity, type)))
        raise exception.DatabaseException(str(e))