Example #1
0
def cors_app(app):
    cfg = config.get_config()
    opts = {}
    if cfg.has_section('cross_domain'):
        options = dict(config.get_config().items("cross_domain"))
        opts = _parse_options(options)
    CORS(app, **opts)
Example #2
0
    def test_get_response_if_expired_token_raises(self):
        credentials = {"login": "******", "password": "******"}
        with self._app.app_context():
            config.get_config().set("auth", "auth_token_expiration", "-1")
            user = tables.User(is_super_admin=True, **credentials)
            user.add()

        token = self.post('/auth/tokens', data={"user": credentials},
                          url_is_absolute=True)["token"]["token"]
        response = self.get("books", headers={"X-Auth-Token": token})
        self.assertExceptionWrapped(response, exceptions.ExpiredToken())
Example #3
0
def load_config(app):
    """
    Try to load config.
    :param app: flask app instance
    :param test: is test
    :return: None
    """
    if config.get_config().has_section("service:api"):
        opts = dict(config.get_config().items("service:api"))
        options = {name.upper(): value
                   for name, value in six.iteritems(_parse_options(opts))}
        app.config.update(**options)
Example #4
0
def _get_engine():
    global _ENGINE
    if _ENGINE:
        return _ENGINE

    if not bs_config.get_config().has_section("storage"):
        raise bs_exceptions.ConfigurationError(
            "Storage section was missing.")
    try:
        connection = bs_config.get_config().get("storage", "connection")
    except Exception as e:
        raise bs_exceptions.ConfigurationError(str(e))
    _ENGINE = sa.create_engine(connection)
    return _ENGINE
Example #5
0
 def tokens_generate_token(self, user_obj):
     try:
         user = tables.User.get_user(login=user_obj.data["login"])
     except exc.NoResultFound:
         raise exceptions.BadCredentials
     if not user.verify_password(password=user_obj.data["password"]):
         raise exceptions.BadCredentials
     try:
         auth_token_expiration = config.get_config().getint(
             "auth", "auth_token_expiration")
         secret_key = config.get_config().get("auth", "secret_key")
     except Exception as e:
         raise exceptions.ConfigurationError(str(e))
     token = user.generate_auth_token(
         secret_key, expiration=auth_token_expiration)
     return token
Example #6
0
    def before_request_handler():
        token = request.headers.get('X-Auth-Token', None)

        params = {
            "auth_token": token,
            "auth_status": enums.AuthStatus.unauthorized
        }
        user = None
        try:
            user = tables.User.verify_auth_token(config.get_config().get(
                "auth", "secret_key"), token)
            params["auth_status"] = enums.AuthStatus.authorized
        except itsdangerous.SignatureExpired:
            params["auth_status"] = enums.AuthStatus.expired_token
        except itsdangerous.BadSignature:
            params["auth_status"] = enums.AuthStatus.invalid_token
        except itsdangerous.BadData:
            params["auth_status"] = enums.AuthStatus.unauthorized
        except configparser.Error as e:
            raise exceptions.ConfigurationError(str(e))

        if user:
            params.update({
                "user_id": user.id,
                "login": user.login,
                "is_guest": False,
                "is_super_admin": user.is_super_admin,
            })
        ctxt = context.Context(**params)
        request.environ['context'] = ctxt
Example #7
0
    def _create_app(self):
        global GOLDEN_DB_URI
        _, self.working_database_path = tempfile.mkstemp(prefix='bookstore-',
                                                         suffix='.sqlite')

        basedir = os.path.abspath(os.path.dirname(__file__))
        os.environ["APP_CONFIG_PATH"] = os.path.join(basedir,
                                                     'conf',
                                                     'config_test.cfg')
        config.get_config().set(
            "storage", "connection",
            'sqlite:///' + self.working_database_path)
        if not GOLDEN_DB_URI:
            storage.Base.metadata.create_all(tables._get_engine())

            _, golden_database_path = tempfile.mkstemp(prefix='bookstore-',
                                                       suffix='.sqlite')
            shutil.copyfile(self.working_database_path, golden_database_path)
            GOLDEN_DB_URI = golden_database_path
        else:
            shutil.copyfile(GOLDEN_DB_URI, self.working_database_path)

        self._app = create_app()