From 4e83c2d321f57cac5e5605097faf15bf1b46efa5 Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Mon, 1 Sep 2008 16:06:01 -0700 Subject: [PATCH] Lots of StyleCop changes. --- src/DotNetOAuth.Test/MessageSerializerTest.cs | 15 +- src/DotNetOAuth.Test/Mocks/TestMessage.cs | 28 ++- src/DotNetOAuth.Test/ServiceProviderTest.cs | 13 +- src/{ => DotNetOAuth.Test}/Settings.StyleCop | 6 +- src/DotNetOAuth/Consumer.cs | 8 +- src/DotNetOAuth/DictionaryXmlReader.cs | 336 +++++++------------------- src/DotNetOAuth/DictionaryXmlWriter.cs | 173 +++++++++++-- src/DotNetOAuth/DirectMessageChannel.cs | 18 +- src/DotNetOAuth/DotNetOAuth.csproj | 2 +- src/DotNetOAuth/IProtocolMessage.cs | 16 +- src/DotNetOAuth/IProtocolMessageRequest.cs | 12 +- src/DotNetOAuth/IndirectMessage.cs | 41 +++- src/DotNetOAuth/IndirectMessageEncoder.cs | 18 +- src/DotNetOAuth/Logger.cs | 2 +- src/DotNetOAuth/MessageScheme.cs | 11 +- src/DotNetOAuth/MessageTransport.cs | 10 +- src/DotNetOAuth/Protocol.cs | 66 ++++- src/DotNetOAuth/ProtocolException.cs | 38 ++- src/DotNetOAuth/ProtocolMessageSerializer.cs | 83 +++++-- src/DotNetOAuth/ServiceProvider.cs | 35 ++- src/DotNetOAuth/UriUtil.cs | 29 ++- src/Settings.StyleCop | 20 ++ 22 files changed, 620 insertions(+), 360 deletions(-) copy src/{ => DotNetOAuth.Test}/Settings.StyleCop (70%) rewrite src/DotNetOAuth/DictionaryXmlReader.cs (73%) diff --git a/src/DotNetOAuth.Test/MessageSerializerTest.cs b/src/DotNetOAuth.Test/MessageSerializerTest.cs index 9faebfe..24b7c9c 100644 --- a/src/DotNetOAuth.Test/MessageSerializerTest.cs +++ b/src/DotNetOAuth.Test/MessageSerializerTest.cs @@ -1,8 +1,17 @@ -using System; -using System.Collections.Generic; -using Microsoft.VisualStudio.TestTools.UnitTesting; +//----------------------------------------------------------------------- +// +// Copyright (c) Andrew Arnott. All rights reserved. +// +//----------------------------------------------------------------------- namespace DotNetOAuth.Test { + using System; + using System.Collections.Generic; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + /// + /// Tests for the class. + /// [TestClass()] public class MessageSerializerTest : TestBase { [TestMethod, ExpectedException(typeof(ArgumentNullException))] diff --git a/src/DotNetOAuth.Test/Mocks/TestMessage.cs b/src/DotNetOAuth.Test/Mocks/TestMessage.cs index 6fbac65..1fb58ad 100644 --- a/src/DotNetOAuth.Test/Mocks/TestMessage.cs +++ b/src/DotNetOAuth.Test/Mocks/TestMessage.cs @@ -1,9 +1,15 @@ -using System.Runtime.Serialization; -using System; +//----------------------------------------------------------------------- +// +// Copyright (c) Andrew Arnott. All rights reserved. +// +//----------------------------------------------------------------------- namespace DotNetOAuth.Test.Mocks { - [DataContract(Namespace = Protocol.DataContractNamespace)] - class TestMessage : IProtocolMessage { + using System; + using System.Runtime.Serialization; + + [DataContract(Namespace = Protocol.DataContractNamespaceV10)] + internal class TestMessage : IProtocolMessage { [DataMember(Name = "age", IsRequired = true)] public int Age { get; set; } [DataMember] @@ -15,14 +21,18 @@ namespace DotNetOAuth.Test.Mocks { #region IProtocolMessage Members - void IProtocolMessage.EnsureValidMessage() { - if (EmptyMember != null || Age < 0) { - throw new ProtocolException(); - } + Protocol IProtocolMessage.Protocol { + get { return Protocol.V10; } } MessageTransport IProtocolMessage.Transport { - get { return MessageTransport.Direct; } + get { return MessageTransport.Direct; } + } + + void IProtocolMessage.EnsureValidMessage() { + if (this.EmptyMember != null || this.Age < 0) { + throw new ProtocolException(); + } } #endregion diff --git a/src/DotNetOAuth.Test/ServiceProviderTest.cs b/src/DotNetOAuth.Test/ServiceProviderTest.cs index 38836f4..d4bced7 100644 --- a/src/DotNetOAuth.Test/ServiceProviderTest.cs +++ b/src/DotNetOAuth.Test/ServiceProviderTest.cs @@ -1,7 +1,16 @@ -using System; -using Microsoft.VisualStudio.TestTools.UnitTesting; +//----------------------------------------------------------------------- +// +// Copyright (c) Andrew Arnott. All rights reserved. +// +//----------------------------------------------------------------------- namespace DotNetOAuth.Test { + using System; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + /// + /// Tests for the class. + /// [TestClass] public class ServiceProviderTest : TestBase { /// diff --git a/src/Settings.StyleCop b/src/DotNetOAuth.Test/Settings.StyleCop similarity index 70% copy from src/Settings.StyleCop copy to src/DotNetOAuth.Test/Settings.StyleCop index 8831d6b..100fae5 100644 --- a/src/Settings.StyleCop +++ b/src/DotNetOAuth.Test/Settings.StyleCop @@ -1,8 +1,8 @@ - + - + False @@ -12,7 +12,7 @@ - + False diff --git a/src/DotNetOAuth/Consumer.cs b/src/DotNetOAuth/Consumer.cs index 7082727..1a73d5f 100644 --- a/src/DotNetOAuth/Consumer.cs +++ b/src/DotNetOAuth/Consumer.cs @@ -1,4 +1,10 @@ -namespace DotNetOAuth { +//----------------------------------------------------------------------- +// +// Copyright (c) Andrew Arnott. All rights reserved. +// +//----------------------------------------------------------------------- + +namespace DotNetOAuth { /// /// A website or application that uses OAuth to access the Service Provider on behalf of the User. /// diff --git a/src/DotNetOAuth/DictionaryXmlReader.cs b/src/DotNetOAuth/DictionaryXmlReader.cs dissimilarity index 73% index ff6ef51..6650d88 100644 --- a/src/DotNetOAuth/DictionaryXmlReader.cs +++ b/src/DotNetOAuth/DictionaryXmlReader.cs @@ -1,255 +1,81 @@ -namespace DotNetOAuth { - using System; - using System.Collections.Generic; - using System.Diagnostics; - using System.IO; - using System.Linq; - using System.Xml; - using System.Xml.Linq; - - /// - /// An XmlReader-looking object that actually reads from a dictionary. - /// - class DictionaryXmlReader { - /// - /// Creates an XmlReader that reads data out of a dictionary instead of XML. - /// - /// The name of the root XML element. - /// The dictionary to read data from. - /// The XmlReader that will read the data out of the given dictionary. - internal static XmlReader Create(XName rootElement, IDictionary fields) { - if (fields == null) { - throw new ArgumentNullException("fields"); - } - - return CreateRoundtripReader(rootElement, fields); - // The pseudo reader MAY be more efficient, but it's buggy. - //return CreatePseudoReader(rootElement, fields); - } - - private static XmlReader CreateRoundtripReader(XName rootElement, IDictionary fields) { - MemoryStream stream = new MemoryStream(); - XmlWriter writer = XmlWriter.Create(stream); - SerializeDictionaryToXml(writer, rootElement, fields); - writer.Flush(); - stream.Seek(0, SeekOrigin.Begin); - - // For debugging purposes. - StreamReader sr = new StreamReader(stream); - Trace.WriteLine(sr.ReadToEnd()); - stream.Seek(0, SeekOrigin.Begin); - - return XmlReader.Create(stream); - } - - static XmlReader CreatePseudoReader(XName rootElement, IDictionary fields) { - return new PseudoXmlReader(fields); - } - - static void SerializeDictionaryToXml(XmlWriter writer, XName rootElement, IDictionary fields) { - if (writer == null) { - throw new ArgumentNullException("writer"); - } - if (fields == null) { - throw new ArgumentNullException("fields"); - } - - writer.WriteStartElement(rootElement.LocalName, rootElement.NamespaceName); - // The elements must be serialized in alphabetical order so the DataContractSerializer will see them. - foreach (var pair in fields.OrderBy(pair => pair.Key, StringComparer.Ordinal)) { - writer.WriteStartElement(pair.Key, rootElement.NamespaceName); - writer.WriteValue(pair.Value); - writer.WriteEndElement(); - } - writer.WriteEndElement(); - } - - /// - /// Reads in a dictionary as if it were XML. - /// - class PseudoXmlReader : XmlReader { - IDictionary dictionary; - IEnumerator keyEnumerator; - bool isFinished; - int depth; - XmlNodeType currentNode = XmlNodeType.None; - - internal PseudoXmlReader(IDictionary dictionary) { - this.dictionary = dictionary; - keyEnumerator = dictionary.Keys.OrderBy(key => key, StringComparer.Ordinal).GetEnumerator(); - } - - public override int AttributeCount { - get { return 0; } - } - - public override bool IsEmptyElement { - get { - if (keyEnumerator.Current == null) { - return isFinished; - } - string value; - bool result = !dictionary.TryGetValue(keyEnumerator.Current, out value) || String.IsNullOrEmpty(value); - return result; - } - } - - public override string LocalName { - get { return keyEnumerator.Current; } - } - - public override bool MoveToElement() { - Trace.WriteLine("MoveToElement()"); - bool result = currentNode != XmlNodeType.Element && depth > 0; - currentNode = depth > 0 ? XmlNodeType.Element : XmlNodeType.None; - return result; - } - - public override bool MoveToNextAttribute() { - Trace.WriteLine("MoveToNextAttribute()"); - if (depth == 1 && currentNode != XmlNodeType.Attribute) { - currentNode = XmlNodeType.Attribute; - return true; - } else { - return false; - } - } - - public override string NamespaceURI { - get { - string result = depth == 1 && currentNode == XmlNodeType.Attribute ? - "http://www.w3.org/2000/xmlns/" : Protocol.DataContractNamespace; - return result; - } - } - - public override XmlNodeType NodeType { - get { return currentNode; } - } - - public override bool Read() { - Trace.WriteLine("Read()"); - if (isFinished) { - if (depth > 0) { - depth--; - } - return depth > 0; - } - switch (depth) { - case 0: // moving to root node - depth++; // -> 1 - currentNode = XmlNodeType.Element; - return true; - case 1: // moving to first content node - depth++; // -> 2 - isFinished = !keyEnumerator.MoveNext(); - currentNode = isFinished ? XmlNodeType.EndElement : XmlNodeType.Element; - return true; - case 2: // content node - switch (currentNode) { - case XmlNodeType.Element: - currentNode = XmlNodeType.Text; - return true; - case XmlNodeType.Text: - currentNode = XmlNodeType.EndElement; - return true; - case XmlNodeType.EndElement: - bool result = keyEnumerator.MoveNext(); - if (!result) { - isFinished = true; - depth--; - currentNode = XmlNodeType.EndElement; - } else { - currentNode = XmlNodeType.Element; - } - return true; - } - break; - } - throw new InvalidOperationException(); - } - - public override string Value { - get { return dictionary[keyEnumerator.Current]; } - } - - #region Unimplemented methods - - public override string BaseURI { - get { throw new NotImplementedException(); } - } - - public override void Close() { - throw new NotImplementedException(); - } - - public override int Depth { - get { - Trace.WriteLine("Depth: " + (depth - 1)); - return depth - 1; - } - } - - public override bool EOF { - get { throw new NotImplementedException(); } - } - - public override string GetAttribute(int i) { - throw new NotImplementedException(); - } - - public override string GetAttribute(string name, string namespaceURI) { - throw new NotImplementedException(); - } - - public override string GetAttribute(string name) { - throw new NotImplementedException(); - } - - public override bool HasValue { - get { throw new NotImplementedException(); } - } - - public override string LookupNamespace(string prefix) { - throw new NotImplementedException(); - } - - public override bool MoveToAttribute(string name, string ns) { - throw new NotImplementedException(); - } - - public override bool MoveToAttribute(string name) { - throw new NotImplementedException(); - } - - public override bool MoveToFirstAttribute() { - throw new NotImplementedException(); - } - - public override XmlNameTable NameTable { - get { throw new NotImplementedException(); } - } - - public override string Prefix { - get { throw new NotImplementedException(); } - } - - public override ReadState ReadState { - get { - Trace.WriteLine("ReadState"); - return ReadState.Interactive; - } - } - - public override bool ReadAttributeValue() { - throw new NotImplementedException(); - } - - public override void ResolveEntity() { - throw new NotImplementedException(); - } - - #endregion - } - } -} +//----------------------------------------------------------------------- +// +// Copyright (c) Andrew Arnott. All rights reserved. +// +//----------------------------------------------------------------------- + +namespace DotNetOAuth { + using System; + using System.Collections.Generic; + using System.Diagnostics; + using System.IO; + using System.Linq; + using System.Xml; + using System.Xml.Linq; + + /// + /// An XmlReader-looking object that actually reads from a dictionary. + /// + internal class DictionaryXmlReader { + /// + /// Creates an XmlReader that reads data out of a dictionary instead of XML. + /// + /// The name of the root XML element. + /// The dictionary to read data from. + /// The XmlReader that will read the data out of the given dictionary. + internal static XmlReader Create(XName rootElement, IDictionary fields) { + if (fields == null) { + throw new ArgumentNullException("fields"); + } + + return CreateRoundtripReader(rootElement, fields); + } + + /// + /// Creates an that will read values out of a dictionary. + /// + /// The surrounding root XML element to generate. + /// The dictionary to list values from. + /// The generated . + private static XmlReader CreateRoundtripReader(XName rootElement, IDictionary fields) { + MemoryStream stream = new MemoryStream(); + XmlWriter writer = XmlWriter.Create(stream); + SerializeDictionaryToXml(writer, rootElement, fields); + writer.Flush(); + stream.Seek(0, SeekOrigin.Begin); + + // For debugging purposes. + StreamReader sr = new StreamReader(stream); + Trace.WriteLine(sr.ReadToEnd()); + stream.Seek(0, SeekOrigin.Begin); + + return XmlReader.Create(stream); + } + + /// + /// Writes out the values in a dictionary as XML. + /// + /// The to write out the XML to. + /// The name of the root element to use to surround the dictionary values. + /// The dictionary with values to serialize. + private static void SerializeDictionaryToXml(XmlWriter writer, XName rootElement, IDictionary fields) { + if (writer == null) { + throw new ArgumentNullException("writer"); + } + if (fields == null) { + throw new ArgumentNullException("fields"); + } + + writer.WriteStartElement(rootElement.LocalName, rootElement.NamespaceName); + + // The elements must be serialized in alphabetical order so the DataContractSerializer will see them. + foreach (var pair in fields.OrderBy(pair => pair.Key, StringComparer.Ordinal)) { + writer.WriteStartElement(pair.Key, rootElement.NamespaceName); + writer.WriteValue(pair.Value); + writer.WriteEndElement(); + } + + writer.WriteEndElement(); + } + } +} diff --git a/src/DotNetOAuth/DictionaryXmlWriter.cs b/src/DotNetOAuth/DictionaryXmlWriter.cs index 0068f21..6d166fd 100644 --- a/src/DotNetOAuth/DictionaryXmlWriter.cs +++ b/src/DotNetOAuth/DictionaryXmlWriter.cs @@ -1,4 +1,10 @@ -namespace DotNetOAuth { +//----------------------------------------------------------------------- +// +// Copyright (c) Andrew Arnott. All rights reserved. +// +//----------------------------------------------------------------------- + +namespace DotNetOAuth { using System; using System.Collections.Generic; using System.Text; @@ -7,9 +13,9 @@ /// /// An XmlWriter-looking object that actually saves data to a dictionary. /// - class DictionaryXmlWriter { + internal class DictionaryXmlWriter { /// - /// Creates an that actually writes to an instance. + /// Creates an that actually writes to an IDictionary<string, string> instance. /// /// The dictionary to save the written XML to. /// The XmlWriter that will save data to the given dictionary. @@ -20,116 +26,243 @@ /// /// Writes out a dictionary as if it were XML. /// - class PseudoXmlWriter : XmlWriter { - IDictionary dictionary; - string key; - StringBuilder value = new StringBuilder(); + private class PseudoXmlWriter : XmlWriter { + /// + /// The dictionary to write values to. + /// + private IDictionary dictionary; + + /// + /// The key being written at the moment. + /// + private string key; + /// + /// The value being written out at the moment. + /// + private StringBuilder value = new StringBuilder(); + + /// + /// Initializes a new instance of the class. + /// + /// The dictionary that will be written to. internal PseudoXmlWriter(IDictionary dictionary) { + if (dictionary == null) { + throw new ArgumentNullException("dictionary"); + } + this.dictionary = dictionary; } + /// + /// Gets the spoofed state of the . + /// + public override WriteState WriteState { + get { return WriteState.Element; } + } + + /// + /// Prepares to write out a new key/value pair with the given key name to the dictionary. + /// + /// This parameter is ignored. + /// The key to store in the dictionary. + /// This parameter is ignored. public override void WriteStartElement(string prefix, string localName, string ns) { - key = localName; - value.Length = 0; + this.key = localName; + this.value.Length = 0; } + /// + /// Appends some text to the value that is to be stored in the dictionary. + /// + /// The text to append to the value. public override void WriteString(string text) { - if (!string.IsNullOrEmpty(key)) { - value.Append(text); + if (!string.IsNullOrEmpty(this.key)) { + this.value.Append(text); } } + /// + /// Writes out a completed key/value to the dictionary. + /// public override void WriteEndElement() { - if (key != null) { - dictionary[key] = value.ToString(); - key = null; - value.Length = 0; + if (this.key != null) { + this.dictionary[this.key] = this.value.ToString(); + this.key = null; + this.value.Length = 0; } } - public override WriteState WriteState { - get { return WriteState.Element; } - } - + /// + /// Clears the internal key/value building state. + /// + /// This parameter is ignored. + /// This parameter is ignored. + /// This parameter is ignored. public override void WriteStartAttribute(string prefix, string localName, string ns) { - key = null; + this.key = null; } + /// + /// This method does not do anything. + /// public override void WriteEndAttribute() { } + /// + /// This method does not do anything. + /// public override void Close() { } #region Unimplemented methods + /// + /// Throws . + /// public override void Flush() { throw new NotImplementedException(); } + /// + /// Throws . + /// + /// This parameter is ignored. + /// None, since an exception is always thrown. public override string LookupPrefix(string ns) { throw new NotImplementedException(); } + /// + /// Throws . + /// + /// This parameter is ignored. + /// This parameter is ignored. + /// This parameter is ignored. public override void WriteBase64(byte[] buffer, int index, int count) { throw new NotImplementedException(); } + /// + /// Throws . + /// + /// This parameter is ignored. public override void WriteCData(string text) { throw new NotImplementedException(); } + /// + /// Throws . + /// + /// This parameter is ignored. public override void WriteCharEntity(char ch) { throw new NotImplementedException(); } + /// + /// Throws . + /// + /// This parameter is ignored. + /// This parameter is ignored. + /// This parameter is ignored. public override void WriteChars(char[] buffer, int index, int count) { throw new NotImplementedException(); } + /// + /// Throws . + /// + /// This parameter is ignored. public override void WriteComment(string text) { throw new NotImplementedException(); } + /// + /// Throws . + /// + /// This parameter is ignored. + /// This parameter is ignored. + /// This parameter is ignored. + /// This parameter is ignored. public override void WriteDocType(string name, string pubid, string sysid, string subset) { throw new NotImplementedException(); } + /// + /// Throws . + /// public override void WriteEndDocument() { throw new NotImplementedException(); } + /// + /// Throws . + /// + /// This parameter is ignored. public override void WriteEntityRef(string name) { throw new NotImplementedException(); } + /// + /// Throws . + /// public override void WriteFullEndElement() { throw new NotImplementedException(); } + /// + /// Throws . + /// + /// This parameter is ignored. + /// This parameter is ignored. public override void WriteProcessingInstruction(string name, string text) { throw new NotImplementedException(); } + /// + /// Throws . + /// + /// This parameter is ignored. public override void WriteRaw(string data) { throw new NotImplementedException(); } + /// + /// Throws . + /// + /// This parameter is ignored. + /// This parameter is ignored. + /// This parameter is ignored. public override void WriteRaw(char[] buffer, int index, int count) { throw new NotImplementedException(); } + /// + /// Throws . + /// + /// This parameter is ignored. public override void WriteStartDocument(bool standalone) { throw new NotImplementedException(); } + /// + /// Throws . + /// public override void WriteStartDocument() { throw new NotImplementedException(); } + /// + /// Throws . + /// + /// This parameter is ignored. + /// This parameter is ignored. public override void WriteSurrogateCharEntity(char lowChar, char highChar) { throw new NotImplementedException(); } + /// + /// Throws . + /// + /// This parameter is ignored. public override void WriteWhitespace(string ws) { throw new NotImplementedException(); } diff --git a/src/DotNetOAuth/DirectMessageChannel.cs b/src/DotNetOAuth/DirectMessageChannel.cs index 2ece51d..147b7d5 100644 --- a/src/DotNetOAuth/DirectMessageChannel.cs +++ b/src/DotNetOAuth/DirectMessageChannel.cs @@ -1,6 +1,20 @@ -namespace DotNetOAuth { +//----------------------------------------------------------------------- +// +// Copyright (c) Andrew Arnott. All rights reserved. +// +//----------------------------------------------------------------------- + +namespace DotNetOAuth { + /// + /// Manages sending direct messages to a remote party and receiving responses. + /// internal class DirectMessageChannel { - public void Send(IProtocolMessage message) { + /// + /// Sends a direct message and returns the response. + /// + /// The message to send. + /// The message response. + public IProtocolMessage Send(IProtocolMessage message) { throw new System.NotImplementedException(); } } diff --git a/src/DotNetOAuth/DotNetOAuth.csproj b/src/DotNetOAuth/DotNetOAuth.csproj index d8de8ba..b510238 100644 --- a/src/DotNetOAuth/DotNetOAuth.csproj +++ b/src/DotNetOAuth/DotNetOAuth.csproj @@ -106,4 +106,4 @@ - + \ No newline at end of file diff --git a/src/DotNetOAuth/IProtocolMessage.cs b/src/DotNetOAuth/IProtocolMessage.cs index a1ba259..76512a4 100644 --- a/src/DotNetOAuth/IProtocolMessage.cs +++ b/src/DotNetOAuth/IProtocolMessage.cs @@ -1,4 +1,10 @@ -namespace DotNetOAuth { +//----------------------------------------------------------------------- +// +// Copyright (c) Andrew Arnott. All rights reserved. +// +//----------------------------------------------------------------------- + +namespace DotNetOAuth { using System; using System.Collections.Generic; using System.Text; @@ -7,11 +13,17 @@ /// The interface that classes must implement to be serialized/deserialized /// as OAuth messages. /// - interface IProtocolMessage { + internal interface IProtocolMessage { + /// + /// Gets the version of the protocol this message is prepared to implement. + /// + Protocol Protocol { get; } + /// /// Gets whether this is a direct or indirect message. /// MessageTransport Transport { get; } + /// /// Checks the message state for conformity to the protocol specification /// and throws an exception if the message is invalid. diff --git a/src/DotNetOAuth/IProtocolMessageRequest.cs b/src/DotNetOAuth/IProtocolMessageRequest.cs index 5d38b41..61dfddc 100644 --- a/src/DotNetOAuth/IProtocolMessageRequest.cs +++ b/src/DotNetOAuth/IProtocolMessageRequest.cs @@ -1,12 +1,18 @@ -namespace DotNetOAuth { +//----------------------------------------------------------------------- +// +// Copyright (c) Andrew Arnott. All rights reserved. +// +//----------------------------------------------------------------------- + +namespace DotNetOAuth { using System; /// /// Implemented by messages that are sent as requests. /// - interface IProtocolMessageRequest : IProtocolMessage { + internal interface IProtocolMessageRequest : IProtocolMessage { /// - /// The URL of the intended receiver of this message. + /// Gets or sets the URL of the intended receiver of this message. /// Uri Recipient { get; diff --git a/src/DotNetOAuth/IndirectMessage.cs b/src/DotNetOAuth/IndirectMessage.cs index 98295bf..638b4fd 100644 --- a/src/DotNetOAuth/IndirectMessage.cs +++ b/src/DotNetOAuth/IndirectMessage.cs @@ -1,16 +1,49 @@ -namespace DotNetOAuth { +//----------------------------------------------------------------------- +// +// Copyright (c) Andrew Arnott. All rights reserved. +// +//----------------------------------------------------------------------- + +namespace DotNetOAuth { using System.Net; + /// + /// A protocol message that passes between Consumer and Service Provider + /// via the user agent using a redirect or form POST submission. + /// + /// + /// An instance describes the HTTP response that must + /// be sent to the user agent to initiate the message transfer. + /// public class IndirectMessage { - public WebHeaderCollection Headers { get; set; } + /// + /// Gets the headers that must be included in the response to the user agent. + /// + /// + /// The headers in this collection are not meant to be a comprehensive list + /// of exactly what should be sent, but are meant to augment whatever headers + /// are generally included in a typical response. + /// + public WebHeaderCollection Headers { get; internal set; } - public byte[] Body { get; set; } + /// + /// Gets the body of the HTTP response. + /// + public byte[] Body { get; internal set; } - public HttpStatusCode Status { get; set; } + /// + /// Gets the HTTP status code to use in the HTTP response. + /// + public HttpStatusCode Status { get; internal set; } + /// + /// Gets or sets a reference to the actual protocol message that + /// is being sent via the user agent. + /// internal IProtocolMessage OriginalMessage { get; set; } /// + /// Automatically sends the appropriate response to the user agent. /// Requires a current HttpContext. /// public void Send() { diff --git a/src/DotNetOAuth/IndirectMessageEncoder.cs b/src/DotNetOAuth/IndirectMessageEncoder.cs index 3f55b71..fd9495e 100644 --- a/src/DotNetOAuth/IndirectMessageEncoder.cs +++ b/src/DotNetOAuth/IndirectMessageEncoder.cs @@ -1,6 +1,20 @@ -namespace DotNetOAuth { +//----------------------------------------------------------------------- +// +// Copyright (c) Andrew Arnott. All rights reserved. +// +//----------------------------------------------------------------------- + +namespace DotNetOAuth { + /// + /// A message encoder that prepares messages for transmittal. + /// internal class IndirectMessageEncoder { - public IndirectMessage Encode(IProtocolMessage message) { + /// + /// Prepares a protocol message for sending + /// + /// The indirect message to send. + /// The encoded message to send to the user agent. + internal IndirectMessage Encode(IProtocolMessage message) { throw new System.NotImplementedException(); } } diff --git a/src/DotNetOAuth/Logger.cs b/src/DotNetOAuth/Logger.cs index 8e7e2cb..844dcc6 100644 --- a/src/DotNetOAuth/Logger.cs +++ b/src/DotNetOAuth/Logger.cs @@ -7,8 +7,8 @@ namespace DotNetOAuth { using System; using System.Globalization; - using log4net.Core; using DotNetOAuth.Loggers; + using log4net.Core; /// /// A general logger for the entire YOURLIBNAME library. diff --git a/src/DotNetOAuth/MessageScheme.cs b/src/DotNetOAuth/MessageScheme.cs index 282194f..9336159 100644 --- a/src/DotNetOAuth/MessageScheme.cs +++ b/src/DotNetOAuth/MessageScheme.cs @@ -1,6 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Text; +//----------------------------------------------------------------------- +// +// Copyright (c) Andrew Arnott. All rights reserved. +// +//----------------------------------------------------------------------- namespace DotNetOAuth { /// @@ -14,14 +16,17 @@ namespace DotNetOAuth { /// In the HTTP Authorization header as defined in OAuth HTTP Authorization Scheme (OAuth HTTP Authorization Scheme). /// AuthorizationHeaderRequest, + /// /// As the HTTP POST request body with a content-type of application/x-www-form-urlencoded. /// PostRequest, + /// /// Added to the URLs in the query part (as defined by [RFC3986] (Berners-Lee, T., “Uniform Resource Identifiers (URI): Generic Syntax,” .) section 3). /// GetRequest, + /// /// Response parameters are sent by the Service Provider to return Tokens and other information to the Consumer in the HTTP response body. The parameter names and values are first encoded as per Parameter Encoding (Parameter Encoding), and concatenated with the ‘&’ character (ASCII code 38) as defined in [RFC3986] (Berners-Lee, T., “Uniform Resource Identifiers (URI): Generic Syntax,” .) Section 2.1. /// diff --git a/src/DotNetOAuth/MessageTransport.cs b/src/DotNetOAuth/MessageTransport.cs index 4097cba..5e08197 100644 --- a/src/DotNetOAuth/MessageTransport.cs +++ b/src/DotNetOAuth/MessageTransport.cs @@ -1,7 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +//----------------------------------------------------------------------- +// +// Copyright (c) Andrew Arnott. All rights reserved. +// +//----------------------------------------------------------------------- namespace DotNetOAuth { /// @@ -12,6 +13,7 @@ namespace DotNetOAuth { /// A message that is sent directly from the Consumer to the Service Provider, or vice versa. /// Direct, + /// /// A message that is sent from one party to another via a redirect in the user agent. /// diff --git a/src/DotNetOAuth/Protocol.cs b/src/DotNetOAuth/Protocol.cs index 0969a26..e96b4d2 100644 --- a/src/DotNetOAuth/Protocol.cs +++ b/src/DotNetOAuth/Protocol.cs @@ -1,9 +1,15 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +//----------------------------------------------------------------------- +// +// Copyright (c) Andrew Arnott. All rights reserved. +// +//----------------------------------------------------------------------- namespace DotNetOAuth { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + /// /// Constants used in the OAuth protocol. /// @@ -11,13 +17,33 @@ namespace DotNetOAuth { /// OAuth Protocol Parameter names and values are case sensitive. Each OAuth Protocol Parameters MUST NOT appear more than once per request, and are REQUIRED unless otherwise noted, /// per OAuth 1.0 section 5. /// - class Protocol { + internal class Protocol { + /// + /// Gets the default instance. + /// internal static readonly Protocol Default = V10; + + /// + /// The namespace to use for V1.0 of the protocol. + /// + internal const string DataContractNamespaceV10 = "http://oauth.net/core/1.0/"; + + /// + /// Gets the instance with values initialized for V1.0 of the protocol. + /// internal static readonly Protocol V10 = new Protocol { + dataContractNamespace = DataContractNamespaceV10, }; - internal const string DataContractNamespace = "http://oauth.net/core/1.0/"; - internal string ParameterPrefix = "oauth_"; + /// + /// The namespace to use for this version of the protocol. + /// + private string dataContractNamespace; + + /// + /// The prefix used for all key names in the protocol. + /// + private string parameterPrefix = "oauth_"; /// /// Strings that identify the various message schemes. @@ -25,8 +51,32 @@ namespace DotNetOAuth { /// /// These strings should be checked with case INsensitivity. /// - internal Dictionary MessageSchemes = new Dictionary { + private Dictionary messageSchemes = new Dictionary { { MessageScheme.AuthorizationHeaderRequest, "OAuth" }, }; + + /// + /// Gets the namespace to use for this version of the protocol. + /// + internal string DataContractNamespace { + get { return this.dataContractNamespace; } + } + + /// + /// Gets the prefix used for all key names in the protocol. + /// + internal string ParameterPrefix { + get { return this.parameterPrefix; } + } + + /// + /// Gets the strings that identify the various message schemes. + /// + /// + /// These strings should be checked with case INsensitivity. + /// + internal IDictionary MessageSchemes { + get { return this.messageSchemes; } + } } } diff --git a/src/DotNetOAuth/ProtocolException.cs b/src/DotNetOAuth/ProtocolException.cs index b2bd93f..4505c59 100644 --- a/src/DotNetOAuth/ProtocolException.cs +++ b/src/DotNetOAuth/ProtocolException.cs @@ -1,14 +1,42 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +//----------------------------------------------------------------------- +// +// Copyright (c) Andrew Arnott. All rights reserved. +// +//----------------------------------------------------------------------- namespace DotNetOAuth { - [global::System.Serializable] + using System; + + /// + /// An exception to represent errors in the local or remote implementation of the protocol. + /// + [Serializable] public class ProtocolException : Exception { + /// + /// Initializes a new instance of the class. + /// public ProtocolException() { } + + /// + /// Initializes a new instance of the class. + /// + /// A message describing the specific error the occurred or was detected. public ProtocolException(string message) : base(message) { } + + /// + /// Initializes a new instance of the class. + /// + /// A message describing the specific error the occurred or was detected. + /// The inner exception to include. public ProtocolException(string message, Exception inner) : base(message, inner) { } + + /// + /// Initializes a new instance of the class. + /// + /// The + /// that holds the serialized object data about the exception being thrown. + /// The System.Runtime.Serialization.StreamingContext + /// that contains contextual information about the source or destination. protected ProtocolException( System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) diff --git a/src/DotNetOAuth/ProtocolMessageSerializer.cs b/src/DotNetOAuth/ProtocolMessageSerializer.cs index b44aec6..153bd95 100644 --- a/src/DotNetOAuth/ProtocolMessageSerializer.cs +++ b/src/DotNetOAuth/ProtocolMessageSerializer.cs @@ -1,40 +1,88 @@ -using System; -using System.Collections.Generic; -using System.Runtime.Serialization; -using System.Xml; -using System.Xml.Linq; +//----------------------------------------------------------------------- +// +// Copyright (c) Andrew Arnott. All rights reserved. +// +//----------------------------------------------------------------------- namespace DotNetOAuth { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Runtime.Serialization; + using System.Xml; + using System.Xml.Linq; + /// /// Serializes/deserializes OAuth messages for/from transit. /// + /// The specific -derived type + /// that will be serialized and deserialized using this class. internal class ProtocolMessageSerializer where T : IProtocolMessage { - DataContractSerializer serializer; - readonly XName rootElement = XName.Get("root", Protocol.DataContractNamespace); + /// + /// Backing field for the property + /// + private XName rootElement; + + /// + /// The serializer that will be used as a reflection engine to extract + /// the OAuth message properties out of their containing + /// objects. + /// + private DataContractSerializer serializer; + /// + /// Initializes a new instance of the ProtocolMessageSerializer class. + /// internal ProtocolMessageSerializer() { - serializer = new DataContractSerializer(typeof(T), rootElement.LocalName, rootElement.NamespaceName); + this.serializer = new DataContractSerializer( + typeof(T), this.RootElement.LocalName, this.RootElement.NamespaceName); + } + + /// + /// Gets the XML element that is used to surround all the XML values from the dictionary. + /// + private XName RootElement { + get { + if (this.rootElement == null) { + DataContractAttribute attribute = typeof(T).GetCustomAttributes(typeof(DataContractAttribute), false).OfType().Single(); + this.rootElement = XName.Get("root", attribute.Namespace); + } + + return this.rootElement; + } } /// /// Reads the data from a message instance and returns a series of name=value pairs for the fields that must be included in the message. /// /// The message to be serialized. + /// The dictionary of values to send for the message. internal IDictionary Serialize(T message) { - if (message == null) throw new ArgumentNullException("message"); + if (message == null) { + throw new ArgumentNullException("message"); + } var fields = new Dictionary(StringComparer.Ordinal); - Serialize(fields, message); + this.Serialize(fields, message); return fields; } + /// + /// Saves the [DataMember] properties of a message to an existing dictionary. + /// + /// The dictionary to save values to. + /// The message to pull values from. internal void Serialize(IDictionary fields, T message) { - if (fields == null) throw new ArgumentNullException("fields"); - if (message == null) throw new ArgumentNullException("message"); + if (fields == null) { + throw new ArgumentNullException("fields"); + } + if (message == null) { + throw new ArgumentNullException("message"); + } message.EnsureValidMessage(); using (XmlWriter writer = DictionaryXmlWriter.Create(fields)) { - serializer.WriteObjectContent(writer, message); + this.serializer.WriteObjectContent(writer, message); } } @@ -42,13 +90,16 @@ namespace DotNetOAuth { /// Reads name=value pairs into an OAuth message. /// /// The name=value pairs that were read in from the transport. + /// The instantiated and initialized instance. internal T Deserialize(IDictionary fields) { - if (fields == null) throw new ArgumentNullException("fields"); + if (fields == null) { + throw new ArgumentNullException("fields"); + } - var reader = DictionaryXmlReader.Create(rootElement, fields); + var reader = DictionaryXmlReader.Create(this.RootElement, fields); T result; try { - result = (T)serializer.ReadObject(reader, false); + result = (T)this.serializer.ReadObject(reader, false); } catch (SerializationException ex) { // Missing required fields is one cause of this exception. throw new ProtocolException(Strings.InvalidIncomingMessage, ex); diff --git a/src/DotNetOAuth/ServiceProvider.cs b/src/DotNetOAuth/ServiceProvider.cs index 345c5cb..49bfb9f 100644 --- a/src/DotNetOAuth/ServiceProvider.cs +++ b/src/DotNetOAuth/ServiceProvider.cs @@ -1,9 +1,15 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Web; +//----------------------------------------------------------------------- +// +// Copyright (c) Andrew Arnott. All rights reserved. +// +//----------------------------------------------------------------------- namespace DotNetOAuth { + using System; + using System.Collections.Generic; + using System.Text; + using System.Web; + /// /// A web application that allows access via OAuth. /// @@ -16,32 +22,41 @@ namespace DotNetOAuth { /// /// internal class ServiceProvider { + /// + /// The field used to store the value of the property. + /// private Uri requestTokenUri; /// - /// The URL used to obtain an unauthorized Request Token, described in Section 6.1 (Obtaining an Unauthorized Request Token). + /// Gets or sets the URL used to obtain an unauthorized Request Token, + /// described in Section 6.1 (Obtaining an Unauthorized Request Token). /// /// /// The request URL query MUST NOT contain any OAuth Protocol Parameters. /// /// Thrown if this property is set to a URI with OAuth protocol parameters. public Uri RequestTokenUri { - get { return requestTokenUri; } + get { + return this.requestTokenUri; + } + set { if (UriUtil.QueryStringContainsOAuthParameters(value)) { throw new ArgumentException(Strings.RequestUrlMustNotHaveOAuthParameters); } - requestTokenUri = value; + this.requestTokenUri = value; } } /// - /// The URL used to obtain User authorization for Consumer access, described in Section 6.2 (Obtaining User Authorization). + /// Gets or sets the URL used to obtain User authorization for Consumer access, + /// described in Section 6.2 (Obtaining User Authorization). /// - public Uri UserAuthorizationUri { get; set; } + public Uri UserAuthorizationUri { get; set; } /// - /// The URL used to exchange the User-authorized Request Token for an Access Token, described in Section 6.3 (Obtaining an Access Token). + /// Gets or sets the URL used to exchange the User-authorized Request Token + /// for an Access Token, described in Section 6.3 (Obtaining an Access Token). /// public Uri AccessTokenUri { get; set; } } diff --git a/src/DotNetOAuth/UriUtil.cs b/src/DotNetOAuth/UriUtil.cs index 9ce913d..74f9e3e 100644 --- a/src/DotNetOAuth/UriUtil.cs +++ b/src/DotNetOAuth/UriUtil.cs @@ -1,12 +1,29 @@ -using System; -using System.Collections.Specialized; -using System.Linq; -using System.Web; +//----------------------------------------------------------------------- +// +// Copyright (c) Andrew Arnott. All rights reserved. +// +//----------------------------------------------------------------------- namespace DotNetOAuth { - class UriUtil { + using System; + using System.Collections.Specialized; + using System.Linq; + using System.Web; + + /// + /// Utility methods for working with URIs. + /// + internal class UriUtil { + /// + /// Tests a URI for the presence of an OAuth payload. + /// + /// The URI to test. + /// True if the URI contains an OAuth message. internal static bool QueryStringContainsOAuthParameters(Uri uri) { - if (uri == null) return false; + if (uri == null) { + return false; + } + NameValueCollection nvc = HttpUtility.ParseQueryString(uri.Query); return nvc.Keys.OfType().Any(key => key.StartsWith(Protocol.V10.ParameterPrefix, StringComparison.Ordinal)); } diff --git a/src/Settings.StyleCop b/src/Settings.StyleCop index 8831d6b..791e073 100644 --- a/src/Settings.StyleCop +++ b/src/Settings.StyleCop @@ -17,6 +17,26 @@ False + + + False + + + + + False + + + + + + + + + + False + + -- 2.11.4.GIT