# 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

        for v in value:
            validate_redirect_uri(v)

        self._redirect_uris = "\n".join(value) or ""

    @property
    def default_redirect_uri(self):
        """Return default redirect uri."""
        try:
            return self.redirect_uris[0]
        except IndexError:
            pass

    @property
    def default_scopes(self):
        """List of default scopes for client."""
        if self._default_scopes:
            return self._default_scopes.split(" ")
        return []

    @default_scopes.setter
    def default_scopes(self, scopes):
        """Set default scopes for client."""
        validate_scopes(scopes)
        self._default_scopes = " ".join(set(scopes)) if scopes else ""

    def validate_scopes(self, scopes):
        """Validate if client is allowed to access scopes."""
        try:

# 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


        for v in value:
            validate_redirect_uri(v)

        self._redirect_uris = "\n".join(value) or ""

    @property
    def default_redirect_uri(self):
        """Return default redirect uri."""
        try:
            return self.redirect_uris[0]
        except IndexError:
            pass

    @property
    def default_scopes(self):
        """List of default scopes for client."""
        if self._default_scopes:
            return self._default_scopes.split(" ")
        return []

    @default_scopes.setter
    def default_scopes(self, scopes):
        """Set default scopes for client."""
        validate_scopes(scopes)
        self._default_scopes = " ".join(set(scopes)) if scopes else ""

    def validate_scopes(self, scopes):
        """Validate if client is allowed to access scopes."""
        try:

            validate_scopes(scopes)