# Path: opennsa/shared/requestinfo.py
# class RequestInfo:
#     """
#     Holds various data about the request.
#     Somewhat adding things as we go.
#     """
#     def __init__(self, cert_subject=None, cert_host_dn=None):
#         self.cert_subject = cert_subject
#         self.cert_host_dn = cert_host_dn
# 
# Path: opennsa/protocols/shared/minisoap.py
# LOG_SYSTEM = 'opennsa.protocols.soap'
# SOAP_ENVELOPE_NS        = "http://schemas.xmlsoap.org/soap/envelope/"
# SOAP_ENV                = ET.QName("{%s}Envelope"   % SOAP_ENVELOPE_NS)
# SOAP_HEADER             = ET.QName("{%s}Header"     % SOAP_ENVELOPE_NS)
# SOAP_BODY               = ET.QName("{%s}Body"       % SOAP_ENVELOPE_NS)
# SOAP_FAULT              = ET.QName("{%s}Fault"      % SOAP_ENVELOPE_NS)
# FAULTCODE               = 'faultcode'
# FAULTSTRING             = 'faultstring'
# DETAIL                  = 'detail'
# FAULTCODE_SERVER        = 'soap:Server' # must match with the namespace below
# def _indent(elem, level=0):
# def createSoapEnvelope():
# def createSoapPayload(body_element=None, header_element=None):
# def createSoapFault(fault_msg, detail_element=None):
# def parseSoapPayload(payload):
# def parseFault(payload):


from twisted.python import log
from twisted.internet import defer
from twisted.web import resource, server
from opennsa.shared.requestinfo import RequestInfo
from opennsa.protocols.shared import minisoap

    def registerDecoder(self, soap_action, decoder):

        self.soap_actions[soap_action] = decoder


    def render_POST(self, request):

        if self.allowed_hosts is not None:
            if not request.isSecure():
                request.setResponseCode(401) # Not Authorized
                log.msg('Rejecting request, not secure (no ssl/tls)', system=LOG_SYSTEM)
                return 'Insecure requests not allowed for this resource\r\n'

            cert = request.transport.getPeerCertificate()
            if not cert:
                request.setResponseCode(401) # Not Authorized
                log.msg('Rejecting request, no client certificate presented', system=LOG_SYSTEM)
                return 'Requests without client certificate not allowed\r\n'

            cert_subject = cert.get_subject()
            log.msg('Certificate subject %s' % cert.get_subject(), system=LOG_SYSTEM)
            host_dn = (dict(cert.get_subject().get_components()).get(b'CN')).decode('utf-8')
            log.msg('Host DN: %s' % host_dn, system=LOG_SYSTEM)

            if not host_dn in self.allowed_hosts:
                request.setResponseCode(401) # Not Authorized
                log.msg('Rejecting request, certificate host dn does not match allowed hosts', system=LOG_SYSTEM)
                return 'Requests without certificate not allowed\r\n'


# Path: opennsa/shared/requestinfo.py
# class RequestInfo:
#     """
#     Holds various data about the request.
#     Somewhat adding things as we go.
#     """
#     def __init__(self, cert_subject=None, cert_host_dn=None):
#         self.cert_subject = cert_subject
#         self.cert_host_dn = cert_host_dn
# 
# Path: opennsa/protocols/shared/minisoap.py
# LOG_SYSTEM = 'opennsa.protocols.soap'
# SOAP_ENVELOPE_NS        = "http://schemas.xmlsoap.org/soap/envelope/"
# SOAP_ENV                = ET.QName("{%s}Envelope"   % SOAP_ENVELOPE_NS)
# SOAP_HEADER             = ET.QName("{%s}Header"     % SOAP_ENVELOPE_NS)
# SOAP_BODY               = ET.QName("{%s}Body"       % SOAP_ENVELOPE_NS)
# SOAP_FAULT              = ET.QName("{%s}Fault"      % SOAP_ENVELOPE_NS)
# FAULTCODE               = 'faultcode'
# FAULTSTRING             = 'faultstring'
# DETAIL                  = 'detail'
# FAULTCODE_SERVER        = 'soap:Server' # must match with the namespace below
# def _indent(elem, level=0):
# def createSoapEnvelope():
# def createSoapPayload(body_element=None, header_element=None):
# def createSoapFault(fault_msg, detail_element=None):
# def parseSoapPayload(payload):
# def parseFault(payload):


# Path: opennsa/protocols/shared/soapresource.py
from twisted.python import log
from twisted.internet import defer
from twisted.web import resource, server
from opennsa.shared.requestinfo import RequestInfo
from opennsa.protocols.shared import minisoap


    def registerDecoder(self, soap_action, decoder):

        self.soap_actions[soap_action] = decoder


    def render_POST(self, request):

        if self.allowed_hosts is not None:
            if not request.isSecure():
                request.setResponseCode(401) # Not Authorized
                log.msg('Rejecting request, not secure (no ssl/tls)', system=LOG_SYSTEM)
                return 'Insecure requests not allowed for this resource\r\n'

            cert = request.transport.getPeerCertificate()
            if not cert:
                request.setResponseCode(401) # Not Authorized
                log.msg('Rejecting request, no client certificate presented', system=LOG_SYSTEM)
                return 'Requests without client certificate not allowed\r\n'

            cert_subject = cert.get_subject()
            log.msg('Certificate subject %s' % cert.get_subject(), system=LOG_SYSTEM)
            host_dn = (dict(cert.get_subject().get_components()).get(b'CN')).decode('utf-8')
            log.msg('Host DN: %s' % host_dn, system=LOG_SYSTEM)

            if not host_dn in self.allowed_hosts:
                request.setResponseCode(401) # Not Authorized
                log.msg('Rejecting request, certificate host dn does not match allowed hosts', system=LOG_SYSTEM)
                return 'Requests without certificate not allowed\r\n'


            request_info = RequestInfo(str(cert_subject), host_dn)