forked from kabdelrazek-do/SecurityAndAuthentication
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthorizationService.cs
More file actions
507 lines (466 loc) · 20 KB
/
AuthorizationService.cs
File metadata and controls
507 lines (466 loc) · 20 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SafeVault.Data;
using SafeVault.Services;
namespace SafeVault.Services
{
/// <summary>
/// Role-based authorization service (RBAC)
/// </summary>
public class AuthorizationService
{
private readonly SecureDatabaseManager _dbManager;
private readonly SessionManager _sessionManager;
private readonly AuditLogger _auditLogger;
public AuthorizationService(SecureDatabaseManager dbManager, SessionManager sessionManager, AuditLogger auditLogger)
{
_dbManager = dbManager ?? throw new ArgumentNullException(nameof(dbManager));
_sessionManager = sessionManager ?? throw new ArgumentNullException(nameof(sessionManager));
_auditLogger = auditLogger ?? throw new ArgumentNullException(nameof(auditLogger));
}
/// <summary>
/// Checks if a user has a specific role
/// </summary>
/// <param name="userId">User ID</param>
/// <param name="roleName">Role name</param>
/// <returns>True if user has the role</returns>
public async Task<bool> UserHasRoleAsync(int userId, string roleName)
{
try
{
if (userId <= 0 || string.IsNullOrWhiteSpace(roleName))
return false;
var userRoles = await _dbManager.GetUserRolesAsync(userId);
return userRoles.Any(role => role.Equals(roleName, StringComparison.OrdinalIgnoreCase));
}
catch
{
return false;
}
}
/// <summary>
/// Checks if a user has any of the specified roles
/// </summary>
/// <param name="userId">User ID</param>
/// <param name="roleNames">Role names</param>
/// <returns>True if user has any of the roles</returns>
public async Task<bool> UserHasAnyRoleAsync(int userId, params string[] roleNames)
{
try
{
if (userId <= 0 || roleNames == null || roleNames.Length == 0)
return false;
var userRoles = await _dbManager.GetUserRolesAsync(userId);
return roleNames.Any(roleName => userRoles.Any(role => role.Equals(roleName, StringComparison.OrdinalIgnoreCase)));
}
catch
{
return false;
}
}
/// <summary>
/// Checks if a user has all of the specified roles
/// </summary>
/// <param name="userId">User ID</param>
/// <param name="roleNames">Role names</param>
/// <returns>True if user has all of the roles</returns>
public async Task<bool> UserHasAllRolesAsync(int userId, params string[] roleNames)
{
try
{
if (userId <= 0 || roleNames == null || roleNames.Length == 0)
return false;
var userRoles = await _dbManager.GetUserRolesAsync(userId);
return roleNames.All(roleName => userRoles.Any(role => role.Equals(roleName, StringComparison.OrdinalIgnoreCase)));
}
catch
{
return false;
}
}
/// <summary>
/// Checks if a user can perform a specific action
/// </summary>
/// <param name="userId">User ID</param>
/// <param name="action">Action name</param>
/// <param name="resource">Resource name (optional)</param>
/// <returns>True if user can perform the action</returns>
public async Task<bool> UserCanPerformActionAsync(int userId, string action, string resource = null)
{
try
{
if (userId <= 0 || string.IsNullOrWhiteSpace(action))
return false;
var userRoles = await _dbManager.GetUserRolesAsync(userId);
var permissions = await _dbManager.GetRolePermissionsAsync(userRoles.ToArray());
return permissions.Any(permission =>
permission.Action.Equals(action, StringComparison.OrdinalIgnoreCase) &&
(string.IsNullOrWhiteSpace(resource) || permission.Resource == null ||
permission.Resource.Equals(resource, StringComparison.OrdinalIgnoreCase)));
}
catch
{
return false;
}
}
/// <summary>
/// Authorizes a user for a specific action and logs the attempt
/// </summary>
/// <param name="userId">User ID</param>
/// <param name="action">Action name</param>
/// <param name="resource">Resource name (optional)</param>
/// <param name="ipAddress">Client IP address</param>
/// <param name="userAgent">Client user agent</param>
/// <returns>Authorization result</returns>
public async Task<AuthorizationResult> AuthorizeUserAsync(int userId, string action, string resource, string ipAddress, string userAgent)
{
try
{
if (userId <= 0 || string.IsNullOrWhiteSpace(action))
{
await _auditLogger.LogAsync(userId, "AUTHORIZATION_FAILED", $"Invalid parameters: userId={userId}, action={action}", ipAddress, userAgent);
return new AuthorizationResult
{
IsAuthorized = false,
ErrorMessage = "Invalid authorization parameters"
};
}
bool isAuthorized = await UserCanPerformActionAsync(userId, action, resource);
if (isAuthorized)
{
await _auditLogger.LogAsync(userId, "AUTHORIZATION_SUCCESS", $"Authorized for action: {action}, resource: {resource}", ipAddress, userAgent);
return new AuthorizationResult
{
IsAuthorized = true,
Message = "Access granted"
};
}
else
{
await _auditLogger.LogAsync(userId, "AUTHORIZATION_DENIED", $"Denied access to action: {action}, resource: {resource}", ipAddress, userAgent);
return new AuthorizationResult
{
IsAuthorized = false,
ErrorMessage = "Access denied"
};
}
}
catch (Exception ex)
{
await _auditLogger.LogAsync(userId, "AUTHORIZATION_ERROR", $"Authorization error: {ex.Message}", ipAddress, userAgent);
return new AuthorizationResult
{
IsAuthorized = false,
ErrorMessage = "An error occurred during authorization"
};
}
}
/// <summary>
/// Validates a session and checks authorization
/// </summary>
/// <param name="token">JWT-like token</param>
/// <param name="action">Required action</param>
/// <param name="resource">Resource name (optional)</param>
/// <param name="ipAddress">Client IP address</param>
/// <param name="userAgent">Client user agent</param>
/// <returns>Authorization result with user information</returns>
public async Task<SessionAuthorizationResult> AuthorizeSessionAsync(string token, string action, string resource, string ipAddress, string userAgent)
{
try
{
if (string.IsNullOrWhiteSpace(token))
{
await _auditLogger.LogAsync(null, "AUTHORIZATION_FAILED", "Empty token", ipAddress, userAgent);
return new SessionAuthorizationResult
{
IsAuthorized = false,
ErrorMessage = "Token is required"
};
}
// Validate session
var session = await _sessionManager.ValidateTokenAsync(token);
if (session == null)
{
await _auditLogger.LogAsync(null, "AUTHORIZATION_FAILED", "Invalid or expired token", ipAddress, userAgent);
return new SessionAuthorizationResult
{
IsAuthorized = false,
ErrorMessage = "Invalid or expired session"
};
}
// Check authorization
var authResult = await AuthorizeUserAsync(session.UserId, action, resource, ipAddress, userAgent);
if (authResult.IsAuthorized)
{
// Get user information
var user = await _dbManager.GetUserByIdAsync(session.UserId);
var userRoles = await _dbManager.GetUserRolesAsync(session.UserId);
return new SessionAuthorizationResult
{
IsAuthorized = true,
UserId = session.UserId,
Username = user?.Username,
Email = user?.Email,
Roles = userRoles.ToList(),
SessionId = session.SessionId,
Message = "Access granted"
};
}
else
{
return new SessionAuthorizationResult
{
IsAuthorized = false,
UserId = session.UserId,
ErrorMessage = authResult.ErrorMessage
};
}
}
catch (Exception ex)
{
await _auditLogger.LogAsync(null, "AUTHORIZATION_ERROR", $"Session authorization error: {ex.Message}", ipAddress, userAgent);
return new SessionAuthorizationResult
{
IsAuthorized = false,
ErrorMessage = "An error occurred during authorization"
};
}
}
/// <summary>
/// Assigns a role to a user
/// </summary>
/// <param name="userId">User ID</param>
/// <param name="roleName">Role name</param>
/// <param name="assignedBy">User ID who assigned the role</param>
/// <param name="ipAddress">Client IP address</param>
/// <param name="userAgent">Client user agent</param>
/// <returns>Assignment result</returns>
public async Task<RoleAssignmentResult> AssignRoleToUserAsync(int userId, string roleName, int assignedBy, string ipAddress, string userAgent)
{
try
{
if (userId <= 0 || string.IsNullOrWhiteSpace(roleName) || assignedBy <= 0)
{
await _auditLogger.LogAsync(assignedBy, "ROLE_ASSIGNMENT_FAILED", $"Invalid parameters: userId={userId}, role={roleName}", ipAddress, userAgent);
return new RoleAssignmentResult
{
IsSuccess = false,
ErrorMessage = "Invalid parameters"
};
}
// Check if the assigner has permission to assign roles
bool canAssignRoles = await UserCanPerformActionAsync(assignedBy, "AssignRole");
if (!canAssignRoles)
{
await _auditLogger.LogAsync(assignedBy, "ROLE_ASSIGNMENT_DENIED", $"User {assignedBy} attempted to assign role {roleName} to user {userId}", ipAddress, userAgent);
return new RoleAssignmentResult
{
IsSuccess = false,
ErrorMessage = "Insufficient permissions to assign roles"
};
}
// Check if role exists
bool roleExists = await _dbManager.RoleExistsAsync(roleName);
if (!roleExists)
{
await _auditLogger.LogAsync(assignedBy, "ROLE_ASSIGNMENT_FAILED", $"Role does not exist: {roleName}", ipAddress, userAgent);
return new RoleAssignmentResult
{
IsSuccess = false,
ErrorMessage = "Role does not exist"
};
}
// Check if user exists
var user = await _dbManager.GetUserByIdAsync(userId);
if (user == null)
{
await _auditLogger.LogAsync(assignedBy, "ROLE_ASSIGNMENT_FAILED", $"User does not exist: {userId}", ipAddress, userAgent);
return new RoleAssignmentResult
{
IsSuccess = false,
ErrorMessage = "User does not exist"
};
}
// Check if user already has the role
bool hasRole = await UserHasRoleAsync(userId, roleName);
if (hasRole)
{
await _auditLogger.LogAsync(assignedBy, "ROLE_ASSIGNMENT_FAILED", $"User {userId} already has role {roleName}", ipAddress, userAgent);
return new RoleAssignmentResult
{
IsSuccess = false,
ErrorMessage = "User already has this role"
};
}
// Assign the role
await _dbManager.AssignRoleToUserAsync(userId, roleName);
await _auditLogger.LogAsync(assignedBy, "ROLE_ASSIGNMENT_SUCCESS", $"Assigned role {roleName} to user {userId}", ipAddress, userAgent);
return new RoleAssignmentResult
{
IsSuccess = true,
Message = $"Role {roleName} assigned successfully"
};
}
catch (Exception ex)
{
await _auditLogger.LogAsync(assignedBy, "ROLE_ASSIGNMENT_ERROR", $"Role assignment error: {ex.Message}", ipAddress, userAgent);
return new RoleAssignmentResult
{
IsSuccess = false,
ErrorMessage = "An error occurred while assigning the role"
};
}
}
/// <summary>
/// Removes a role from a user
/// </summary>
/// <param name="userId">User ID</param>
/// <param name="roleName">Role name</param>
/// <param name="removedBy">User ID who removed the role</param>
/// <param name="ipAddress">Client IP address</param>
/// <param name="userAgent">Client user agent</param>
/// <returns>Removal result</returns>
public async Task<RoleAssignmentResult> RemoveRoleFromUserAsync(int userId, string roleName, int removedBy, string ipAddress, string userAgent)
{
try
{
if (userId <= 0 || string.IsNullOrWhiteSpace(roleName) || removedBy <= 0)
{
await _auditLogger.LogAsync(removedBy, "ROLE_REMOVAL_FAILED", $"Invalid parameters: userId={userId}, role={roleName}", ipAddress, userAgent);
return new RoleAssignmentResult
{
IsSuccess = false,
ErrorMessage = "Invalid parameters"
};
}
// Check if the remover has permission to remove roles
bool canRemoveRoles = await UserCanPerformActionAsync(removedBy, "RemoveRole");
if (!canRemoveRoles)
{
await _auditLogger.LogAsync(removedBy, "ROLE_REMOVAL_DENIED", $"User {removedBy} attempted to remove role {roleName} from user {userId}", ipAddress, userAgent);
return new RoleAssignmentResult
{
IsSuccess = false,
ErrorMessage = "Insufficient permissions to remove roles"
};
}
// Check if user has the role
bool hasRole = await UserHasRoleAsync(userId, roleName);
if (!hasRole)
{
await _auditLogger.LogAsync(removedBy, "ROLE_REMOVAL_FAILED", $"User {userId} does not have role {roleName}", ipAddress, userAgent);
return new RoleAssignmentResult
{
IsSuccess = false,
ErrorMessage = "User does not have this role"
};
}
// Remove the role
await _dbManager.RemoveRoleFromUserAsync(userId, roleName);
await _auditLogger.LogAsync(removedBy, "ROLE_REMOVAL_SUCCESS", $"Removed role {roleName} from user {userId}", ipAddress, userAgent);
return new RoleAssignmentResult
{
IsSuccess = true,
Message = $"Role {roleName} removed successfully"
};
}
catch (Exception ex)
{
await _auditLogger.LogAsync(removedBy, "ROLE_REMOVAL_ERROR", $"Role removal error: {ex.Message}", ipAddress, userAgent);
return new RoleAssignmentResult
{
IsSuccess = false,
ErrorMessage = "An error occurred while removing the role"
};
}
}
/// <summary>
/// Gets all available roles
/// </summary>
/// <returns>List of roles</returns>
public async Task<List<Role>> GetAllRolesAsync()
{
try
{
return await _dbManager.GetAllRolesAsync();
}
catch
{
return new List<Role>();
}
}
/// <summary>
/// Gets all permissions for a role
/// </summary>
/// <param name="roleName">Role name</param>
/// <returns>List of permissions</returns>
public async Task<List<Permission>> GetRolePermissionsAsync(string roleName)
{
try
{
if (string.IsNullOrWhiteSpace(roleName))
return new List<Permission>();
return await _dbManager.GetRolePermissionsAsync(new[] { roleName });
}
catch
{
return new List<Permission>();
}
}
}
/// <summary>
/// Authorization result model
/// </summary>
public class AuthorizationResult
{
public bool IsAuthorized { get; set; }
public string Message { get; set; }
public string ErrorMessage { get; set; }
}
/// <summary>
/// Session authorization result model
/// </summary>
public class SessionAuthorizationResult
{
public bool IsAuthorized { get; set; }
public int UserId { get; set; }
public string Username { get; set; }
public string Email { get; set; }
public List<string> Roles { get; set; }
public string SessionId { get; set; }
public string Message { get; set; }
public string ErrorMessage { get; set; }
}
/// <summary>
/// Role assignment result model
/// </summary>
public class RoleAssignmentResult
{
public bool IsSuccess { get; set; }
public string Message { get; set; }
public string ErrorMessage { get; set; }
}
/// <summary>
/// Role model
/// </summary>
public class Role
{
public int RoleId { get; set; }
public string RoleName { get; set; }
public string Description { get; set; }
public bool IsActive { get; set; }
public DateTime CreatedAt { get; set; }
}
/// <summary>
/// Permission model
/// </summary>
public class Permission
{
public int PermissionId { get; set; }
public string Action { get; set; }
public string Resource { get; set; }
public string Description { get; set; }
}
}