moved everything to a new module called backend
This commit is contained in:
70
backend/__init__.py
Normal file
70
backend/__init__.py
Normal file
@@ -0,0 +1,70 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Backend base module
|
||||
"""
|
||||
|
||||
import jwt
|
||||
import requests
|
||||
from flask import Flask, jsonify
|
||||
from flask_httpauth import HTTPTokenAuth, HTTPBasicAuth, MultiAuth
|
||||
from flask_jwt_extended import JWTManager, decode_token
|
||||
from flask_login import LoginManager
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from flask_cors import CORS
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config.from_object('backend.config.Config')
|
||||
db = SQLAlchemy(app)
|
||||
|
||||
|
||||
login_manager = LoginManager()
|
||||
login_manager.init_app(app)
|
||||
|
||||
# flask_jwt_extended: to be used usually by API
|
||||
jwt_extended = JWTManager(app)
|
||||
#
|
||||
jwt_auth = HTTPTokenAuth('Bearer')
|
||||
|
||||
|
||||
@jwt_auth.verify_token
|
||||
def verify_token(token):
|
||||
"""This function (and HTTPTokenAuth('Bearer')) has been defined to be used together with MultiAuth. For API calls
|
||||
solely using JWT authentication, jwt_required of flask_jwt_extended should be used directly."""
|
||||
app.logger.info(token)
|
||||
try:
|
||||
decoded = decode_token(token)
|
||||
except jwt.exceptions.DecodeError as e:
|
||||
app.logger.warn("Could not verify token: {}".format(str(e)))
|
||||
return False
|
||||
except jwt.exceptions.ExpiredSignatureError as e:
|
||||
app.logger.warn("Could not verify token: {}".format(str(e)))
|
||||
return False
|
||||
app.logger.info(decoded)
|
||||
return True
|
||||
|
||||
basic_auth = HTTPBasicAuth()
|
||||
multi_auth = MultiAuth(basic_auth, jwt_auth)
|
||||
|
||||
from backend.auth import oidc_auth, auth_bp
|
||||
|
||||
try:
|
||||
oidc_auth.init_app(app)
|
||||
except requests.exceptions.ConnectionError as err:
|
||||
app.logger.error("Could not connect to OIDC!!", err)
|
||||
|
||||
# oidc_multi_auth = MultiAuth(oidc_auth, jwt_auth) <- can't work as OIDCAuthentication not implementing HTTPAuth
|
||||
|
||||
from .serve_frontend import fe_bp
|
||||
from .api import auth_api_bp, api_v1, api_bp
|
||||
|
||||
app.register_blueprint(auth_bp)
|
||||
app.register_blueprint(auth_api_bp)
|
||||
app.register_blueprint(api_bp)
|
||||
app.register_blueprint(fe_bp)
|
||||
|
||||
CORS(app)
|
||||
CORS(api_bp)
|
||||
|
||||
# Fix flask-restplus by duck typing error handlers
|
||||
jwt_extended._set_error_handler_callbacks(api_v1)
|
||||
|
||||
Reference in New Issue
Block a user