Skip to content
This repository was archived by the owner on Mar 20, 2019. It is now read-only.

Commit 38a1162

Browse files
committed
OAuth 1.0 Consumers are now *much* simpler, entirely avoiding channels.
Build breaks in other projects, however.
1 parent 10fc3ad commit 38a1162

37 files changed

Lines changed: 902 additions & 1257 deletions

samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,6 @@
101101
<Compile Include="GoogleConsumer.cs" />
102102
<Compile Include="HttpAsyncHandlerBase.cs" />
103103
<Compile Include="InMemoryClientAuthorizationTracker.cs" />
104-
<Compile Include="InMemoryTokenManager.cs">
105-
<SubType>Code</SubType>
106-
</Compile>
107104
<Compile Include="Properties\AssemblyInfo.cs" />
108105
<Compile Include="TwitterConsumer.cs" />
109106
<Compile Include="Util.cs" />
@@ -138,6 +135,10 @@
138135
<Project>{60426312-6AE5-4835-8667-37EDEA670222}</Project>
139136
<Name>DotNetOpenAuth.Core</Name>
140137
</ProjectReference>
138+
<ProjectReference Include="..\..\src\DotNetOpenAuth.OAuth.Common\DotNetOpenAuth.OAuth.Common.csproj">
139+
<Project>{115217c5-22cd-415c-a292-0dd0238cdd89}</Project>
140+
<Name>DotNetOpenAuth.OAuth.Common</Name>
141+
</ProjectReference>
141142
<ProjectReference Include="..\..\src\DotNetOpenAuth.OAuth.Consumer\DotNetOpenAuth.OAuth.Consumer.csproj">
142143
<Project>{B202E40D-4663-4A2B-ACDA-865F88FF7CAA}</Project>
143144
<Name>DotNetOpenAuth.OAuth.Consumer</Name>

samples/DotNetOpenAuth.ApplicationBlock/GoogleConsumer.cs

Lines changed: 15 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
// </copyright>
55
//-----------------------------------------------------------------------
66

7-
namespace DotNetOpenAuth.ApplicationBlock
8-
{
7+
namespace DotNetOpenAuth.ApplicationBlock {
98
using System;
109
using System.Collections.Generic;
1110
using System.Diagnostics;
@@ -29,17 +28,15 @@ namespace DotNetOpenAuth.ApplicationBlock
2928
/// <summary>
3029
/// A consumer capable of communicating with Google Data APIs.
3130
/// </summary>
32-
public static class GoogleConsumer
33-
{
31+
public class GoogleConsumer : Consumer {
3432
/// <summary>
3533
/// The Consumer to use for accessing Google data APIs.
3634
/// </summary>
37-
public static readonly ServiceProviderDescription ServiceDescription = new ServiceProviderDescription {
38-
RequestTokenEndpoint = new MessageReceivingEndpoint("https://www.google.com/accounts/OAuthGetRequestToken", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest),
39-
UserAuthorizationEndpoint = new MessageReceivingEndpoint("https://www.google.com/accounts/OAuthAuthorizeToken", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest),
40-
AccessTokenEndpoint = new MessageReceivingEndpoint("https://www.google.com/accounts/OAuthGetAccessToken", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest),
41-
TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() },
42-
};
35+
public static readonly ServiceProviderDescription ServiceDescription =
36+
new ServiceProviderDescription(
37+
"https://www.google.com/accounts/OAuthGetRequestToken",
38+
"https://www.google.com/accounts/OAuthAuthorizeToken",
39+
"https://www.google.com/accounts/OAuthGetAccessToken");
4340

4441
/// <summary>
4542
/// A mapping between Google's applications and their URI scope values.
@@ -72,8 +69,7 @@ public static class GoogleConsumer
7269
/// The many specific authorization scopes Google offers.
7370
/// </summary>
7471
[Flags]
75-
public enum Applications : long
76-
{
72+
public enum Applications : long {
7773
/// <summary>
7874
/// The Gmail address book.
7975
/// </summary>
@@ -155,23 +151,7 @@ public enum Applications : long
155151
Maps = 0x8000,
156152
}
157153

158-
/// <summary>
159-
/// The service description to use for accessing Google data APIs using an X509 certificate.
160-
/// </summary>
161-
/// <param name="signingCertificate">The signing certificate.</param>
162-
/// <returns>A service description that can be used to create an instance of
163-
/// <see cref="DesktopConsumer"/> or <see cref="WebConsumer"/>. </returns>
164-
public static ServiceProviderDescription CreateRsaSha1ServiceDescription(X509Certificate2 signingCertificate) {
165-
if (signingCertificate == null) {
166-
throw new ArgumentNullException("signingCertificate");
167-
}
168-
169-
return new ServiceProviderDescription {
170-
RequestTokenEndpoint = new MessageReceivingEndpoint("https://www.google.com/accounts/OAuthGetRequestToken", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest),
171-
UserAuthorizationEndpoint = new MessageReceivingEndpoint("https://www.google.com/accounts/OAuthAuthorizeToken", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest),
172-
AccessTokenEndpoint = new MessageReceivingEndpoint("https://www.google.com/accounts/OAuthGetAccessToken", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest),
173-
TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new RsaSha1ConsumerSigningBindingElement(signingCertificate) },
174-
};
154+
public GoogleConsumer() {
175155
}
176156

177157
/// <summary>
@@ -184,40 +164,12 @@ public static ServiceProviderDescription CreateRsaSha1ServiceDescription(X509Cer
184164
/// A task that completes with the asynchronous operation.
185165
/// </returns>
186166
/// <exception cref="System.ArgumentNullException">consumer</exception>
187-
public static async Task RequestAuthorizationAsync(WebConsumer consumer, Applications requestedAccessScope, CancellationToken cancellationToken = default(CancellationToken)) {
188-
if (consumer == null) {
189-
throw new ArgumentNullException("consumer");
190-
}
191-
167+
public Task<Uri> RequestUserAuthorizationAsync(Applications requestedAccessScope, CancellationToken cancellationToken = default(CancellationToken)) {
192168
var extraParameters = new Dictionary<string, string> {
193169
{ "scope", GetScopeUri(requestedAccessScope) },
194170
};
195171
Uri callback = Util.GetCallbackUrlFromContext();
196-
var request = await consumer.PrepareRequestUserAuthorizationAsync(callback, extraParameters, null, cancellationToken);
197-
var redirectingResponse = await consumer.Channel.PrepareResponseAsync(request, cancellationToken);
198-
redirectingResponse.Send();
199-
}
200-
201-
/// <summary>
202-
/// Requests authorization from Google to access data from a set of Google applications.
203-
/// </summary>
204-
/// <param name="consumer">The Google consumer previously constructed using <see cref="CreateWebConsumer" /> or <see cref="CreateDesktopConsumer" />.</param>
205-
/// <param name="requestedAccessScope">The requested access scope.</param>
206-
/// <param name="cancellationToken">The cancellation token.</param>
207-
/// <returns>
208-
/// The URI to redirect to and the request token.
209-
/// </returns>
210-
/// <exception cref="System.ArgumentNullException">consumer</exception>
211-
public static Task<Tuple<Uri, string>> RequestAuthorizationAsync(DesktopConsumer consumer, Applications requestedAccessScope, CancellationToken cancellationToken = default(CancellationToken)) {
212-
if (consumer == null) {
213-
throw new ArgumentNullException("consumer");
214-
}
215-
216-
var extraParameters = new Dictionary<string, string> {
217-
{ "scope", GetScopeUri(requestedAccessScope) },
218-
};
219-
220-
return consumer.RequestUserAuthorizationAsync(extraParameters, null, cancellationToken);
172+
return this.RequestUserAuthorizationAsync(callback, extraParameters, cancellationToken);
221173
}
222174

223175
/// <summary>
@@ -232,14 +184,10 @@ public static ServiceProviderDescription CreateRsaSha1ServiceDescription(X509Cer
232184
/// An XML document returned by Google.
233185
/// </returns>
234186
/// <exception cref="System.ArgumentNullException">consumer</exception>
235-
public static async Task<XDocument> GetContactsAsync(ConsumerBase consumer, string accessToken, int maxResults = 25, int startIndex = 1, CancellationToken cancellationToken = default(CancellationToken)) {
236-
if (consumer == null) {
237-
throw new ArgumentNullException("consumer");
238-
}
239-
187+
public async Task<XDocument> GetContactsAsync(AccessToken accessToken, int maxResults = 25, int startIndex = 1, CancellationToken cancellationToken = default(CancellationToken)) {
240188
// Enable gzip compression. Google only compresses the response for recognized user agent headers. - Mike Lim
241189
var handler = new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip };
242-
using (var httpClient = consumer.CreateHttpClient(accessToken, handler)) {
190+
using (var httpClient = this.CreateHttpClient(accessToken, handler)) {
243191
var request = new HttpRequestMessage(HttpMethod.Get, GetContactsEndpoint);
244192
request.Content = new FormUrlEncodedContent(
245193
new Dictionary<string, string>() {
@@ -255,7 +203,7 @@ public static ServiceProviderDescription CreateRsaSha1ServiceDescription(X509Cer
255203
}
256204
}
257205

258-
public static async Task PostBlogEntryAsync(ConsumerBase consumer, string accessToken, string blogUrl, string title, XElement body, CancellationToken cancellationToken) {
206+
public async Task PostBlogEntryAsync(AccessToken accessToken, string blogUrl, string title, XElement body, CancellationToken cancellationToken) {
259207
string feedUrl;
260208
var getBlogHome = WebRequest.Create(blogUrl);
261209
using (var blogHomeResponse = getBlogHome.GetResponse()) {
@@ -285,7 +233,7 @@ public static async Task PostBlogEntryAsync(ConsumerBase consumer, string access
285233
var request = new HttpRequestMessage(HttpMethod.Post, feedUrl);
286234
request.Content = new StreamContent(ms);
287235
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/atom+xml");
288-
using (var httpClient = consumer.CreateHttpClient(accessToken)) {
236+
using (var httpClient = this.CreateHttpClient(accessToken)) {
289237
using (var response = await httpClient.SendAsync(request, cancellationToken)) {
290238
if (response.StatusCode == HttpStatusCode.Created) {
291239
// Success

samples/DotNetOpenAuth.ApplicationBlock/InMemoryTokenManager.cs

Lines changed: 0 additions & 147 deletions
This file was deleted.

0 commit comments

Comments
 (0)