Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import os.path
from envparse import env

import sys
from flask import Flask, json, make_response
from app.settings import get_settings, get_setts
Expand All @@ -13,7 +14,7 @@
from flask_cors import CORS
from flask_rest_jsonapi.errors import jsonapi_errors
from flask_rest_jsonapi.exceptions import JsonApiException
from healthcheck import HealthCheck, EnvironmentDump
from healthcheck import HealthCheck
from apscheduler.schedulers.background import BackgroundScheduler
from elasticsearch_dsl.connections import connections
from pytz import utc
Expand Down Expand Up @@ -46,7 +47,6 @@
static_dir = os.path.dirname(os.path.dirname(__file__)) + "/static"
template_dir = os.path.dirname(__file__) + "/templates"
app = Flask(__name__, static_folder=static_dir, template_folder=template_dir)

env.read_envfile()


Expand Down Expand Up @@ -127,7 +127,7 @@ def create_app():
from app.api.users import user_misc_routes
from app.api.orders import order_misc_routes
from app.api.role_invites import role_invites_misc_routes
from app.api.auth import ticket_blueprint
from app.api.auth import ticket_blueprint, authorised_blueprint
from app.api.admin_translations import admin_blueprint

app.register_blueprint(api_v1)
Expand All @@ -143,6 +143,7 @@ def create_app():
app.register_blueprint(order_misc_routes)
app.register_blueprint(role_invites_misc_routes)
app.register_blueprint(ticket_blueprint)
app.register_blueprint(authorised_blueprint)
app.register_blueprint(admin_blueprint)

sa.orm.configure_mappers()
Expand Down Expand Up @@ -205,7 +206,6 @@ def __call__(self, *args, **kwargs):

# Health-check
health = HealthCheck(current_app, "/health-check")
envdump = EnvironmentDump(current_app, "/environment", include_config=False)
health.add_check(health_check_celery)
health.add_check(health_check_db)
with current_app.app_context():
Expand Down
36 changes: 36 additions & 0 deletions app/api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import string

import requests
from healthcheck import EnvironmentDump
from functools import wraps
from flask import request, jsonify, make_response, Blueprint, send_file, url_for, redirect
from flask_jwt import current_identity as current_user, jwt_required
from sqlalchemy.orm.exc import NoResultFound
Expand All @@ -28,6 +30,7 @@
from app.api.helpers.storage import UPLOAD_PATHS


authorised_blueprint = Blueprint('authorised_blueprint', __name__, url_prefix='/')
ticket_blueprint = Blueprint('ticket_blueprint', __name__, url_prefix='/v1')
auth_routes = Blueprint('auth', __name__, url_prefix='/v1/auth')

Expand Down Expand Up @@ -328,3 +331,36 @@ def order_invoices(order_identifier):
return ForbiddenError({'source': ''}, 'Unauthorized Access').respond()
else:
return ForbiddenError({'source': ''}, 'Authentication Required to access Invoice').respond()


# Access for Environment details & Basic Auth Support
def check_auth_admin(username, password):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expected 2 blank lines, found 1

"""
This function is called to check for proper authentication & admin rights
"""
if username and password:
user = User.query.filter_by(_email=username).first()
if user:
if user.is_correct_password(password):
if user.is_admin:
return True
return False


def requires_basic_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth or not check_auth_admin(auth.username, auth.password):
return make_response('Could not verify your access level for that URL.\n'
'You have to login with proper credentials', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'})
return f(*args, **kwargs)
return decorated


@authorised_blueprint.route('/environment')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expected 2 blank lines, found 1

@requires_basic_auth
def environment_details():
envdump = EnvironmentDump(include_config=False)
return envdump.dump_environment()