Ejemplo n.º 1
0
        public bool Validate(string input, string secret = null)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                return(false);
            }

            PermissionTicket ticket;

            try
            {
                ticket = JsonConvert.DeserializeObject <PermissionTicket>(input);
            }
            catch
            {
                try
                {
                    if (string.IsNullOrWhiteSpace(secret))
                    {
                        return(false);
                    }
                    ticket = PermissionTicket.FromJwt(input, secret);
                }
                catch
                {
                    return(false);
                }
            }

            return(Validate(ticket));
        }
Ejemplo n.º 2
0
        public IActionResult Validate([FromHeader(Name = "X-Permission-Ticket")] string ticket)
        {
            var valid = this.manager.Validate(ticket, Secret);

            if (valid)
            {
                return(Ok(PermissionTicket.FromJwt(ticket, Secret)));
            }
            return(BadRequest("Ticket is Invalid"));
        }
        public void ValidateFromJwt(string jwt, bool expectedResult)
        {
            // Arrange
            var ticket = PermissionTicket.FromJwt(jwt, Common.Secret);

            this.storage.Add(ticket.GetHash(), ticket);

            // Act
            var validationResult = this.manager.Validate(jwt, Common.Secret);

            // Assert
            validationResult.Should().Be(expectedResult);
        }
Ejemplo n.º 4
0
        public void FromJwtSuccessful()
        {
            // Act
            var ticket = PermissionTicket.FromJwt(ExistingJwt, Common.Secret);

            // fix timestamp for hash generation
            ticket.WithExpiry(DateTimeOffset.MinValue.AddMinutes(1));

            // Assert
            var retrievedHash = ticket.GetHash();
            var existingHash  = ExistingTicket.GetHash();

            retrievedHash.Should().BeEquivalentTo(existingHash);
        }
Ejemplo n.º 5
0
        private PermissionTicket GetPermissionTicket(string token)
        {
            PermissionTicket pemTicket;

            try
            {
                pemTicket = PermissionTicket.FromJwt(token, "secret");
            }
            catch (SignatureVerificationException sigVerifyEx)
            {
                return(null);
            }

            return(pemTicket);
        }