@@ -0,0 +1,3321 @@
+# Copyright 2012 Locaweb.%0A# All Rights Reserved.%0A#%0A#    Licensed under the Apache License, Version 2.0 (the %22License%22);%0A#    you may not use this file except in compliance with the License.%0A#    You may obtain a copy of the License at%0A#%0A#        http://www.apache.org/licenses/LICENSE-2.0%0A#%0A#    Unless required by applicable law or agreed to in writing, software%0A#    distributed under the License is distributed on an %22AS IS%22 BASIS,%0A#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A#    See the License for the specific language governing permissions and%0A#    limitations under the License.%0A#%0A# @author: Eduardo Scarpelini (odraude), Locaweb.%0A# @author: Juliano Martinez (ncode), Locaweb.%0A# @author: Luiz Ozaki, Locaweb.%0A%0A__copyright__ = %22Copyright 2012, Locaweb IDC%22%0A%0Afrom ast import literal_eval%0Afrom functools import wraps%0Afrom bottle import request, abort%0Afrom locautils.cas import CASClient, CasError%0Afrom simplenet.common.cache import RedisClient%0Aimport logging%0Aimport hashlib%0A%0Alogger = logging.getLogger('simplenet.server')%0A%0A__all__ = %5B%22cas_authenticate%22, %22authorize%22%5D%0A%0A%0Adef cas_authenticate(servers=None, service=None):%0A    def proxy(f):%0A        @wraps(f)%0A        def auth(*args, **kwargs):%0A            cas_ticket = request.query.get(%22ticket%22)%0A            if not cas_ticket:%0A                abort(403, %22Null Authentication Ticket (CAS)%22)%0A            redis_db = RedisClient()%0A            auth_cache = redis_db.get_connection()%0A            m = hashlib.md5()%0A            m.update(cas_ticket)%0A            cas_hash = m.hexdigest()%0A            if (auth_cache.exists(cas_hash) == False):%0A                print %22Not found %22 + cas_hash%0A                try:%0A                    logger.info(%22Trying to validate CAS ticket '%25s' on server '%25s'%22 %25 (cas_ticket, servers%5B0%5D))%0A                    user_info = CASClient(server=servers%5B0%5D).validate_ticket(ticket=cas_ticket, service=service)%0A                except CasError:%0A                    logger.exception(%22CAS ticket validation failed%22)%0A                    try:%0A                        logger.info(%22Trying to validate CAS-ticket '%25s' on server '%25s'%22 %25 (cas_ticket, servers%5B1%5D))%0A                        user_info = CASClient(server=servers%5B1%5D).validate_ticket(ticket=cas_ticket, service=service)%0A                    except CasError:%0A                        logger.exception(%22CAS ticket validation failed%22)%0A                        abort(403, %22Invalid Authentication Ticket (CAS)%22)%0A                auth_cache.hset(cas_hash, %22user_info%22, user_info)%0A                auth_cache.expire(cas_hash, 60)%0A                request%5B%22authentication_info%22%5D = user_info%0A                return f(*args, **kwargs)%0A            else:%0A                print %22FOund %22 + cas_hash%0A                request%5B%22authentication_info%22%5D = literal_eval(auth_cache.hget(cas_hash, %22user_info%22))%0A                return f(*args, **kwargs)%0A        return auth%0A    return proxy%0A%0A%0Adef authorize(rolecfg=None, required_role=None):%0A    def proxy(f):%0A        def auth(*args, **kwargs):%0A            # TODO: Use CAS Authority for ACLs%0A            username = request%5B%22authentication_info%22%5D%5B%22cn%22%5D%5B0%5D.lower().replace(%22 %22,%22.%22)%0A            if username not in rolecfg%5Brequired_role%5D:%0A                abort(403, %22Forbidden%22)%0A            return f(*args, **kwargs)%0A        return auth%0A    return proxy%0A

add file...