# Path: yithlibraryserver/oauth2/authorization.py
# class AccessCodes(Codes):
# 
#     collection_name = 'access_codes'
# 
#     def create(self, user_id, grant):
#         return super(AccessCodes, self).create(user_id,
#                                                scope=grant['scope'],
#                                                client_id=grant['client_id'])


from pyramid.httpexceptions import HTTPBadRequest, HTTPUnauthorized
from pyramid.security import Allow, Authenticated
from yithlibraryserver.oauth2.authorization import AccessCodes


class RootFactory(object):

    __acl__ = (
        (Allow, Authenticated, 'user-registration'),
        (Allow, Authenticated, 'view-applications'),
        (Allow, Authenticated, 'edit-application'),
        (Allow, Authenticated, 'add-application'),
        (Allow, Authenticated, 'delete-application'),
        (Allow, Authenticated, 'add-authorized-app'),
        (Allow, Authenticated, 'revoke-authorized-app'),
        (Allow, Authenticated, 'edit-profile'),
        (Allow, Authenticated, 'destroy-account'),
        (Allow, Authenticated, 'backups'),
        )

    def __init__(self, request):
        self.request = request


def authorize_user(request):
    authorization = request.headers.get('Authorization')
    if authorization is None:
        raise HTTPUnauthorized()

    method, credentials = request.authorization
    if method.lower() != 'bearer':
        raise HTTPBadRequest('Authorization method not supported')


# Path: yithlibraryserver/oauth2/authorization.py
# class AccessCodes(Codes):
# 
#     collection_name = 'access_codes'
# 
#     def create(self, user_id, grant):
#         return super(AccessCodes, self).create(user_id,
#                                                scope=grant['scope'],
#                                                client_id=grant['client_id'])


# Path: yithlibraryserver/security.py
from pyramid.httpexceptions import HTTPBadRequest, HTTPUnauthorized
from pyramid.security import Allow, Authenticated
from yithlibraryserver.oauth2.authorization import AccessCodes



class RootFactory(object):

    __acl__ = (
        (Allow, Authenticated, 'user-registration'),
        (Allow, Authenticated, 'view-applications'),
        (Allow, Authenticated, 'edit-application'),
        (Allow, Authenticated, 'add-application'),
        (Allow, Authenticated, 'delete-application'),
        (Allow, Authenticated, 'add-authorized-app'),
        (Allow, Authenticated, 'revoke-authorized-app'),
        (Allow, Authenticated, 'edit-profile'),
        (Allow, Authenticated, 'destroy-account'),
        (Allow, Authenticated, 'backups'),
        )

    def __init__(self, request):
        self.request = request


def authorize_user(request):
    authorization = request.headers.get('Authorization')
    if authorization is None:
        raise HTTPUnauthorized()

    method, credentials = request.authorization
    if method.lower() != 'bearer':
        raise HTTPBadRequest('Authorization method not supported')


    access_code = AccessCodes(request.db).find(credentials)