import sentry_sdk from flask import Flask, Response, jsonify from auth_api import config from auth_api.auth import auth from auth_api.errors import AuthError from auth_api.logs import configure_logging, logger, register_logging_context_hooks, register_logging_events_hooks from auth_api.users import current_user from auth_api.utils import encode_payload configure_logging() if config.SENTRY_DSN: from sentry_sdk.integrations.flask import FlaskIntegration from sentry_sdk.integrations.redis import RedisIntegration from sentry_sdk.utils import BadDsn try: sentry_sdk.init( dsn=config.SENTRY_DSN, environment=config.FLASK_ENV, integrations=[FlaskIntegration(), RedisIntegration()], ) except BadDsn: pass app = Flask(__name__) app.config.from_object("auth_api.config") register_logging_context_hooks(app) register_logging_events_hooks(app) @app.errorhandler(AuthError) def handle_auth_error(ex): response = jsonify(ex.error) response.status_code = ex.status_code logger.bind(error_code=ex.error.code).warning(ex.error.description) return response @app.route("/authorize") @auth def authorize(): headers = {} if current_user.is_m2m: headers["X-Client-Id"] = current_user.client_id else: headers["X-Client-Id"] = current_user.client_id headers["X-User-Id"] = current_user.user_id headers["Treatments"] = current_user.get_treatments() headers["X-Userinfo"] = encode_payload(current_user.user_info) # base64 encoded json return Response(headers=headers) @app.route("/health") def health(): return jsonify({}) if __name__ == "__main__": app.run(host="0.0.0.0", port=5000) # nosec