1
- from flask import Blueprint , request , jsonify , session
2
- import jwt
3
- import datetime
1
+ from flask import Blueprint , request , jsonify
4
2
from config .auth_config import AuthMethod , AuthConfig
5
- from services .auth_service import generate_jwt_token , is_username_taken , add_user , signup_user , login_user , logout_user , blacklist_token , validate_refresh_token , refresh_tokens
3
+ from services .auth_service import (
4
+ signup_user ,
5
+ validate_refresh_token ,
6
+ generate_jwt_token ,
7
+ refresh_tokens ,
8
+ login_jwt ,
9
+ login_session ,
10
+ logout_jwt ,
11
+ logout_session
12
+ )
6
13
7
- auth_bp = Blueprint ("auth" , __name__ )
8
- auth_config = None
14
+ # --- Configuration ---
15
+ auth_bp = Blueprint ("auth" , __name__ ) # Flask blueprint for auth routes
16
+ auth_config = None # Global configuration object set during initialization
9
17
10
18
def init_auth_routes (config : AuthConfig ):
11
19
global auth_config
12
20
auth_config = config
13
21
22
+ # --- Authentication Routes ---
14
23
@auth_bp .route ("/signup" , methods = ["POST" ])
15
24
def signup ():
25
+ """
26
+ Register a new user
27
+ Expects JSON: {"username": "user", "password": "pass"}
28
+ """
16
29
if auth_config .auth_method == AuthMethod .API_KEY :
17
30
return jsonify ({"error" : "Signup not available with API key authentication" }), 400
18
31
@@ -21,14 +34,60 @@ def signup():
21
34
22
35
@auth_bp .route ("/login" , methods = ["POST" ])
23
36
def login ():
37
+ """
38
+ Authenticate user and return tokens (JWT) or create session
39
+ Expects JSON: {"username": "user", "password": "pass"}
40
+ """
24
41
if auth_config .auth_method == AuthMethod .API_KEY :
25
42
return jsonify ({"error" : "Login not available with API key authentication" }), 400
26
43
27
44
data = request .get_json ()
28
- return login_user (data )
45
+ if not data or "username" not in data or "password" not in data :
46
+ return jsonify ({"error" : "Username and password are required" }), 400
47
+
48
+ username = data ["username" ]
49
+ password = data ["password" ]
50
+
51
+ if auth_config .auth_method == AuthMethod .JWT :
52
+ return login_jwt (username , password )
53
+
54
+ return login_session (username , password )
55
+
56
+ @auth_bp .route ("/logout" , methods = ["POST" ])
57
+ def logout ():
58
+ """
59
+ End user session or invalidate JWT tokens
60
+ For JWT: Requires Authorization header with Bearer token and refresh_token in JSON body
61
+ For Session: No additional requirements
62
+ """
63
+ if auth_config .auth_method == AuthMethod .API_KEY :
64
+ return jsonify ({"error" : "Logout not available with API key authentication" }), 400
65
+
66
+ if auth_config .auth_method == AuthMethod .JWT :
67
+ auth_header = request .headers .get ('Authorization' )
68
+ if not auth_header or not auth_header .startswith ('Bearer ' ):
69
+ return jsonify ({"error" : "Access token is required in Authorization header" }), 401
70
+ access_token = auth_header .split (' ' )[1 ]
71
+
72
+ if not request .is_json :
73
+ return jsonify ({"error" : "Request must be JSON" }), 415
74
+
75
+ data = request .get_json ()
76
+ refresh_token = data .get ("refresh_token" )
77
+ if not refresh_token :
78
+ return jsonify ({"error" : "Refresh token is required in request body" }), 400
79
+
80
+ return logout_jwt (access_token , refresh_token )
81
+
82
+ return logout_session ()
29
83
30
84
@auth_bp .route ("/refresh" , methods = ["POST" ])
31
85
def refresh_token ():
86
+ """
87
+ Get new access token using refresh token
88
+ Expects JSON: {"refresh_token": "token"}
89
+ Returns: New access token and refresh token pair
90
+ """
32
91
data = request .get_json ()
33
92
refresh_token = data .get ("refresh_token" )
34
93
username = validate_refresh_token (refresh_token )
@@ -37,36 +96,10 @@ def refresh_token():
37
96
38
97
access_token , new_refresh_token = generate_jwt_token (username )
39
98
40
- # Remove old refresh token and store new one
41
99
if refresh_token in refresh_tokens :
42
100
del refresh_tokens [refresh_token ]
43
101
44
102
return jsonify ({
45
103
"access_token" : access_token ,
46
104
"refresh_token" : new_refresh_token
47
- }), 200
48
-
49
- @auth_bp .route ("/logout" , methods = ["POST" ])
50
- def logout ():
51
- if auth_config .auth_method == AuthMethod .API_KEY :
52
- return jsonify ({"error" : "Logout not available with API key authentication" }), 400
53
-
54
- elif auth_config .auth_method == AuthMethod .JWT :
55
- auth_header = request .headers .get ('Authorization' )
56
- if auth_header and auth_header .startswith ('Bearer ' ):
57
- token = auth_header .split (' ' )[1 ]
58
- blacklist_token (token )
59
-
60
- # Invalidate refresh token
61
- data = request .get_json ()
62
- refresh_token = data .get ("refresh_token" )
63
- if refresh_token in refresh_tokens :
64
- del refresh_tokens [refresh_token ]
65
-
66
- return jsonify ({"message" : "Logout successful" })
67
-
68
- elif auth_config .auth_method == AuthMethod .SESSION :
69
- session .clear ()
70
- return jsonify ({"message" : "Logout successful" })
71
-
72
- return jsonify ({"error" : "Invalid authentication method" }), 500
105
+ }), 200
0 commit comments