Skip to content
Snippets Groups Projects
Commit 0c281d0c authored by Robert Goldmann's avatar Robert Goldmann
Browse files

Fixed #4 - api key authentication

parent 3bed664a
No related branches found
No related tags found
No related merge requests found
......@@ -139,6 +139,13 @@ paths:
post:
summary: Adds a new measurement. Non-existent device or sensor will be created automatically.
operationId: addMeasurement
parameters:
- in: header
name: apiKey
description: The api key
required: true
schema:
type: string
requestBody:
description: Measurement to add
required: true
......
......@@ -11,6 +11,7 @@
"databasePath": "storageLeaf.db"
},
"api": {
"url": "http://localhost:10003"
"url": "http://localhost:10003",
"key": ""
}
}
\ No newline at end of file
......@@ -7,6 +7,7 @@ import yaml
from flask import Blueprint, request, jsonify, render_template
from logic import Constants
from logic.AuthenticationWrapper import require_api_key
from logic.Database import Database
from logic.RequestValidator import ValidationError, RequestValidator
......@@ -96,6 +97,7 @@ def construct_blueprint(settings, version):
return jsonify(database.get_all_measurements_for_sensor(sensorID))
@routes.route('/measurements', methods=['POST'])
@require_api_key(password=settings['api']['key'])
def addMeasurement():
try:
parameters = RequestValidator.validate(request, DeviceParameters.get_values())
......
from functools import wraps
from flask import request, jsonify
def require_api_key(password):
def wrap_route(func):
@wraps(func)
def check_api_key(*args, **kwargs):
apiKey = request.headers.get('apiKey')
if not apiKey:
return jsonify({'message': 'apiKey missing'}), 401
if apiKey == password:
# redirect to requested url
return func(*args, **kwargs)
return jsonify({'message': 'apiKey invalid'}), 401
return check_api_key
return wrap_route
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment