-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Register): register management created (#7)
* fix(User):add exception for login * feat(UserRegister):solve confilict * feat(Register User):add JWT Validation * feat(User):delete extra line in login method * feat(user):add jwt middleware * fix(user): jwt middleware * fix(user): login * feat(User): Add register. * fix(user): login * fix(User) : change structure of database * fix(User): inner Exception * fix(User): jwt problem and permission api * fix(User): return login api * fix(User): fix retutn of login api * fix(User): fix some conflicts * fix(User): change some method of testProject --------- Co-authored-by: AmirReza <[email protected]> Co-authored-by: K-Kabiri <[email protected]>
- Loading branch information
1 parent
0e022b1
commit 97b3e8d
Showing
39 changed files
with
625 additions
and
396 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
AnalysisData/AnalysisData/CookieService/abstractions/ICookieService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
AnalysisData/AnalysisData/Exception/DuplicateUserException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace AnalysisData.Exception; | ||
|
||
public class DuplicateUserException : System.Exception | ||
{ | ||
public DuplicateUserException() : base(Resources.DuplicateUserException) | ||
{ | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
AnalysisData/AnalysisData/Exception/InvalidPasswordException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace AnalysisData.Exception; | ||
using System; | ||
|
||
|
||
public class InvalidPasswordException : Exception | ||
{ | ||
public InvalidPasswordException() : base(Resources.InvalidPasswordException) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace AnalysisData.Exception; | ||
using System; | ||
|
||
public class NotFoundUserException : Exception | ||
{ | ||
public NotFoundUserException() : base(Resources.NotFoundUserException) | ||
{ | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
AnalysisData/AnalysisData/Exception/PasswordMismatchException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace AnalysisData.Exception; | ||
|
||
public class PasswordMismatchException : System.Exception | ||
{ | ||
public PasswordMismatchException() : base(Resources.PasswordMismatchException ) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace AnalysisData.Exception; | ||
|
||
public class RoleNotFoundException : System.Exception | ||
{ | ||
public RoleNotFoundException() : base(Resources.RoleNotFoundException) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
AnalysisData/AnalysisData/JwtService/abstractions/IJwtService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
AnalysisData/AnalysisData/MiddleWare/ExceptionHandlingMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using Microsoft.IdentityModel.Tokens; | ||
|
||
namespace AnalysisData.Exception; | ||
|
||
public class ExceptionHandlingMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
|
||
public ExceptionHandlingMiddleware(RequestDelegate next) | ||
{ | ||
_next = next; | ||
} | ||
|
||
public async Task InvokeAsync(HttpContext httpContext) | ||
{ | ||
try | ||
{ | ||
await _next(httpContext); | ||
} | ||
catch (NotFoundUserException ex) | ||
{ | ||
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status404NotFound); | ||
} | ||
catch (UnauthorizedAccessException ex) | ||
{ | ||
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status401Unauthorized); | ||
} | ||
catch (ValidationException ex) | ||
{ | ||
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status400BadRequest); | ||
} | ||
catch (TimeoutException ex) | ||
{ | ||
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status408RequestTimeout); | ||
} | ||
catch (SecurityTokenExpiredException ex) | ||
{ | ||
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status401Unauthorized); | ||
} | ||
catch (SecurityTokenInvalidSignatureException ex) | ||
{ | ||
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status401Unauthorized); | ||
} | ||
catch (SecurityTokenInvalidAudienceException ex) | ||
{ | ||
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); | ||
} | ||
|
||
{} | ||
|
||
} | ||
|
||
private Task HandleExceptionAsync(HttpContext context, System.Exception exception, int statusCode) | ||
{ | ||
context.Response.ContentType = "application/json"; | ||
context.Response.StatusCode = statusCode; | ||
|
||
var response = new | ||
{ | ||
statusCode = statusCode, | ||
message = exception.Message, | ||
}; | ||
|
||
return context.Response.WriteAsync(System.Text.Json.JsonSerializer.Serialize(response)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Text; | ||
using Microsoft.IdentityModel.Tokens; | ||
|
||
namespace AnalysisData; | ||
|
||
public class JwtMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
private readonly string? _jwtSecret; | ||
|
||
public JwtMiddleware(RequestDelegate next,IConfiguration configuration) | ||
{ | ||
_next = next; | ||
_jwtSecret = configuration["Jwt:Key"]; | ||
} | ||
|
||
|
||
public async Task InvokeAsync(HttpContext context) | ||
{ | ||
if (context.Request.Path.StartsWithSegments("/api/User/login") || context.Request.Path.StartsWithSegments("/api/User/register")) | ||
{ | ||
await _next(context); | ||
return; | ||
} | ||
var token = context.Request.Cookies["AuthToken"]; | ||
if (token != null) | ||
{ | ||
try | ||
{ | ||
AttachUserToContext(context, token); | ||
} | ||
catch | ||
{ | ||
context.Response.StatusCode = StatusCodes.Status401Unauthorized; | ||
await context.Response.WriteAsync("Invalid token."); | ||
return; | ||
} | ||
} | ||
|
||
await _next(context); | ||
} | ||
|
||
private void AttachUserToContext(HttpContext context, string token) | ||
{ | ||
var tokenHandler = new JwtSecurityTokenHandler(); | ||
var key = Encoding.ASCII.GetBytes(_jwtSecret); | ||
|
||
var validationParameters = new TokenValidationParameters | ||
{ | ||
ValidateIssuerSigningKey = true, | ||
IssuerSigningKey = new SymmetricSecurityKey(key), | ||
ValidateIssuer = false, | ||
ValidateAudience = false, | ||
ValidateLifetime = true, | ||
ClockSkew = TimeSpan.Zero | ||
}; | ||
|
||
var principal = tokenHandler.ValidateToken(token, validationParameters, out _); | ||
context.User = principal; | ||
} | ||
} |
Oops, something went wrong.