Пример #1
0
        public void ValidateAsync_GarbageJwt_Throws()
        {
            var publicKeyProvider = new Mock <IPublicKeyProvider>(MockBehavior.Strict).Object;
            IAccessTokenValidator accessTokenValidator = new AccessTokenValidator(publicKeyProvider);

            Assert.Throws <ValidationException>(() =>
                                                accessTokenValidator.ValidateAsync("garbage").ConfigureAwait(false).GetAwaiter().GetResult()
                                                );
        }
Пример #2
0
        private async Task RunTest(
            bool signJwt,
            DateTime jwtExpiry,
            Type expectedExceptionType = null
            )
        {
            string             keyId              = Guid.NewGuid().ToString();
            D2LSecurityToken   signingToken       = D2LSecurityTokenUtility.CreateActiveToken(id: keyId);
            SigningCredentials signingCredentials = null;

            if (signJwt)
            {
                signingCredentials = signingToken.GetSigningCredentials();
            }

            var jwtToken = new JwtSecurityToken(
                issuer: "someissuer",
                signingCredentials: signingCredentials,
                expires: jwtExpiry
                );

            var    tokenHandler  = new JwtSecurityTokenHandler();
            string serializedJwt = tokenHandler.WriteToken(jwtToken);

            IPublicKeyProvider publicKeyProvider = PublicKeyProviderMock.Create(
                m_jwksEndpoint,
                keyId,
                signingToken
                ).Object;

            IAccessTokenValidator tokenValidator = new AccessTokenValidator(
                publicKeyProvider
                );

            IAccessToken accessToken = null;
            Exception    exception   = null;

            try {
                accessToken = await tokenValidator.ValidateAsync(
                    accessToken : serializedJwt
                    ).ConfigureAwait(false);
            } catch (Exception e) {
                exception = e;
            }

            if (expectedExceptionType != null)
            {
                Assert.IsNull(accessToken, "Unexpected access token returned from validation");
                Assert.IsNotNull(exception, "Expected an exception but got null");
                Assert.AreEqual(expectedExceptionType, exception.GetType(), "Wrong exception type");
            }
            else
            {
                Assert.IsNotNull(accessToken, "Expected an access token but got none");
            }
        }