Beispiel #1
0
    def create_app(self):
        app = Flask(__name__)

        @app.route("/errors", methods=["GET"])
        def a_terrible_handler():
            raise errors.Conflict(msg=TestErrors.ERROR_MSG)

        @app.route("/uncaught_errors", methods=["GET"])
        def an_even_worse_handler():
            raise ArithmeticError()

        @app.route("/verbose_errors", methods=["GET"])
        def a_fancy_handler():
            raise errors.Conflict(
                msg=TestErrors.ERROR_MSG, additional_data={"foo": "bar"}
            )

        @app.route("/route_that_fails_validation", methods=["GET"])
        def validation_fails_handler():
            raise BadRequest()

        @app.route("/slow", methods=["GET"])
        def a_slow_handler():
            raise SystemExit()

        Rebar().init_app(app=app)

        return app
Beispiel #2
0
    def create_app(self):
        app = Flask(__name__)
        Rebar().init_app(app=app)

        class Schema(validation.RequestSchema):
            foo = fields.Integer(required=True)
            bar = fields.Boolean()
            baz = validation.CommaSeparatedList(fields.Integer())

        @app.route("/stuffs", methods=["GET"])
        def query_string_handler():
            params = get_query_string_params_or_400(schema=Schema())
            return response(data=params)

        return app
Beispiel #3
0
    def create_app(self):
        app = Flask(__name__)
        Rebar().init_app(app=app)

        class NestedSchema(validation.RequestSchema):
            baz = fields.List(fields.Integer())

        class Schema(validation.RequestSchema):
            foo = fields.Integer(required=True)
            bar = fields.Email()
            nested = fields.Nested(NestedSchema)

        @app.route("/stuffs", methods=["POST"])
        def json_body_handler():
            data = get_json_body_params_or_400(schema=Schema)
            return response(data)

        return app
Beispiel #4
0
    def create_app(self):
        app = Flask(__name__)

        @app.route('/errors', methods=['GET'])
        def a_terrible_handler():
            raise errors.Conflict(msg=TestErrors.ERROR_MSG)

        @app.route('/uncaught_errors', methods=['GET'])
        def an_even_worse_handler():
            raise ArithmeticError()

        @app.route('/verbose_errors', methods=['GET'])
        def a_fancy_handler():
            raise errors.Conflict(msg=TestErrors.ERROR_MSG,
                                  additional_data={'foo': 'bar'})

        Rebar().init_app(app=app)

        return app
Beispiel #5
0
    def __init__(self):
        super().__init__(__name__)

        # TODO: apply proper CORS values
        CORS(self)
        config = get_config()
        self.config.from_object(config)
        self.url_map.strict_slashes = False
        self.app_context().push()

        self.rebar = Rebar()
        self.rebar.add_handler_registry(version_1_registry)
        self.rebar.init_app(app=self)

        self.flask_marshmallow = FlaskMarshmallow(app=self)

        self.secret_key = config.API_SECRET_KEY
        self.login_manager = LoginManager()
        self.login_manager.init_app(app=self)
        self.login_manager.unauthorized_callback = raise_unauthorized

        self._init_logger()
        self._init_db_client()
Beispiel #6
0
import marshmallow as m

from flask_rebar import Rebar
from flask_rebar import HeaderApiKeyAuthenticator
from flask_rebar import compat
from flask_rebar.swagger_generation import SwaggerV2Generator, SwaggerV3Generator

rebar = Rebar()
registry = rebar.create_handler_registry()

swagger_v2_generator = SwaggerV2Generator()
swagger_v3_generator_with_hidden = SwaggerV3Generator(include_hidden=True)
normal_swagger_v3_generator = SwaggerV3Generator()

authenticator = HeaderApiKeyAuthenticator(header="x-auth")
default_authenticator = HeaderApiKeyAuthenticator(header="x-another", name="default")


class HeaderSchema(m.Schema):
    user_id = compat.set_data_key(field=m.fields.String(required=True), key="X-UserId")


class FooSchema(m.Schema):
    __swagger_title__ = "Foo"

    uid = m.fields.String()
    name = m.fields.String()


class NestedFoosSchema(m.Schema):
    data = m.fields.Nested(FooSchema, many=True)
Beispiel #7
0
from functools import partial

from flask import Flask
from flask_cors import CORS
from flask_login import LoginManager
from flask_migrate import Migrate
from flask_rebar import Rebar
from flask_sqlalchemy import SQLAlchemy

# Globally accessible libraries
from JDISCTF.flask_login_authenticator import FlaskLoginAuthenticator, register_authenticators
from config import Config

DB = SQLAlchemy()
MIGRATE = Migrate()
REBAR = Rebar()
LOGIN_MANAGER = LoginManager()
REGISTRY = REBAR.create_handler_registry(prefix="/api")
REGISTRY.set_default_authenticator(FlaskLoginAuthenticator())

# make columns non-nullable by default, most of them should be
DB.Column = partial(DB.Column, nullable=False)

# register authenticator for Swagger
register_authenticators(REGISTRY)


def create_app(test_config=None) -> Flask:
    """Initialize the core application"""

    app = Flask(__name__, instance_relative_config=True)