<commit_before># This file is part of Indico.
# Copyright (C) 2002 - 2015 European Organization for Nuclear Research (CERN).
#
# Indico is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# Indico is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Indico; if not, see <http://www.gnu.org/licenses/>.

from __future__ import unicode_literals

from uuid import UUID

from flask import flash, redirect, request, render_template, session
from sqlalchemy.orm.exc import NoResultFound
from werkzeug.exceptions import Forbidden

from indico.core.db import db
from indico.modules.users.controllers import RHUserBase
from indico.modules.oauth import logger
from indico.modules.oauth.provider import oauth
from indico.modules.oauth.forms import ApplicationForm
from indico.modules.oauth.models.applications import OAuthApplication, SCOPES
from indico.modules.oauth.models.tokens import OAuthToken
from indico.modules.oauth.views import WPOAuthUserProfile, WPOAuthAdmin
from indico.util.i18n import _
from indico.web.flask.util import url_for
from MaKaC.webinterface.rh.admins import RHAdminBase
from MaKaC.webinterface.rh.base import RH, RHProtected


class RHOAuthAuthorize(RHProtected):
    def _checkParams(self):
        try:
            UUID(hex=request.args['client_id'])
        except ValueError:
            raise NoResultFound
        self.application = OAuthApplication.find_one(client_id=request.args['client_id'])

    @oauth.authorize_handler
    def _process(self, **kwargs):
        if request.method == 'POST':
            if 'confirm' not in request.form:
                return False
            logger.info('User {} authorized {}'.format(session.user, self.application))
            return True
        if self.application.is_trusted:
            logger.info('User {} automatically authorized {}'.format(session.user, self.application))
            return True
        requested_scopes = set(kwargs['scopes'])
        token = self.application.tokens.filter_by(user=session.user).first()
        authorized_scopes = token.scopes if token else set()
        if requested_scopes <= authorized_scopes:
            return True
        new_scopes = requested_scopes - authorized_scopes
        return render_template('oauth/authorize.html', application=self.application,
                               authorized_scopes=filter(None, [SCOPES.get(s) for s in authorized_scopes]),
                               new_scopes=filter(None, [SCOPES.get(s) for s in new_scopes]))


class RHOAuthErrors(RHProtected):
    def _process(self, **kwargs):
        return render_template('oauth/authorize_errors.html', error=request.args['error'])


class RHOAuthToken(RH):
    @oauth.token_handler
    def _process(self, **kwargs):
        return None


class RHOAuthAdmin(RHAdminBase):
    """OAuth server administration settings"""

    def _process(self):
        applications = OAuthApplication.find().order_by(db.func.lower(OAuthApplication.name)).all()
        return WPOAuthAdmin.render_template('apps.html', applications=applications)


class RHOAuthAdminApplicationBase(RHAdminBase):
    """Base class for single OAuth application RHs"""
    def _checkParams(self):
        self.application = OAuthApplication.get(request.view_args['id'])


class RHOAuthAdminApplication(RHOAuthAdminApplicationBase):
    """Handles application details page"""

    def _process(self):
        form = ApplicationForm(obj=self.application, application=self.application)
        if form.validate_on_submit():
            form.populate_obj(self.application)
            flash(_("Application {} was modified").format(self.application.name), 'success')
            return redirect(url_for('.apps'))
        return WPOAuthAdmin.render_template('app_details.html', application=self.application, form=form)


class RHOAuthAdminApplicationDelete(RHOAuthAdminApplicationBase):
    """Handles OAuth application deletion"""

    CSRF_ENABLED = True

    def _process(self):
        db.session.delete(self.application)
        logger.info("Application {} was deleted.".format(self.application))
        flash(_("Application deleted successfully"), 'success')
        return redirect(url_for('.apps'))


class RHOAuthAdminApplicationNew(RHAdminBase):
    """Handles OAuth application registration"""

    def _process(self):
        form = ApplicationForm()<commit_msg>Enable new OAuth apps by default<commit_after># This file is part of Indico.
# Copyright (C) 2002 - 2015 European Organization for Nuclear Research (CERN).
#
# Indico is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# Indico is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Indico; if not, see <http://www.gnu.org/licenses/>.

from __future__ import unicode_literals

from uuid import UUID

from flask import flash, redirect, request, render_template, session
from sqlalchemy.orm.exc import NoResultFound
from werkzeug.exceptions import Forbidden

from indico.core.db import db
from indico.modules.users.controllers import RHUserBase
from indico.modules.oauth import logger
from indico.modules.oauth.provider import oauth
from indico.modules.oauth.forms import ApplicationForm
from indico.modules.oauth.models.applications import OAuthApplication, SCOPES
from indico.modules.oauth.models.tokens import OAuthToken
from indico.modules.oauth.views import WPOAuthUserProfile, WPOAuthAdmin
from indico.util.i18n import _
from indico.web.flask.util import url_for
from indico.web.forms.base import FormDefaults
from MaKaC.webinterface.rh.admins import RHAdminBase
from MaKaC.webinterface.rh.base import RH, RHProtected


class RHOAuthAuthorize(RHProtected):
    def _checkParams(self):
        try:
            UUID(hex=request.args['client_id'])
        except ValueError:
            raise NoResultFound
        self.application = OAuthApplication.find_one(client_id=request.args['client_id'])

    @oauth.authorize_handler
    def _process(self, **kwargs):
        if request.method == 'POST':
            if 'confirm' not in request.form:
                return False
            logger.info('User {} authorized {}'.format(session.user, self.application))
            return True
        if self.application.is_trusted:
            logger.info('User {} automatically authorized {}'.format(session.user, self.application))
            return True
        requested_scopes = set(kwargs['scopes'])
        token = self.application.tokens.filter_by(user=session.user).first()
        authorized_scopes = token.scopes if token else set()
        if requested_scopes <= authorized_scopes:
            return True
        new_scopes = requested_scopes - authorized_scopes
        return render_template('oauth/authorize.html', application=self.application,
                               authorized_scopes=filter(None, [SCOPES.get(s) for s in authorized_scopes]),
                               new_scopes=filter(None, [SCOPES.get(s) for s in new_scopes]))


class RHOAuthErrors(RHProtected):
    def _process(self, **kwargs):
        return render_template('oauth/authorize_errors.html', error=request.args['error'])


class RHOAuthToken(RH):
    @oauth.token_handler
    def _process(self, **kwargs):
        return None


class RHOAuthAdmin(RHAdminBase):
    """OAuth server administration settings"""

    def _process(self):
        applications = OAuthApplication.find().order_by(db.func.lower(OAuthApplication.name)).all()
        return WPOAuthAdmin.render_template('apps.html', applications=applications)


class RHOAuthAdminApplicationBase(RHAdminBase):
    """Base class for single OAuth application RHs"""
    def _checkParams(self):
        self.application = OAuthApplication.get(request.view_args['id'])


class RHOAuthAdminApplication(RHOAuthAdminApplicationBase):
    """Handles application details page"""

    def _process(self):
        form = ApplicationForm(obj=self.application, application=self.application)
        if form.validate_on_submit():
            form.populate_obj(self.application)
            flash(_("Application {} was modified").format(self.application.name), 'success')
            return redirect(url_for('.apps'))
        return WPOAuthAdmin.render_template('app_details.html', application=self.application, form=form)


class RHOAuthAdminApplicationDelete(RHOAuthAdminApplicationBase):
    """Handles OAuth application deletion"""

    CSRF_ENABLED = True

    def _process(self):
        db.session.delete(self.application)
        logger.info("Application {} was deleted.".format(self.application))
        flash(_("Application deleted successfully"), 'success')
        return redirect(url_for('.apps'))


class RHOAuthAdminApplicationNew(RHAdminBase):
    """Handles OAuth application registration"""

    def _process(self):
        form = ApplicationForm(obj=FormDefaults(is_enabled=True))
{'fraction_non_alphanumeric': 0.09186949766960124, 'fraction_numerical': 0.0022786121180735372, 'mean_word_length': 3.7873078829945466, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7641208', 'n_tokens_mistral': 2673, 'n_tokens_neox': 2586, 'n_words': 722}