# Path: papaye/authentification.py
# class RouteNameAuthPolicy(AuthTktAuthenticationPolicy):
# 
#     def __init__(self, **policies):
#         self.policies = policies
# 
#     def get_policy(self, request):
#         if request.matched_route is None:
#             return self.policies.get('default')
#         else:
#             route_name = request.matched_route.name
#         return self.policies.get(route_name, self.policies.get('default'))
# 
#     def authenticated_userid(self, request):
#         return self.get_policy(request).authenticated_userid(request)
# 
#     def unauthenticated_userid(self, request):
#         return self.get_policy(request).unauthenticated_userid(request)
# 
#     def effective_principals(self, request):
#         return self.get_policy(request).effective_principals(request)
# 
#     def remember(self, request, principal, **kw):
#         return self.get_policy(request).remember(request, principal, **kw)
# 
#     def forget(self, request):
#         return self.get_policy(request).forget(request)
# 
# Path: papaye/models.py
# class User(Model):
# 
#     def __init__(self, username, password, **kwargs):
#         self.username = username
#         self.password = self.hash_password(password)
#         self.groups = kwargs.get('groups', [])
# 
#     def hash_password(self, password):
#         return hashlib.sha512(password.encode('utf-8')).hexdigest()
# 
#     def password_verify(self, clear_password):
#         return self.hash_password(clear_password) == self.password
# 
#     def __repr__(self):
#         return '<{}.{} "{}" at {}>'.format(self.__module__, self.__class__.__name__, self.username, id(self))
# 
#     @classmethod
#     def by_username(cls, username, request):
#         """Return user instance by username"""
#         root = user_root_factory(request)
#         if username in [user.username for user in root]:
#             return root[username]
#         return None


from random import choice
from pyramid.authentication import AuthTktAuthenticationPolicy
from pyramid.authentication import BasicAuthAuthenticationPolicy
from pyramid.authorization import ACLAuthorizationPolicy
from pyramid.session import SignedCookieSessionFactory
from papaye.authentification import RouteNameAuthPolicy
from papaye.models import User




CHOICES = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&(-_=+)'


def random_passphrase():
    return ''.join([choice(CHOICES) for i in range(50)])


def auth_check_func(username, password, request):
    user = User.by_username(username, request)
    if user and user.password_verify(password):
        return user.groups
    return None


def includeme(config):
    session_factory = SignedCookieSessionFactory(random_passphrase())
    authtkt_policy = AuthTktAuthenticationPolicy(
        random_passphrase(), hashalg='sha512',
    )


# Path: papaye/authentification.py
# class RouteNameAuthPolicy(AuthTktAuthenticationPolicy):
# 
#     def __init__(self, **policies):
#         self.policies = policies
# 
#     def get_policy(self, request):
#         if request.matched_route is None:
#             return self.policies.get('default')
#         else:
#             route_name = request.matched_route.name
#         return self.policies.get(route_name, self.policies.get('default'))
# 
#     def authenticated_userid(self, request):
#         return self.get_policy(request).authenticated_userid(request)
# 
#     def unauthenticated_userid(self, request):
#         return self.get_policy(request).unauthenticated_userid(request)
# 
#     def effective_principals(self, request):
#         return self.get_policy(request).effective_principals(request)
# 
#     def remember(self, request, principal, **kw):
#         return self.get_policy(request).remember(request, principal, **kw)
# 
#     def forget(self, request):
#         return self.get_policy(request).forget(request)
# 
# Path: papaye/models.py
# class User(Model):
# 
#     def __init__(self, username, password, **kwargs):
#         self.username = username
#         self.password = self.hash_password(password)
#         self.groups = kwargs.get('groups', [])
# 
#     def hash_password(self, password):
#         return hashlib.sha512(password.encode('utf-8')).hexdigest()
# 
#     def password_verify(self, clear_password):
#         return self.hash_password(clear_password) == self.password
# 
#     def __repr__(self):
#         return '<{}.{} "{}" at {}>'.format(self.__module__, self.__class__.__name__, self.username, id(self))
# 
#     @classmethod
#     def by_username(cls, username, request):
#         """Return user instance by username"""
#         root = user_root_factory(request)
#         if username in [user.username for user in root]:
#             return root[username]
#         return None


# Path: papaye/config/auth.py
from random import choice
from pyramid.authentication import AuthTktAuthenticationPolicy
from pyramid.authentication import BasicAuthAuthenticationPolicy
from pyramid.authorization import ACLAuthorizationPolicy
from pyramid.session import SignedCookieSessionFactory
from papaye.authentification import RouteNameAuthPolicy
from papaye.models import User





CHOICES = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&(-_=+)'


def random_passphrase():
    return ''.join([choice(CHOICES) for i in range(50)])


def auth_check_func(username, password, request):
    user = User.by_username(username, request)
    if user and user.password_verify(password):
        return user.groups
    return None


def includeme(config):
    session_factory = SignedCookieSessionFactory(random_passphrase())
    authtkt_policy = AuthTktAuthenticationPolicy(
        random_passphrase(), hashalg='sha512',
    )


    authn_policy = RouteNameAuthPolicy(