diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2008-11-03 07:30:06 -0800 |
---|---|---|
committer | Andrew <andrewarnott@gmail.com> | 2008-11-03 07:30:06 -0800 |
commit | f1794fc8779ae39ac3d5bc6e8b811523e62ca482 (patch) | |
tree | 47fc2cc84483e4864c016542938937c172b22cca /src/DotNetOAuth | |
parent | 8cc2fa50f05f4b3cc30856b40336e9fd8ee85e33 (diff) | |
download | DotNetOpenAuth-f1794fc8779ae39ac3d5bc6e8b811523e62ca482.zip DotNetOpenAuth-f1794fc8779ae39ac3d5bc6e8b811523e62ca482.tar.gz DotNetOpenAuth-f1794fc8779ae39ac3d5bc6e8b811523e62ca482.tar.bz2 |
Moved the Google Consumer app-specific class into a new "sample" project.
Diffstat (limited to 'src/DotNetOAuth')
-rw-r--r-- | src/DotNetOAuth/CommonConsumers/GoogleConsumer.cs | 151 | ||||
-rw-r--r-- | src/DotNetOAuth/DotNetOAuth.csproj | 1 | ||||
-rw-r--r-- | src/DotNetOAuth/Messaging/MessagingUtilities.cs | 6 | ||||
-rw-r--r-- | src/DotNetOAuth/Messaging/Response.cs | 6 | ||||
-rw-r--r-- | src/DotNetOAuth/Util.cs | 17 |
5 files changed, 8 insertions, 173 deletions
diff --git a/src/DotNetOAuth/CommonConsumers/GoogleConsumer.cs b/src/DotNetOAuth/CommonConsumers/GoogleConsumer.cs deleted file mode 100644 index 97367aa..0000000 --- a/src/DotNetOAuth/CommonConsumers/GoogleConsumer.cs +++ /dev/null @@ -1,151 +0,0 @@ -//-----------------------------------------------------------------------
-// <copyright file="GoogleConsumer.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.Xml.Linq;
- using DotNetOAuth.ChannelElements;
- using DotNetOAuth.Messages;
- using DotNetOAuth.Messaging;
-
- /// <summary>
- /// A consumer capable of communicating with Google Data APIs.
- /// </summary>
- public static class GoogleConsumer {
- /// <summary>
- /// The Consumer to use for accessing Google data APIs.
- /// </summary>
- private static readonly ServiceProviderDescription GoogleDescription = new ServiceProviderDescription {
- RequestTokenEndpoint = new MessageReceivingEndpoint("https://www.google.com/accounts/OAuthGetRequestToken", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest),
- UserAuthorizationEndpoint = new MessageReceivingEndpoint("https://www.google.com/accounts/OAuthAuthorizeToken", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest),
- AccessTokenEndpoint = new MessageReceivingEndpoint("https://www.google.com/accounts/OAuthGetAccessToken", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest),
- TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() },
- };
-
- /// <summary>
- /// A mapping between Google's applications and their URI scope values.
- /// </summary>
- private static readonly Dictionary<Applications, string> DataScopeUris = new Dictionary<Applications, string> {
- { Applications.Contacts, "http://www.google.com/m8/feeds/" },
- { Applications.Calendar, "http://www.google.com/calendar/feeds/" },
- };
-
- /// <summary>
- /// The URI to get contacts once authorization is granted.
- /// </summary>
- private static readonly MessageReceivingEndpoint GetContactsEndpoint = new MessageReceivingEndpoint("http://www.google.com/m8/feeds/contacts/default/full/", HttpDeliveryMethods.GetRequest);
-
- /// <summary>
- /// The many specific authorization scopes Google offers.
- /// </summary>
- [Flags]
- public enum Applications : long {
- /// <summary>
- /// The Gmail address book.
- /// </summary>
- Contacts = 0x1,
-
- /// <summary>
- /// Appointments in Google Calendar.
- /// </summary>
- Calendar = 0x2,
- }
-
- /// <summary>
- /// Initializes a new instance of the <see cref="WebConsumer"/> 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="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,
- };
- }
-
- /// <summary>
- /// Requests authorization from Google to access data from a set of Google applications.
- /// </summary>
- /// <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(WebConsumer consumer, Applications requestedAccessScope) {
- Uri callback = MessagingUtilities.GetRequestUrlFromContext().StripQueryArgumentsWithPrefix(Protocol.Default.ParameterPrefix);
- string requestToken;
- var request = RequestAuthorizationInternal(consumer, callback, requestedAccessScope, out requestToken);
- consumer.Channel.Send(request).Send();
- }
-
- /// <summary>
- /// Requests authorization from Google to access data from a set of Google applications.
- /// </summary>
- /// <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>
- /// <param name="requestToken">The unauthorized request token assigned by Google.</param>
- /// <returns>The request token</returns>
- public static Uri RequestAuthorization(DesktopConsumer consumer, Applications requestedAccessScope, out string requestToken) {
- var request = RequestAuthorizationInternal(consumer, null, requestedAccessScope, out requestToken);
- return consumer.Channel.Send(request).DirectUriRequest;
- }
-
- /// <summary>
- /// Gets the Gmail address book's contents.
- /// </summary>
- /// <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 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>
- /// 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 static string GetScopeUri(Applications scope) {
- return string.Join(" ", Util.GetIndividualFlags(scope).Select(app => DataScopeUris[(Applications)app]).ToArray());
- }
-
- /// <summary>
- /// Requests authorization from Google to access data from a set of Google applications.
- /// </summary>
- /// <param name="consumer">The Google consumer previously constructed using <see cref="CreateWebConsumer"/> or <see cref="CreateDesktopConsumer"/>.</param>
- /// <param name="callback">The callback URI, if in web mode.</param>
- /// <param name="requestedAccessScope">The requested access scope.</param>
- /// <param name="requestToken">The unauthorized request token assigned by Google.</param>
- /// <returns>The request token</returns>
- private static UserAuthorizationRequest RequestAuthorizationInternal(ConsumerBase consumer, Uri callback, Applications requestedAccessScope, out string requestToken) {
- if (consumer == null) {
- throw new ArgumentNullException("consumer");
- }
-
- var extraParameters = new Dictionary<string, string> {
- { "scope", GetScopeUri(requestedAccessScope) },
- };
- return consumer.PrepareRequestUserAuthorization(callback, extraParameters, null, out requestToken);
- }
- }
-}
diff --git a/src/DotNetOAuth/DotNetOAuth.csproj b/src/DotNetOAuth/DotNetOAuth.csproj index 22b0e52..5447fd2 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\GoogleConsumer.cs" />
<Compile Include="ConsumerBase.cs" />
<Compile Include="DesktopConsumer.cs" />
<Compile Include="GlobalSuppressions.cs" />
diff --git a/src/DotNetOAuth/Messaging/MessagingUtilities.cs b/src/DotNetOAuth/Messaging/MessagingUtilities.cs index a645ad3..374855d 100644 --- a/src/DotNetOAuth/Messaging/MessagingUtilities.cs +++ b/src/DotNetOAuth/Messaging/MessagingUtilities.cs @@ -19,7 +19,7 @@ namespace DotNetOAuth.Messaging { /// <summary>
/// A grab-bag of utility methods useful for the channel stack of the protocol.
/// </summary>
- internal static class MessagingUtilities {
+ public static class MessagingUtilities {
/// <summary>
/// Adds a set of HTTP headers to an <see cref="HttpResponse"/> instance,
/// taking care to set some headers to the appropriate properties of
@@ -137,7 +137,7 @@ namespace DotNetOAuth.Messaging { /// Cookieless session directory (if applicable) is also included.
/// </summary>
/// <returns>The URL in the user agent's Location bar.</returns>
- internal static Uri GetRequestUrlFromContext() {
+ public static Uri GetRequestUrlFromContext() {
HttpContext context = HttpContext.Current;
if (context == null) {
throw new InvalidOperationException(MessagingStrings.CurrentHttpContextRequired);
@@ -159,7 +159,7 @@ namespace DotNetOAuth.Messaging { /// <param name="uri">The URI that may have a query with parameters to remove.</param>
/// <param name="prefix">The prefix for parameters to remove.</param>
/// <returns>Either a new Uri with the parameters removed if there were any to remove, or the same Uri instance if no parameters needed to be removed.</returns>
- internal static Uri StripQueryArgumentsWithPrefix(this Uri uri, string prefix) {
+ public static Uri StripQueryArgumentsWithPrefix(this Uri uri, string prefix) {
NameValueCollection queryArgs = HttpUtility.ParseQueryString(uri.Query);
var matchingKeys = queryArgs.Keys.OfType<string>().Where(key => key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)).ToList();
if (matchingKeys.Count > 0) {
diff --git a/src/DotNetOAuth/Messaging/Response.cs b/src/DotNetOAuth/Messaging/Response.cs index d837017..2bce887 100644 --- a/src/DotNetOAuth/Messaging/Response.cs +++ b/src/DotNetOAuth/Messaging/Response.cs @@ -92,8 +92,12 @@ namespace DotNetOAuth.Messaging { /// <summary>
/// Gets the URI that, when requested with an HTTP GET request,
- /// would transmit the message that normally be transmitted via a user agent redirect.
+ /// would transmit the message that normally would be transmitted via a user agent redirect.
/// </summary>
+ /// <remarks>
+ /// This is useful for desktop applications that will spawn a user agent to transmit the message
+ /// rather than cause a redirect.
+ /// </remarks>
internal Uri DirectUriRequest {
get {
var message = this.OriginalMessage as IDirectedProtocolMessage;
diff --git a/src/DotNetOAuth/Util.cs b/src/DotNetOAuth/Util.cs index 4a1cab9..27d8299 100644 --- a/src/DotNetOAuth/Util.cs +++ b/src/DotNetOAuth/Util.cs @@ -26,22 +26,5 @@ 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;
- }
- }
- }
}
}
|