-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
45 lines (36 loc) · 1.24 KB
/
auth.py
File metadata and controls
45 lines (36 loc) · 1.24 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
# auth.py
"""
This module configures authentication for the Flask application using Flask-HTTPAuth.
It provides mechanisms to verify user credentials and handle authentication errors.
"""
from flask_httpauth import HTTPBasicAuth
from flask import g, jsonify
auth = HTTPBasicAuth()
users = {
"admin": {"password": "secret", "role": "admin"},
"user": {"password": "password", "role": "user"}
}
@auth.verify_password
def verify_password(username, password):
"""
Verify the provided username and password.
Parameters:
username (str): The username provided by the user.
password (str): The password provided by the user.
Returns:
bool: True if the username and password are correct, otherwise False.
"""
user = users.get(username)
if user and user['password'] == password:
g.user = user
return username
@auth.error_handler
def auth_error(status):
"""
Handle authentication errors by returning a JSON response.
Parameters:
status (int): HTTP status code to return in the error response.
Returns:
Response: JSON response containing an error message.
"""
return jsonify({"error": "Access denied, invalid credentials or insufficient permissions"}), status