-
Notifications
You must be signed in to change notification settings - Fork 0
Implement JWT authentication improvements and enhance error handling #1
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ffe21f2
Implement JWT authentication improvements and enhance error handling
jvz-devx 0b1a39c
Enhance JWT authentication and project configuration
jvz-devx 080e077
Refactor JWT options and token generator for improved validation and …
jvz-devx a04546e
Refactor JWT key handling and validation logic
jvz-devx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
60 changes: 60 additions & 0 deletions
60
MySvelteApp.Server/Infrastructure/Authentication/JwtOptions.cs
This file contains hidden or 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,60 @@ | ||
| using System.ComponentModel.DataAnnotations; | ||
| using System.Text; | ||
|
|
||
| namespace MySvelteApp.Server.Infrastructure.Authentication; | ||
|
|
||
| /// <summary> | ||
| /// Public validator class for JwtOptions to support DataAnnotations validation. | ||
| /// </summary> | ||
| public static class JwtOptionsValidator | ||
| { | ||
| /// <summary> | ||
| /// Validates that a string value is not null, empty, or whitespace-only. | ||
| /// </summary> | ||
| public static ValidationResult ValidateNotWhitespace(object? value, ValidationContext context) | ||
| { | ||
| var str = value as string; | ||
| if (string.IsNullOrWhiteSpace(str)) | ||
| { | ||
| var displayName = context.DisplayName ?? context.MemberName ?? "Value"; | ||
| return new ValidationResult($"{displayName} cannot be blank or whitespace-only."); | ||
| } | ||
| return ValidationResult.Success!; | ||
| } | ||
|
|
||
| public static ValidationResult ValidateKeyStrength(object? value, ValidationContext context) | ||
| { | ||
| var s = value as string ?? string.Empty; | ||
| byte[] bytes = DeriveKeyBytes(s); | ||
| return bytes.Length >= 32 | ||
| ? ValidationResult.Success! | ||
| : new ValidationResult("Jwt:Key must be at least 32 bytes (256-bit) after decoding."); | ||
| } | ||
|
|
||
| private static byte[] DeriveKeyBytes(string key) => | ||
| key.StartsWith("base64:", StringComparison.Ordinal) | ||
| ? Convert.FromBase64String(key["base64:".Length..]) | ||
| : Encoding.UTF8.GetBytes(key); | ||
| } | ||
|
|
||
| public sealed class JwtOptions | ||
| { | ||
| [Required] | ||
| [MinLength(32, ErrorMessage = "Jwt:Key must be at least 32 characters long.")] | ||
| [CustomValidation(typeof(JwtOptionsValidator), nameof(JwtOptionsValidator.ValidateKeyStrength))] | ||
| public string Key { get; set; } = string.Empty; | ||
|
|
||
| [Required] | ||
| [CustomValidation(typeof(JwtOptionsValidator), nameof(JwtOptionsValidator.ValidateNotWhitespace))] | ||
| public string Issuer { get; set; } = string.Empty; | ||
|
|
||
| [Required] | ||
| [CustomValidation(typeof(JwtOptionsValidator), nameof(JwtOptionsValidator.ValidateNotWhitespace))] | ||
| public string Audience { get; set; } = string.Empty; | ||
|
|
||
| /// <summary> | ||
| /// Lifetime of the access token in hours. | ||
| /// </summary> | ||
| [Range(1, 168, ErrorMessage = "Jwt:AccessTokenLifetimeHours must be between 1 and 168 hours.")] | ||
| public int AccessTokenLifetimeHours { get; set; } = 24; | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicate origin 'http://localhost:5173' was removed, but this may indicate a configuration issue. Consider using a configuration-based approach for CORS origins to avoid hardcoded values and potential duplicates.