Skip to content

Commit

Permalink
JWT validity now defined in hours
Browse files Browse the repository at this point in the history
  • Loading branch information
kolappannathan committed Mar 1, 2023
1 parent 397f012 commit 07463b6
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 17 deletions.
15 changes: 9 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

## [7.1.0] - 2023-03-02
### Added
- Added interfaces for library classes, helper functions
- New AuthLib class for checking login and generating token
- Added interfaces for library classes, helper functions.
- New AuthLib class for checking login and generating token.

### Changed
- Helper functions now use dependency injection
- Made all non-derived classes sealed for security and performance
- Optimizations to JWT helpers, moved JWT token builder in core project
- Private variables now start with underscore
- Helper functions now use dependency injection.
- Made all non-derived classes sealed for security and performance.
- Optimizations to JWT helpers, moved JWT token builder in core project.
- Private variables now start with underscore.
- JWT config now has Hours valid instead of days.

### Removed
- Base class for operations is removed
Expand Down
2 changes: 1 addition & 1 deletion src/WebApiBolierplate/API/Operations/AuthLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public string GenerateToken(string userId, string userRole = null, string userNa
.AddSecurityKey(_configuration["AppConfig:JWT:Key"])
.AddIssuer(_configuration["AppConfig:JWT:Issuer"])
.AddAudience(_configuration["AppConfig:JWT:Audience"])
.AddExpiry(Convert.ToInt32(_configuration["AppConfig:JWT:DaysValid"]))
.AddExpiry(Convert.ToInt32(_configuration["AppConfig:JWT:HoursValid"]))
.AddRole(userRole)
.AddName(userName)
.AddUserId(userId)
Expand Down
2 changes: 1 addition & 1 deletion src/WebApiBolierplate/API/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"Audience": "https://www.example.com",
"Issuer": "WebApiBoilerplate",
"Key": "This is a JWT secret key",
"DaysValid": 30
"HoursValid": 48
}
}
}
2 changes: 1 addition & 1 deletion src/WebApiBolierplate/API/appsettings.production.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"Audience": "https://www.example.com",
"Issuer": "WebApiBoilerplate",
"Key": "This is a JWT secret key",
"DaysValid": 30
"HoursValid": 48
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public interface IJwtUtils
public IJwtUtils AddSecurityKey(string securityKey);

/// <summary>
/// Adds expiry time in days to JWT token
/// Adds expiry time in hours to JWT token
/// </summary>
public IJwtUtils AddExpiry(int expiryInDays);
public IJwtUtils AddExpiry(int expiryInHours);

#endregion [Adding values]

Expand Down
12 changes: 6 additions & 6 deletions src/WebApiBolierplate/Core.Lib/Utilities/JwtUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public sealed class JwtUtils: IJwtUtils
#region [Declarations]

private SecurityKey _securityKey = null;
private int _expiryInDays = 0;
private int _expiryInHours = 0;
private List<Claim> _claims = new();

#endregion [Declarations]
Expand All @@ -35,7 +35,7 @@ private void EnsureArguments()
{
ArgumentNullException.ThrowIfNull(_securityKey);

if (_expiryInDays == 0)
if (_expiryInHours == 0)
{
throw new ArgumentNullException("Expiry Time");
}
Expand All @@ -55,7 +55,7 @@ private JwtSecurityToken BuildJwtSecurityToken()

return new JwtSecurityToken(
claims: _claims,
expires: DateTime.UtcNow.AddDays(_expiryInDays),
expires: DateTime.UtcNow.AddHours(_expiryInHours),
signingCredentials: new SigningCredentials(_securityKey, SecurityAlgorithms.HmacSha256));
}

Expand All @@ -65,13 +65,13 @@ private JwtSecurityToken BuildJwtSecurityToken()

public IJwtUtils AddSecurityKey(string securityKey)
{
this._securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(securityKey));
_securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(securityKey));
return this;
}

public IJwtUtils AddExpiry(int expiryInDays)
public IJwtUtils AddExpiry(int expiryInHours)
{
this._expiryInDays = expiryInDays;
_expiryInHours = expiryInHours;
return this;
}

Expand Down

0 comments on commit 07463b6

Please sign in to comment.