summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAndrew Arnott <andrewarnott@gmail.com>2008-11-02 22:05:41 -0800
committerAndrew <andrewarnott@gmail.com>2008-11-02 22:05:41 -0800
commit8969dfec9e4e4ecc45f909137dc3a23d7af0bee8 (patch)
treec903f51425a18182463965093ac3791f98c75145 /src
parent71e99449ee02155f34bb4928313c2200246b8a78 (diff)
downloadDotNetOpenAuth-8969dfec9e4e4ecc45f909137dc3a23d7af0bee8.zip
DotNetOpenAuth-8969dfec9e4e4ecc45f909137dc3a23d7af0bee8.tar.gz
DotNetOpenAuth-8969dfec9e4e4ecc45f909137dc3a23d7af0bee8.tar.bz2
Second stab at app-specific consumer classes.
Refactored to be a static class that operates on some consumer object so that desktop and web consumer alike can use it.
Diffstat (limited to 'src')
-rw-r--r--src/DotNetOAuth.Test/DotNetOAuth.Test.csproj2
-rw-r--r--src/DotNetOAuth.Test/UtilTest.cs (renamed from src/DotNetOAuth.Test/CommonConsumers/CommonConsumerBaseTest.cs)17
-rw-r--r--src/DotNetOAuth/CommonConsumers/CommonConsumerBase.cs61
-rw-r--r--src/DotNetOAuth/CommonConsumers/GoogleConsumer.cs84
-rw-r--r--src/DotNetOAuth/DotNetOAuth.csproj1
-rw-r--r--src/DotNetOAuth/Util.cs20
6 files changed, 75 insertions, 110 deletions
diff --git a/src/DotNetOAuth.Test/DotNetOAuth.Test.csproj b/src/DotNetOAuth.Test/DotNetOAuth.Test.csproj
index 04389be..443e65e 100644
--- a/src/DotNetOAuth.Test/DotNetOAuth.Test.csproj
+++ b/src/DotNetOAuth.Test/DotNetOAuth.Test.csproj
@@ -60,7 +60,6 @@
<ItemGroup>
<Compile Include="ChannelElements\SigningBindingElementBaseTests.cs" />
<Compile Include="ChannelElements\HmacSha1SigningBindingElementTests.cs" />
- <Compile Include="CommonConsumers\CommonConsumerBaseTest.cs" />
<Compile Include="ConsumerDescription.cs" />
<Compile Include="Messaging\CollectionAssert.cs" />
<Compile Include="Messaging\MessageSerializerTests.cs" />
@@ -100,6 +99,7 @@
<Compile Include="ServiceProviderDescriptionTests.cs" />
<Compile Include="TestBase.cs" />
<Compile Include="UriUtilTests.cs" />
+ <Compile Include="UtilTest.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DotNetOAuth\DotNetOAuth.csproj">
diff --git a/src/DotNetOAuth.Test/CommonConsumers/CommonConsumerBaseTest.cs b/src/DotNetOAuth.Test/UtilTest.cs
index f61b34e..5e69975 100644
--- a/src/DotNetOAuth.Test/CommonConsumers/CommonConsumerBaseTest.cs
+++ b/src/DotNetOAuth.Test/UtilTest.cs
@@ -1,17 +1,18 @@
//-----------------------------------------------------------------------
-// <copyright file="CommonConsumerBaseTest.cs" company="Andrew Arnott">
+// <copyright file="UtilTest.cs" company="Andrew Arnott">
// Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
-namespace DotNetOAuth.Test.CommonConsumers {
+namespace DotNetOAuth.Test {
+ using System;
using System.Collections.Generic;
using System.Linq;
- using DotNetOAuth.CommonConsumers;
+ using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
- public class CommonConsumerBaseTest : TestBase {
+ public class UtilTest {
private enum SomeFlags : int {
None = 0,
Flag1 = 0x1,
@@ -23,12 +24,12 @@ namespace DotNetOAuth.Test.CommonConsumers {
[TestMethod]
public void GetIndividualFlagsTest() {
- Assert.IsFalse(CommonConsumerBase_Accessor.GetIndividualFlags(SomeFlags.None).Any());
- Assert.AreEqual(SomeFlags.Flag1, (SomeFlags)CommonConsumerBase_Accessor.GetIndividualFlags(SomeFlags.Flag1).Single());
- IList<long> flags = CommonConsumerBase_Accessor.GetIndividualFlags(SomeFlags.Flag1and2).ToList();
+ Assert.IsFalse(Util.GetIndividualFlags(SomeFlags.None).Any());
+ Assert.AreEqual(SomeFlags.Flag1, (SomeFlags)Util.GetIndividualFlags(SomeFlags.Flag1).Single());
+ IList<long> flags = Util.GetIndividualFlags(SomeFlags.Flag1and2).ToList();
Assert.AreEqual(SomeFlags.Flag1, (SomeFlags)flags[0]);
Assert.AreEqual(SomeFlags.Flag2, (SomeFlags)flags[1]);
- flags = CommonConsumerBase_Accessor.GetIndividualFlags(SomeFlags.All).ToList();
+ flags = Util.GetIndividualFlags(SomeFlags.All).ToList();
Assert.AreEqual(SomeFlags.Flag1, (SomeFlags)flags[0]);
Assert.AreEqual(SomeFlags.Flag2, (SomeFlags)flags[1]);
Assert.AreEqual(SomeFlags.Flag3, (SomeFlags)flags[2]);
diff --git a/src/DotNetOAuth/CommonConsumers/CommonConsumerBase.cs b/src/DotNetOAuth/CommonConsumers/CommonConsumerBase.cs
deleted file mode 100644
index c3f9209..0000000
--- a/src/DotNetOAuth/CommonConsumers/CommonConsumerBase.cs
+++ /dev/null
@@ -1,61 +0,0 @@
-//-----------------------------------------------------------------------
-// <copyright file="CommonConsumerBase.cs" company="Andrew Arnott">
-// Copyright (c) Andrew Arnott. All rights reserved.
-// </copyright>
-//-----------------------------------------------------------------------
-
-namespace DotNetOAuth.CommonConsumers {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using DotNetOAuth.ChannelElements;
-
- /// <summary>
- /// A useful base class to derive from for Consumers written against a specific Service Provider.
- /// </summary>
- public abstract class CommonConsumerBase {
- /// <summary>
- /// Initializes a new instance of the <see cref="CommonConsumerBase"/> class.
- /// </summary>
- /// <param name="serviceDescription">The service description.</param>
- /// <param name="tokenManager">The token manager.</param>
- /// <param name="consumerKey">The consumer key.</param>
- protected CommonConsumerBase(ServiceProviderDescription serviceDescription, ITokenManager tokenManager, string consumerKey) {
- if (serviceDescription == null) {
- throw new ArgumentNullException("serviceDescription");
- }
- if (tokenManager == null) {
- throw new ArgumentNullException("tokenManager");
- }
- if (consumerKey == null) {
- throw new ArgumentNullException("consumerKey");
- }
- this.Consumer = new WebConsumer(serviceDescription, tokenManager) {
- ConsumerKey = consumerKey,
- };
- }
-
- /// <summary>
- /// Gets the consumer.
- /// </summary>
- protected WebConsumer Consumer { get; private set; }
-
- /// <summary>
- /// Enumerates through the individual set bits in a flag enum.
- /// </summary>
- /// <param name="flags">The flags enum value.</param>
- /// <returns>An enumeration of just the <i>set</i> bits in the flags enum.</returns>
- protected static IEnumerable<long> GetIndividualFlags(Enum flags) {
- long flagsLong = Convert.ToInt64(flags);
- for (int i = 0; i < sizeof(long) * 8; i++) { // long is the type behind the largest enum
- // Select an individual application from the scopes.
- long individualFlagPosition = (long)Math.Pow(2, i);
- long individualFlag = flagsLong & individualFlagPosition;
- if (individualFlag == individualFlagPosition) {
- yield return individualFlag;
- }
- }
- }
- }
-}
diff --git a/src/DotNetOAuth/CommonConsumers/GoogleConsumer.cs b/src/DotNetOAuth/CommonConsumers/GoogleConsumer.cs
index b8c14e7..5f6bf21 100644
--- a/src/DotNetOAuth/CommonConsumers/GoogleConsumer.cs
+++ b/src/DotNetOAuth/CommonConsumers/GoogleConsumer.cs
@@ -15,7 +15,7 @@ namespace DotNetOAuth.CommonConsumers {
/// <summary>
/// A consumer capable of communicating with Google Data APIs.
/// </summary>
- public class GoogleConsumer : CommonConsumerBase {
+ public static class GoogleConsumer {
/// <summary>
/// The Consumer to use for accessing Google data APIs.
/// </summary>
@@ -40,15 +40,6 @@ namespace DotNetOAuth.CommonConsumers {
private static readonly MessageReceivingEndpoint GetContactsEndpoint = new MessageReceivingEndpoint("http://www.google.com/m8/feeds/contacts/default/full/", HttpDeliveryMethods.GetRequest);
/// <summary>
- /// Initializes a new instance of the <see cref="GoogleConsumer"/> class.
- /// </summary>
- /// <param name="tokenManager">The token manager.</param>
- /// <param name="consumerKey">The consumer key.</param>
- public GoogleConsumer(ITokenManager tokenManager, string consumerKey)
- : base(GoogleDescription, tokenManager, consumerKey) {
- }
-
- /// <summary>
/// The many specific authorization scopes Google offers.
/// </summary>
[Flags]
@@ -65,55 +56,72 @@ namespace DotNetOAuth.CommonConsumers {
}
/// <summary>
- /// Requests authorization from Google to access data from a set of Google applications.
+ /// Initializes a new instance of the <see cref="WebConsumer"/> class that is prepared to communicate with Google.
/// </summary>
- /// <param name="requestedAccessScope">The requested access scope.</param>
- public void RequestAuthorization(Applications requestedAccessScope) {
- Uri callback = MessagingUtilities.GetRequestUrlFromContext().StripQueryArgumentsWithPrefix(Protocol.Default.ParameterPrefix);
- var extraParameters = new Dictionary<string, string> {
- { "scope", this.GetScopeUri(requestedAccessScope) },
+ /// <param name="tokenManager">The token manager.</param>
+ /// <param name="consumerKey">The consumer key.</param>
+ /// <returns>The newly instantiated <see cref="WebConsumer"/>.</returns>
+ public static WebConsumer CreateWebConsumer(ITokenManager tokenManager, string consumerKey)
+ {
+ return new WebConsumer(GoogleDescription, tokenManager) {
+ ConsumerKey = consumerKey,
+ };
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="DesktopConsumer"/> class that is prepared to communicate with Google.
+ /// </summary>
+ /// <param name="tokenManager">The token manager.</param>
+ /// <param name="consumerKey">The consumer key.</param>
+ /// <returns>The newly instantiated <see cref="DesktopConsumer"/>.</returns>
+ public static DesktopConsumer CreateDesktopConsumer(ITokenManager tokenManager, string consumerKey) {
+ return new DesktopConsumer(GoogleDescription, tokenManager) {
+ ConsumerKey = consumerKey,
};
- var request = this.Consumer.PrepareRequestUserAuthorization(callback, extraParameters, null);
- this.Consumer.Channel.Send(request).Send();
}
/// <summary>
- /// Gets the access token on the next page request after a call to <see cref="RequestAuthorization"/>.
+ /// Requests authorization from Google to access data from a set of Google applications.
/// </summary>
- /// <returns>The access token that should be stored for later use.</returns>
- public string GetAccessToken() {
- var response = this.Consumer.ProcessUserAuthorization();
- return response != null ? response.AccessToken : null;
+ /// <param name="consumer">The Google consumer previously constructed using <see cref="CreateWebConsumer"/> or <see cref="CreateDesktopConsumer"/>.</param>
+ /// <param name="requestedAccessScope">The requested access scope.</param>
+ public static void RequestAuthorization(ConsumerBase consumer, Applications requestedAccessScope) {
+ if (consumer == null) {
+ throw new ArgumentNullException("consumer");
+ }
+
+ Uri callback = MessagingUtilities.GetRequestUrlFromContext().StripQueryArgumentsWithPrefix(Protocol.Default.ParameterPrefix);
+ var extraParameters = new Dictionary<string, string> {
+ { "scope", GetScopeUri(requestedAccessScope) },
+ };
+ string requestToken;
+ var request = consumer.PrepareRequestUserAuthorization(callback, extraParameters, null, out requestToken);
+ consumer.Channel.Send(request).Send();
}
/// <summary>
/// Gets the Gmail address book's contents.
/// </summary>
- /// <param name="accessToken">The access token previously retrieved from the <see cref="GetAccessToken"/> method.</param>
+ /// <param name="consumer">The Google consumer previously constructed using <see cref="CreateWebConsumer"/> or <see cref="CreateDesktopConsumer"/>.</param>
+ /// <param name="accessToken">The access token previously retrieved.</param>
/// <returns>An XML document returned by Google.</returns>
- public XDocument GetContacts(string accessToken) {
- var response = this.PrepareAuthorizedRequestAndSend(GetContactsEndpoint, accessToken);
+ public static XDocument GetContacts(ConsumerBase consumer, string accessToken) {
+ if (consumer == null) {
+ throw new ArgumentNullException("consumer");
+ }
+
+ var response = consumer.PrepareAuthorizedRequestAndSend(GetContactsEndpoint, accessToken);
XDocument result = XDocument.Parse(response.Body);
return result;
}
/// <summary>
- /// A general method for sending OAuth-authorized requests for user data from Google.
- /// </summary>
- /// <param name="endpoint">The Google URL to retrieve the data from.</param>
- /// <param name="accessToken">The access token previously retrieved from the <see cref="GetAccessToken"/> method.</param>
- /// <returns>Whatever the response Google sends.</returns>
- public Response PrepareAuthorizedRequestAndSend(MessageReceivingEndpoint endpoint, string accessToken) {
- return this.Consumer.PrepareAuthorizedRequestAndSend(endpoint, accessToken);
- }
-
- /// <summary>
/// Gets the scope URI in Google's format.
/// </summary>
/// <param name="scope">The scope, which may include one or several Google applications.</param>
/// <returns>A space-delimited list of URIs for the requested Google applications.</returns>
- private string GetScopeUri(Applications scope) {
- return string.Join(" ", GetIndividualFlags(scope).Select(app => DataScopeUris[(Applications)app]).ToArray());
+ private static string GetScopeUri(Applications scope) {
+ return string.Join(" ", Util.GetIndividualFlags(scope).Select(app => DataScopeUris[(Applications)app]).ToArray());
}
}
}
diff --git a/src/DotNetOAuth/DotNetOAuth.csproj b/src/DotNetOAuth/DotNetOAuth.csproj
index 0ec21f6..22b0e52 100644
--- a/src/DotNetOAuth/DotNetOAuth.csproj
+++ b/src/DotNetOAuth/DotNetOAuth.csproj
@@ -72,7 +72,6 @@
<Compile Include="ChannelElements\SigningBindingElementChain.cs" />
<Compile Include="ChannelElements\StandardTokenGenerator.cs" />
<Compile Include="ChannelElements\TokenType.cs" />
- <Compile Include="CommonConsumers\CommonConsumerBase.cs" />
<Compile Include="CommonConsumers\GoogleConsumer.cs" />
<Compile Include="ConsumerBase.cs" />
<Compile Include="DesktopConsumer.cs" />
diff --git a/src/DotNetOAuth/Util.cs b/src/DotNetOAuth/Util.cs
index 1bf76b1..4a1cab9 100644
--- a/src/DotNetOAuth/Util.cs
+++ b/src/DotNetOAuth/Util.cs
@@ -4,9 +4,10 @@
// </copyright>
//-----------------------------------------------------------------------
namespace DotNetOAuth {
+ using System;
+ using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
- using DotNetOAuth.Messaging;
/// <summary>
/// A grab-bag utility class.
@@ -25,5 +26,22 @@ namespace DotNetOAuth {
return string.Format(CultureInfo.InvariantCulture, "{0} ({1})", assemblyFullName, official ? "official" : "private");
}
}
+
+ /// <summary>
+ /// Enumerates through the individual set bits in a flag enum.
+ /// </summary>
+ /// <param name="flags">The flags enum value.</param>
+ /// <returns>An enumeration of just the <i>set</i> bits in the flags enum.</returns>
+ internal static IEnumerable<long> GetIndividualFlags(Enum flags) {
+ long flagsLong = Convert.ToInt64(flags);
+ for (int i = 0; i < sizeof(long) * 8; i++) { // long is the type behind the largest enum
+ // Select an individual application from the scopes.
+ long individualFlagPosition = (long)Math.Pow(2, i);
+ long individualFlag = flagsLong & individualFlagPosition;
+ if (individualFlag == individualFlagPosition) {
+ yield return individualFlag;
+ }
+ }
+ }
}
}