<commit_before>from webob import Request

from twitcher.owsexceptions import OWSServiceNotAllowed
from twitcher.owsrequest import OWSRequest

import logging
logger = logging.getLogger(__name__)


allowed_service_types = (
    'wps',
    )

    
allowed_requests = (
    'getcapabilities', 'describeprocess',
    )


class OWSSecurityMiddleware(object):
    def __init__(self, app, tokenstore, **kwargs):
        self.app = app
        self.tokenstore = tokenstore


    def __call__(self, environ, start_response):
        self.request = Request(environ)
        self.ows_request = OWSRequest(self.request)
        if self.is_route_path_protected():
            self.validate_ows_service()
            self.validate_ows_request()
        else:
            logger.warn('unprotected access')

        return self.app(environ, start_response)

    
    def is_route_path_protected(self):
        try:
            # TODO: configure path which should be secured
            logger.debug('path %s', self.request.path)
            return 'ows' in self.request.path
        except ValueError:
            logger.exception('route path check failed')
            return True

        
    def validate_ows_service(self):
        if self.ows_request.service is None:
            raise OWSServiceNotAllowed()

        if not self.ows_request.service in allowed_service_types:
            raise OWSServiceNotAllowed()


    def validate_ows_request(self):
        if not self.ows_request.request in allowed_requests:
           self.tokenstore.validate_access_token(self.request)


    

<commit_msg>fixed usage of owsexceptions in middleware<commit_after>from webob import Request

from twitcher.owsexceptions import OWSException, OWSServiceNotAllowed
from twitcher.owsrequest import OWSRequest

import logging
logger = logging.getLogger(__name__)


allowed_service_types = (
    'wps',
    )

    
allowed_requests = (
    'getcapabilities', 'describeprocess',
    )


class OWSSecurityMiddleware(object):
    def __init__(self, app, tokenstore, **kwargs):
        self.app = app
        self.tokenstore = tokenstore


    def __call__(self, environ, start_response):
        self.request = Request(environ)
        self.ows_request = OWSRequest(self.request)
        if self.is_route_path_protected():
            try:
                self.validate_ows_service()
                self.validate_ows_request()
            except OWSException as e:
                return e(environ, start_response)
            except Exception,e:
                return [e]
        else:
            logger.warn('unprotected access')

        return self.app(environ, start_response)

    
    def is_route_path_protected(self):
        try:
            # TODO: configure path which should be secured
            logger.debug('path %s', self.request.path)
            return 'ows' in self.request.path
        except ValueError:
            logger.exception('route path check failed')
            return True

        
    def validate_ows_service(self):
        if self.ows_request.service is None:
            raise OWSServiceNotAllowed()

        if not self.ows_request.service in allowed_service_types:
            raise OWSServiceNotAllowed()


    def validate_ows_request(self):
        if not self.ows_request.request in allowed_requests:
           self.tokenstore.validate_access_token(self.request)


    


{'fraction_non_alphanumeric': 0.07494836234877544, 'fraction_numerical': 0.0, 'mean_word_length': 2.45565749235474, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '4974756', 'n_tokens_mistral': 947, 'n_tokens_neox': 923, 'n_words': 214}