forked from ericsopa/flask-api-key
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goaway.py
33 lines (29 loc) · 1.08 KB
/
goaway.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from flask import Flask
app = Flask(__name__)
from functools import wraps
from flask import request, abort
import ssl
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain('example.crt', 'example.key')
# The actual decorator function
def require_appkey(view_function):
@wraps(view_function)
# the new, post-decoration function. Note *args and **kwargs here.
def decorated_function(*args, **kwargs):
with open('api.key', 'r') as apikey:
key=apikey.read().replace('\n', '')
#if request.args.get('key') and request.args.get('key') == key:
if request.headers.get('x-api-key') and request.headers.get('x-api-key') == key:
return view_function(*args, **kwargs)
else:
abort(401)
return decorated_function
@app.route('/json/', methods=['POST'])
@require_appkey
def put_user():
return 'Posted JSON!'
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(host='127.0.0.1',port='443', debug=False/True, ssl_context=context)