-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
770 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
|
||
namespace JWT.Extensions.Attributes | ||
{ | ||
/// <summary> | ||
/// Jwt check attribute | ||
/// it can be used on controller and method | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] | ||
public class JwtCheckAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// ignore jwt check | ||
/// </summary> | ||
public bool Ignore { get; set; } = false; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/Jwt.Extensions/DependencyInjection/Options/JwtOptions.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 |
---|---|---|
@@ -1,6 +1,51 @@ | ||
namespace JWT.Extensions.DependencyInjection.Options | ||
{ | ||
/// <summary> | ||
/// options | ||
/// </summary> | ||
public class JwtOptions | ||
{ | ||
/// <summary> | ||
/// secret key of type <see cref="string"/>, | ||
/// if <see cref="SecretStr"/> and <see cref="SecretBytes"/> are specified at the same time, the <see cref="SecretStr"/> would be the priority choice | ||
/// </summary> | ||
public string SecretStr { get; set; } | ||
|
||
/// <summary> | ||
/// secret key of type <see cref="byte[]"/>, | ||
/// if <see cref="SecretStr"/> and <see cref="SecretBytes"/> are specified at the same time, the <see cref="SecretStr"/> would be the priority choice | ||
/// </summary> | ||
public byte[] SecretBytes { get; set; } | ||
|
||
/// <summary> | ||
/// token bearer location in <see cref="TokenBearer"/>, default location is at <see cref="TokenBearer.Header"/> | ||
/// </summary> | ||
public TokenBearer Bearer { get; set; } = TokenBearer.Header; | ||
|
||
/// <summary> | ||
/// token bearer key, default key is "Token" | ||
/// </summary> | ||
public string TokenBearerKey { get; set; } = "Token"; | ||
|
||
/// <summary> | ||
/// redirect url for token verify failed | ||
/// </summary> | ||
public string RedirectUrl { get; set; } | ||
} | ||
|
||
/// <summary> | ||
/// token bearer location | ||
/// </summary> | ||
public enum TokenBearer | ||
{ | ||
/// <summary> | ||
/// in request header | ||
/// </summary> | ||
Header, | ||
|
||
/// <summary> | ||
/// in request query string | ||
/// </summary> | ||
QueryString | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
src/Jwt.Extensions/Exceptions/NoSecretSpecifiedException.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,13 @@ | ||
using System; | ||
|
||
namespace JWT.Extensions.Exceptions | ||
{ | ||
/// <summary> | ||
/// no secret specified | ||
/// </summary> | ||
public class NoSecretSpecifiedException : Exception | ||
{ | ||
public NoSecretSpecifiedException(string message = "No secret specified") : base(message) | ||
{ } | ||
} | ||
} |
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,144 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace JWT.Extensions | ||
{ | ||
/// <summary> | ||
/// Extensions for IJwtDecoder | ||
/// </summary> | ||
public static class IJwtDecoderExtensions | ||
{ | ||
/// <summary> | ||
/// use <see cref="IJwtDecoder.Decode(string, string, bool)"/> and verify is always on | ||
/// </summary> | ||
/// <param name="jwtDecoder"></param> | ||
/// <param name="token">The JWT.</param> | ||
/// <param name="key">The key that was used to sign the JWT.</param> | ||
/// <param name="result">A string containing the JSON payload.</param> | ||
/// <returns>true if s was converted successfully; otherwise, false.</returns> | ||
public static bool TryDecode(this IJwtDecoder jwtDecoder, string token, string key, out string result) | ||
{ | ||
try | ||
{ | ||
result = jwtDecoder.Decode(token, key, true); | ||
return true; | ||
} | ||
catch | ||
{ | ||
result = null; | ||
return false; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// use <see cref="IJwtDecoder.Decode(string, byte[], bool)"/> and verify is always on | ||
/// </summary> | ||
/// <param name="jwtDecoder"></param> | ||
/// <param name="token">The JWT.</param> | ||
/// <param name="key">The key that was used to sign the JWT.</param> | ||
/// <param name="result">A string containing the JSON payload.</param> | ||
/// <returns>true if s was converted successfully; otherwise, false.</returns> | ||
public static bool TryDecode(this IJwtDecoder jwtDecoder, string token, byte[] key, out string result) | ||
{ | ||
try | ||
{ | ||
result = jwtDecoder.Decode(token, key, true); | ||
return true; | ||
} | ||
catch | ||
{ | ||
result = null; | ||
return false; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// use <see cref="IJwtDecoder.DecodeToObject(string, string, bool)"/> and verify is always on | ||
/// </summary> | ||
/// <param name="jwtDecoder"></param> | ||
/// <param name="token">The JWT.</param> | ||
/// <param name="key">The key that was used to sign the JWT.</param> | ||
/// <param name="result">An object representing the payload.</param> | ||
/// <returns>true if s was converted successfully; otherwise, false.</returns> | ||
public static bool TryDecodeToObject(this IJwtDecoder jwtDecoder, string token, string key, out IDictionary<string, object> result) | ||
{ | ||
try | ||
{ | ||
result = jwtDecoder.DecodeToObject(token, key, true); | ||
return true; | ||
} | ||
catch | ||
{ | ||
result = null; | ||
return false; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// use <see cref="IJwtDecoder.DecodeToObject(string, byte[], bool)"/> and verify is always on | ||
/// </summary> | ||
/// <param name="jwtDecoder"></param> | ||
/// <param name="token">The JWT.</param> | ||
/// <param name="key">The key that was used to sign the JWT.</param> | ||
/// <param name="result">An object representing the payload.</param> | ||
/// <returns>true if s was converted successfully; otherwise, false.</returns> | ||
public static bool TryDecodeToObject(this IJwtDecoder jwtDecoder, string token, byte[] key, out IDictionary<string, object> result) | ||
{ | ||
try | ||
{ | ||
result = jwtDecoder.DecodeToObject(token, key, true); | ||
return true; | ||
} | ||
catch | ||
{ | ||
result = null; | ||
return false; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// use <see cref="IJwtDecoder.DecodeToObject{T}(string, string, bool)"/> and verify is always on | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="jwtDecoder"></param> | ||
/// <param name="token">The JWT.</param> | ||
/// <param name="key">The key that was used to sign the JWT.</param> | ||
/// <param name="result">An object representing the payload.</param> | ||
/// <returns>true if s was converted successfully; otherwise, false.</returns> | ||
public static bool TryDecodeToObject<T>(this IJwtDecoder jwtDecoder, string token, string key, out T result) | ||
{ | ||
try | ||
{ | ||
result = jwtDecoder.DecodeToObject<T>(token, key, true); | ||
return true; | ||
} | ||
catch | ||
{ | ||
result = default(T); | ||
return false; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// use <see cref="IJwtDecoder.DecodeToObject{T}(string, byte[], bool)"/> and verify is always on | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="jwtDecoder"></param> | ||
/// <param name="token">The JWT.</param> | ||
/// <param name="key">The key that was used to sign the JWT.</param> | ||
/// <param name="result">An object representing the payload.</param> | ||
/// <returns>true if s was converted successfully; otherwise, false.</returns> | ||
public static bool TryDecodeToObject<T>(this IJwtDecoder jwtDecoder, string token, byte[] key, out T result) | ||
{ | ||
try | ||
{ | ||
result = jwtDecoder.DecodeToObject<T>(token, key, true); | ||
return true; | ||
} | ||
catch | ||
{ | ||
result = default(T); | ||
return false; | ||
} | ||
} | ||
} | ||
} |
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,15 @@ | ||
using JWT.Extensions.DependencyInjection.Options; | ||
|
||
namespace JWT.Extensions.Mvc | ||
{ | ||
/// <summary> | ||
/// token verifier with <see cref="byte[]"/> secret | ||
/// </summary> | ||
public class ByteArraySecretTokenVerifier : ITokenVerifier | ||
{ | ||
public bool VerifyToken(IJwtDecoder jwtDecoder, string token, JwtOptions options) | ||
{ | ||
return jwtDecoder.TryDecode(token, options.SecretBytes, out string result); | ||
} | ||
} | ||
} |
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,15 @@ | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace JWT.Extensions.Mvc | ||
{ | ||
/// <summary> | ||
/// gain token from request header | ||
/// </summary> | ||
public class HeaderTokenGainer : ITokenGainer | ||
{ | ||
public string GainToken(HttpRequest request, string key) | ||
{ | ||
return request.Headers[key]; | ||
} | ||
} | ||
} |
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 Microsoft.AspNetCore.Http; | ||
using JWT.Extensions.DependencyInjection.Options; | ||
|
||
namespace JWT.Extensions.Mvc | ||
{ | ||
/// <summary> | ||
/// token gainer | ||
/// </summary> | ||
public interface ITokenGainer | ||
{ | ||
/// <summary> | ||
/// gain token | ||
/// </summary> | ||
/// <param name="request"><see cref="HttpRequest"/></param> | ||
/// <param name="key"><see cref="JwtOptions.TokenBearerKey"/></param> | ||
/// <returns></returns> | ||
string GainToken(HttpRequest request, string key); | ||
} | ||
} |
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 JWT.Extensions.DependencyInjection.Options; | ||
|
||
namespace JWT.Extensions.Mvc | ||
{ | ||
/// <summary> | ||
/// token verifier | ||
/// </summary> | ||
public interface ITokenVerifier | ||
{ | ||
/// <summary> | ||
/// verify token | ||
/// </summary> | ||
/// <param name="jwtDecoder">jwt decoder</param> | ||
/// <param name="token">token string</param> | ||
/// <param name="options">jwt options</param> | ||
/// <returns>if the verify of token is success</returns> | ||
bool VerifyToken(IJwtDecoder jwtDecoder, string token, JwtOptions options); | ||
} | ||
} |
Oops, something went wrong.