I am implementing a Delete API which requires basic authentication before deleting any user. following is my code for basic auth and deleting a user which works perfectly fine via curl commands.
def auth_required(f):
@wraps(f)
def decorated(*args, **kwargs):
if(request.authorization != None and request.authorization["username"] != None and request.authorization["password"] != None):
username = request.authorization["username"]
password = request.authorization["password"]
else:
return make_response('User does not exists.\n' 'Please provide user credentials', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'})
if check_auth(username, password):
return f(*args, **kwargs)
else:
return make_response('Could not verify the credentials.\n' 'Please use correct credentials', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'})
return decorated
def check_auth(username, password):
cur = get_db().cursor().execute("SELECT user_name, password from users WHERE user_name=?", (username,))
row = cur.fetchone()
if row and row[0] == username and pbkdf2_sha256.verify(password, row[1]):
return True
else:
return False
#curl command to execure delete function - curl -u parag:parag --include --verbose --request DELETE --header 'Content-Type: application/json' http://localhost:5000/delete_user/
@app.route('/delete_user', methods=['DELETE'])
@auth_required
def api_delete_user():
if request.method == 'DELETE':
status_code:bool = False
cur = get_db().cursor()
username = request.authorization["username"]
try:
cur.execute("UPDATE users SET active_status =? WHERE user_name=?",(0, username,))
if(cur.rowcount >= 1):
get_db().commit()
status_code = True
except:
get_db().rollback()
status_code = False
finally:
if status_code:
return jsonify(message="Passed"), 201
else:
return jsonify(message="Fail"), 409
I have created a YAML file to test the above delete API but I am unable to add basic authentication in it. following is my YAML file for testing the delete API.
test_name: Delete existing user
stages:
- name: Make sure you delete existing user
request:
url: http://localhost:5000/delete_user
json:
method: DELETE
headers:
content-type: application/json
response:
status_code: 201
body:
message: Passed
save:
$ext:
context:
parameters:
auth_required:
username: parag
password: bhingre
Above file does not help me delete user by basic authenticating before deleting user. Please let me know if any solutions or suggestions.