/// <summary> /// Gets a dictionary of all the message part names and values /// that are included in the message signature. /// </summary> /// <param name="channel">The channel.</param> /// <returns> /// A dictionary of the signed message parts. /// </returns> internal IDictionary <string, string> GetSignedMessageParts(Channel channel) { Requires.NotNull(channel, "channel"); ITamperResistantOpenIdMessage signedSelf = this; if (signedSelf.SignedParameterOrder == null) { return(EmptyDictionary <string, string> .Instance); } MessageDictionary messageDictionary = channel.MessageDescriptions.GetAccessor(this); string[] signedPartNamesWithoutPrefix = signedSelf.SignedParameterOrder.Split(','); Dictionary <string, string> signedParts = new Dictionary <string, string>(signedPartNamesWithoutPrefix.Length); var signedPartNames = signedPartNamesWithoutPrefix.Select(part => Protocol.openid.Prefix + part); foreach (string partName in signedPartNames) { signedParts[partName] = messageDictionary[partName]; } return(signedParts); }
public List <MessageDictionary> GetDictionaryAll() { var masterDataTable = db.GetData("Select * from MSG_DICTIONARY t order by word_order", null); if (masterDataTable == null) { return(null); } if (masterDataTable.Rows.Count == 0) { return(null); } var results = new List <MessageDictionary>(); foreach (DataRow row in masterDataTable.Rows) { var obj = new MessageDictionary(); obj.Order = row["word_order"] == DBNull.Value ? -1 : int.Parse(row["word_order"].ToString()); obj.Id = row["word_id"] == DBNull.Value ? string.Empty : row["word_id"].ToString(); obj.Name = row["word_name"] == DBNull.Value ? string.Empty : row["word_name"].ToString(); obj.ObjectMember = row["obj_member"] == DBNull.Value ? null : row["obj_member"].ToString().Split('|'); results.Add(obj); } return(results); }
internal IDictionary <string, string> Serialize(IMessage message) { ErrorUtilities.VerifyArgumentNotNull(message, "message"); var messageDescription = MessageDescription.Get(this.messageType, message.Version); var messageDictionary = new MessageDictionary(message); // Rather than hand back the whole message dictionary (which // includes keys with blank values), create a new dictionary // that only has required keys, and optional keys whose // values are not empty. var result = new Dictionary <string, string>(); foreach (var pair in messageDictionary) { MessagePart partDescription; if (messageDescription.Mapping.TryGetValue(pair.Key, out partDescription)) { if (partDescription.IsRequired || partDescription.IsNondefaultValueSet(message)) { result.Add(pair.Key, pair.Value); } } else { // This is extra data. We always write it out. result.Add(pair.Key, pair.Value); } } return(result); }
private void EnsureValidMessageParts(IProtocolMessage message) { MessageDictionary dictionary = this.MessageDescriptions.GetAccessor(message); MessageDescription description = this.MessageDescriptions.Get(message); description.EnsureMessagePartsPassBasicValidation(dictionary); }
/// <summary> /// Calculates the signature for a given message. /// </summary> /// <param name="signedMessage">The message to sign or verify.</param> /// <param name="association">The association to use to sign the message.</param> /// <returns>The calculated signature of the method.</returns> private static string GetSignature(ITamperResistantOpenIdMessage signedMessage, Association association) { ErrorUtilities.VerifyArgumentNotNull(signedMessage, "signedMessage"); ErrorUtilities.VerifyNonZeroLength(signedMessage.SignedParameterOrder, "signedMessage.SignedParameterOrder"); ErrorUtilities.VerifyArgumentNotNull(association, "association"); // Prepare the parts to sign, taking care to replace an openid.mode value // of check_authentication with its original id_res so the signature matches. MessageDictionary dictionary = new MessageDictionary(signedMessage); var parametersToSign = from name in signedMessage.SignedParameterOrder.Split(',') let prefixedName = Protocol.V20.openid.Prefix + name select new KeyValuePair <string, string>(name, dictionary[prefixedName]); byte[] dataToSign = KeyValueFormEncoding.GetBytes(parametersToSign); string signature = Convert.ToBase64String(association.Sign(dataToSign)); if (signingLogger.IsDebugEnabled) { signingLogger.DebugFormat( CultureInfo.InvariantCulture, "Signing these message parts: {0}{1}{0}Base64 representation of signed data: {2}{0}Signature: {3}", Environment.NewLine, parametersToSign.ToStringDeferred(), Convert.ToBase64String(dataToSign), signature); } return(signature); }
internal IDictionary <string, string> Serialize(MessageDictionary messageDictionary) { Requires.NotNull(messageDictionary, "messageDictionary"); // Rather than hand back the whole message dictionary (which // includes keys with blank values), create a new dictionary // that only has required keys, and optional keys whose // values are not empty (or default). var result = new Dictionary <string, string>(); foreach (var pair in messageDictionary) { MessagePart partDescription; if (messageDictionary.Description.Mapping.TryGetValue(pair.Key, out partDescription)) { Assumes.True(partDescription != null); if (partDescription.IsRequired || partDescription.IsNondefaultValueSet(messageDictionary.Message)) { result.Add(pair.Key, pair.Value); } } else { // This is extra data. We always write it out. result.Add(pair.Key, pair.Value); } } return(result); }
/// <summary> /// Reads name=value pairs into a message. /// </summary> /// <param name="fields">The name=value pairs that were read in from the transport.</param> /// <param name="messageDictionary">The message to deserialize into.</param> /// <exception cref="ProtocolException">Thrown when protocol rules are broken by the incoming message.</exception> internal void Deserialize(IDictionary <string, string> fields, MessageDictionary messageDictionary) { Requires.NotNull(fields, "fields"); Requires.NotNull(messageDictionary, "messageDictionary"); var messageDescription = messageDictionary.Description; // Before we deserialize the message, make sure all the required parts are present. messageDescription.EnsureMessagePartsPassBasicValidation(fields); try { foreach (var pair in fields) { messageDictionary[pair.Key] = pair.Value; } } catch (ArgumentException ex) { throw ErrorUtilities.Wrap(ex, MessagingStrings.ErrorDeserializingMessage, this.messageType.Name); } messageDictionary.Message.EnsureValidMessage(); var originalPayloadMessage = messageDictionary.Message as IMessageOriginalPayload; if (originalPayloadMessage != null) { originalPayloadMessage.OriginalPayload = fields; } }
public MessageDictionary GetDictionarySingle(string id) { #region Parameters var parameters = new List <OracleParameter> { new OracleParameter { ParameterName = "MsgId", OracleDbType = OracleDbType.Varchar2, Value = id } }; #endregion var masterDataTable = db.GetData("Select * from MSG_DICTIONARY t where word_id = :MsgId", parameters); if (masterDataTable == null) { return(null); } if (masterDataTable.Rows.Count == 0) { return(null); } DataRow row = masterDataTable.Rows[0]; var obj = new MessageDictionary(); obj.Order = row["word_order"] == DBNull.Value ? -1 : int.Parse(row["word_order"].ToString()); obj.Id = row["word_id"] == DBNull.Value ? string.Empty : row["word_id"].ToString(); obj.Name = row["word_name"] == DBNull.Value ? string.Empty : row["word_name"].ToString(); obj.ObjectMember = row["obj_member"] == DBNull.Value ? null : row["obj_member"].ToString().Split('|'); return(obj); }
/// <summary> /// Returns a human-friendly string describing the message and all serializable properties. /// </summary> /// <param name="channel">The channel that will carry this message.</param> /// <returns> /// The string representation of this object. /// </returns> internal virtual string ToString(Channel channel) { Contract.Requires <ArgumentNullException>(channel != null); StringBuilder builder = new StringBuilder(); builder.AppendFormat(CultureInfo.InvariantCulture, "{0} message", GetType().Name); if (this.recipient != null) { builder.AppendFormat(CultureInfo.InvariantCulture, " as {0} to {1}", this.recipient.AllowedMethods, this.recipient.Location); } builder.AppendLine(); MessageDictionary dictionary = channel.MessageDescriptions.GetAccessor(this); foreach (var pair in dictionary) { string value = pair.Value; if (pair.Key == "oauth_signature" && !LowSecurityMode) { value = "xxxxxxxxxxxxx (not shown)"; } builder.Append('\t'); builder.Append(pair.Key); builder.Append(": "); builder.AppendLine(value); } return(builder.ToString()); }
private void Connector_PrivateMessageEvent(object sender, MessageDictionary e) { bool found = false; Sender s = (Sender)Enum.Parse(typeof(Sender), e[MesKeyStr.Sender]); string targetUserID = s == Sender.others ? e[MesKeyStr.UserID] : e[MesKeyStr.TargetUserID]; foreach (ChatWindow cw in privateWindows) { if (cw.TargetUser.UserID.Equals(targetUserID)) { found = true; Application.Current.Dispatcher.Invoke(() => { cw.MessageArrive(e); }); } } if (found == false) { User target = new User(e[MesKeyStr.UserID], e[MesKeyStr.NickName]); ChatWindow chatWindow = null; Application.Current.Dispatcher.Invoke(() => { chatWindow = new ChatWindow(target); chatWindow.ManualCloseEvent += ChatWindow_ManualCloseEvent; chatWindow.Show(); Application.Current.Dispatcher.Invoke(() => { chatWindow.MessageArrive(e); }); }); privateWindows.Add(chatWindow); } }
private void Connector_GroupMessageEvent(object sender, MessageDictionary e) { Application.Current.Dispatcher.Invoke(() => { GroupChatWindow.MessageArrive(e); }); }
public bool SendServerClosingMessage(Socket socket) { MessageDictionary messageD = new MessageDictionary(); messageD.Add(MesKeyStr.CommandType, CommandType.ServerDisconnect.ToString()); return(Send(socket, messageD.ToString())); }
internal static void Serialize(MessageDictionary messageDictionary, XmlDictionaryWriter writer) { Requires.NotNull(messageDictionary, "messageDictionary"); Requires.NotNull(writer, "writer"); writer.WriteStartElement("root"); writer.WriteAttributeString("type", "object"); foreach (var pair in messageDictionary) { bool include = false; string type = "string"; MessagePart partDescription; if (messageDictionary.Description.Mapping.TryGetValue(pair.Key, out partDescription)) { Contract.Assume(partDescription != null); if (partDescription.IsRequired || partDescription.IsNondefaultValueSet(messageDictionary.Message)) { include = true; if (IsNumeric(partDescription.MemberDeclaredType)) { type = "number"; } else if (partDescription.MemberDeclaredType.IsAssignableFrom(typeof(bool))) { type = "boolean"; } } } else { // This is extra data. We always write it out. include = true; } if (include) { writer.WriteStartElement(pair.Key); writer.WriteAttributeString("type", type); writer.WriteString(pair.Value); writer.WriteEndElement(); } } writer.WriteEndElement(); }
internal void ToString(XmlDictionaryWriter writer) { if (this.IsDisposed) { throw TraceUtility.ThrowHelperError(this.CreateMessageDisposedException(), this); } if (this.Version.Envelope != EnvelopeVersion.None) { this.WriteStartEnvelope(writer); this.WriteStartHeaders(writer); MessageHeaders headers = this.Headers; for (int i = 0; i < headers.Count; i++) { headers.WriteHeader(i, writer); } writer.WriteEndElement(); MessageDictionary messageDictionary = XD.MessageDictionary; this.WriteStartBody(writer); } this.BodyToString(writer); if (this.Version.Envelope != EnvelopeVersion.None) { writer.WriteEndElement(); writer.WriteEndElement(); } }
private void Manager_MessageArrivedEvent(object sender, MessageDictionary e) { Application.Current.Dispatcher.Invoke(() => { if (messageListSP.Children.Count >= maxMesListLen) { messageListSP.Children.RemoveAt(0); } if ((CommandType)Enum.Parse(typeof(CommandType), e[MesKeyStr.CommandType]) == CommandType.GroupMessage) { e.Add(MesKeyStr.Remark, "群聊消息"); } else { e.Add(MesKeyStr.Remark, "私聊消息,目标:" + e[MesKeyStr.UserID]); } DisplayMethod displayMethod = DisplayMethod.OnlyRemark; if (displayStyle) { displayMethod = DisplayMethod.Both; } MessageUC messageUC = new MessageUC(e, displayMethod); messageListSP.Children.Add(messageUC); messageListSV.ScrollToEnd(); }); }
/// <summary> /// Calculates the signature for a given message. /// </summary> /// <param name="signedMessage">The message to sign or verify.</param> /// <param name="association">The association to use to sign the message.</param> /// <returns>The calculated signature of the method.</returns> protected string GetSignature(ITamperResistantOpenIdMessage signedMessage, Association association) { Requires.NotNull(signedMessage, "signedMessage"); Requires.That(!string.IsNullOrEmpty(signedMessage.SignedParameterOrder), "signedMessage", "SignedParameterOrder must not be null or empty."); Requires.NotNull(association, "association"); // Prepare the parts to sign, taking care to replace an openid.mode value // of check_authentication with its original id_res so the signature matches. MessageDictionary dictionary = this.Channel.MessageDescriptions.GetAccessor(signedMessage); var parametersToSign = from name in signedMessage.SignedParameterOrder.Split(',') let prefixedName = Protocol.V20.openid.Prefix + name select new KeyValuePair <string, string>(name, dictionary.GetValueOrThrow(prefixedName, signedMessage)); byte[] dataToSign = KeyValueFormEncoding.GetBytes(parametersToSign); string signature = Convert.ToBase64String(association.Sign(dataToSign)); if (Logger.Signatures.IsDebugEnabled) { Logger.Signatures.DebugFormat( "Signing these message parts: {0}{1}{0}Base64 representation of signed data: {2}{0}Signature: {3}", Environment.NewLine, parametersToSign.ToStringDeferred(), Convert.ToBase64String(dataToSign), signature); } return(signature); }
public void TestMessageHandler() { MockSender sender = new() { Id = 42 }; TestMessageHandler handler = new(); MockReceiver receiver = new(); MessageProcessor processor = new(); processor.SetMessageReceiver(receiver); MessageDictionary dic = MessageDictionary.GetInstance(); processor.RegisterMessageHandler(handler); dic.AddOutgoingMessage(Module, Type, p => { return(Tuple.Create(TestValue, 42)); }); var msg = dic.CreateMessage(Type); receiver.ReceiveMessage(new RawMessage(sender, msg.Serialize())); Assert.IsTrue(handler.Tcs.Task.Wait(100)); } }
public void ModeEncoding() { var request = new UIRequest(); MessageDictionary dictionary = this.MessageDescriptions.GetAccessor(request); Assert.AreEqual("popup", dictionary["mode"]); }
/// <summary> /// Returns a human-friendly string describing the message and all serializable properties. /// </summary> /// <returns>The string representation of this object.</returns> public override string ToString() { StringBuilder builder = new StringBuilder(); builder.AppendFormat(CultureInfo.InvariantCulture, "{0} message", GetType().Name); if (this.recipient != null) { builder.AppendFormat(CultureInfo.InvariantCulture, " as {0} to {1}", this.recipient.AllowedMethods, this.recipient.Location); } builder.AppendLine(); MessageDictionary dictionary = new MessageDictionary(this); foreach (var pair in dictionary) { string value = pair.Value; if (pair.Key == "oauth_signature" && !LowSecurityMode) { value = "xxxxxxxxxxxxx (not shown)"; } builder.Append('\t'); builder.Append(pair.Key); builder.Append(": "); builder.AppendLine(value); } return(builder.ToString()); }
public void TestCharacterMap() { var msgDict = new MessageDictionary(); msgDict.ProcessKey(Key.A); var msgs = msgDict.ProcessKey(Key.A); Assert.AreEqual("a => a.InputCharacter(a)", msgs.Single().ToString()); }
public void SpaceStillWorksAfterHittingShift() { var msgDict = new MessageDictionary(); msgDict.ProcessKey(Key.A); msgDict.ProcessKey(Key.LeftShift); var msgs = msgDict.ProcessKey(Key.Space); Assert.AreEqual("a => a.InputCharacter( )", msgs.Single().ToString()); }
private void MessageSorter(byte[] buffer, int start, int length, Socket clientSocket) { string content = Encoding.Default.GetString(buffer, 0, length); MessageDictionary messageD = new MessageDictionary(content); ShowMessage("从" + clientSocket.RemoteEndPoint.ToString() + "接收消息:" + content + "\n"); CommandType command = (CommandType)Enum.Parse(typeof(CommandType), messageD[MesKeyStr.CommandType]); switch (command) { case CommandType.Login: { LoginEvent?.Invoke(this, new LoginEventArgs() { UserID = messageD[MesKeyStr.UserID], PassWord = messageD[MesKeyStr.PassWord], ReceiveSocket = clientSocket }); break; } case CommandType.Logout: { LogoutEvent?.Invoke(this, new User(messageD[MesKeyStr.UserID], messageD[MesKeyStr.NickName])); break; } case CommandType.SignUp: { SignUpEvent?.Invoke(this, new SignUpEventArgs(clientSocket, messageD[MesKeyStr.NickName], messageD[MesKeyStr.PassWord])); break; } case CommandType.GroupMessage: { GroupMessageEvent?.Invoke(this, messageD); break; } case CommandType.PrivateMessage: { PrivateMessageEvent?.Invoke(this, messageD); break; } case CommandType.UserJoin: case CommandType.UserQuit: case CommandType.LoginResult: case CommandType.SignUpResult: case CommandType.ServerDisconnect: case CommandType.Remove: { ShowMessage("收到错误的消息类型!"); throw new Exception("收到错误的消息类型!"); } } }
public void MessagePartConvertibility() { var message = new MessageWithIdentifier(); var messageDescription = new MessageDescription(message.GetType(), new Version(1, 0)); var messageDictionary = new MessageDictionary(message, messageDescription, false); messageDictionary["Identifier"] = OpenId.OpenIdTestBase.IdentifierSelect; Assert.That(messageDictionary["Identifier"], Is.EqualTo(OpenId.OpenIdTestBase.IdentifierSelect)); }
public void TestAutoProcess() { var msgDict = new MessageDictionary(); Assert.AreEqual(InputMode.Normal, msgDict.InputMode); msgDict.ProcessKey(Key.I); Assert.AreEqual(InputMode.Insert, msgDict.InputMode); msgDict.ProcessKey(Key.Escape); Assert.AreEqual(InputMode.Normal, msgDict.InputMode); }
/// <summary> /// Gets the extensions on a message. /// </summary> /// <param name="message">The carrier of the extensions.</param> /// <param name="ignoreUnsigned">If set to <c>true</c> only signed extensions will be available.</param> /// <param name="extensionFilter">A optional filter that takes an extension type URI and /// returns a value indicating whether that extension should be deserialized and /// returned in the sequence. May be null.</param> /// <returns>A sequence of extensions in the message.</returns> private IEnumerable <IOpenIdMessageExtension> GetExtensions(IProtocolMessageWithExtensions message, bool ignoreUnsigned, Func <string, bool> extensionFilter) { bool isAtProvider = message is SignedResponseRequest; // We have a helper class that will do all the heavy-lifting of organizing // all the extensions, their aliases, and their parameters. var extensionManager = ExtensionArgumentsManager.CreateIncomingExtensions(this.GetExtensionsDictionary(message, ignoreUnsigned)); foreach (string typeUri in extensionManager.GetExtensionTypeUris()) { // Our caller may have already obtained a signed version of this extension, // so skip it if they don't want this one. if (extensionFilter != null && !extensionFilter(typeUri)) { continue; } var extensionData = extensionManager.GetExtensionArguments(typeUri); // Initialize this particular extension. IOpenIdMessageExtension extension = this.ExtensionFactory.Create(typeUri, extensionData, message, isAtProvider); if (extension != null) { try { // Make sure the extension fulfills spec requirements before deserializing it. MessageDescription messageDescription = this.Channel.MessageDescriptions.Get(extension); messageDescription.EnsureMessagePartsPassBasicValidation(extensionData); // Deserialize the extension. MessageDictionary extensionDictionary = messageDescription.GetDictionary(extension); foreach (var pair in extensionData) { extensionDictionary[pair.Key] = pair.Value; } // Give extensions that require custom serialization a chance to do their work. var customSerializingExtension = extension as IMessageWithEvents; if (customSerializingExtension != null) { customSerializingExtension.OnReceiving(); } } catch (ProtocolException ex) { Logger.OpenId.ErrorFormat(OpenIdStrings.BadExtension, extension.GetType(), ex); extension = null; } if (extension != null) { yield return(extension); } } else { Logger.OpenId.DebugFormat("Extension with type URI '{0}' ignored because it is not a recognized extension.", typeUri); } } }
/// <summary> /// Verifies that all required message parts are initialized to values /// prior to sending the message to a remote party. /// </summary> /// <param name="message">The message to verify.</param> /// <exception cref="ProtocolException"> /// Thrown when any required message part does not have a value. /// </exception> private static void EnsureValidMessageParts(IProtocolMessage message) { Debug.Assert(message != null, "message == null"); MessageDictionary dictionary = new MessageDictionary(message); MessageDescription description = MessageDescription.Get(message.GetType(), message.Version); description.EnsureMessagePartsPassBasicValidation(dictionary); }
public void MessagePartConvertibility() { var message = new MessageWithRealm(); var messageDescription = new MessageDescription(message.GetType(), new Version(1, 0)); var messageDictionary = new MessageDictionary(message, messageDescription, false); messageDictionary["Realm"] = OpenId.OpenIdTestBase.RPRealmUri.AbsoluteUri; Assert.That(messageDictionary["Realm"], Is.EqualTo(OpenId.OpenIdTestBase.RPRealmUri.AbsoluteUri)); }
public Task <MessageProtections?> ProcessOutgoingMessageAsync(IProtocolMessage message, CancellationToken cancellationToken) { var extendableMessage = message as IProtocolMessageWithExtensions; if (extendableMessage != null) { Protocol protocol = Protocol.Lookup(message.Version); MessageDictionary baseMessageDictionary = this.Channel.MessageDescriptions.GetAccessor(message); // We have a helper class that will do all the heavy-lifting of organizing // all the extensions, their aliases, and their parameters. var extensionManager = ExtensionArgumentsManager.CreateOutgoingExtensions(protocol); foreach (IExtensionMessage protocolExtension in extendableMessage.Extensions) { var extension = protocolExtension as IOpenIdMessageExtension; if (extension != null) { Reporting.RecordFeatureUse(protocolExtension); // Give extensions that require custom serialization a chance to do their work. var customSerializingExtension = extension as IMessageWithEvents; if (customSerializingExtension != null) { customSerializingExtension.OnSending(); } // OpenID 2.0 Section 12 forbids two extensions with the same TypeURI in the same message. ErrorUtilities.VerifyProtocol(!extensionManager.ContainsExtension(extension.TypeUri), OpenIdStrings.ExtensionAlreadyAddedWithSameTypeURI, extension.TypeUri); // Ensure that we're sending out a valid extension. var extensionDescription = this.Channel.MessageDescriptions.Get(extension); var extensionDictionary = extensionDescription.GetDictionary(extension).Serialize(); extensionDescription.EnsureMessagePartsPassBasicValidation(extensionDictionary); // Add the extension to the outgoing message payload. extensionManager.AddExtensionArguments(extension.TypeUri, extensionDictionary); } else { Logger.OpenId.WarnFormat("Unexpected extension type {0} did not implement {1}.", protocolExtension.GetType(), typeof(IOpenIdMessageExtension).Name); } } // We use a cheap trick (for now at least) to determine whether the 'openid.' prefix // belongs on the parameters by just looking at what other parameters do. // Technically, direct message responses from Provider to Relying Party are the only // messages that leave off the 'openid.' prefix. bool includeOpenIdPrefix = baseMessageDictionary.Keys.Any(key => key.StartsWith(protocol.openid.Prefix, StringComparison.Ordinal)); // Add the extension parameters to the base message for transmission. baseMessageDictionary.AddExtraParameters(extensionManager.GetArgumentsToSend(includeOpenIdPrefix)); return(NoneTask); } return(NullTask); }
protected virtual void OnWriteStartHeaders(XmlDictionaryWriter writer) { EnvelopeVersion envelope = this.Version.Envelope; if (envelope != EnvelopeVersion.None) { MessageDictionary messageDictionary = XD.MessageDictionary; writer.WriteStartElement(messageDictionary.Prefix.Value, messageDictionary.Header, envelope.DictionaryNamespace); } }
public void TestRightShiftAliasing() { var msgDict = new MessageDictionary(); var msgs = msgDict.ProcessKey(Key.A); Assert.AreEqual(2, msgs.Count(), "Failed to enter insert mode"); //A should send to Messages: ChangeMode and InsertAfter msgDict.ProcessKey(Key.RightShift); msgs = msgDict.ProcessKey(Key.A); Assert.AreEqual("a => a.InputCharacter(A)", msgs.Single().ToString(), "Did not process RightShift"); }
public override void AddToDictionary(MessageDictionary dictionary, MethodInfo info) { var keyList = _parser.Parse(_keyList); if (keyList.Count() != _charList.Length) throw new Exception("CharacterMapAttribute requires the key list and the character list to be of identical length"); keyList.Do( (i, key) => dictionary.AddKey(_mode, key, BuildMessage(info, _charList[i]))); }
public override void AddToDictionary(MessageDictionary dictionary, MethodInfo info) { if (_keys.Length != _charList.Length * _width) throw new Exception("CharacterMapAttribute requires the key list and the character list to be of proportional length"); Enumerable.Range(0, _charList.Length).Do( i => dictionary.AddKeys(_mode, _keys.Skip(i*2).Take(2), BuildMessage(info, _charList[i]))); }
/// <summary> /// Reads XML/JSON into a message dictionary. /// </summary> /// <param name="messageDictionary">The message to deserialize into.</param> /// <param name="reader">The XML/JSON to read into the message.</param> /// <exception cref="ProtocolException">Thrown when protocol rules are broken by the incoming message.</exception> /// <remarks> /// Use <see cref="System.Runtime.Serialization.Json.JsonReaderWriterFactory.CreateJsonReader(System.IO.Stream, System.Xml.XmlDictionaryReaderQuotas)"/> /// to create the <see cref="XmlDictionaryReader"/> instance capable of reading JSON. /// </remarks> internal static void Deserialize(MessageDictionary messageDictionary, XmlDictionaryReader reader) { Requires.NotNull(messageDictionary, "messageDictionary"); Requires.NotNull(reader, "reader"); DeserializeJsonAsFlatDictionary(messageDictionary, reader); // Make sure all the required parts are present and valid. messageDictionary.Description.EnsureMessagePartsPassBasicValidation(messageDictionary); messageDictionary.Message.EnsureValidMessage(); }
public override void AddToDictionary(MessageDictionary dictionary, MethodInfo info) { var keyList = _parser.Parse(_keyList); if (keyList.Count() != _charList.Length * 2) throw new Exception("CharacterMapAttribute requires the key list and the character list to be of identical length"); Enumerable.Range(0, _charList.Length).Do( i => dictionary.AddKeys(_mode, keyList.Skip(i*2).Take(2), BuildMessage(info, _charList[i]))); }
private static void AddMapping(MessageDictionary dictionary, string mapping) { var elems = mapping.Split(' '); var mode = GetMode(elems.First()); var mapCount = (elems.Count() - 1)/2; Enumerable.Range(0, mapCount).Do(i => { var mappings = elems.Skip(i*2 + 1).Take(2); dictionary.AddString(mode, mappings.First(), GetModeChangeMessage(mappings.Skip(1).First())); }); }
/// <summary> /// Copies some extra parameters into a message. /// </summary> /// <param name="messageDictionary">The message to copy the extra data into.</param> /// <param name="extraParameters">The extra data to copy into the message. May be null to do nothing.</param> internal static void AddExtraParameters(this MessageDictionary messageDictionary, IDictionary <string, string> extraParameters) { Contract.Requires <ArgumentNullException>(messageDictionary != null); if (extraParameters != null) { foreach (var pair in extraParameters) { messageDictionary.Add(pair); } } }
public Task InitializeModules() { foreach (var m in Modules.Values) { log.Debug("initializing " + m.Name); m.Logic.Initialize(); } log.Debug("all loaded modules initialized"); MessageDictionary.GetInstance().CreatePDF("messages.txt"); return(Task.CompletedTask); }
public bool SendMessage(Socket socket, MessageDictionary chatMessage) { if (Send(socket, chatMessage.ToString())) { return(true); } else { ShowMessage("发送信息失败"); return(false); } }
/// <summary> /// Copies some extra parameters into a message. /// </summary> /// <param name="message">The message to copy the extra data into.</param> /// <param name="extraParameters">The extra data to copy into the message. May be null to do nothing.</param> internal static void AddExtraParameters(this IMessage message, IDictionary <string, string> extraParameters) { ErrorUtilities.VerifyArgumentNotNull(message, "message"); if (extraParameters != null) { MessageDictionary messageDictionary = new MessageDictionary(message); foreach (var pair in extraParameters) { messageDictionary.Add(pair); } } }
/// <summary> /// Reads name=value pairs into a message. /// </summary> /// <param name="fields">The name=value pairs that were read in from the transport.</param> /// <param name="messageDictionary">The message to deserialize into.</param> /// <exception cref="ProtocolException">Thrown when protocol rules are broken by the incoming message.</exception> internal void Deserialize(IDictionary<string, string> fields, MessageDictionary messageDictionary) { Contract.Requires(fields != null); Contract.Requires(messageDictionary != null); ErrorUtilities.VerifyArgumentNotNull(fields, "fields"); ErrorUtilities.VerifyArgumentNotNull(messageDictionary, "messageDictionary"); var messageDescription = messageDictionary.Description; // Before we deserialize the message, make sure all the required parts are present. messageDescription.EnsureMessagePartsPassBasicValidation(fields); try { foreach (var pair in fields) { messageDictionary[pair.Key] = pair.Value; } } catch (ArgumentException ex) { throw ErrorUtilities.Wrap(ex, MessagingStrings.ErrorDeserializingMessage, this.messageType.Name); } messageDictionary.Message.EnsureValidMessage(); }
public void TestCommand() { var msgDict = new MessageDictionary(); msgDict.ProcessKey(Key.LeftShift); msgDict.ProcessKey(Key.OemSemicolon); }
internal IDictionary<string, string> Serialize(MessageDictionary messageDictionary) { Requires.NotNull(messageDictionary, "messageDictionary"); Contract.Ensures(Contract.Result<IDictionary<string, string>>() != null); // Rather than hand back the whole message dictionary (which // includes keys with blank values), create a new dictionary // that only has required keys, and optional keys whose // values are not empty (or default). var result = new Dictionary<string, string>(); foreach (var pair in messageDictionary) { MessagePart partDescription; if (messageDictionary.Description.Mapping.TryGetValue(pair.Key, out partDescription)) { Contract.Assume(partDescription != null); if (partDescription.IsRequired || partDescription.IsNondefaultValueSet(messageDictionary.Message)) { result.Add(pair.Key, pair.Value); } } else { // This is extra data. We always write it out. result.Add(pair.Key, pair.Value); } } return result; }
public void TestIAliasMap() { var msgDict = new MessageDictionary(); var msgs = msgDict.ProcessKey(Key.RightShift); Assert.AreEqual("a => a.SetAlias(LeftShift)", msgs.Single().ToString()); }
public void TestModeChange() { var msgDict = new MessageDictionary(); msgDict.ChangeMode(InputMode.Normal); }
/// <summary> /// Reads XML/JSON into a message dictionary. /// </summary> /// <param name="messageDictionary">The message to deserialize into.</param> /// <param name="reader">The XML/JSON to read into the message.</param> /// <exception cref="ProtocolException">Thrown when protocol rules are broken by the incoming message.</exception> /// <remarks> /// Use <see cref="System.Runtime.Serialization.Json.JsonReaderWriterFactory.CreateJsonReader(System.IO.Stream, System.Xml.XmlDictionaryReaderQuotas)"/> /// to create the <see cref="XmlDictionaryReader"/> instance capable of reading JSON. /// </remarks> internal static void Deserialize(MessageDictionary messageDictionary, XmlDictionaryReader reader) { Contract.Requires<ArgumentNullException>(messageDictionary != null); Contract.Requires<ArgumentNullException>(reader != null); DeserializeJsonAsFlatDictionary(messageDictionary, reader); // Make sure all the required parts are present and valid. messageDictionary.Description.EnsureMessagePartsPassBasicValidation(messageDictionary); messageDictionary.Message.EnsureValidMessage(); }
public MessageDictionaryEnumerator(MessageDictionary md, IDictionary hashtable) { _md = md; if (hashtable != null) { _enumHash = hashtable.GetEnumerator(); } else { _enumHash = null; } }
/// <summary> /// Uri-escapes the names and values in a dictionary per OAuth 1.0 section 5.1. /// </summary> /// <param name="message">The message with data to encode.</param> /// <returns>A dictionary of name-value pairs with their strings encoded.</returns> internal static IDictionary<string, string> GetUriEscapedParameters(MessageDictionary message) { var encodedDictionary = new Dictionary<string, string>(); UriEscapeParameters(message, encodedDictionary); return encodedDictionary; }
public abstract void AddToDictionary(MessageDictionary dictionary, MethodInfo info);
public override void AddToDictionary(MessageDictionary dictionary, MethodInfo info) { _mappings.Do(mapping => AddMapping(dictionary, mapping)); }
public override void AddToDictionary(MessageDictionary dictionary, MethodInfo info) { dictionary.AddString(_mode, _keyList, BuildMessage(info)); }
internal static string ConstructSignatureBaseString(ITamperResistantOAuthMessage message, MessageDictionary messageDictionary) { Requires.NotNull(message, "message"); Requires.NotNullOrEmpty(message.HttpMethod, "message.HttpMethod"); Requires.NotNull(messageDictionary, "messageDictionary"); ErrorUtilities.VerifyInternal(messageDictionary.Message == message, "Message references are not equal."); List<string> signatureBaseStringElements = new List<string>(3); signatureBaseStringElements.Add(message.HttpMethod.ToUpperInvariant()); // For multipart POST messages, only include the message parts that are NOT // in the POST entity (those parts that may appear in an OAuth authorization header). var encodedDictionary = new Dictionary<string, string>(); IEnumerable<KeyValuePair<string, string>> partsToInclude = Enumerable.Empty<KeyValuePair<string, string>>(); var binaryMessage = message as IMessageWithBinaryData; if (binaryMessage != null && binaryMessage.SendAsMultipart) { HttpDeliveryMethods authHeaderInUseFlags = HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest; ErrorUtilities.VerifyProtocol((binaryMessage.HttpMethods & authHeaderInUseFlags) == authHeaderInUseFlags, OAuthStrings.MultipartPostMustBeUsedWithAuthHeader); // Include the declared keys in the signature as those will be signable. // Cache in local variable to avoid recalculating DeclaredKeys in the delegate. ICollection<string> declaredKeys = messageDictionary.DeclaredKeys; partsToInclude = messageDictionary.Where(pair => declaredKeys.Contains(pair.Key)); } else { partsToInclude = messageDictionary; } // If this message was deserialized, include only those explicitly included message parts (excludes defaulted values) // in the signature. var originalPayloadMessage = (IMessageOriginalPayload)message; if (originalPayloadMessage.OriginalPayload != null) { partsToInclude = partsToInclude.Where(pair => originalPayloadMessage.OriginalPayload.ContainsKey(pair.Key)); } foreach (var pair in OAuthChannel.GetUriEscapedParameters(partsToInclude)) { encodedDictionary[pair.Key] = pair.Value; } // An incoming message will already have included the query and form parameters // in the message dictionary, but an outgoing message COULD have SOME parameters // in the query that are not in the message dictionary because they were included // in the receiving endpoint (the original URL). // In an outgoing message, the POST entity can only contain parameters if they were // in the message dictionary, so no need to pull out any parameters from there. if (message.Recipient.Query != null) { NameValueCollection nvc = HttpUtility.ParseQueryString(message.Recipient.Query); foreach (string key in nvc) { string escapedKey = MessagingUtilities.EscapeUriDataStringRfc3986(key); string escapedValue = MessagingUtilities.EscapeUriDataStringRfc3986(nvc[key]); string existingValue; if (!encodedDictionary.TryGetValue(escapedKey, out existingValue)) { encodedDictionary.Add(escapedKey, escapedValue); } else { ErrorUtilities.VerifyInternal(escapedValue == existingValue, "Somehow we have conflicting values for the '{0}' parameter.", escapedKey); } } } encodedDictionary.Remove("oauth_signature"); UriBuilder endpoint = new UriBuilder(message.Recipient); endpoint.Query = null; endpoint.Fragment = null; signatureBaseStringElements.Add(endpoint.Uri.AbsoluteUri); var sortedKeyValueList = new List<KeyValuePair<string, string>>(encodedDictionary); sortedKeyValueList.Sort(SignatureBaseStringParameterComparer); StringBuilder paramBuilder = new StringBuilder(); foreach (var pair in sortedKeyValueList) { if (paramBuilder.Length > 0) { paramBuilder.Append("&"); } paramBuilder.Append(pair.Key); paramBuilder.Append('='); paramBuilder.Append(pair.Value); } signatureBaseStringElements.Add(paramBuilder.ToString()); StringBuilder signatureBaseString = new StringBuilder(); foreach (string element in signatureBaseStringElements) { if (signatureBaseString.Length > 0) { signatureBaseString.Append("&"); } signatureBaseString.Append(MessagingUtilities.EscapeUriDataStringRfc3986(element)); } Logger.Bindings.DebugFormat("Constructed signature base string: {0}", signatureBaseString); return signatureBaseString.ToString(); }
/// <summary> /// Constructs the OAuth Signature Base String and returns the result. /// </summary> /// <param name="message">The message.</param> /// <param name="messageDictionary">The message to derive the signature base string from.</param> /// <returns>The signature base string.</returns> /// <remarks> /// This method implements OAuth 1.0 section 9.1. /// </remarks> internal static string ConstructSignatureBaseString(ITamperResistantOAuthMessage message, MessageDictionary messageDictionary) { Contract.Requires(message != null); Contract.Requires(messageDictionary != null); Contract.Requires(messageDictionary.Message == message); if (String.IsNullOrEmpty(message.HttpMethod)) { throw new ArgumentException( string.Format( CultureInfo.CurrentCulture, MessagingStrings.ArgumentPropertyMissing, typeof(ITamperResistantOAuthMessage).Name, "HttpMethod"), "message"); } List<string> signatureBaseStringElements = new List<string>(3); signatureBaseStringElements.Add(message.HttpMethod.ToUpperInvariant()); UriBuilder endpoint = new UriBuilder(message.Recipient); endpoint.Query = null; endpoint.Fragment = null; signatureBaseStringElements.Add(endpoint.Uri.AbsoluteUri); var encodedDictionary = OAuthChannel.GetUriEscapedParameters(messageDictionary); encodedDictionary.Remove("oauth_signature"); var sortedKeyValueList = new List<KeyValuePair<string, string>>(encodedDictionary); sortedKeyValueList.Sort(SignatureBaseStringParameterComparer); StringBuilder paramBuilder = new StringBuilder(); foreach (var pair in sortedKeyValueList) { if (paramBuilder.Length > 0) { paramBuilder.Append("&"); } paramBuilder.Append(pair.Key); paramBuilder.Append('='); paramBuilder.Append(pair.Value); } signatureBaseStringElements.Add(paramBuilder.ToString()); StringBuilder signatureBaseString = new StringBuilder(); foreach (string element in signatureBaseStringElements) { if (signatureBaseString.Length > 0) { signatureBaseString.Append("&"); } signatureBaseString.Append(Uri.EscapeDataString(element)); } Logger.Bindings.DebugFormat("Constructed signature base string: {0}", signatureBaseString); return signatureBaseString.ToString(); }
/// <summary> /// Constructs the OAuth Signature Base String and returns the result. /// </summary> /// <param name="message">The message.</param> /// <param name="messageDictionary">The message to derive the signature base string from.</param> /// <returns>The signature base string.</returns> /// <remarks> /// This method implements OAuth 1.0 section 9.1. /// </remarks> internal static string ConstructSignatureBaseString(ITamperResistantOAuthMessage message, MessageDictionary messageDictionary) { Contract.Requires(message != null); Contract.Requires(messageDictionary != null); Contract.Requires(messageDictionary.Message == message); if (String.IsNullOrEmpty(message.HttpMethod)) { throw new ArgumentException( string.Format( CultureInfo.CurrentCulture, MessagingStrings.ArgumentPropertyMissing, typeof(ITamperResistantOAuthMessage).Name, "HttpMethod"), "message"); } List<string> signatureBaseStringElements = new List<string>(3); signatureBaseStringElements.Add(message.HttpMethod.ToUpperInvariant()); var encodedDictionary = OAuthChannel.GetUriEscapedParameters(messageDictionary); // An incoming message will already have included the query and form parameters // in the message dictionary, but an outgoing message COULD have SOME parameters // in the query that are not in the message dictionary because they were included // in the receiving endpoint (the original URL). // In an outgoing message, the POST entity can only contain parameters if they were // in the message dictionary, so no need to pull out any parameters from there. if (message.Recipient.Query != null) { NameValueCollection nvc = HttpUtility.ParseQueryString(message.Recipient.Query); foreach (string key in nvc) { string escapedKey = MessagingUtilities.EscapeUriDataStringRfc3986(key); string escapedValue = MessagingUtilities.EscapeUriDataStringRfc3986(nvc[key]); string existingValue; if (!encodedDictionary.TryGetValue(escapedKey, out existingValue)) { encodedDictionary.Add(escapedKey, escapedValue); } else { ErrorUtilities.VerifyInternal(escapedValue == existingValue, "Somehow we have conflicting values for the '{0}' parameter.", escapedKey); } } } encodedDictionary.Remove("oauth_signature"); UriBuilder endpoint = new UriBuilder(message.Recipient); endpoint.Query = null; endpoint.Fragment = null; signatureBaseStringElements.Add(endpoint.Uri.AbsoluteUri); var sortedKeyValueList = new List<KeyValuePair<string, string>>(encodedDictionary); sortedKeyValueList.Sort(SignatureBaseStringParameterComparer); StringBuilder paramBuilder = new StringBuilder(); foreach (var pair in sortedKeyValueList) { if (paramBuilder.Length > 0) { paramBuilder.Append("&"); } paramBuilder.Append(pair.Key); paramBuilder.Append('='); paramBuilder.Append(pair.Value); } signatureBaseStringElements.Add(paramBuilder.ToString()); StringBuilder signatureBaseString = new StringBuilder(); foreach (string element in signatureBaseStringElements) { if (signatureBaseString.Length > 0) { signatureBaseString.Append("&"); } signatureBaseString.Append(MessagingUtilities.EscapeUriDataStringRfc3986(element)); } Logger.Bindings.DebugFormat("Constructed signature base string: {0}", signatureBaseString); return signatureBaseString.ToString(); }