Exemplo n.º 1
0
def create_app():

    app = Flask(__name__)
    api = Api(app)
    config_name = os.getenv("FLASK_ENV")

    #CORS(app)
    cors = CORS(
        app, resources={r"/api/*": {
            "origins": [
                "http://localhost:8080",
            ]
        }})
    app.config['CORS_HEADERS'] = 'Content-Type'
    app.config.from_object(app_config[config_name])

    jwt = JWTManager(app)

    @jwt.user_claims_loader
    def add_claims_to_jwt(identity):
        from app.api.user.models import User
        user = User.find_by_id(identity)

        if user.is_admin:
            return {'roles': "admin"}
        return {'roles': "user"}

    @jwt.token_in_blacklist_loader
    def check_if_token_in_blacklist(decrypted_token):
        from app.security import TokenBlacklist

        return decrypted_token['jti'] in TokenBlacklist.get_all()

    @jwt.expired_token_loader
    def expired_token_callback():

        return jsonify({
            'description': 'The token has expired',
            'error': 'token_expired'
        }), 401

    @jwt.invalid_token_loader
    def invalid_token_callback(error):
        return jsonify({
            'description': 'Signature verification failed.',
            'error': 'invalid_token'
        }), 401

    @jwt.unauthorized_loader
    def missing_token_callback(error):
        return jsonify({
            'description': 'Request does not contain an access token.',
            'error': 'authorization_required'
        }), 401

    @jwt.needs_fresh_token_loader
    def token_not_fresh_callback():
        return jsonify({
            'description': 'The token is not fresh.',
            'error': 'fresh_token_required'
        }), 401

    @jwt.revoked_token_loader
    def revoked_token_callback():
        return jsonify({
            'description': 'The token has been revoked.',
            'error': 'token_revoked'
        }), 401

    ##import area
    from app.api.user.resources import UserLoginApi, UserLogoutApi, UserRegisterApi, UserApi
    api.add_resource(UserLoginApi, "/api/v1/login")
    api.add_resource(UserLogoutApi, "/api/v1/logout")
    api.add_resource(UserRegisterApi, "/api/v1/register")
    api.add_resource(UserApi, "/api/v1/user")

    from app.api.system.resources import SystemSettingApi, SystemSettingUpdateApi
    api.add_resource(SystemSettingApi, "/api/v1/system/settings")
    api.add_resource(SystemSettingUpdateApi, "/api/v1/system/setting")

    #redis_client.init_app(app)
    db.init_app(app)
    migrate = Migrate(app, db)
    ma.init_app(app)

    return app
Exemplo n.º 2
0
def init_marshmallow(app):
    ma.init_app(app)
Exemplo n.º 3
0
        'description': 'The token is not fresh.',
        'error': 'fresh_token_required'
    	}), 401


	@jwt.revoked_token_loader
	def revoked_token_callback():
		return jsonify({
        'description': 'The token has been revoked.',
        'error': 'token_revoked'
    	}), 401
	

	## import area für resource
    from app.api.user.resource import (
            AdminUserListApi,
            UserLoginApi,
            UserLogoutApi
            )
    api.add_resource(UserLoginApi, "/api/v1/login")
    api.add_resource(UserLogoutApi, "/api/v1/logout")
    api.add_resource(AdminUserListApi, "/api/v1/admin/user")



	db.init_app(app)
	migrate = Migrate(app, db)
	ma.init_app(app)

	return app
Exemplo n.º 4
0
def create_app():

    app = Flask(__name__)
    api = Api(app)
    config_name = os.getenv("APP_SETTINGS")
    app.config['CORS_HEADERS'] = 'Content-Type'

    cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
    app.config.from_object(app_config[config_name])

    jwt = JWTManager(app)

    @jwt.user_claims_loader
    def add_claims_to_jwt(identity):

        #TODO: change condition to check if user is admin
        if identity == 1:  #instead of hard-coding, read from a config or database
            return {'is_admin': True}
        return {'is_admin': False}

    @jwt.token_in_blacklist_loader
    def check_if_token_in_blacklist(decrypted_token):
        from app.security import TokenBlacklist

        return decrypted_token['jti'] in TokenBlacklist.get_all()

    @jwt.expired_token_loader
    def expired_token_callback():

        return jsonify({
            'description': 'The token has expired',
            'error': 'token_expired'
        }), 401

    @jwt.invalid_token_loader
    def invalid_token_callback(error):
        return jsonify({
            'description': 'Signature verification failed.',
            'error': 'invalid_token'
        }), 401

    @jwt.unauthorized_loader
    def missing_token_callback(error):
        return jsonify({
            'description': 'Request does not contain an access token.',
            'error': 'authorization_required'
        }), 401

    @jwt.needs_fresh_token_loader
    def token_not_fresh_callback():
        return jsonify({
            'description': 'The token is not fresh.',
            'error': 'fresh_token_required'
        }), 401

    @jwt.revoked_token_loader
    def revoked_token_callback():
        return jsonify({
            'description': 'The token has been revoked.',
            'error': 'token_revoked'
        }), 401

    ## import area für resource
    from app.api.user.resources import UserLoginApi, UserLogoutApi, UserRegisterApi, UserApi
    api.add_resource(UserLoginApi, "/api/v1/login")
    api.add_resource(UserLogoutApi, "/api/v1/logout")
    api.add_resource(UserApi, "/api/v1/user")

    api.add_resource(UserRegisterApi, "/api/v1/admin/register")

    db.init_app(app)
    migrate = Migrate(app, db)
    ma.init_app(app)

    return app
Exemplo n.º 5
0
def create_app():

    app = Flask(__name__)
    api = Api(app)
    config_name = os.getenv("APP_SETTINGS")
    app.config['CORS_HEADERS'] = 'Content-Type'

    cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
    app.config.from_object(app_config[config_name])

    jwt = JWTManager(app)

    @jwt.user_claims_loader
    def add_claims_to_jwt(identity):

        #TODO: change condition to check if user is admin
        if identity == 1:  #instead of hard-coding, read from a config or database
            return {'is_admin': True}
        return {'is_admin': False}

    @jwt.token_in_blacklist_loader
    def check_if_token_in_blacklist(decrypted_token):
        from app.security import TokenBlacklist

        return decrypted_token['jti'] in TokenBlacklist.get_all()

    @jwt.expired_token_loader
    def expired_token_callback():

        return jsonify({
            'description': 'The token has expired',
            'error': 'token_expired'
        }), 401

    @jwt.invalid_token_loader
    def invalid_token_callback(error):
        return jsonify({
            'description': 'Signature verification failed.',
            'error': 'invalid_token'
        }), 401

    @jwt.unauthorized_loader
    def missing_token_callback(error):
        return jsonify({
            'description': 'Request does not contain an access token.',
            'error': 'authorization_required'
        }), 401

    @jwt.needs_fresh_token_loader
    def token_not_fresh_callback():
        return jsonify({
            'description': 'The token is not fresh.',
            'error': 'fresh_token_required'
        }), 401

    @jwt.revoked_token_loader
    def revoked_token_callback():
        return jsonify({
            'description': 'The token has been revoked.',
            'error': 'token_revoked'
        }), 401

    @app.route("/plot/<string:filename>")
    def getPlot(filename):

        return send_file("static/plots/{}.png".format(filename))

    @app.route("/placeholder")
    def placeholder():
        return send_file("static/plots/placeholder.png")

    ## import area für resource
    from app.api.qrk.resources import QrkApi, QrkListApi, MesswertApi, MesswertListApi
    api.add_resource(QrkListApi, "/api/v1/qrk")
    api.add_resource(QrkApi, "/api/v1/qrk/<string:qrk_id>")
    api.add_resource(MesswertListApi, "/api/v1/qrk/<string:qrk_id>/messwert")
    api.add_resource(
        MesswertApi,
        "/api/v1/qrk/<string:qrk_id>/messwert/<string:messwert_id>")

    db.init_app(app)
    migrate = Migrate(app, db)
    ma.init_app(app)

    return app