public void CreateToken_NullClaim()
        {
            TokenClaim  claim   = null;
            TestPayload payload = CreatePayload();

            CreateToken(claim, payload);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        private bool ValueEquals(TestPayload a, TestPayload b)
        {
            if (a.IntValue != b.IntValue)
            {
                return(false);
            }
            if (a.StringValue != b.StringValue)
            {
                return(false);
            }

            if ((a.Items == null) != (b.Items == null))
            {
                return(false);
            }

            if (a.Items != null)
            {
                if (a.Items.Count != b.Items.Count)
                {
                    return(false);
                }

                for (int i = 0; i < a.Items.Count; ++i)
                {
                    if (a.Items[i].Value != b.Items[i].Value)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
        public void CreateToken()
        {
            TokenClaim  claim   = CreateClaim();
            TestPayload payload = CreatePayload();

            CreateToken(claim, payload);
        }
        public void CreateToken_NullPayload()
        {
            TokenClaim  claim   = CreateClaim();
            TestPayload payload = null;

            claim.Expiration = DateTimeOffset.UtcNow.AddSeconds(-1);

            CreateToken(claim, payload);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="claim"></param>
        /// <param name="payload"></param>
        /// <returns></returns>
        private ITokenPair GetToken(TokenClaim claim, TestPayload payload)
        {
            MsJwtProviderBase <TestPayload> provider = new TestMsJwtRS256Provider().CreateProvider();

            TokenInfo accessToken = new TokenInfo(provider.CreateToken(claim, payload), claim.Expiration, claim.NotBefore);

            claim.Expiration = DateTimeOffset.UtcNow.AddSeconds(TestConstants.RefreshTokenLifetimeSeconds);

            TokenInfo refreshToken = new TokenInfo(provider.CreateToken(claim, payload), claim.Expiration, claim.NotBefore);

            return(new TokenPair(accessToken, refreshToken));
        }
        public void CreateToken_NotBefore()
        {
            TokenClaim  claim   = TestMsJwtProviderBase.CreateClaim();
            TestPayload payload = TestMsJwtProviderBase.CreatePayload();

            claim.NotBefore = DateTimeOffset.UtcNow.AddSeconds(5);

            string token = GetProvider().CreateToken(claim, payload);

            MsJwtValidatorBase <TestPayload> validator = CreateValidator(TestConstants.Issuer, TestConstants.Audience);

            Assert.IsFalse(validator.ValidateToken(token, out ITokenClaim tokenClaim, out TestPayload tokenPayload, out TokenState state, out string errorMessage));

            Assert.AreEqual(state, TokenState.NotBefore);
        }
        public void CreateToken_NullPayload()
        {
            TokenClaim  claim   = TestMsJwtProviderBase.CreateClaim();
            TestPayload payload = null;

            string token = GetProvider().CreateToken(claim, payload);

            MsJwtValidatorBase <TestPayload> validator = CreateValidator(TestConstants.Issuer, TestConstants.Audience);

            Assert.IsTrue(validator.ValidateToken(token, out ITokenClaim tokenClaim, out TestPayload tokenPayload, out TokenState state, out string errorMessage));

            Assert.IsTrue(ValueEquals(claim, tokenClaim));
            Assert.IsNull(tokenPayload);
            Assert.AreEqual(state, TokenState.Valid);
        }
        /// <summary>
        ///
        /// </summary>
        private void CreateToken_InvalidAudience(bool validateAudience)
        {
            TokenClaim  claim   = TestMsJwtProviderBase.CreateClaim();
            TestPayload payload = TestMsJwtProviderBase.CreatePayload();

            string token = GetProvider().CreateToken(claim, payload);

            MsJwtValidatorBase <TestPayload> validator = CreateValidator(TestConstants.Issuer, validateAudience ? "thisAudience" : null);

            if (validateAudience)
            {
                Assert.IsFalse(validator.ValidateToken(token, out ITokenClaim tokenClaim, out TestPayload tokenPayload, out TokenState state, out string errorMessage));
                Assert.AreEqual(state, TokenState.InvalidAudience);
            }
            else
            {
                Assert.IsTrue(validator.ValidateToken(token, out ITokenClaim tokenClaim, out TestPayload tokenPayload, out TokenState state, out string errorMessage));
                Assert.AreEqual(state, TokenState.Valid);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="claim"></param>
        /// <param name="payload"></param>
        /// <returns></returns>
        public string CreateToken(TokenClaim claim, TestPayload payload)
        {
            MsJwtProviderBase <TestPayload> provider = CreateProvider();

            return(provider.CreateToken(claim, payload));
        }