# Path: invenio_oauth2server/errors.py
# class ScopeDoesNotExists(OAuth2ServerError):
#     """Scope is not registered it scopes registry."""
# 
#     def __init__(self, scope, *args, **kwargs):
#         """Initialize exception by storing invalid scope."""
#         super(ScopeDoesNotExists, self).__init__(*args, **kwargs)
#         self.scope = scope
# 
# Path: invenio_oauth2server/proxies.py

# 
# Path: invenio_oauth2server/validators.py
# def validate_redirect_uri(value):
#     """Validate a redirect URI.
# 
#     Redirect URIs must be a valid URL and use https unless the host is
#     localhost for which http is accepted.
# 
#     :param value: The redirect URI.
#     """
#     sch, netloc, path, par, query, fra = urlparse(value)
#     if not (sch and netloc):
#         raise InvalidRedirectURIError()
#     if sch != 'https':
#         if ':' in netloc:
#             netloc, port = netloc.split(':', 1)
#         if not (netloc in ('localhost', '127.0.0.1') and sch == 'http'):
#             raise InsecureTransportError()
# 
# def validate_scopes(value_list):
#     """Validate if each element in a list is a registered scope.
# 
#     :param value_list: The list of scopes.
#     :raises invenio_oauth2server.errors.ScopeDoesNotExists: The exception is
#         raised if a scope is not registered.
#     :returns: ``True`` if it's successfully validated.
#     """
#     for value in value_list:
#         if value not in current_oauth2server.scopes:
#             raise ScopeDoesNotExists(value)
#     return True


import six
from flask import current_app
from flask_babelex import lazy_gettext as _
from flask_login import current_user
from invenio_accounts.models import User
from invenio_db import db
from sqlalchemy.schema import Index
from sqlalchemy_utils import EncryptedType
from sqlalchemy_utils.types import URLType
from sqlalchemy_utils.types.encrypted.encrypted_type import AesEngine
from werkzeug.security import gen_salt
from wtforms import validators
from .errors import ScopeDoesNotExists
from .proxies import current_oauth2server
from .validators import validate_redirect_uri, validate_scopes
    def allowed_response_types(self):
        """Return allowed response types."""
        return current_app.config['OAUTH2SERVER_ALLOWED_RESPONSE_TYPES']

    # def validate_scopes(self, scopes):
    #     return self._validate_scopes

    @property
    def client_type(self):
        """Return client type."""
        if self.is_confidential:
            return 'confidential'
        return 'public'

    @property
    def redirect_uris(self):
        """Return redirect uris."""
        if self._redirect_uris:
            return self._redirect_uris.splitlines()
        return []

    @redirect_uris.setter
    def redirect_uris(self, value):
        """Validate and store redirect URIs for client."""
        if isinstance(value, six.text_type):
            value = value.split("\n")

        value = [v.strip() for v in value]

        for v in value:

# Path: invenio_oauth2server/errors.py
# class ScopeDoesNotExists(OAuth2ServerError):
#     """Scope is not registered it scopes registry."""
# 
#     def __init__(self, scope, *args, **kwargs):
#         """Initialize exception by storing invalid scope."""
#         super(ScopeDoesNotExists, self).__init__(*args, **kwargs)
#         self.scope = scope
# 
# Path: invenio_oauth2server/proxies.py

# 
# Path: invenio_oauth2server/validators.py
# def validate_redirect_uri(value):
#     """Validate a redirect URI.
# 
#     Redirect URIs must be a valid URL and use https unless the host is
#     localhost for which http is accepted.
# 
#     :param value: The redirect URI.
#     """
#     sch, netloc, path, par, query, fra = urlparse(value)
#     if not (sch and netloc):
#         raise InvalidRedirectURIError()
#     if sch != 'https':
#         if ':' in netloc:
#             netloc, port = netloc.split(':', 1)
#         if not (netloc in ('localhost', '127.0.0.1') and sch == 'http'):
#             raise InsecureTransportError()
# 
# def validate_scopes(value_list):
#     """Validate if each element in a list is a registered scope.
# 
#     :param value_list: The list of scopes.
#     :raises invenio_oauth2server.errors.ScopeDoesNotExists: The exception is
#         raised if a scope is not registered.
#     :returns: ``True`` if it's successfully validated.
#     """
#     for value in value_list:
#         if value not in current_oauth2server.scopes:
#             raise ScopeDoesNotExists(value)
#     return True


# Path: invenio_oauth2server/models.py
import six
from flask import current_app
from flask_babelex import lazy_gettext as _
from flask_login import current_user
from invenio_accounts.models import User
from invenio_db import db
from sqlalchemy.schema import Index
from sqlalchemy_utils import EncryptedType
from sqlalchemy_utils.types import URLType
from sqlalchemy_utils.types.encrypted.encrypted_type import AesEngine
from werkzeug.security import gen_salt
from wtforms import validators
from .errors import ScopeDoesNotExists
from .proxies import current_oauth2server
from .validators import validate_redirect_uri, validate_scopes

    def allowed_response_types(self):
        """Return allowed response types."""
        return current_app.config['OAUTH2SERVER_ALLOWED_RESPONSE_TYPES']

    # def validate_scopes(self, scopes):
    #     return self._validate_scopes

    @property
    def client_type(self):
        """Return client type."""
        if self.is_confidential:
            return 'confidential'
        return 'public'

    @property
    def redirect_uris(self):
        """Return redirect uris."""
        if self._redirect_uris:
            return self._redirect_uris.splitlines()
        return []

    @redirect_uris.setter
    def redirect_uris(self, value):
        """Validate and store redirect URIs for client."""
        if isinstance(value, six.text_type):
            value = value.split("\n")

        value = [v.strip() for v in value]

        for v in value:

            validate_redirect_uri(v)