diff options
Diffstat (limited to 'src/DotNetOAuth')
-rw-r--r-- | src/DotNetOAuth/CommonConsumers/CommonConsumerBase.cs | 61 | ||||
-rw-r--r-- | src/DotNetOAuth/CommonConsumers/GoogleConsumer.cs | 84 | ||||
-rw-r--r-- | src/DotNetOAuth/DotNetOAuth.csproj | 1 | ||||
-rw-r--r-- | src/DotNetOAuth/Util.cs | 20 |
4 files changed, 65 insertions, 101 deletions
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;
+ }
+ }
+ }
}
}
|