Exemplo n.º 1
0
        async public void ItShouldRetrieveJwksAndCacheKeysWhenKeyIsNotInCache()
        {
            var seq = new MockSequence();

            m_keyCache
            .InSequence(seq)
            .Setup(x => x.Get(SRC_NAMESPACE, KEY_ID))
            .Returns <D2LSecurityToken>(null);

            var otherKeyId = Guid.NewGuid();
            var jwks       = new JsonWebKeySet(
                new JavaScriptSerializer().Serialize(
                    new {
                keys = new object[] {
                    D2LSecurityTokenUtility
                    .CreateActiveToken(KEY_ID)
                    .ToJsonWebKey()
                    .ToJwkDto(),
                    D2LSecurityTokenUtility
                    .CreateActiveToken(otherKeyId)
                    .ToJsonWebKey()
                    .ToJwkDto()
                }
            }
                    ),
                new Uri("http://localhost/dummy")
                );

            m_jwksProvider
            .InSequence(seq)
            .Setup(x => x.RequestJwksAsync())
            .ReturnsAsync(jwks);

            m_keyCache
            .Setup(x => x.Set(SRC_NAMESPACE, It.Is <D2LSecurityToken>(k => k.KeyId == KEY_ID)));
            m_keyCache
            .Setup(x => x.Set(SRC_NAMESPACE, It.Is <D2LSecurityToken>(k => k.KeyId == otherKeyId)));

            var cachedKey = new D2LSecurityToken(
                KEY_ID,
                DateTime.UtcNow,
                DateTime.UtcNow + TimeSpan.FromHours(1),
                () => null as Tuple <AsymmetricSecurityKey, IDisposable>
                );

            m_keyCache
            .InSequence(seq)
            .Setup(x => x.Get(SRC_NAMESPACE, KEY_ID))
            .Returns(cachedKey);

            D2LSecurityToken result = await m_publicKeyProvider
                                      .GetByIdAsync(KEY_ID)
                                      .SafeAsync();

            m_keyCache.VerifyAll();

            Assert.AreEqual(cachedKey, result);
        }
        public async Task ItShouldRetrieveJwksAndIgnoreInvalidKeysWithoutErroring(string keyId)
        {
            var seq = new MockSequence();

            m_keyCache
            .InSequence(seq)
            .Setup(x => x.Get(SRC_NAMESPACE, keyId))
            .Returns <D2LSecurityToken>(null);

            var otherKeyId = Guid.NewGuid().ToString();
            var jwks       = new JsonWebKeySet(
                JsonSerializer.Serialize(
                    new {
                keys = new[] {
                    D2LSecurityTokenUtility
                    .CreateActiveToken(keyId)
                    .ToJsonWebKey()
                    .ToJwkDto(),
                    D2LSecurityTokenUtility
                    .CreateTokenWithTimeRemaining(TimeSpan.FromSeconds(-1), otherKeyId)
                    .ToJsonWebKey()
                    .ToJwkDto()
                }
            }
                    ),
                new Uri("http://localhost/dummy")
                );

            m_jwksProvider
            .InSequence(seq)
            .Setup(x => x.RequestJwkAsync(keyId))
            .ReturnsAsync(jwks);

            m_keyCache
            .Setup(x => x.Set(SRC_NAMESPACE, It.Is <D2LSecurityToken>(k => k.KeyId == keyId)));

            var cachedKey = new D2LSecurityToken(
                keyId,
                DateTime.UtcNow,
                DateTime.UtcNow + TimeSpan.FromHours(1),
                () => null as Tuple <AsymmetricSecurityKey, IDisposable>
                );

            m_keyCache
            .InSequence(seq)
            .Setup(x => x.Get(SRC_NAMESPACE, keyId))
            .Returns(cachedKey);

            D2LSecurityToken result = await m_publicKeyProvider
                                      .GetByIdAsync(keyId)
                                      .ConfigureAwait(false);

            m_keyCache.VerifyAll();
            m_keyCache.Verify(x => x.Set(SRC_NAMESPACE, It.IsAny <D2LSecurityToken>()), Times.Once);

            Assert.AreEqual(cachedKey, result);
        }
		async public void ItShouldRetrieveJwksAndCacheKeysWhenKeyIsNotInCache() {
			var seq = new MockSequence();

			m_keyCache
				.InSequence( seq )
				.Setup( x => x.Get( KEY_ID ) )
				.Returns<D2LSecurityToken>( null );

			var otherKeyId = Guid.NewGuid();
			var jwks = new JsonWebKeySet(
				new JavaScriptSerializer().Serialize(
					new {
						keys = new object[] {
							D2LSecurityTokenUtility
								.CreateActiveToken( KEY_ID )
								.ToJsonWebKey()
								.ToJwkDto(),
							D2LSecurityTokenUtility
								.CreateActiveToken( otherKeyId )
								.ToJsonWebKey()
								.ToJwkDto()
						}
					}
				)
			);
			m_jwksProvider
				.InSequence( seq )
				.Setup( x => x.RequestJwksAsync() )
				.ReturnsAsync( jwks );

			m_keyCache
				.Setup( x => x.Set( It.Is<D2LSecurityToken>( k => k.KeyId == KEY_ID ) ) );
			m_keyCache
				.Setup( x => x.Set( It.Is<D2LSecurityToken>( k => k.KeyId == otherKeyId ) ) );

			var cachedKey = new D2LSecurityToken(
				KEY_ID,
				DateTime.UtcNow,
				DateTime.UtcNow + TimeSpan.FromHours( 1 ),
				() => null as Tuple<AsymmetricSecurityKey, IDisposable>
			);
			m_keyCache
				.InSequence( seq )
				.Setup( x => x.Get( KEY_ID ) )
				.Returns( cachedKey );

			D2LSecurityToken result = await m_publicKeyProvider
				.GetByIdAsync( KEY_ID )
				.SafeAsync();

			m_keyCache.VerifyAll();

			Assert.AreEqual( cachedKey, result );
		}
		private static void CacheJwks( IInMemoryPublicKeyCache cache, JsonWebKeySet jwks ) {
			foreach( var jwk in jwks ) {
				D2LSecurityToken token;
				try {
					token = jwk.ToSecurityToken();
				} catch {
					continue;
				}

				cache.Set( token );
			}
		}
Exemplo n.º 5
0
        async Task <D2LSecurityToken> IPublicKeyProvider.GetByIdAsync(string id)
        {
            D2LSecurityToken result = m_cache.Get(m_jwksProvider.Namespace, id);

            if (result != null)
            {
                return(result);
            }

            JsonWebKeySet jwks = await m_jwksProvider
                                 .RequestJwkAsync(id)
                                 .ConfigureAwait(false);

            CacheJwks(m_cache, m_jwksProvider.Namespace, jwks);

            result = m_cache.Get(m_jwksProvider.Namespace, id);
            if (result != null)
            {
                return(result);
            }

            throw new PublicKeyNotFoundException(id, jwks.Source.AbsoluteUri);
        }
Exemplo n.º 6
0
        private static void CacheJwks(IInMemoryPublicKeyCache cache, string srcNamespace, JsonWebKeySet jwks)
        {
            foreach (var jwk in jwks)
            {
                D2LSecurityToken token;
                try {
                    token = jwk.ToSecurityToken();
                } catch {
                    continue;
                }

                cache.Set(srcNamespace, token);
            }
        }
		async public void ItShouldRetrieveJwksAndIgnoreInvalidKeysWithoutErroring() {
			var seq = new MockSequence();

			m_keyCache
				.InSequence( seq )
				.Setup( x => x.Get( SRC_NAMESPACE, KEY_ID ) )
				.Returns<D2LSecurityToken>( null );

			var otherKeyId = Guid.NewGuid();
			var jwks = new JsonWebKeySet(
				new JavaScriptSerializer().Serialize(
					new {
						keys = new[] {
							D2LSecurityTokenUtility
								.CreateActiveToken( KEY_ID )
								.ToJsonWebKey()
								.ToJwkDto(),
							D2LSecurityTokenUtility
								.CreateTokenWithTimeRemaining( TimeSpan.FromSeconds( -1 ), otherKeyId )
								.ToJsonWebKey()
								.ToJwkDto()
						}
					}
				),
				new Uri( "http://localhost/dummy" )
			);
			m_jwksProvider
				.InSequence( seq )
				.Setup( x => x.RequestJwksAsync() )
				.ReturnsAsync( jwks );

			m_keyCache
				.Setup( x => x.Set( SRC_NAMESPACE, It.Is<D2LSecurityToken>( k => k.KeyId == KEY_ID ) ) );

			var cachedKey = new D2LSecurityToken(
				KEY_ID,
				DateTime.UtcNow,
				DateTime.UtcNow + TimeSpan.FromHours( 1 ),
				() => null as Tuple<AsymmetricSecurityKey, IDisposable>
			);
			m_keyCache
				.InSequence( seq )
				.Setup( x => x.Get( SRC_NAMESPACE, KEY_ID ) )
				.Returns( cachedKey );

			D2LSecurityToken result = await m_publicKeyProvider
				.GetByIdAsync( KEY_ID )
				.SafeAsync();

			m_keyCache.VerifyAll();
			m_keyCache.Verify( x => x.Set( SRC_NAMESPACE, It.IsAny<D2LSecurityToken>() ), Times.Once );

			Assert.AreEqual( cachedKey, result );
		}