Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix jwt update and exception handling #59

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions AnalysisData/AnalysisData/User/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ public IActionResult UploadImage(IFormFile file)
}

[HttpPut("UpdateUser")]
public IActionResult UpdateUser([FromBody] UpdateUserModel updateUserModel)
public async Task<IActionResult> UpdateUser([FromBody] UpdateUserModel updateUserModel)
{
var user = User;
var updatedUser = _userService.UpdateUserInformationByUser(user, updateUserModel);
var updatedUser = await _userService.UpdateUserInformationByUser(user, updateUserModel);
if (updatedUser != null)
{
return Ok(new {massage = "updated successfully"});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace AnalysisData.Exception;

public class AdminExistenceException:System.Exception
public class AdminExistenceException: ServiceException
{
public AdminExistenceException() : base(Resources.AdminExistenceException)
public AdminExistenceException() : base(Resources.AdminExistenceException,StatusCodes.Status403Forbidden)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace AnalysisData.Exception;

public class CategoryResultNotFoundException : System.Exception
public class CategoryResultNotFoundException : ServiceException
{
public CategoryResultNotFoundException() : base(Resources.CategoryResultNotFoundException)
public CategoryResultNotFoundException() : base(Resources.CategoryResultNotFoundException,StatusCodes.Status404NotFound)
{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace AnalysisData.Exception;

public class DuplicateRoleExistException : System.Exception
public class DuplicateRoleExistException : ServiceException
{
public DuplicateRoleExistException() : base(Resources.DuplicateRoleException)
public DuplicateRoleExistException() : base(Resources.DuplicateRoleException,StatusCodes.Status403Forbidden)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace AnalysisData.Exception;

public class DuplicateUserException : System.Exception
public class DuplicateUserException : ServiceException
{
public DuplicateUserException() : base(Resources.DuplicateUserException)
public DuplicateUserException() : base(Resources.DuplicateUserException,StatusCodes.Status401Unauthorized)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace AnalysisData.Exception;

public class EdgeNotFoundException : System.Exception
public class EdgeNotFoundException : ServiceException
{
public EdgeNotFoundException() : base(Resources.EdgeNotFoundException)
public EdgeNotFoundException() : base(Resources.EdgeNotFoundException,StatusCodes.Status404NotFound)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace AnalysisData.Exception;

public class FileExistenceException : System.Exception
public class FileExistenceException : ServiceException
{
public FileExistenceException() : base(Resources.FileExistenceException)
public FileExistenceException() : base(Resources.FileExistenceException,StatusCodes.Status404NotFound)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace AnalysisData.Exception;

public class InvalidEmailFormatException : System.Exception
public class InvalidEmailFormatException : ServiceException
{
public InvalidEmailFormatException() : base(Resources.InvalidEmailFormatException)
public InvalidEmailFormatException() : base(Resources.InvalidEmailFormatException,StatusCodes.Status401Unauthorized)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ namespace AnalysisData.Exception;
using System;


public class InvalidPasswordException : Exception
public class InvalidPasswordException : ServiceException
{
public InvalidPasswordException() : base(Resources.InvalidPasswordException)
public InvalidPasswordException() : base(Resources.InvalidPasswordException,StatusCodes.Status401Unauthorized)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace AnalysisData.Exception;

public class InvalidPasswordFormatException : System.Exception
public class InvalidPasswordFormatException : ServiceException
{
public InvalidPasswordFormatException() : base(Resources.InvalidPasswordFormatException)
public InvalidPasswordFormatException() : base(Resources.InvalidPasswordFormatException, StatusCodes.Status401Unauthorized)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace AnalysisData.Exception;

public class InvalidPhoneNumberFormatException : System.Exception
public class InvalidPhoneNumberFormatException : ServiceException
{
public InvalidPhoneNumberFormatException() : base(Resources.InvalidPhoneNumberFormatException)
public InvalidPhoneNumberFormatException() : base(Resources.InvalidPhoneNumberFormatException,StatusCodes.Status401Unauthorized)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace AnalysisData.Exception;

public class NoFileUploadedException : System.Exception
public class NoFileUploadedException : ServiceException
{
public NoFileUploadedException() : base(Resources.NoFileUploadedException)
public NoFileUploadedException() : base(Resources.NoFileUploadedException,StatusCodes.Status404NotFound)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace AnalysisData.Exception;

public class NodeNotFoundException : System.Exception
public class NodeNotFoundException :ServiceException
{
public NodeNotFoundException() : base(Resources.NodeNotFoundException)
public NodeNotFoundException() : base(Resources.NodeNotFoundException,StatusCodes.Status404NotFound)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace AnalysisData.Exception;

public class PasswordMismatchException : System.Exception
public class PasswordMismatchException : ServiceException
{
public PasswordMismatchException() : base(Resources.PasswordMismatchException )
public PasswordMismatchException() : base(Resources.PasswordMismatchException,StatusCodes.Status401Unauthorized )
{
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace AnalysisData.Exception;

public class RoleNotFoundException : System.Exception
public class RoleNotFoundException : ServiceException
{
public RoleNotFoundException() : base(Resources.RoleNotFoundException)
public RoleNotFoundException() : base(Resources.RoleNotFoundException,StatusCodes.Status401Unauthorized)
{
}
}
11 changes: 11 additions & 0 deletions AnalysisData/AnalysisData/User/Exception/ServiceException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace AnalysisData.Exception;

public class ServiceException : System.Exception
{
public int StatusCode { get; }

public ServiceException(string message, int statusCode) : base(message)
{
StatusCode = statusCode;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace AnalysisData.Exception;

public class TokenNotFoundInCookieException : System.Exception
public class TokenNotFoundInCookieException : ServiceException
{
public TokenNotFoundInCookieException() : base(Resources.TokenNotFoundInCookieException)
public TokenNotFoundInCookieException() : base(Resources.TokenNotFoundInCookieException,StatusCodes.Status404NotFound)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace AnalysisData.Exception;
using System;

public class UserNotFoundException : Exception
public class UserNotFoundException : ServiceException
{
public UserNotFoundException() : base(Resources.UserNotFoundException)
public UserNotFoundException() : base(Resources.UserNotFoundException, StatusCodes.Status404NotFound)
{
}

Expand Down
5 changes: 5 additions & 0 deletions AnalysisData/AnalysisData/User/JwtService/JwtService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,10 @@ public async Task<string> GenerateJwtToken(string userName)

return new JwtSecurityTokenHandler().WriteToken(token);
}
public async Task UpdateUserCookie(string userName, bool rememberMe)
{
var token = await GenerateJwtToken(userName);
_cookieService.UpdateCookie("AuthToken", token, rememberMe);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ namespace AnalysisData.JwtService.abstractions;
public interface IJwtService
{
Task<string> GenerateJwtToken(string userName);
Task UpdateUserCookie(string userName, bool rememberMe);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public async Task InvokeAsync(HttpContext httpContext)
{
await _next(httpContext);
}
catch (ServiceException ex)
{
await HandleExceptionAsync(httpContext, ex, ex.StatusCode);
}
catch (AggregateException aggEx)
{
foreach (var ex in aggEx.InnerExceptions)
Expand All @@ -28,21 +32,19 @@ public async Task InvokeAsync(HttpContext httpContext)
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status404NotFound);
return;
}

if (ex is RoleNotFoundException)
{
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status404NotFound);
return;
}

if (ex is PasswordMismatchException)
{
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status401Unauthorized);
}
}
}
catch (UserNotFoundException ex)
{
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status404NotFound);
}
catch (UnauthorizedAccessException ex)
{
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status401Unauthorized);
Expand All @@ -67,64 +69,8 @@ public async Task InvokeAsync(HttpContext httpContext)
{
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status401Unauthorized);
}
catch (InvalidPasswordException ex)
{
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status401Unauthorized);
}
catch (DuplicateUserException ex)
{
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status401Unauthorized);
}
catch (PasswordMismatchException ex)
{
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status401Unauthorized);
}
catch (InvalidEmailFormatException ex)
{
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status401Unauthorized);
}
catch (InvalidPasswordFormatException ex)
{
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status401Unauthorized);
}
catch (InvalidPhoneNumberFormatException ex)
{
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status401Unauthorized);
}
catch (RoleNotFoundException ex)
{
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status401Unauthorized);
}
catch (AdminExistenceException ex)
{
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status403Forbidden);
}
catch (CategoryResultNotFoundException ex)
{
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status404NotFound);
}
catch (NodeNotFoundException ex)
{
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status404NotFound);
}
catch (EdgeNotFoundException ex)
{
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status404NotFound);
}
}

// private static List<Type> GetCustomExceptions()
// {
// var baseType = typeof(System.Exception);
// var exceptionTypes = AppDomain.CurrentDomain.GetAssemblies()
// .SelectMany(assembly => assembly.GetTypes())
// .Where(type => type.IsSubclassOf(baseType) && !type.IsAbstract)
// .ToList();
//
// return exceptionTypes;
// }


private Task HandleExceptionAsync(HttpContext context, System.Exception exception, int _statusCode)
{
context.Response.ContentType = "application/json";
Expand Down
8 changes: 7 additions & 1 deletion AnalysisData/AnalysisData/User/Services/AdminService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Security.Cryptography;
using System.Text;
using AnalysisData.Exception;
using AnalysisData.JwtService.abstractions;
using AnalysisData.Repository.RoleRepository.Abstraction;
using AnalysisData.Repository.UserRepository.Abstraction;
using AnalysisData.Services.Abstraction;
Expand All @@ -18,13 +19,16 @@ public class AdminService : IAdminService
private readonly IUserRepository _userRepository;
private readonly IRegexService _regexService;
private readonly IRoleRepository _roleRepository;
private readonly IJwtService _jwtService;


public AdminService(IUserRepository userRepository, IRegexService regexService, IRoleRepository roleRepository)

public AdminService(IUserRepository userRepository, IRegexService regexService, IRoleRepository roleRepository,IJwtService jwtService)
{
_userRepository = userRepository;
_regexService = regexService;
_roleRepository = roleRepository;
_jwtService = jwtService;
}

public async Task Register(UserRegisterModel userRegisterModel)
Expand Down Expand Up @@ -99,6 +103,7 @@ public async Task UpdateUserInformationByAdmin(Guid id, UpdateAdminModel updateA
}

SetUpdatedInformation(user, updateAdminModel);
_jwtService.UpdateUserCookie(user.Username, false);
}

private void SetUpdatedInformation(User user, UpdateAdminModel updateAdminModel)
Expand All @@ -110,6 +115,7 @@ private void SetUpdatedInformation(User user, UpdateAdminModel updateAdminModel)
user.Username = updateAdminModel.Username;
user.Role.RoleName = updateAdminModel.RoleName;
_userRepository.UpdateUser(user.Id, user);

}

public async Task<bool> DeleteUser(Guid id)
Expand Down
1 change: 1 addition & 0 deletions AnalysisData/AnalysisData/User/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public async Task<bool> UpdateUserInformationByUser(ClaimsPrincipal userClaim, U
_regexService.EmailCheck(updateUserModel.Email);
_regexService.PhoneNumberCheck(updateUserModel.PhoneNumber);
await ReplaceUserDetailsAsync(user, updateUserModel);
await _jwtService.UpdateUserCookie(userName, false);
return true;
}

Expand Down
Loading