public void When_Passing_JsonWeb_Key_Used_For_The_Signature_With_Ec_Key_Then_JsonWeb_Key_Is_Returned() { // ARRANGE var xCoordinate = "x_coordinate".Base64Encode(); var yCoordinate = "y_coordinate".Base64Encode(); InitializeFakeObjects(); var jsonWebKey = new Dictionary <string, object> { { Constants.JsonWebKeyParameterNames.KeyTypeName, Constants.KeyTypeValues.EcName }, { Constants.JsonWebKeyParameterNames.KeyIdentifierName, "kid" }, { Constants.JsonWebKeyParameterNames.UseName, Constants.UseValues.Signature }, { Constants.JsonWebKeyParameterNames.EcKey.XCoordinateName, xCoordinate }, { Constants.JsonWebKeyParameterNames.EcKey.YCoordinateName, yCoordinate } }; var jsonWebKeySet = new JsonWebKeySet { Keys = new List <Dictionary <string, object> > { jsonWebKey } }; var cngKeySerialized = new CngKeySerialized { X = xCoordinate.Base64DecodeBytes(), Y = yCoordinate.Base64DecodeBytes() }; var serializer = new XmlSerializer(typeof(CngKeySerialized)); var serializedKeys = string.Empty; using (var writer = new StringWriter()) { serializer.Serialize(writer, cngKeySerialized); serializedKeys = writer.ToString(); } // ACT & ASSERTS var result = _jsonWebKeyConverter.ExtractSerializedKeys(jsonWebKeySet); Assert.NotNull(result); Assert.True(result.Count() == 1); Assert.True(result.First().SerializedKey == serializedKeys); }
private string ExtractEcKeyInformation(Dictionary <string, object> information) { var xCoordinate = information.FirstOrDefault(i => i.Key == Constants.JsonWebKeyParameterNames.EcKey.XCoordinateName); var yCoordinate = information.FirstOrDefault(i => i.Key == Constants.JsonWebKeyParameterNames.EcKey.YCoordinateName); if (xCoordinate.Equals(default(KeyValuePair <string, object>)) || yCoordinate.Equals(default(KeyValuePair <string, object>))) { throw new InvalidOperationException(ErrorDescriptions.CannotExtractParametersFromJsonWebKey); } byte[] xCoordinateBytes, yCoordinateBytes; try { xCoordinateBytes = xCoordinate.Value.ToString().Base64DecodeBytes(); yCoordinateBytes = yCoordinate.Value.ToString().Base64DecodeBytes(); } catch (Exception) { throw new InvalidOperationException(ErrorDescriptions.OneOfTheParameterIsNotBase64Encoded); } var cngKeySerialized = new CngKeySerialized { X = xCoordinateBytes, Y = yCoordinateBytes }; var serializer = new XmlSerializer(typeof(CngKeySerialized)); using (var writer = new StringWriter()) { serializer.Serialize(writer, cngKeySerialized); return(writer.ToString()); } }