Example #1
0
 public Token(String secretKey, IAdviceDateTimeProvider dateProvider, Guid userId, String userName) : this(secretKey, dateProvider)
 {
     this.UserId    = userId;
     this.UserName  = userName;
     this.IssuedAt  = DateTime.GetNow();
     this.ExpiresAt = DateTime.GetNow().AddMinutes(15);
 }
Example #2
0
        public Token(String secretKey, IAdviceDateTimeProvider dateProvider, String token) : this(secretKey, dateProvider)
        {
            IJsonSerializer   serializer = new JsonNetSerializer();
            IJwtValidator     validator  = new JwtValidator(serializer, this.DateTime);
            IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
            IJwtDecoder       decoder    = new JwtDecoder(serializer, validator, urlEncoder);

            try
            {
                var json = decoder.Decode(token, this.SecretKey, verify: true);

                var jsonObject = JObject.Parse(json);

                this.UserId    = Guid.Parse(jsonObject["userId"].Value <String>());
                this.UserName  = jsonObject["userName"].Value <String>();
                this.IssuedAt  = ConvertFromUnixTimestamp(jsonObject["iat"].Value <Int64>());
                this.ExpiresAt = ConvertFromUnixTimestamp(jsonObject["exp"].Value <Int64>());

                foreach (var child in jsonObject.Children())
                {
                    if (child.Path.Equals("userId") || child.Path.Equals("userName") || child.Path.Equals("iat") || child.Path.Equals("exp"))
                    {
                        continue;
                    }

                    //JProperty childProperty = child as JProperty;

                    //if(childProperty != null)
                    //    this.CustomClaims.Add(childProperty.Name, childProperty.Value<String>());

                    if (child.Type == JTokenType.Property)
                    {
                        var childProperty = (JProperty)child;
                        this.CustomClaims.Add(childProperty.Name, child.First.Value <String>());
                    }
                    else
                    {
                        this.CustomClaims.Add(child.Path, child.First.Value <String>());
                    }
                }

                this.Valid = true;
            }
            catch (TokenExpiredException)
            {
                this.Error = "Token Expired";
                var json       = decoder.Decode(token, this.SecretKey, false);
                var jsonObject = JObject.Parse(json);
                this.UserId   = Guid.Parse(jsonObject["userId"].Value <String>());
                this.UserName = jsonObject["userName"].Value <String>();
            }
            catch (SignatureVerificationException)
            {
                this.Error = "Token Signature Failure";
            }
        }
Example #3
0
 public TokenFactory(IAdviceDateTimeProvider dateTimeProvider)
 {
     this.dateTimeProvider = dateTimeProvider;
 }
Example #4
0
 public Token(String secretKey, IAdviceDateTimeProvider dateProvider)
 {
     this.CustomClaims = new Dictionary <string, String>();
     this.SecretKey    = secretKey;
     this.DateTime     = dateProvider;
 }