Skip to content

Commit

Permalink
added basic http auth
Browse files Browse the repository at this point in the history
added remote ip address in request context to be measured
  • Loading branch information
TigerApps committed Dec 5, 2015
1 parent cc988a9 commit 0636146
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
5 changes: 5 additions & 0 deletions examples/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
"enabled": app.config["DEBUG"],
"storage": {
"engine": "sqlite"
},
"basicAuth":{
"enabled": True,
"username": "Admin",
"password": "admin"
}
}

Expand Down
51 changes: 44 additions & 7 deletions flask_profiler/flask_profiler.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
# -*- coding: utf8 -*-
from timeit import default_timer
import time

import functools
from flask import request, jsonify, Blueprint
from . import storage
import time

from pprint import pprint as pp

from flask import Blueprint
from flask import jsonify
from flask import request
from flask.ext.httpauth import HTTPBasicAuth

from . import storage

CONF = {}
collection = None

myUsername = None
myPassword = None

auth = HTTPBasicAuth()
@auth.verify_password
def verify_password(username, password):
if not myUsername:
return True
if username == myUsername and password == myPassword:
return True
return False


class Measurement(object):
"""represents an endpoint measurement"""
Expand Down Expand Up @@ -85,7 +103,9 @@ def wrapper(*args, **kwargs):
"form": dict(request.form.items()),
"body": request.data.decode("utf-8", "strict"),
"headers": dict(request.headers.items()),
"func": request.endpoint}
"func": request.endpoint,
"ip": request.remote_addr
}
endpoint_name = str(request.url_rule)
wrapped = measure(f, endpoint_name, request.method, context)
return wrapped(*args, **kwargs)
Expand Down Expand Up @@ -127,31 +147,37 @@ def registerInternalRouters(app):
static_folder="static/dist/", static_url_path='/static/dist')

@fp.route("/".format(urlPath))
@auth.login_required
def index():
return fp.send_static_file("index.html")

@fp.route("/api/measurements/".format(urlPath))
@auth.login_required
def filtermeasurements():
args = dict(request.args.items())
measurements = collection.filter(args)
return jsonify({"measurements": list(measurements)})

@fp.route("/api/measurements/grouped/".format(urlPath))
@auth.login_required
def getmeasurementsSummary():
args = dict(request.args.items())
measurements = collection.getSummary(args)
return jsonify({"measurements": list(measurements)})

@fp.route("/api/measurements/<measurementId>".format(urlPath))
@auth.login_required
def getContext(measurementId):
return jsonify(collection.get(measurementId))

@fp.route("/api/measurements/timeseries/".format(urlPath))
@auth.login_required
def getReqiestsTimeseries():
args = dict(request.args.items())
return jsonify({"series": collection.getTimeseries(args)})

@fp.route("/api/measurements/methodDistribution/".format(urlPath))
@auth.login_required
def getMethodDistribution():
args = dict(request.args.items())
return jsonify({
Expand All @@ -165,7 +191,7 @@ def init_app(app):

try:
CONF = app.config["flask_profiler"]
except Exception as e:
except Exception:
raise Exception(
"to init flask-profiler, provide "
"required config through flask app's config. please refer: "
Expand All @@ -179,4 +205,15 @@ def init_app(app):
wrapAppEndpoints(app)
registerInternalRouters(app)

print(" * flask-profiler is enabled.")
basicAuth = CONF.get("basicAuth", None)
if basicAuth:
try:
if basicAuth['enabled']:
global myUsername, myPassword
myUsername = basicAuth['username']
myPassword = basicAuth['password']
except Exception:
raise Exception(
"to use auth, provide required config through "
"flask app's config. please refer: "
"https://github.com/muatik/flask-profiler")
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
]

install_requires = [
'Flask'
'Flask',
'Flask-HTTPAuth'
]

setup(
Expand Down

0 comments on commit 0636146

Please sign in to comment.