-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_routes.py
More file actions
271 lines (220 loc) · 8.81 KB
/
auth_routes.py
File metadata and controls
271 lines (220 loc) · 8.81 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
"""
Flask Authentication Routes
===========================
API endpoints for authentication and user management.
"""
from flask import Blueprint, jsonify, request
from auth_core import UserRole
from auth_middleware import AuthMiddleware
from auth_service import (
AuthenticationService,
AuthorizationService,
UserManagementService,
)
auth_bp = Blueprint("auth", __name__, url_prefix="/api/auth")
def init_auth_routes(
auth_service: AuthenticationService,
authz_service: AuthorizationService,
user_mgmt_service: UserManagementService,
auth_middleware: AuthMiddleware,
):
"""Initialize authentication routes with dependencies."""
@auth_bp.route("/login/admin", methods=["POST"])
def login_admin():
"""Admin login endpoint."""
try:
data = request.get_json()
username = data.get("username", "").strip()
password = data.get("password", "")
if not username or not password:
return jsonify({"error": "Username and password required"}), 400
admin = auth_service.authenticate_admin(username, password)
if not admin:
return jsonify({"error": "Invalid credentials"}), 401
ip_address = request.remote_addr
session = auth_service.create_session(admin, ip_address)
response = jsonify(
{
"message": "Login successful",
"session_id": session.session_id,
"user": {
"id": admin.id,
"username": admin.username,
"role": admin.role.value,
"email": admin.email,
},
}
)
response.set_cookie(
"session_id",
session.session_id,
httponly=True,
secure=False,
samesite="Lax",
max_age=86400,
)
return response, 200
except Exception as e:
return jsonify({"error": str(e)}), 500
@auth_bp.route("/login/player", methods=["POST"])
def login_player():
"""Player login endpoint (using name and DOB)."""
try:
data = request.get_json()
name = data.get("name", "").strip()
dob = data.get("date_of_birth", "").strip()
if not name or not dob:
return jsonify({"error": "Name and date of birth required"}), 400
player = auth_service.authenticate_player(name, dob)
if not player:
return jsonify({"error": "Invalid credentials"}), 401
ip_address = request.remote_addr
session = auth_service.create_session(player, ip_address)
response = jsonify(
{
"message": "Login successful",
"session_id": session.session_id,
"user": {
"id": player.id,
"name": player.name,
"player_id": player.player_id,
"role": player.role.value,
"status": player.status.value,
},
}
)
response.set_cookie(
"session_id",
session.session_id,
httponly=True,
secure=False,
samesite="Lax",
max_age=86400,
)
return response, 200
except Exception as e:
return jsonify({"error": str(e)}), 500
@auth_bp.route("/logout", methods=["POST"])
@auth_middleware.require_auth
def logout():
"""Logout endpoint."""
try:
session = auth_middleware.get_current_session()
if session:
auth_service.invalidate_session(session.session_id)
response = jsonify({"message": "Logged out successfully"})
response.set_cookie("session_id", "", expires=0)
return response, 200
except Exception as e:
return jsonify({"error": str(e)}), 500
@auth_bp.route("/register/player", methods=["POST"])
def register_player():
"""Player registration endpoint."""
try:
data = request.get_json()
player_id = data.get("player_id", "").strip()
name = data.get("name", "").strip()
dob = data.get("date_of_birth", "").strip()
if not player_id or not name or not dob:
return (
jsonify({"error": "Player ID, name, and date of birth required"}),
400,
)
player = user_mgmt_service.register_player(player_id, name, dob)
return (
jsonify(
{
"message": "Registration successful",
"user": {
"id": player.id,
"name": player.name,
"player_id": player.player_id,
},
}
),
201,
)
except Exception as e:
return jsonify({"error": str(e)}), 500
@auth_bp.route("/me", methods=["GET"])
@auth_middleware.require_auth
def get_current_user():
"""Get current user information."""
try:
user = auth_middleware.get_current_user()
if not user:
return jsonify({"error": "User not found"}), 404
user_data = {
"id": user.id,
"role": user.role.value,
"status": user.status.value,
"created_at": user.created_at,
"last_login": user.last_login,
}
if user.role == UserRole.ADMIN:
user_data.update({"username": user.username, "email": user.email})
elif user.role == UserRole.PLAYER:
user_data.update(
{
"name": user.name,
"player_id": user.player_id,
"date_of_birth": user.date_of_birth,
}
)
return jsonify(user_data), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
@auth_bp.route("/admin/change-password", methods=["POST"])
@auth_middleware.require_admin
def change_password():
"""Change admin password."""
try:
data = request.get_json()
current_password = data.get("current_password", "")
new_password = data.get("new_password", "")
if not current_password or not new_password:
return jsonify({"error": "Current and new password required"}), 400
user = auth_middleware.get_current_user()
user_mgmt_service.change_admin_password(
user.id, current_password, new_password
)
return jsonify({"message": "Password changed successfully"}), 200
except ValueError as e:
return jsonify({"error": str(e)}), 400
except Exception as e:
return jsonify({"error": str(e)}), 500
@auth_bp.route("/admin/suspend-user/<user_id>", methods=["POST"])
@auth_middleware.require_admin
def suspend_user(user_id):
"""Suspend a user (admin only)."""
try:
data = request.get_json() or {}
reason = data.get("reason", "")
admin = auth_middleware.get_current_user()
user_mgmt_service.suspend_user(user_id, admin.id, reason)
return jsonify({"message": "User suspended successfully"}), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
@auth_bp.route("/admin/shadow-ban-user/<user_id>", methods=["POST"])
@auth_middleware.require_admin
def shadow_ban_user(user_id):
"""Shadow ban a user (admin only)."""
try:
data = request.get_json() or {}
reason = data.get("reason", "")
admin = auth_middleware.get_current_user()
user_mgmt_service.shadow_ban_user(user_id, admin.id, reason)
return jsonify({"message": "User shadow banned successfully"}), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
@auth_bp.route("/admin/reactivate-user/<user_id>", methods=["POST"])
@auth_middleware.require_admin
def reactivate_user(user_id):
"""Reactivate a suspended/banned user (admin only)."""
try:
admin = auth_middleware.get_current_user()
user_mgmt_service.reactivate_user(user_id, admin.id)
return jsonify({"message": "User reactivated successfully"}), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
return auth_bp