public void When_Passing_JsonWeb_Key_Used_For_The_Signature_With_Ec_Key_But_Which_Doesnt_Contains_XCoordinate_Then_Exception_Is_Thrown() { // ARRANGE InitializeFakeObjects(); var jsonWebKey = new Dictionary <string, object> { { Constants.JsonWebKeyParameterNames.KeyTypeName, Constants.KeyTypeValues.EcName }, { Constants.JsonWebKeyParameterNames.KeyIdentifierName, "kid" }, { Constants.JsonWebKeyParameterNames.UseName, Constants.UseValues.Signature } }; var jsonWebKeySet = new JsonWebKeySet { Keys = new List <Dictionary <string, object> > { jsonWebKey } }; var json = jsonWebKeySet.SerializeWithJavascript(); // ACT & ASSERTS var ex = Assert.Throws <InvalidOperationException>(() => _jsonWebKeyConverter.ExtractSerializedKeys(jsonWebKeySet)); Assert.True(ex.Message == ErrorDescriptions.CannotExtractParametersFromJsonWebKey); }
public void When_Passing_JsonWeb_Key_With_Not_Supported_Usage_Then_Exception_Is_Thrown() { // ARRANGE InitializeFakeObjects(); var jsonWebKeySet = new JsonWebKeySet { Keys = new List <Dictionary <string, object> > { new Dictionary <string, object> { { Constants.JsonWebKeyParameterNames.KeyTypeName, Constants.KeyTypeValues.RsaName }, { Constants.JsonWebKeyParameterNames.KeyIdentifierName, "kid" }, { Constants.JsonWebKeyParameterNames.UseName, "invalid_usage" } } } }; var json = jsonWebKeySet.SerializeWithJavascript(); // ACT & ASSERTS var ex = Assert.Throws <InvalidOperationException>(() => _jsonWebKeyConverter.ExtractSerializedKeys(jsonWebKeySet)); Assert.True(ex.Message == ErrorDescriptions.JwkIsInvalid); }
public async Task When_Requesting_JsonWeb_Key_Then_Its_Information_Are_Returned() { // ARRANGE InitializeFakeObjects(); const string url = "http://google.be/"; const string kid = "kid"; var uri = new Uri(url); var jsonWebKeySet = new JsonWebKeySet(); var jsonWebKeys = new List <JsonWebKey> { new JsonWebKey { Kid = kid } }; var json = jsonWebKeySet.SerializeWithJavascript(); var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(json) }; var handler = new FakeHttpMessageHandler(httpResponseMessage); var httpClientFake = new HttpClient(handler); _httpClientFactoryStub.Setup(h => h.GetHttpClient()) .Returns(httpClientFake); _jsonWebKeyConverterStub.Setup(j => j.ExtractSerializedKeys(It.IsAny <JsonWebKeySet>())) .Returns(jsonWebKeys); // ACT var result = await _jsonWebKeyHelper.GetJsonWebKey(kid, uri).ConfigureAwait(false); // ASSERTS Assert.NotNull(result); Assert.True(result.Kid == kid); }
public async Task When_JsonWebKey_Is_Extracted_And_The_Jws_Is_Unsigned_Then_Information_Are_Returned() { // ARRANGE InitializeFakeObjects(); const string url = "http://google.be/"; const string kid = "kid"; var getJwsParameter = new GetJwsParameter { Url = url, Jws = "jws" }; var jsonWebKeySet = new JsonWebKeySet(); var json = jsonWebKeySet.SerializeWithJavascript(); var jwsProtectedHeader = new JwsProtectedHeader { Kid = kid }; var jsonWebKey = new JsonWebKey { Kid = kid }; var dic = new Dictionary <string, object> { { "kid", kid } }; var jwsPayload = new JwsPayload(); _jwsParserStub.Setup(j => j.GetHeader(It.IsAny <string>())) .Returns(jwsProtectedHeader); _jsonWebKeyHelperStub.Setup(h => h.GetJsonWebKey(It.IsAny <string>(), It.IsAny <Uri>())) .Returns(Task.FromResult(jsonWebKey)); _jwsParserStub.Setup(j => j.ValidateSignature(It.IsAny <string>(), It.IsAny <JsonWebKey>())) .Returns(jwsPayload); _jsonWebKeyEnricherStub.Setup(j => j.GetJsonWebKeyInformation(It.IsAny <JsonWebKey>())) .Returns(dic); _jsonWebKeyEnricherStub.Setup(j => j.GetPublicKeyInformation(It.IsAny <JsonWebKey>())) .Returns(() => new Dictionary <string, object>()); // ACT var result = await _getJwsInformationAction.Execute(getJwsParameter); // ASSERTS Assert.NotNull(result); Assert.True(result.JsonWebKey.ContainsKey("kid")); Assert.True(result.JsonWebKey.First().Value == kid); }
public async Task When_The_Signature_Is_Not_Valid_Then_Exception_Is_Thrown() { // ARRANGE InitializeFakeObjects(); const string url = "http://google.be/"; const string kid = "kid"; var getJwsParameter = new GetJwsParameter { Url = url, Jws = "jws" }; var jsonWebKeySet = new JsonWebKeySet(); var json = jsonWebKeySet.SerializeWithJavascript(); var jwsProtectedHeader = new JwsProtectedHeader { Kid = kid }; var jsonWebKey = new JsonWebKey { Kid = kid }; _jwsParserStub.Setup(j => j.GetHeader(It.IsAny <string>())) .Returns(jwsProtectedHeader); _jsonWebKeyHelperStub.Setup(h => h.GetJsonWebKey(It.IsAny <string>(), It.IsAny <Uri>())) .Returns(Task.FromResult(jsonWebKey)); _jwsParserStub.Setup(j => j.ValidateSignature(It.IsAny <string>(), It.IsAny <JsonWebKey>())) .Returns(() => null); // ACT & ASSERTS var innerException = await Assert.ThrowsAsync <IdentityServerManagerException>(async() => await _getJwsInformationAction.Execute(getJwsParameter)).ConfigureAwait(false); Assert.NotNull(innerException); Assert.True(innerException.Code == ErrorCodes.InvalidRequestCode); Assert.True(innerException.Message == ErrorDescriptions.TheSignatureIsNotCorrect); }