Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Weidaicheng committed Jun 11, 2018
2 parents d0dcfac + 9727797 commit 2d93171
Show file tree
Hide file tree
Showing 19 changed files with 770 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Jwt.Extensions/Attributes/JwtCheckAttribute.cs
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 src/Jwt.Extensions/DependencyInjection/Options/JwtOptions.cs
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
}
}
11 changes: 11 additions & 0 deletions src/Jwt.Extensions/Entity/Payload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,16 @@ public class Payload
/// expiration time
/// </summary>
public long exp { get; set; }

public Payload()
{ }

public Payload(string sub, string iss, string aud, long exp)
{
this.sub = sub;
this.iss = iss;
this.aud = aud;
this.exp = exp;
}
}
}
13 changes: 13 additions & 0 deletions src/Jwt.Extensions/Exceptions/NoSecretSpecifiedException.cs
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)
{ }
}
}
144 changes: 144 additions & 0 deletions src/Jwt.Extensions/IJwtDecoderExtensions.cs
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;
}
}
}
}
15 changes: 15 additions & 0 deletions src/Jwt.Extensions/Mvc/ByteArraySecretTokenVerifier.cs
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);
}
}
}
15 changes: 15 additions & 0 deletions src/Jwt.Extensions/Mvc/HeaderTokenGainer.cs
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];
}
}
}
19 changes: 19 additions & 0 deletions src/Jwt.Extensions/Mvc/ITokenGainer.cs
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);
}
}
19 changes: 19 additions & 0 deletions src/Jwt.Extensions/Mvc/ITokenVerifier.cs
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);
}
}
Loading

0 comments on commit 2d93171

Please sign in to comment.