diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-02-20 22:55:15 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-02-20 22:55:15 +0200 |
| commit | 9447a8a09f87d6ea2cb62860021c595386668eec (patch) | |
| tree | a02db15a1247587f14fedb6ccae76f79bd63afb3 /Software/Visual_Studio/Tango.Web/Authentication | |
| parent | 17446569ca8d8dd00331da5926b938593c4b117f (diff) | |
| download | Tango-9447a8a09f87d6ea2cb62860021c595386668eec.tar.gz Tango-9447a8a09f87d6ea2cb62860021c595386668eec.zip | |
A lot of work !!!
Diffstat (limited to 'Software/Visual_Studio/Tango.Web/Authentication')
3 files changed, 152 insertions, 7 deletions
diff --git a/Software/Visual_Studio/Tango.Web/Authentication/TokensManager.cs b/Software/Visual_Studio/Tango.Web/Authentication/TokensManager.cs index 890d69d53..5829bfca3 100644 --- a/Software/Visual_Studio/Tango.Web/Authentication/TokensManager.cs +++ b/Software/Visual_Studio/Tango.Web/Authentication/TokensManager.cs @@ -48,8 +48,8 @@ namespace Tango.Web.Authentication Value = tokenObject, WebToken = new WebToken() { - AccessToken = token, - Expiration = DateTime.UtcNow.Add(ExpirationTime) + //AccessToken = token, + //Expiration = DateTime.UtcNow.Add(ExpirationTime) }, }; @@ -73,7 +73,7 @@ namespace Tango.Web.Authentication if (DateTime.UtcNow > _tokens[token].WebToken.Expiration) { _tokens.Remove(token); - throw new SessionExpiredException("Session Expired."); + throw new TokenExpiredException("Session Expired."); } return _tokens[token].Value; diff --git a/Software/Visual_Studio/Tango.Web/Authentication/WebToken.cs b/Software/Visual_Studio/Tango.Web/Authentication/WebToken.cs index 71ec6eb0b..14fc49942 100644 --- a/Software/Visual_Studio/Tango.Web/Authentication/WebToken.cs +++ b/Software/Visual_Studio/Tango.Web/Authentication/WebToken.cs @@ -1,6 +1,12 @@ -using System; +using JWT; +using JWT.Algorithms; +using JWT.Builder; +using JWT.Serializers; +using Newtonsoft.Json; +using System; using System.Collections.Generic; using System.Linq; +using System.Security.Claims; using System.Text; using System.Threading.Tasks; @@ -8,7 +14,146 @@ namespace Tango.Web.Authentication { public class WebToken { - public DateTime Expiration { get; set; } - public String AccessToken { get; set; } + public DateTime Issued { get; protected set; } + public DateTime? Expiration { get; protected set; } + public String AccessToken { get; protected set; } + + public WebToken() + { + + } + + public static WebToken CreateNew(String secret, DateTime? expiration = null) + { + DateTime issued = DateTime.UtcNow; + + var builder = new JwtBuilder() + .WithAlgorithm(new HMACSHA256Algorithm()) + .WithSecret(secret) + .IssuedAt(issued); + + if (expiration != null) + { + builder = builder.ExpirationTime(expiration.Value); + } + + builder = builder.AddClaim("object", null); + + return new WebToken() + { + AccessToken = builder.Build(), + Expiration = expiration, + Issued = issued, + }; + } + + public static void Validate(String secret, String token) + { + var json = new JwtBuilder() + .WithSecret(secret) + .MustVerifySignature() + .Decode(token); + } + + public void Validate(String secret) + { + var json = new JwtBuilder() + .WithSecret(secret) + .MustVerifySignature() + .Decode(AccessToken); + } + + public static WebToken FromToken(String token) + { + WebToken webToken = new WebToken(); + + var payload = new JwtBuilder() + .WithValidator(null) + .Decode<IDictionary<string, object>>(token); + + webToken.AccessToken = token; + + if (payload.ContainsKey("exp")) + { + long exp = long.Parse(payload["exp"].ToString()); + webToken.Expiration = ConvertEpochToDateTime(exp); + } + + if (payload.ContainsKey("iat")) + { + long iat = long.Parse(payload["iat"].ToString()); + webToken.Issued = ConvertEpochToDateTime(iat); + } + + return webToken; + } + + protected static DateTime ConvertEpochToDateTime(long seconds) + { + var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + return epoch.AddSeconds(seconds); + } + } + + public class WebToken<T> : WebToken where T : class + { + public T Object { get; protected set; } + + private WebToken() + { + + } + + public static WebToken<T> CreateNew(String secret, T obj = null, DateTime? expiration = null) + { + DateTime issued = DateTime.UtcNow; + + var builder = new JwtBuilder() + .WithAlgorithm(new HMACSHA256Algorithm()) + .WithSecret(secret) + .IssuedAt(issued); + + if (expiration != null) + { + builder = builder.ExpirationTime(expiration.Value); + } + + builder = builder.AddClaim("object", obj); + + return new WebToken<T>() + { + AccessToken = builder.Build(), + Expiration = expiration, + Issued = issued, + Object = obj, + }; + } + + public static new WebToken<T> FromToken(String token) + { + WebToken<T> webToken = new WebToken<T>(); + + var payload = new JwtBuilder() + .WithValidator(null) + .Decode<IDictionary<string, object>>(token); + + webToken.AccessToken = token; + + if (payload.ContainsKey("exp")) + { + long exp = long.Parse(payload["exp"].ToString()); + webToken.Expiration = ConvertEpochToDateTime(exp); + } + + if (payload.ContainsKey("iat")) + { + long iat = long.Parse(payload["iat"].ToString()); + webToken.Issued = ConvertEpochToDateTime(iat); + } + + webToken.Object = JsonConvert.DeserializeObject<T>(payload["object"].ToString()); + + return webToken; + } } } diff --git a/Software/Visual_Studio/Tango.Web/Authentication/WebTokenResponse.cs b/Software/Visual_Studio/Tango.Web/Authentication/WebTokenResponse.cs index 17ac6636f..190a47cc2 100644 --- a/Software/Visual_Studio/Tango.Web/Authentication/WebTokenResponse.cs +++ b/Software/Visual_Studio/Tango.Web/Authentication/WebTokenResponse.cs @@ -9,6 +9,6 @@ namespace Tango.Web.Authentication { public class WebTokenResponse : WebResponseMessage { - public WebToken WebToken { get; set; } + public String AccessToken { get; set; } } } |
