<commit_before>from optparse import make_option
import sys
import xml.etree.ElementTree as etree

import lasso
from django.core.management.base import BaseCommand, CommandError

from authentic2.saml.models import *


def md_element_name(tag_name):
    return '{%s}%s' % (lasso.SAML2_METADATA_HREF, tag_name)

ENTITY_DESCRIPTOR_TN = md_element_name('EntityDescriptor')
ENTITIES_DESCRIPTOR_TN = md_element_name('EntitiesDescriptor')
IDP_SSO_DESCRIPTOR_TN = md_element_name('IDPSSODescriptor')
SP_SSO_DESCRIPTOR_TN = md_element_name('SPSSODescriptor')
ORGANIZATION_DISPLAY_NAME = md_element_name('OrganizationDisplayName')
ORGANIZATION_NAME = md_element_name('OrganizationName')
ORGANIZATION = md_element_name('Organization')
ENTITY_ID = 'entityID'

def load_one_entity(tree, options):
    '''Load or update an EntityDescriptor into the database'''
    entity_id = tree.get(ENTITY_ID)
    organization = tree.find(ORGANIZATION)
    name, org = None, None
    if organization:
        organization_display_name = organization.find(ORGANIZATION_DISPLAY_NAME)
        organization_name = organization.find(ORGANIZATION_NAME)
        org = organization_display_name or organization_name
        if org is not None:
            name = org.text
    if not name:
        name = entity_id
    idp, sp = False, False
    idp = len(tree.findall(IDP_SSO_DESCRIPTOR_TN)) != 0
    sp = len(tree.findall(SP_SSO_DESCRIPTOR_TN)) != 0
    if options.get('idp'):
        sp = False
    if options.get('sp'):
        idp = False
    if idp or sp:
        if options.get('delete'):
            LibertyProvider.objects.filter(entity_id=entity_id).delete()
            print 'Deleted', entity_id
            return
        if options['verbosity'] == '2':
            print >>sys.stdout, 'Loading %s, %s' %(name, entity_id)
        provider, created = LibertyProvider.objects.get_or_create(entity_id=entity_id,
                protocol_conformance=3)
        provider.name = name
        provider.metadata = etree.tostring(tree, encoding='utf-8').decode('utf-8')
        provider.protocol_conformance = 3
        provider.save()
        options['count'] = options.get('count', 0) + 1
        if idp:
            identity_provider = LibertyIdentityProvider(liberty_provider=provider,
                enabled=True, allow_create=True)
            identity_provider.save()
        if sp:
            identity_provider = LibertyServiceProvider(liberty_provider=provider,
                enabled=True)
            identity_provider.save()

class Command(BaseCommand):
    '''Load SAMLv2 metadata file into the LibertyProvider, LibertyServiceProvider
    and LibertyIdentityProvider files'''
    can_import_django_settings = True
    output_transaction = True
    requires_model_validation = True
    option_list = BaseCommand.option_list + (
        make_option('--idp',
            action='store_true',
            dest='idp',
            default=False,
            help='Load identity providers only'),
        make_option('--sp',<commit_msg>[saml] in load-metadata command only load services supporting SAMLv2<commit_after>from optparse import make_option
import sys
import xml.etree.ElementTree as etree

import lasso
from django.core.management.base import BaseCommand, CommandError

from authentic2.saml.models import *


def md_element_name(tag_name):
    return '{%s}%s' % (lasso.SAML2_METADATA_HREF, tag_name)

ENTITY_DESCRIPTOR_TN = md_element_name('EntityDescriptor')
ENTITIES_DESCRIPTOR_TN = md_element_name('EntitiesDescriptor')
IDP_SSO_DESCRIPTOR_TN = md_element_name('IDPSSODescriptor')
SP_SSO_DESCRIPTOR_TN = md_element_name('SPSSODescriptor')
ORGANIZATION_DISPLAY_NAME = md_element_name('OrganizationDisplayName')
ORGANIZATION_NAME = md_element_name('OrganizationName')
ORGANIZATION = md_element_name('Organization')
ENTITY_ID = 'entityID'
PROTOCOL_SUPPORT_ENUMERATION = 'protocolSupportEnumeration'

def check_support_saml2(tree):
    if tree is not None and lasso.SAML2_PROTOCOL_HREF in tree.get(PROTOCOL_SUPPORT_ENUMERATION):
        return True
    return False

def load_one_entity(tree, options):
    '''Load or update an EntityDescriptor into the database'''
    entity_id = tree.get(ENTITY_ID)
    organization = tree.find(ORGANIZATION)
    name, org = None, None
    if organization:
        organization_display_name = organization.find(ORGANIZATION_DISPLAY_NAME)
        organization_name = organization.find(ORGANIZATION_NAME)
        org = organization_display_name or organization_name
        if org is not None:
            name = org.text
    if not name:
        name = entity_id
    idp, sp = False, False
    idp = check_support_saml2(tree.find(IDP_SSO_DESCRIPTOR_TN))
    sp = check_support_saml2(tree.find(SP_SSO_DESCRIPTOR_TN))
    if options.get('idp'):
        sp = False
    if options.get('sp'):
        idp = False
    if options.get('delete'):
        LibertyProvider.objects.filter(entity_id=entity_id).delete()
        print 'Deleted', entity_id
        return
    if idp or sp:
        if options['verbosity'] == '2':
            print >>sys.stdout, 'Loading %s, %s' % (name.encode('utf8'), entity_id)
        provider, created = LibertyProvider.objects.get_or_create(entity_id=entity_id,
                protocol_conformance=3)
        provider.name = name
        provider.metadata = etree.tostring(tree, encoding='utf-8').decode('utf-8').strip()
        provider.protocol_conformance = 3
        provider.save()
        import re
        file(re.sub(r'[:/]+', '_', entity_id), 'w').write(etree.tostring(tree, encoding='utf-8'))
        options['count'] = options.get('count', 0) + 1
        if idp:
            identity_provider = LibertyIdentityProvider(liberty_provider=provider,
                enabled=True, allow_create=True)
            identity_provider.save()
        if sp:
            identity_provider = LibertyServiceProvider(liberty_provider=provider,
                enabled=True)
            identity_provider.save()

class Command(BaseCommand):
    '''Load SAMLv2 metadata file into the LibertyProvider, LibertyServiceProvider
    and LibertyIdentityProvider files'''
    can_import_django_settings = True
    output_transaction = True
    requires_model_validation = True
    option_list = BaseCommand.option_list + (
        make_option('--idp',
            action='store_true',
            dest='idp',
            default=False,
            help='Load identity providers only'),
        make_option('--sp',
{'fraction_non_alphanumeric': 0.09526764934057409, 'fraction_numerical': 0.004499612102404965, 'mean_word_length': 3.427197802197802, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1686985', 'n_tokens_mistral': 1965, 'n_tokens_neox': 1928, 'n_words': 390}