diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2012-03-01 23:06:06 -0800 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2012-03-01 23:19:28 -0800 |
commit | 8b88373edc300ba26f260dc80d4ebdf1b4aca2fb (patch) | |
tree | dc3afd50446d9872a56792cd55e7873b83efa7db /src | |
parent | 198bffe042a3650095b27bed29d0f8c98bc5c926 (diff) | |
download | DotNetOpenAuth-8b88373edc300ba26f260dc80d4ebdf1b4aca2fb.zip DotNetOpenAuth-8b88373edc300ba26f260dc80d4ebdf1b4aca2fb.tar.gz DotNetOpenAuth-8b88373edc300ba26f260dc80d4ebdf1b4aca2fb.tar.bz2 |
Replaced manual argument validation with helper methods.
Diffstat (limited to 'src')
-rw-r--r-- | src/DotNetOpenAuth.AspNet/Clients/OAuth/DotNetOpenAuthWebConsumer.cs | 9 | ||||
-rw-r--r-- | src/DotNetOpenAuth.AspNet/Clients/OAuth/InMemoryOAuthTokenManager.cs | 10 | ||||
-rw-r--r-- | src/DotNetOpenAuth.AspNet/Clients/OAuth/OAuthClient.cs | 20 | ||||
-rw-r--r-- | src/DotNetOpenAuth.AspNet/Clients/OAuth2/FacebookClient.cs | 13 | ||||
-rw-r--r-- | src/DotNetOpenAuth.AspNet/Clients/OAuth2/JsonHelper.cs | 4 | ||||
-rw-r--r-- | src/DotNetOpenAuth.AspNet/Clients/OAuth2/OAuth2Client.cs | 24 | ||||
-rw-r--r-- | src/DotNetOpenAuth.AspNet/Clients/OAuth2/WindowsLiveClient.cs | 9 | ||||
-rw-r--r-- | src/DotNetOpenAuth.AspNet/Clients/OpenID/OpenIDClient.cs | 39 | ||||
-rw-r--r-- | src/DotNetOpenAuth.AspNet/DotNetOpenAuth.AspNet.csproj | 4 | ||||
-rw-r--r-- | src/DotNetOpenAuth.AspNet/OpenAuthAuthenticationTicketHelper.cs | 22 | ||||
-rw-r--r-- | src/DotNetOpenAuth.AspNet/WebResources.Designer.cs (renamed from src/DotNetOpenAuth.AspNet/Resources/WebResources.Designer.cs) | 15 | ||||
-rw-r--r-- | src/DotNetOpenAuth.AspNet/WebResources.resx (renamed from src/DotNetOpenAuth.AspNet/Resources/WebResources.resx) | 3 |
12 files changed, 51 insertions, 121 deletions
diff --git a/src/DotNetOpenAuth.AspNet/Clients/OAuth/DotNetOpenAuthWebConsumer.cs b/src/DotNetOpenAuth.AspNet/Clients/OAuth/DotNetOpenAuthWebConsumer.cs index 8edbeed..9979d97 100644 --- a/src/DotNetOpenAuth.AspNet/Clients/OAuth/DotNetOpenAuthWebConsumer.cs +++ b/src/DotNetOpenAuth.AspNet/Clients/OAuth/DotNetOpenAuthWebConsumer.cs @@ -42,13 +42,8 @@ namespace DotNetOpenAuth.AspNet.Clients { /// <exception cref="ArgumentNullException"> /// </exception> public DotNetOpenAuthWebConsumer(ServiceProviderDescription serviceDescription, IConsumerTokenManager tokenManager) { - if (serviceDescription == null) { - throw new ArgumentNullException("consumer"); - } - - if (tokenManager == null) { - throw new ArgumentNullException("tokenManager"); - } + Requires.NotNull(serviceDescription, "serviceDescription"); + Requires.NotNull(tokenManager, "tokenManager"); this._webConsumer = new WebConsumer(serviceDescription, tokenManager); } diff --git a/src/DotNetOpenAuth.AspNet/Clients/OAuth/InMemoryOAuthTokenManager.cs b/src/DotNetOpenAuth.AspNet/Clients/OAuth/InMemoryOAuthTokenManager.cs index e8509af..b319d55 100644 --- a/src/DotNetOpenAuth.AspNet/Clients/OAuth/InMemoryOAuthTokenManager.cs +++ b/src/DotNetOpenAuth.AspNet/Clients/OAuth/InMemoryOAuthTokenManager.cs @@ -7,6 +7,7 @@ namespace DotNetOpenAuth.AspNet.Clients { using System; using System.Collections.Generic; + using DotNetOpenAuth.OAuth; using DotNetOpenAuth.OAuth.ChannelElements; using DotNetOpenAuth.OAuth.Messages; @@ -35,13 +36,8 @@ namespace DotNetOpenAuth.AspNet.Clients { /// The consumer secret. /// </param> public InMemoryOAuthTokenManager(string consumerKey, string consumerSecret) { - if (consumerKey == null) { - throw new ArgumentNullException("consumerKey"); - } - - if (consumerSecret == null) { - throw new ArgumentNullException("consumerSecret"); - } + Requires.NotNull(consumerKey, "consumerKey"); + Requires.NotNull(consumerSecret, "consumerSecret"); this.ConsumerKey = consumerKey; this.ConsumerSecret = consumerSecret; diff --git a/src/DotNetOpenAuth.AspNet/Clients/OAuth/OAuthClient.cs b/src/DotNetOpenAuth.AspNet/Clients/OAuth/OAuthClient.cs index 56e046e..42b960c 100644 --- a/src/DotNetOpenAuth.AspNet/Clients/OAuth/OAuthClient.cs +++ b/src/DotNetOpenAuth.AspNet/Clients/OAuth/OAuthClient.cs @@ -67,13 +67,8 @@ namespace DotNetOpenAuth.AspNet.Clients { /// <exception cref="ArgumentNullException"> /// </exception> protected OAuthClient(string providerName, IOAuthWebWorker webWorker) { - if (providerName == null) { - throw new ArgumentNullException("providerName"); - } - - if (webWorker == null) { - throw new ArgumentNullException("webWorker"); - } + Requires.NotNull(providerName, "providerName"); + Requires.NotNull(webWorker, "webWorker"); this.ProviderName = providerName; this.WebWorker = webWorker; @@ -93,7 +88,7 @@ namespace DotNetOpenAuth.AspNet.Clients { #region Properties /// <summary> - /// Gets the <see cref="OAuthWebConsumer" /> instance which handles constructing requests to the OAuth providers. + /// Gets the OAuthWebConsumer instance which handles constructing requests to the OAuth providers. /// </summary> protected IOAuthWebWorker WebWorker { get; private set; } @@ -111,13 +106,8 @@ namespace DotNetOpenAuth.AspNet.Clients { /// The return url after users have completed authenticating against external website. /// </param> public virtual void RequestAuthentication(HttpContextBase context, Uri returnUrl) { - if (returnUrl == null) { - throw new ArgumentNullException("returnUrl"); - } - - if (context == null) { - throw new ArgumentNullException("context"); - } + Requires.NotNull(returnUrl, "returnUrl"); + Requires.NotNull(context, "context"); Uri callback = returnUrl.StripQueryArgumentsWithPrefix("oauth_"); this.WebWorker.RequestAuthentication(callback); diff --git a/src/DotNetOpenAuth.AspNet/Clients/OAuth2/FacebookClient.cs b/src/DotNetOpenAuth.AspNet/Clients/OAuth2/FacebookClient.cs index 5745bad..54661b2 100644 --- a/src/DotNetOpenAuth.AspNet/Clients/OAuth2/FacebookClient.cs +++ b/src/DotNetOpenAuth.AspNet/Clients/OAuth2/FacebookClient.cs @@ -7,10 +7,8 @@ namespace DotNetOpenAuth.AspNet.Clients { using System; using System.Collections.Generic; - using System.Globalization; using System.Net; using System.Web; - using DotNetOpenAuth.AspNet.Resources; using DotNetOpenAuth.Messaging; /// <summary> @@ -58,15 +56,8 @@ namespace DotNetOpenAuth.AspNet.Clients { /// </exception> public FacebookClient(string appId, string appSecret) : base("facebook") { - if (string.IsNullOrEmpty(appId)) { - throw new ArgumentException( - string.Format(CultureInfo.CurrentCulture, WebResources.Argument_Cannot_Be_Null_Or_Empty, "appId"), "appId"); - } - - if (string.IsNullOrEmpty("appSecret")) { - throw new ArgumentException( - string.Format(CultureInfo.CurrentCulture, WebResources.Argument_Cannot_Be_Null_Or_Empty, "appSecret"), "appSecret"); - } + Requires.NotNullOrEmpty(appId, "appId"); + Requires.NotNullOrEmpty(appSecret, "appSecret"); this._appId = appId; this._appSecret = appSecret; diff --git a/src/DotNetOpenAuth.AspNet/Clients/OAuth2/JsonHelper.cs b/src/DotNetOpenAuth.AspNet/Clients/OAuth2/JsonHelper.cs index ddb8879..6343cb0 100644 --- a/src/DotNetOpenAuth.AspNet/Clients/OAuth2/JsonHelper.cs +++ b/src/DotNetOpenAuth.AspNet/Clients/OAuth2/JsonHelper.cs @@ -28,9 +28,7 @@ namespace DotNetOpenAuth.AspNet.Clients { /// <exception cref="ArgumentNullException"> /// </exception> public static T Deserialize<T>(Stream stream) where T : class { - if (stream == null) { - throw new ArgumentNullException("stream"); - } + Requires.NotNull(stream, "stream"); var serializer = new DataContractJsonSerializer(typeof(T)); return (T)serializer.ReadObject(stream); diff --git a/src/DotNetOpenAuth.AspNet/Clients/OAuth2/OAuth2Client.cs b/src/DotNetOpenAuth.AspNet/Clients/OAuth2/OAuth2Client.cs index 9003bc0..0120615 100644 --- a/src/DotNetOpenAuth.AspNet/Clients/OAuth2/OAuth2Client.cs +++ b/src/DotNetOpenAuth.AspNet/Clients/OAuth2/OAuth2Client.cs @@ -19,7 +19,7 @@ namespace DotNetOpenAuth.AspNet.Clients { /// <summary> /// The _provider name. /// </summary> - private readonly string _providerName; + private readonly string providerName; /// <summary> /// The _return url. @@ -37,11 +37,8 @@ namespace DotNetOpenAuth.AspNet.Clients { /// Name of the provider. /// </param> protected OAuth2Client(string providerName) { - if (providerName == null) { - throw new ArgumentNullException("providerName"); - } - - this._providerName = providerName; + Requires.NotNull(providerName, "providerName"); + this.providerName = providerName; } #endregion @@ -53,7 +50,7 @@ namespace DotNetOpenAuth.AspNet.Clients { /// </summary> public string ProviderName { get { - return this._providerName; + return this.providerName; } } @@ -71,13 +68,8 @@ namespace DotNetOpenAuth.AspNet.Clients { /// The return url after users have completed authenticating against external website. /// </param> public virtual void RequestAuthentication(HttpContextBase context, Uri returnUrl) { - if (context == null) { - throw new ArgumentNullException("context"); - } - - if (returnUrl == null) { - throw new ArgumentNullException("returnUrl"); - } + Requires.NotNull(context, "context"); + Requires.NotNull(returnUrl, "returnUrl"); this._returnUrl = returnUrl; @@ -95,9 +87,7 @@ namespace DotNetOpenAuth.AspNet.Clients { /// An instance of <see cref="AuthenticationResult"/> containing authentication result. /// </returns> public virtual AuthenticationResult VerifyAuthentication(HttpContextBase context) { - if (context == null) { - throw new ArgumentNullException("context"); - } + Requires.NotNull(context, "context"); string code = context.Request.QueryString["code"]; if (string.IsNullOrEmpty(code)) { diff --git a/src/DotNetOpenAuth.AspNet/Clients/OAuth2/WindowsLiveClient.cs b/src/DotNetOpenAuth.AspNet/Clients/OAuth2/WindowsLiveClient.cs index dced87f..c47559f 100644 --- a/src/DotNetOpenAuth.AspNet/Clients/OAuth2/WindowsLiveClient.cs +++ b/src/DotNetOpenAuth.AspNet/Clients/OAuth2/WindowsLiveClient.cs @@ -56,13 +56,8 @@ namespace DotNetOpenAuth.AspNet.Clients { /// </exception> public WindowsLiveClient(string appId, string appSecret) : base("windowslive") { - if (string.IsNullOrEmpty(appId)) { - throw new ArgumentNullException("appId"); - } - - if (string.IsNullOrEmpty("appSecret")) { - throw new ArgumentNullException("appSecret"); - } + Requires.NotNullOrEmpty(appId, "appId"); + Requires.NotNullOrEmpty(appSecret, "appSecret"); this._appId = appId; this._appSecret = appSecret; diff --git a/src/DotNetOpenAuth.AspNet/Clients/OpenID/OpenIDClient.cs b/src/DotNetOpenAuth.AspNet/Clients/OpenID/OpenIDClient.cs index 4f31529..a462e90 100644 --- a/src/DotNetOpenAuth.AspNet/Clients/OpenID/OpenIDClient.cs +++ b/src/DotNetOpenAuth.AspNet/Clients/OpenID/OpenIDClient.cs @@ -8,9 +8,7 @@ namespace DotNetOpenAuth.AspNet.Clients { using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; - using System.Globalization; using System.Web; - using DotNetOpenAuth.AspNet.Resources; using DotNetOpenAuth.OpenId; using DotNetOpenAuth.OpenId.RelyingParty; @@ -23,18 +21,18 @@ namespace DotNetOpenAuth.AspNet.Clients { /// <summary> /// The _openid relaying party. /// </summary> - private static readonly OpenIdRelyingParty _openidRelayingParty = + private static readonly OpenIdRelyingParty RelyingParty = new OpenIdRelyingParty(new StandardRelyingPartyApplicationStore()); /// <summary> /// The _provider identifier. /// </summary> - private readonly Identifier _providerIdentifier; + private readonly Identifier providerIdentifier; /// <summary> /// The _provider name. /// </summary> - private readonly string _providerName; + private readonly string providerName; #endregion @@ -50,20 +48,11 @@ namespace DotNetOpenAuth.AspNet.Clients { /// The provider identifier, which is the usually the login url of the specified provider. /// </param> public OpenIdClient(string providerName, string providerIdentifier) { - if (string.IsNullOrEmpty(providerIdentifier)) { - throw new ArgumentException( - string.Format(CultureInfo.CurrentCulture, WebResources.Argument_Cannot_Be_Null_Or_Empty, "providerIdentifier"), - "providerIdentifier"); - } - - if (string.IsNullOrEmpty(providerName)) { - throw new ArgumentException( - string.Format(CultureInfo.CurrentCulture, WebResources.Argument_Cannot_Be_Null_Or_Empty, "providerName"), - "providerName"); - } + Requires.NotNullOrEmpty(providerName, "providerName"); + Requires.NotNullOrEmpty(providerIdentifier, "providerIdentifier"); - this._providerName = providerName; - if (!Identifier.TryParse(providerIdentifier, out this._providerIdentifier) || this._providerIdentifier == null) { + this.providerName = providerName; + if (!Identifier.TryParse(providerIdentifier, out this.providerIdentifier) || this.providerIdentifier == null) { throw new ArgumentException(WebResources.OpenIDInvalidIdentifier, "providerIdentifier"); } } @@ -77,7 +66,7 @@ namespace DotNetOpenAuth.AspNet.Clients { /// </summary> public string ProviderName { get { - return this._providerName; + return this.providerName; } } @@ -94,15 +83,13 @@ namespace DotNetOpenAuth.AspNet.Clients { /// <param name="returnUrl"> /// The return url after users have completed authenticating against external website. /// </param> - [SuppressMessage("Microsoft.Usage", "CA2234:PassSystemUriObjectsInsteadOfStrings", + [SuppressMessage("Microsoft.Usage", "CA2234:PassSystemUriObjectsInsteadOfStrings", Justification = "We don't have a Uri object handy.")] public virtual void RequestAuthentication(HttpContextBase context, Uri returnUrl) { - if (returnUrl == null) { - throw new ArgumentNullException("returnUrl"); - } + Requires.NotNull(returnUrl, "returnUrl"); var realm = new Realm(returnUrl.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped)); - IAuthenticationRequest request = _openidRelayingParty.CreateRequest(this._providerIdentifier, realm, returnUrl); + IAuthenticationRequest request = RelyingParty.CreateRequest(this.providerIdentifier, realm, returnUrl); // give subclasses a chance to modify request message, e.g. add extension attributes, etc. this.OnBeforeSendingAuthenticationRequest(request); @@ -120,7 +107,7 @@ namespace DotNetOpenAuth.AspNet.Clients { /// An instance of <see cref="AuthenticationResult"/> containing authentication result. /// </returns> public virtual AuthenticationResult VerifyAuthentication(HttpContextBase context) { - IAuthenticationResponse response = _openidRelayingParty.GetResponse(); + IAuthenticationResponse response = RelyingParty.GetResponse(); if (response == null) { throw new InvalidOperationException(WebResources.OpenIDFailedToGetResponse); } @@ -164,7 +151,7 @@ namespace DotNetOpenAuth.AspNet.Clients { /// <param name="request"> /// The request. /// </param> - protected virtual void OnBeforeSendingAuthenticationRequest(IAuthenticationRequest request) {} + protected virtual void OnBeforeSendingAuthenticationRequest(IAuthenticationRequest request) { } #endregion } } diff --git a/src/DotNetOpenAuth.AspNet/DotNetOpenAuth.AspNet.csproj b/src/DotNetOpenAuth.AspNet/DotNetOpenAuth.AspNet.csproj index c2830d2..aacb8f3 100644 --- a/src/DotNetOpenAuth.AspNet/DotNetOpenAuth.AspNet.csproj +++ b/src/DotNetOpenAuth.AspNet/DotNetOpenAuth.AspNet.csproj @@ -65,14 +65,14 @@ <Compile Include="OpenAuthAuthenticationTicketHelper.cs" /> <Compile Include="OpenAuthSecurityManager.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> - <Compile Include="Resources\WebResources.Designer.cs"> + <Compile Include="WebResources.Designer.cs"> <AutoGen>True</AutoGen> <DesignTime>True</DesignTime> <DependentUpon>WebResources.resx</DependentUpon> </Compile> </ItemGroup> <ItemGroup> - <EmbeddedResource Include="Resources\WebResources.resx"> + <EmbeddedResource Include="WebResources.resx"> <Generator>ResXFileCodeGenerator</Generator> <LastGenOutput>WebResources.Designer.cs</LastGenOutput> </EmbeddedResource> diff --git a/src/DotNetOpenAuth.AspNet/OpenAuthAuthenticationTicketHelper.cs b/src/DotNetOpenAuth.AspNet/OpenAuthAuthenticationTicketHelper.cs index 496e420..abf9a9d 100644 --- a/src/DotNetOpenAuth.AspNet/OpenAuthAuthenticationTicketHelper.cs +++ b/src/DotNetOpenAuth.AspNet/OpenAuthAuthenticationTicketHelper.cs @@ -9,7 +9,6 @@ namespace DotNetOpenAuth.AspNet { using System.Diagnostics; using System.Web; using System.Web.Security; - using DotNetOpenAuth.AspNet.Resources; /// <summary> /// Helper methods for setting and retrieving a custom forms authentication ticket for delegation protocols. @@ -99,12 +98,12 @@ namespace DotNetOpenAuth.AspNet { var ticket = new FormsAuthenticationTicket( /* version */ - 2, - userName, - DateTime.Now, - DateTime.Now.Add(FormsAuthentication.Timeout), - createPersistentCookie, - OpenAuthCookieToken, + 2, + userName, + DateTime.Now, + DateTime.Now.Add(FormsAuthentication.Timeout), + createPersistentCookie, + OpenAuthCookieToken, FormsAuthentication.FormsCookiePath); string encryptedTicket = FormsAuthentication.Encrypt(ticket); @@ -112,10 +111,11 @@ namespace DotNetOpenAuth.AspNet { throw new HttpException(WebResources.FailedToEncryptTicket); } - var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket) - { - HttpOnly = true, Path = FormsAuthentication.FormsCookiePath, Secure = FormsAuthentication.RequireSSL - }; + var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket) { + HttpOnly = true, + Path = FormsAuthentication.FormsCookiePath, + Secure = FormsAuthentication.RequireSSL + }; if (FormsAuthentication.CookieDomain != null) { cookie.Domain = FormsAuthentication.CookieDomain; diff --git a/src/DotNetOpenAuth.AspNet/Resources/WebResources.Designer.cs b/src/DotNetOpenAuth.AspNet/WebResources.Designer.cs index 624aa2b..b25b349 100644 --- a/src/DotNetOpenAuth.AspNet/Resources/WebResources.Designer.cs +++ b/src/DotNetOpenAuth.AspNet/WebResources.Designer.cs @@ -1,14 +1,14 @@ //------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. -// Runtime Version:4.0.30319.488 +// Runtime Version:4.0.30319.261 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ -namespace DotNetOpenAuth.AspNet.Resources { +namespace DotNetOpenAuth.AspNet { using System; @@ -39,7 +39,7 @@ namespace DotNetOpenAuth.AspNet.Resources { internal static global::System.Resources.ResourceManager ResourceManager { get { if (object.ReferenceEquals(resourceMan, null)) { - global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("DotNetOpenAuth.AspNet.Resources.WebResources", typeof(WebResources).Assembly); + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("DotNetOpenAuth.AspNet.WebResources", typeof(WebResources).Assembly); resourceMan = temp; } return resourceMan; @@ -61,15 +61,6 @@ namespace DotNetOpenAuth.AspNet.Resources { } /// <summary> - /// Looks up a localized string similar to {0} cannot be null or an empty string.. - /// </summary> - internal static string Argument_Cannot_Be_Null_Or_Empty { - get { - return ResourceManager.GetString("Argument_Cannot_Be_Null_Or_Empty", resourceCulture); - } - } - - /// <summary> /// Looks up a localized string similar to A setting in web.config requires a secure connection for this request but the current connection is not secured.. /// </summary> internal static string ConnectionNotSecure { diff --git a/src/DotNetOpenAuth.AspNet/Resources/WebResources.resx b/src/DotNetOpenAuth.AspNet/WebResources.resx index dce188c..c585c53 100644 --- a/src/DotNetOpenAuth.AspNet/Resources/WebResources.resx +++ b/src/DotNetOpenAuth.AspNet/WebResources.resx @@ -117,9 +117,6 @@ <resheader name="writer"> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> </resheader> - <data name="Argument_Cannot_Be_Null_Or_Empty" xml:space="preserve"> - <value>{0} cannot be null or an empty string.</value> - </data> <data name="ConnectionNotSecure" xml:space="preserve"> <value>A setting in web.config requires a secure connection for this request but the current connection is not secured.</value> </data> |