<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2010 Umeå University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#            http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import base64
import time


from saml2 import samlp
from saml2 import saml
from saml2 import extension_element_to_element
from saml2 import time_util

from saml2.sigver import security_context

from saml2.attribute_converter import to_local
from saml2.time_util import daylight_corrected_now
from saml2.time_util import str_to_time
from saml2.time_util import not_before

from saml2.validate import validate_on_or_after
from saml2.validate import validate_before

# ---------------------------------------------------------------------------

class IncorrectlySigned(Exception):
    pass
    
# ---------------------------------------------------------------------------

def for_me(condition, myself ):
    # Am I among the intended audiences
    for restriction in condition.audience_restriction:
        for audience in restriction.audience:
            if audience.text.strip() == myself:
                return True
            else:
                #print "Not for me: %s != %s" % (audience.text.strip(), myself)
                pass
    
    return False

def authn_response(conf, requestor, outstanding_queries=None, log=None,
                    timeslack=0, debug=0):
    sec = security_context(conf)
    if not timeslack:
        try:
            timeslack = int(conf["timeslack"])
        except KeyError:
            pass
    
    return AuthnResponse(sec, conf.attribute_converters, requestor,
                            outstanding_queries, log, timeslack, debug)

class AuthnResponse(object):
    
    def __init__(self, sec_context, attribute_converters, requestor,
                    outstanding_queries=None, log=None, timeslack=0, debug=0):
        self.sec = sec_context
        self.attribute_converters = attribute_converters
        self.requestor = requestor
        if outstanding_queries:
            self.outstanding_queries = outstanding_queries
        else:
            self.outstanding_queries = {}
        self.context = "AuthnReq"
        self.timeslack = timeslack
        self.log = log
        self.debug = debug
        if self.debug and not self.log:
            self.debug = 0
        
        self.xmlstr = ""
        self.came_from = ""
        self.name_id = ""
        self.ava = None
        self.response = None
        self.not_on_or_after = 0
        self.assertion = None
    
    def loads(self, xmldata, decode=True):
        if self.debug:
            self.log.info("--- Loads AuthnResponse ---")
        if decode:
            decoded_xml = base64.b64decode(xmldata)
        else:
            decoded_xml = xmldata
        
        # own copy
        self.xmlstr = decoded_xml[:]
        if self.debug:
            self.log.info("xmlstr: %s" % (self.xmlstr,))
        try:
            self.response = self.sec.correctly_signed_response(decoded_xml)
        except Exception, excp:
            self.log and self.log.info("EXCEPTION: %s", excp)
        
        if not self.response:
            if self.log:
                self.log.error("Response was not correctly signed")
                self.log.info(decoded_xml)
            raise IncorrectlySigned()
        
        if self.debug:
            self.log.info("response: %s" % (self.response,))
        
        return self
    
    def clear(self):
        self.xmlstr = ""
        self.came_from = ""
        self.name_id = ""
        self.ava = None
        self.response = None
        self.not_on_or_after = 0
        self.assertion = None
    
    def status_ok(self):
        if self.response.status:
            status = self.response.status
            if self.log:
                self.log.info("status: %s" % (status,))<commit_msg>Validate response<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2010 Umeå University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#            http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import base64
import time
import sys

from saml2 import samlp
from saml2 import saml
from saml2 import extension_element_to_element
from saml2 import time_util

from saml2.sigver import security_context

from saml2.attribute_converter import to_local
from saml2.time_util import daylight_corrected_now
from saml2.time_util import str_to_time
from saml2.time_util import not_before

from saml2.validate import validate_on_or_after
from saml2.validate import validate_before
from saml2.validate import valid_instance

# ---------------------------------------------------------------------------

class IncorrectlySigned(Exception):
    pass
    
# ---------------------------------------------------------------------------

def for_me(condition, myself ):
    # Am I among the intended audiences
    for restriction in condition.audience_restriction:
        for audience in restriction.audience:
            if audience.text.strip() == myself:
                return True
            else:
                #print "Not for me: %s != %s" % (audience.text.strip(), myself)
                pass
    
    return False

def authn_response(conf, requestor, outstanding_queries=None, log=None,
                    timeslack=0, debug=0):
    sec = security_context(conf)
    if not timeslack:
        try:
            timeslack = int(conf["timeslack"])
        except KeyError:
            pass
    
    return AuthnResponse(sec, conf.attribute_converters, requestor,
                            outstanding_queries, log, timeslack, debug)

class AuthnResponse(object):
    
    def __init__(self, sec_context, attribute_converters, requestor,
                    outstanding_queries=None, log=None, timeslack=0, debug=0):
        self.sec = sec_context
        self.attribute_converters = attribute_converters
        self.requestor = requestor
        if outstanding_queries:
            self.outstanding_queries = outstanding_queries
        else:
            self.outstanding_queries = {}
        self.context = "AuthnReq"
        self.timeslack = timeslack
        self.log = log
        self.debug = debug
        if self.debug and not self.log:
            self.debug = 0
        
        self.xmlstr = ""
        self.came_from = ""
        self.name_id = ""
        self.ava = None
        self.response = None
        self.not_on_or_after = 0
        self.assertion = None
    
    def loads(self, xmldata, decode=True):
        if self.debug:
            self.log.info("--- Loads AuthnResponse ---")
        if decode:
            decoded_xml = base64.b64decode(xmldata)
        else:
            decoded_xml = xmldata
        
        # own copy
        self.xmlstr = decoded_xml[:]
        if self.debug:
            self.log.info("xmlstr: %s" % (self.xmlstr,))
        try:
            self.response = self.sec.correctly_signed_response(decoded_xml)
        except Exception, excp:
            self.log and self.log.info("EXCEPTION: %s", excp)
        
        if not self.response:
            if self.log:
                self.log.error("Response was not correctly signed")
                self.log.info(decoded_xml)
            raise IncorrectlySigned()
        
        if self.debug:
            self.log.info("response: %s" % (self.response,))

        if not valid_instance(self.response):
            if self.log:
                self.log.error("Not valid response")
            else:
                print >> sys.stderr, "Not valid response"
            
        return self
    
    def clear(self):
        self.xmlstr = ""
        self.came_from = ""
        self.name_id = ""
        self.ava = None
        self.response = None
        self.not_on_or_after = 0
        self.assertion = None
    
    def status_ok(self):
        if self.response.status:
            status = self.response.status
            if self.log:
                self.log.info("status: %s" % (status,))
{'fraction_non_alphanumeric': 0.1141825566963782, 'fraction_numerical': 0.00755951709353492, 'mean_word_length': 1.8995747464834805, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 26}, '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': '487618', 'n_tokens_mistral': 2506, 'n_tokens_neox': 2454, 'n_words': 672}