64 lines
1.8 KiB
Python
64 lines
1.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
Backend base module
|
|
"""
|
|
|
|
import jwt
|
|
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
|
|
|
|
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
|
|
|
|
oidc_auth.init_app(app)
|
|
|
|
# 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)
|
|
|
|
# Fix flask-restplus by duck typing error handlers
|
|
jwt_extended._set_error_handler_callbacks(api_v1)
|
|
|