mirror of
https://github.com/kennethreitz/httpbin.git
synced 2026-06-05 23:00:18 +00:00
Merge branch 'master' of https://github.com/zsiciarz/httpbin
This commit is contained in:
+15
-1
@@ -18,7 +18,7 @@ from time import time as now
|
||||
from decorator import decorator
|
||||
from flask import Flask, Response, request, render_template, redirect, g
|
||||
|
||||
from .helpers import get_files, get_headers, status_code, get_dict
|
||||
from .helpers import get_files, get_headers, status_code, get_dict, check_basic_authorization
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
@@ -36,6 +36,10 @@ def json_resource(f, runtime=True, *args, **kwargs):
|
||||
data = f(*args, **kwargs)
|
||||
_t1 = now()
|
||||
|
||||
# we already have a formatted response, move along
|
||||
if isinstance(data, Response):
|
||||
return data
|
||||
|
||||
dump = json.dumps(data, sort_keys=True, indent=3)
|
||||
|
||||
r = app.make_response(dump)
|
||||
@@ -196,5 +200,15 @@ def set_cookie(name, value):
|
||||
return r
|
||||
|
||||
|
||||
@app.route('/basic-auth')
|
||||
@json_resource
|
||||
def basic_auth():
|
||||
"""Prompts the user for authorization using HTTP Basic Auth."""
|
||||
|
||||
if not check_basic_authorization():
|
||||
return status_code(401)
|
||||
return dict(authenticated=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
|
||||
+8
-1
@@ -108,4 +108,11 @@ def status_code(code):
|
||||
if 'headers' in m:
|
||||
r.headers = m['headers']
|
||||
|
||||
return r
|
||||
return r
|
||||
|
||||
|
||||
def check_basic_authorization():
|
||||
"""Checks user authentication using HTTP Basic Auth."""
|
||||
|
||||
auth = request.authorization
|
||||
return auth and auth.username == "httpbin" and auth.password == "secret"
|
||||
|
||||
Reference in New Issue
Block a user