示例#1
0
        internal void Generate(string secretKey, bool resetTokenIdentifier = true)
        {
            if (resetTokenIdentifier)
            {
                TokenIdentifier = GetTokenIdentifier(_tokenIdentifierPrefix);
            }

            try
            {
                var dto = new HeaderDto()
                {
                    CustomerId      = CustomerId,
                    EventId         = EventId,
                    TokenIdentifier = TokenIdentifier,
                    Issued          = (new DateTimeOffset(Issued)).ToUnixTimeMilliseconds(),
                    Expires         = Expires == DateTime.MaxValue ? null : (long?)(new DateTimeOffset(Expires)).ToUnixTimeMilliseconds(),
                    Encryption      = EncryptionType.AES256.ToString(),
                    TokenVersion    = TokenVersion.QT1.ToString(),
                    IpAddress       = IpAddress,
                    XForwardedFor   = XForwardedFor
                };

                string serialized = dto.Serialize() + ".";
                if (Payload != null)
                {
                    serialized = serialized + Payload.EncryptAndEncode(secretKey, TokenIdentifier);
                }
                TokenWithoutHash = serialized;

                HashCode = Base64UrlEncoding.Encode(ShaHashing.GenerateHash(secretKey, TokenWithoutHash));
            }
            catch (Exception ex)
            {
                throw new TokenSerializationException(ex);
            }
        }
示例#2
0
        public static IEnqueueToken Parse(string tokenString, string secretKey)
        {
            if (string.IsNullOrEmpty(secretKey))
            {
                throw new ArgumentException("Invalid secret key", nameof(secretKey));
            }

            if (string.IsNullOrEmpty(tokenString))
            {
                throw new ArgumentException("Invalid token", nameof(tokenString));
            }

            var tokenParts  = tokenString.Split('.');
            var headerPart  = tokenParts[0];
            var payloadPart = tokenParts[1];
            var hashPart    = tokenParts[2];

            if (string.IsNullOrEmpty(headerPart))
            {
                throw new ArgumentException("Invalid token", nameof(tokenString));
            }
            if (string.IsNullOrEmpty(hashPart))
            {
                throw new ArgumentException("Invalid token", nameof(tokenString));
            }

            var token = headerPart + "." + payloadPart;

            var expectedHash = Base64UrlEncoding.Encode(ShaHashing.GenerateHash(secretKey, token));

            if (expectedHash != hashPart)
            {
                throw new InvalidHashException();
            }

            try
            {
                var headerModel = HeaderDto.DeserializeHeader(headerPart);

                EnqueueTokenPayload payload = null;
                if (!string.IsNullOrEmpty(payloadPart))
                {
                    payload = EnqueueTokenPayload.Deserialize(payloadPart, secretKey, headerModel.TokenIdentifier);
                }

                return(new EnqueueToken(
                           headerModel.TokenIdentifier,
                           headerModel.CustomerId,
                           headerModel.EventId,
                           DateTimeOffset.FromUnixTimeMilliseconds(headerModel.Issued).DateTime,
                           headerModel.Expires.HasValue
                        ? new DateTime?(DateTimeOffset.FromUnixTimeMilliseconds(headerModel.Expires.Value).DateTime)
                        : null,
                           headerModel.IpAddress,
                           headerModel.XForwardedFor,
                           payload)
                {
                    TokenWithoutHash = token,
                    HashCode = expectedHash
                });
            }
            catch (Exception ex)
            {
                throw new TokenDeserializationException("Unable to deserialize token", ex);
            }
        }