summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/DotNetOAuth.Test/Scenarios/Coordinator.cs6
-rw-r--r--src/DotNetOAuth/ClassDiagram.cd31
-rw-r--r--src/DotNetOAuth/ConsumerBase.cs (renamed from src/DotNetOAuth/Consumer.cs)130
-rw-r--r--src/DotNetOAuth/DesktopConsumer.cs54
-rw-r--r--src/DotNetOAuth/DotNetOAuth.csproj4
-rw-r--r--src/DotNetOAuth/WebConsumer.cs99
6 files changed, 211 insertions, 113 deletions
diff --git a/src/DotNetOAuth.Test/Scenarios/Coordinator.cs b/src/DotNetOAuth.Test/Scenarios/Coordinator.cs
index 9b48490..00aa625 100644
--- a/src/DotNetOAuth.Test/Scenarios/Coordinator.cs
+++ b/src/DotNetOAuth.Test/Scenarios/Coordinator.cs
@@ -18,7 +18,7 @@ namespace DotNetOAuth.Test.Scenarios {
internal class Coordinator {
private ConsumerDescription consumerDescription;
private ServiceProviderDescription serviceDescription;
- private Action<Consumer> consumerAction;
+ private Action<WebConsumer> consumerAction;
private Action<ServiceProvider> serviceProviderAction;
/// <summary>Initializes a new instance of the <see cref="Coordinator"/> class.</summary>
@@ -26,7 +26,7 @@ namespace DotNetOAuth.Test.Scenarios {
/// <param name="serviceDescription">The service description that will be used to construct the Consumer and ServiceProvider objects.</param>
/// <param name="consumerAction">The code path of the Consumer.</param>
/// <param name="serviceProviderAction">The code path of the Service Provider.</param>
- internal Coordinator(ConsumerDescription consumerDescription, ServiceProviderDescription serviceDescription, Action<Consumer> consumerAction, Action<ServiceProvider> serviceProviderAction) {
+ internal Coordinator(ConsumerDescription consumerDescription, ServiceProviderDescription serviceDescription, Action<WebConsumer> consumerAction, Action<ServiceProvider> serviceProviderAction) {
if (consumerDescription == null) {
throw new ArgumentNullException("consumerDescription");
}
@@ -67,7 +67,7 @@ namespace DotNetOAuth.Test.Scenarios {
serviceProviderChannel.RemoteChannel = consumerChannel;
// Prepare the Consumer and Service Provider objects
- Consumer consumer = new Consumer(this.serviceDescription, consumerTokenManager) {
+ WebConsumer consumer = new WebConsumer(this.serviceDescription, consumerTokenManager) {
Channel = consumerChannel,
ConsumerKey = this.consumerDescription.ConsumerKey,
ConsumerSecret = this.consumerDescription.ConsumerSecret,
diff --git a/src/DotNetOAuth/ClassDiagram.cd b/src/DotNetOAuth/ClassDiagram.cd
index ac54a5f..d8baa13 100644
--- a/src/DotNetOAuth/ClassDiagram.cd
+++ b/src/DotNetOAuth/ClassDiagram.cd
@@ -7,11 +7,12 @@
<FileName>ServiceProvider.cs</FileName>
</TypeIdentifier>
</Class>
- <Class Name="DotNetOAuth.Consumer">
- <Position X="6.5" Y="0.5" Width="4.75" />
+ <Class Name="DotNetOAuth.DesktopConsumer">
+ <Position X="11.5" Y="4.5" Width="4.75" />
<TypeIdentifier>
- <HashCode>AAAAAAAgAAABEAAAAIAABgAAACAIAAQAAYAAAAAAAAA=</HashCode>
- <FileName>Consumer.cs</FileName>
+ <HashCode>AAAAAAAAAAAAEAAAAAAABAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
+ <FileName>DesktopConsumer.cs</FileName>
+ <NewMemberFileName>WebConsumer.cs</NewMemberFileName>
</TypeIdentifier>
</Class>
<Class Name="DotNetOAuth.ServiceProviderDescription">
@@ -24,5 +25,27 @@
<FileName>ServiceProviderDescription.cs</FileName>
</TypeIdentifier>
</Class>
+ <Class Name="DotNetOAuth.WebConsumer">
+ <Position X="6.5" Y="4.5" Width="4.75" />
+ <InheritanceLine Type="DotNetOAuth.ConsumerBase" FixedToPoint="true">
+ <Path>
+ <Point X="8.875" Y="3.978" />
+ <Point X="8.875" Y="4.2" />
+ <Point X="8.125" Y="4.2" />
+ <Point X="8.125" Y="4.5" />
+ </Path>
+ </InheritanceLine>
+ <TypeIdentifier>
+ <HashCode>AAAAAAAAAAAAEAAAAAAABAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
+ <FileName>WebConsumer.cs</FileName>
+ </TypeIdentifier>
+ </Class>
+ <Class Name="DotNetOAuth.ConsumerBase">
+ <Position X="6.5" Y="0.5" Width="4.75" />
+ <TypeIdentifier>
+ <HashCode>ACAAAAAgAAABEAAAAAAABgAAAAAIAAQAAQAAAAAAABA=</HashCode>
+ <FileName>ConsumerBase.cs</FileName>
+ </TypeIdentifier>
+ </Class>
<Font Name="Segoe UI" Size="9" />
</ClassDiagram> \ No newline at end of file
diff --git a/src/DotNetOAuth/Consumer.cs b/src/DotNetOAuth/ConsumerBase.cs
index 9000a08..9b3b922 100644
--- a/src/DotNetOAuth/Consumer.cs
+++ b/src/DotNetOAuth/ConsumerBase.cs
@@ -1,5 +1,5 @@
//-----------------------------------------------------------------------
-// <copyright file="Consumer.cs" company="Andrew Arnott">
+// <copyright file="ConsumerBase.cs" company="Andrew Arnott">
// Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
@@ -15,19 +15,15 @@ namespace DotNetOAuth {
using DotNetOAuth.Messaging.Bindings;
/// <summary>
- /// A website or application that uses OAuth to access the Service Provider on behalf of the User.
+ /// Base class for <see cref="WebConsumer"/> and <see cref="DesktopConsumer"/> types.
/// </summary>
- /// <remarks>
- /// The methods on this class are thread-safe. Provided the properties are set and not changed
- /// afterward, a single instance of this class may be used by an entire web application safely.
- /// </remarks>
- public class Consumer {
+ public class ConsumerBase {
/// <summary>
- /// Initializes a new instance of the <see cref="Consumer"/> class.
+ /// Initializes a new instance of the <see cref="ConsumerBase"/> class.
/// </summary>
/// <param name="serviceDescription">The endpoints and behavior of the Service Provider.</param>
/// <param name="tokenManager">The host's method of storing and recalling tokens and secrets.</param>
- public Consumer(ServiceProviderDescription serviceDescription, ITokenManager tokenManager) {
+ protected ConsumerBase(ServiceProviderDescription serviceDescription, ITokenManager tokenManager) {
if (serviceDescription == null) {
throw new ArgumentNullException("serviceDescription");
}
@@ -79,85 +75,6 @@ namespace DotNetOAuth {
internal OAuthChannel Channel { get; set; }
/// <summary>
- /// Begins an OAuth authorization request and redirects the user to the Service Provider
- /// to provide that authorization. Upon successful authorization, the user is redirected
- /// back to the current page.
- /// </summary>
- /// <returns>The pending user agent redirect based message to be sent as an HttpResponse.</returns>
- /// <remarks>
- /// Requires HttpContext.Current.
- /// </remarks>
- public Response RequestUserAuthorization() {
- Uri callback = MessagingUtilities.GetRequestUrlFromContext().StripQueryArgumentsWithPrefix(Protocol.Default.ParameterPrefix);
- return this.RequestUserAuthorization(callback, null, null);
- }
-
- /// <summary>
- /// Begins an OAuth authorization request and redirects the user to the Service Provider
- /// to provide that authorization.
- /// </summary>
- /// <param name="callback">
- /// An optional Consumer URL that the Service Provider should redirect the
- /// User Agent to upon successful authorization.
- /// </param>
- /// <param name="requestParameters">Extra parameters to add to the request token message. Optional.</param>
- /// <param name="redirectParameters">Extra parameters to add to the redirect to Service Provider message. Optional.</param>
- /// <returns>The pending user agent redirect based message to be sent as an HttpResponse.</returns>
- public Response RequestUserAuthorization(Uri callback, IDictionary<string, string> requestParameters, IDictionary<string, string> redirectParameters) {
- string token;
- return this.RequestUserAuthorization(callback, requestParameters, redirectParameters, out token);
- }
-
- /// <summary>
- /// Begins an OAuth authorization request from a desktop client app.
- /// </summary>
- /// <param name="requestParameters">Extra parameters to add to the request token message. Optional.</param>
- /// <param name="redirectParameters">Extra parameters to add to the redirect to Service Provider message. Optional.</param>
- /// <param name="requestToken">The request token that must be exchanged for an access token after the user has provided authorization.</param>
- /// <returns>The URL to open a browser window to allow the user to provide authorization.</returns>
- public Uri RequestUserAuthorization(IDictionary<string, string> requestParameters, IDictionary<string, string> redirectParameters, out string requestToken) {
- return this.RequestUserAuthorization(null, requestParameters, redirectParameters, out requestToken).DirectUriRequest;
- }
-
- /// <summary>
- /// Processes an incoming authorization-granted message from an SP and obtains an access token.
- /// </summary>
- /// <returns>The access token, or null if no incoming authorization message was recognized.</returns>
- /// <remarks>
- /// Requires HttpContext.Current.
- /// </remarks>
- public GrantAccessTokenMessage ProcessUserAuthorization() {
- return this.ProcessUserAuthorization(this.Channel.GetRequestFromContext());
- }
-
- /// <summary>
- /// Processes an incoming authorization-granted message from an SP and obtains an access token.
- /// </summary>
- /// <param name="request">The incoming HTTP request.</param>
- /// <returns>The access token, or null if no incoming authorization message was recognized.</returns>
- public GrantAccessTokenMessage ProcessUserAuthorization(HttpRequest request) {
- return this.ProcessUserAuthorization(new HttpRequestInfo(request));
- }
-
- /// <summary>
- /// Exchanges a given request token for access token.
- /// </summary>
- /// <param name="requestToken">The request token that the user has authorized.</param>
- /// <returns>The access token assigned by the Service Provider.</returns>
- public GrantAccessTokenMessage ProcessUserAuthorization(string requestToken) {
- string requestTokenSecret = this.TokenManager.GetTokenSecret(requestToken);
- var requestAccess = new GetAccessTokenMessage(this.ServiceProvider.AccessTokenEndpoint) {
- RequestToken = requestToken,
- TokenSecret = requestTokenSecret,
- ConsumerKey = this.ConsumerKey,
- ConsumerSecret = this.ConsumerSecret,
- };
- var grantAccess = this.Channel.Request<GrantAccessTokenMessage>(requestAccess);
- this.TokenManager.ExpireRequestTokenAndStoreNewAccessToken(this.ConsumerKey, requestToken, grantAccess.AccessToken, grantAccess.TokenSecret);
- return grantAccess;
- }
-
- /// <summary>
/// Creates a web request prepared with OAuth authorization
/// that may be further tailored by adding parameters by the caller.
/// </summary>
@@ -196,7 +113,7 @@ namespace DotNetOAuth {
/// <param name="redirectParameters">Extra parameters to add to the redirect to Service Provider message. Optional.</param>
/// <param name="token">The request token that must be exchanged for an access token after the user has provided authorization.</param>
/// <returns>The pending user agent redirect based message to be sent as an HttpResponse.</returns>
- internal Response RequestUserAuthorization(Uri callback, IDictionary<string, string> requestParameters, IDictionary<string, string> redirectParameters, out string token) {
+ protected internal Response RequestUserAuthorization(Uri callback, IDictionary<string, string> requestParameters, IDictionary<string, string> redirectParameters, out string token) {
// Obtain an unauthorized request token.
var requestToken = new GetRequestTokenMessage(this.ServiceProvider.RequestTokenEndpoint) {
ConsumerKey = this.ConsumerKey,
@@ -217,28 +134,13 @@ namespace DotNetOAuth {
}
/// <summary>
- /// Processes an incoming authorization-granted message from an SP and obtains an access token.
- /// </summary>
- /// <param name="request">The incoming HTTP request.</param>
- /// <returns>The access token, or null if no incoming authorization message was recognized.</returns>
- internal GrantAccessTokenMessage ProcessUserAuthorization(HttpRequestInfo request) {
- DirectUserToConsumerMessage authorizationMessage;
- if (this.Channel.TryReadFromRequest<DirectUserToConsumerMessage>(request, out authorizationMessage)) {
- string requestToken = authorizationMessage.RequestToken;
- return this.ProcessUserAuthorization(requestToken);
- } else {
- return null;
- }
- }
-
- /// <summary>
/// Creates a web request prepared with OAuth authorization
/// that may be further tailored by adding parameters by the caller.
/// </summary>
/// <param name="endpoint">The URL and method on the Service Provider to send the request to.</param>
/// <param name="accessToken">The access token that permits access to the protected resource.</param>
/// <returns>The initialized WebRequest object.</returns>
- internal AccessProtectedResourceMessage CreateAuthorizingMessage(MessageReceivingEndpoint endpoint, string accessToken) {
+ protected internal AccessProtectedResourceMessage CreateAuthorizingMessage(MessageReceivingEndpoint endpoint, string accessToken) {
if (endpoint == null) {
throw new ArgumentNullException("endpoint");
}
@@ -255,5 +157,23 @@ namespace DotNetOAuth {
return message;
}
+
+ /// <summary>
+ /// Exchanges a given request token for access token.
+ /// </summary>
+ /// <param name="requestToken">The request token that the user has authorized.</param>
+ /// <returns>The access token assigned by the Service Provider.</returns>
+ protected GrantAccessTokenMessage ProcessUserAuthorization(string requestToken) {
+ string requestTokenSecret = this.TokenManager.GetTokenSecret(requestToken);
+ var requestAccess = new GetAccessTokenMessage(this.ServiceProvider.AccessTokenEndpoint) {
+ RequestToken = requestToken,
+ TokenSecret = requestTokenSecret,
+ ConsumerKey = this.ConsumerKey,
+ ConsumerSecret = this.ConsumerSecret,
+ };
+ var grantAccess = this.Channel.Request<GrantAccessTokenMessage>(requestAccess);
+ this.TokenManager.ExpireRequestTokenAndStoreNewAccessToken(this.ConsumerKey, requestToken, grantAccess.AccessToken, grantAccess.TokenSecret);
+ return grantAccess;
+ }
}
}
diff --git a/src/DotNetOAuth/DesktopConsumer.cs b/src/DotNetOAuth/DesktopConsumer.cs
new file mode 100644
index 0000000..99f3765
--- /dev/null
+++ b/src/DotNetOAuth/DesktopConsumer.cs
@@ -0,0 +1,54 @@
+//-----------------------------------------------------------------------
+// <copyright file="DesktopConsumer.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOAuth {
+ using System;
+ using System.Collections.Generic;
+ using System.Net;
+ using System.Web;
+ using DotNetOAuth.ChannelElements;
+ using DotNetOAuth.Messages;
+ using DotNetOAuth.Messaging;
+ using DotNetOAuth.Messaging.Bindings;
+
+ /// <summary>
+ /// Used by a desktop application to use OAuth to access the Service Provider on behalf of the User.
+ /// </summary>
+ /// <remarks>
+ /// The methods on this class are thread-safe. Provided the properties are set and not changed
+ /// afterward, a single instance of this class may be used by an entire web application safely.
+ /// </remarks>
+ public class DesktopConsumer : ConsumerBase {
+ /// <summary>
+ /// Initializes a new instance of the <see cref="DesktopConsumer"/> class.
+ /// </summary>
+ /// <param name="serviceDescription">The endpoints and behavior of the Service Provider.</param>
+ /// <param name="tokenManager">The host's method of storing and recalling tokens and secrets.</param>
+ public DesktopConsumer(ServiceProviderDescription serviceDescription, ITokenManager tokenManager)
+ : base(serviceDescription, tokenManager) {
+ }
+
+ /// <summary>
+ /// Begins an OAuth authorization request.
+ /// </summary>
+ /// <param name="requestParameters">Extra parameters to add to the request token message. Optional.</param>
+ /// <param name="redirectParameters">Extra parameters to add to the redirect to Service Provider message. Optional.</param>
+ /// <param name="requestToken">The request token that must be exchanged for an access token after the user has provided authorization.</param>
+ /// <returns>The URL to open a browser window to allow the user to provide authorization.</returns>
+ public Uri RequestUserAuthorization(IDictionary<string, string> requestParameters, IDictionary<string, string> redirectParameters, out string requestToken) {
+ return this.RequestUserAuthorization(null, requestParameters, redirectParameters, out requestToken).DirectUriRequest;
+ }
+
+ /// <summary>
+ /// Exchanges a given request token for access token.
+ /// </summary>
+ /// <param name="requestToken">The request token that the user has authorized.</param>
+ /// <returns>The access token assigned by the Service Provider.</returns>
+ public new GrantAccessTokenMessage ProcessUserAuthorization(string requestToken) {
+ return base.ProcessUserAuthorization(requestToken);
+ }
+ }
+}
diff --git a/src/DotNetOAuth/DotNetOAuth.csproj b/src/DotNetOAuth/DotNetOAuth.csproj
index 43e0849..4238e0c 100644
--- a/src/DotNetOAuth/DotNetOAuth.csproj
+++ b/src/DotNetOAuth/DotNetOAuth.csproj
@@ -69,13 +69,15 @@
<Compile Include="ChannelElements\SigningBindingElementChain.cs" />
<Compile Include="ChannelElements\StandardTokenGenerator.cs" />
<Compile Include="ChannelElements\TokenType.cs" />
+ <Compile Include="ConsumerBase.cs" />
+ <Compile Include="DesktopConsumer.cs" />
<Compile Include="Messaging\ITamperProtectionChannelBindingElement.cs" />
<Compile Include="ServiceProviderDescription.cs" />
<Compile Include="Messages\ITokenContainingMessage.cs" />
<Compile Include="Messages\SignedMessageBase.cs" />
<Compile Include="Messaging\Bindings\NonceMemoryStore.cs" />
<Compile Include="ChannelElements\SigningBindingElementBase.cs" />
- <Compile Include="Consumer.cs" />
+ <Compile Include="WebConsumer.cs" />
<Compile Include="ChannelElements\IWebRequestHandler.cs" />
<Compile Include="ChannelElements\ITamperResistantOAuthMessage.cs" />
<Compile Include="Messages\MessageBase.cs" />
diff --git a/src/DotNetOAuth/WebConsumer.cs b/src/DotNetOAuth/WebConsumer.cs
new file mode 100644
index 0000000..d1de7f1
--- /dev/null
+++ b/src/DotNetOAuth/WebConsumer.cs
@@ -0,0 +1,99 @@
+//-----------------------------------------------------------------------
+// <copyright file="WebConsumer.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOAuth {
+ using System;
+ using System.Collections.Generic;
+ using System.Net;
+ using System.Web;
+ using DotNetOAuth.ChannelElements;
+ using DotNetOAuth.Messages;
+ using DotNetOAuth.Messaging;
+ using DotNetOAuth.Messaging.Bindings;
+
+ /// <summary>
+ /// A website or application that uses OAuth to access the Service Provider on behalf of the User.
+ /// </summary>
+ /// <remarks>
+ /// The methods on this class are thread-safe. Provided the properties are set and not changed
+ /// afterward, a single instance of this class may be used by an entire web application safely.
+ /// </remarks>
+ public class WebConsumer : ConsumerBase {
+ /// <summary>
+ /// Initializes a new instance of the <see cref="WebConsumer"/> class.
+ /// </summary>
+ /// <param name="serviceDescription">The endpoints and behavior of the Service Provider.</param>
+ /// <param name="tokenManager">The host's method of storing and recalling tokens and secrets.</param>
+ public WebConsumer(ServiceProviderDescription serviceDescription, ITokenManager tokenManager)
+ : base(serviceDescription, tokenManager) {
+ }
+
+ /// <summary>
+ /// Begins an OAuth authorization request and redirects the user to the Service Provider
+ /// to provide that authorization. Upon successful authorization, the user is redirected
+ /// back to the current page.
+ /// </summary>
+ /// <returns>The pending user agent redirect based message to be sent as an HttpResponse.</returns>
+ /// <remarks>
+ /// Requires HttpContext.Current.
+ /// </remarks>
+ public Response RequestUserAuthorization() {
+ Uri callback = MessagingUtilities.GetRequestUrlFromContext().StripQueryArgumentsWithPrefix(Protocol.Default.ParameterPrefix);
+ return this.RequestUserAuthorization(callback, null, null);
+ }
+
+ /// <summary>
+ /// Begins an OAuth authorization request and redirects the user to the Service Provider
+ /// to provide that authorization.
+ /// </summary>
+ /// <param name="callback">
+ /// An optional Consumer URL that the Service Provider should redirect the
+ /// User Agent to upon successful authorization.
+ /// </param>
+ /// <param name="requestParameters">Extra parameters to add to the request token message. Optional.</param>
+ /// <param name="redirectParameters">Extra parameters to add to the redirect to Service Provider message. Optional.</param>
+ /// <returns>The pending user agent redirect based message to be sent as an HttpResponse.</returns>
+ public Response RequestUserAuthorization(Uri callback, IDictionary<string, string> requestParameters, IDictionary<string, string> redirectParameters) {
+ string token;
+ return this.RequestUserAuthorization(callback, requestParameters, redirectParameters, out token);
+ }
+
+ /// <summary>
+ /// Processes an incoming authorization-granted message from an SP and obtains an access token.
+ /// </summary>
+ /// <returns>The access token, or null if no incoming authorization message was recognized.</returns>
+ /// <remarks>
+ /// Requires HttpContext.Current.
+ /// </remarks>
+ public GrantAccessTokenMessage ProcessUserAuthorization() {
+ return this.ProcessUserAuthorization(this.Channel.GetRequestFromContext());
+ }
+
+ /// <summary>
+ /// Processes an incoming authorization-granted message from an SP and obtains an access token.
+ /// </summary>
+ /// <param name="request">The incoming HTTP request.</param>
+ /// <returns>The access token, or null if no incoming authorization message was recognized.</returns>
+ public GrantAccessTokenMessage ProcessUserAuthorization(HttpRequest request) {
+ return this.ProcessUserAuthorization(new HttpRequestInfo(request));
+ }
+
+ /// <summary>
+ /// Processes an incoming authorization-granted message from an SP and obtains an access token.
+ /// </summary>
+ /// <param name="request">The incoming HTTP request.</param>
+ /// <returns>The access token, or null if no incoming authorization message was recognized.</returns>
+ internal GrantAccessTokenMessage ProcessUserAuthorization(HttpRequestInfo request) {
+ DirectUserToConsumerMessage authorizationMessage;
+ if (this.Channel.TryReadFromRequest<DirectUserToConsumerMessage>(request, out authorizationMessage)) {
+ string requestToken = authorizationMessage.RequestToken;
+ return this.ProcessUserAuthorization(requestToken);
+ } else {
+ return null;
+ }
+ }
+ }
+}