-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(security): Now it uses NetDevPack.Security.Jwt
feat(refresh-token): Added RefreshToken support
- Loading branch information
1 parent
169d221
commit 9427db1
Showing
27 changed files
with
800 additions
and
363 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 was deleted.
Oops, something went wrong.
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,19 @@ | ||
using System.Threading.Tasks; | ||
using NetDevPack.Identity.Jwt; | ||
using NetDevPack.Identity.Jwt.Model; | ||
|
||
namespace NetDevPack.Identity.Interfaces; | ||
|
||
public interface IJwtBuilder | ||
{ | ||
IJwtBuilder WithEmail(string email); | ||
IJwtBuilder WithUsername(string username); | ||
IJwtBuilder WithUserId(string id); | ||
IJwtBuilder WithJwtClaims(); | ||
IJwtBuilder WithUserClaims(); | ||
IJwtBuilder WithUserRoles(); | ||
IJwtBuilder WithRefreshToken(); | ||
Task<string> BuildToken(); | ||
Task<UserResponse> BuildUserResponse(); | ||
Task<RefreshTokenValidation> ValidateRefreshToken(string refreshToken); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,33 @@ | ||
using System.Collections.Generic; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace NetDevPack.Identity.Jwt | ||
{ | ||
public class AppJwtSettings | ||
{ | ||
[Obsolete("For better security use IJwtBuilder and set null for this field")] | ||
public string SecretKey { get; set; } | ||
public int Expiration { get; set; } | ||
public string Issuer { get; set; } | ||
public IList<string> Issuers { get; set; } | ||
public string Audience { get; set; } | ||
public IList<string> Audiences { get; set; } | ||
|
||
/// <summary> | ||
/// Hours | ||
/// </summary> | ||
public int Expiration { get; set; } = 1; | ||
/// <summary> | ||
/// Days | ||
/// </summary> | ||
public int RefreshTokenExpiration { get; set; } = 30; | ||
public string Issuer { get; set; } = "NetDevPack.Identity"; | ||
public string Audience { get; set; } = "Api"; | ||
|
||
/// <summary> | ||
/// One Time => Only the lastest refresh token is valid. Ignore olders refresh token. | ||
/// Better security and best suit for application with only one Frontend | ||
/// | ||
/// ReUse => Accept olders refresh tokens | ||
/// Decrease security but better for scenarios where there are more than one frontend. Like a browser + mobile app | ||
/// </summary> | ||
public RefreshTokenType RefreshTokenType { get; set; } = RefreshTokenType.OneTime; | ||
|
||
|
||
} | ||
} |
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,24 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Security.Claims; | ||
using System.Text; | ||
using Microsoft.IdentityModel.JsonWebTokens; | ||
|
||
namespace NetDevPack.Identity.Jwt | ||
{ | ||
internal static class Extensions | ||
{ | ||
public static void RemoveRefreshToken(this ICollection<Claim> claims) | ||
{ | ||
var refreshToken = claims.FirstOrDefault(f => f.Type == "LastRefreshToken"); | ||
if (refreshToken is not null) | ||
claims.Remove(refreshToken); | ||
} | ||
|
||
public static string GetJwtId(this ClaimsIdentity principal) | ||
{ | ||
return principal.FindFirst(JwtRegisteredClaimNames.Jti)?.Value; | ||
} | ||
} | ||
} |
Oops, something went wrong.