diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2010-07-05 07:47:24 -0700 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2010-07-05 07:47:24 -0700 |
commit | 46e4f75e2ea9fb66c6e7a2a32657ad442505b95e (patch) | |
tree | 94a2f7525dfca1f8bd58099cd083581727a52d82 | |
parent | 5d1f7e810dd7169a75098e80031ae89926581042 (diff) | |
download | DotNetOpenAuth-46e4f75e2ea9fb66c6e7a2a32657ad442505b95e.zip DotNetOpenAuth-46e4f75e2ea9fb66c6e7a2a32657ad442505b95e.tar.gz DotNetOpenAuth-46e4f75e2ea9fb66c6e7a2a32657ad442505b95e.tar.bz2 |
Simplified using the web server client a bit (no "token manager" needed any more).
-rw-r--r-- | samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj | 2 | ||||
-rw-r--r-- | samples/DotNetOpenAuth.ApplicationBlock/InMemoryClientAuthorizationTracker.cs (renamed from samples/DotNetOpenAuth.ApplicationBlock/InMemoryClientTokenManager.cs) | 4 | ||||
-rw-r--r-- | samples/DotNetOpenAuth.ApplicationBlock/TokenManager.cs | 2 | ||||
-rw-r--r-- | samples/OAuthConsumer/OAuthConsumer.csproj | 3 | ||||
-rw-r--r-- | samples/OAuthConsumer/SampleWcf2.aspx.cs | 41 | ||||
-rw-r--r-- | src/DotNetOpenAuth/DotNetOpenAuth.csproj | 2 | ||||
-rw-r--r-- | src/DotNetOpenAuth/OAuth2/AuthorizationState.cs | 1 | ||||
-rw-r--r-- | src/DotNetOpenAuth/OAuth2/ClientBase.cs | 6 | ||||
-rw-r--r-- | src/DotNetOpenAuth/OAuth2/IClientAuthorizationTracker.cs (renamed from src/DotNetOpenAuth/OAuth2/IClientTokenManager.cs) | 18 | ||||
-rw-r--r-- | src/DotNetOpenAuth/OAuth2/WebServerClient.cs | 23 |
10 files changed, 50 insertions, 52 deletions
diff --git a/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj b/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj index 7e1a0dd..340c639 100644 --- a/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj +++ b/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj @@ -91,7 +91,7 @@ <Compile Include="Facebook\FacebookClient.cs" /> <Compile Include="Facebook\FacebookGraph.cs" /> <Compile Include="GoogleConsumer.cs" /> - <Compile Include="InMemoryClientTokenManager.cs" /> + <Compile Include="InMemoryClientAuthorizationTracker.cs" /> <Compile Include="InMemoryTokenManager.cs"> <SubType>Code</SubType> </Compile> diff --git a/samples/DotNetOpenAuth.ApplicationBlock/InMemoryClientTokenManager.cs b/samples/DotNetOpenAuth.ApplicationBlock/InMemoryClientAuthorizationTracker.cs index 24144bf..239b3ac 100644 --- a/samples/DotNetOpenAuth.ApplicationBlock/InMemoryClientTokenManager.cs +++ b/samples/DotNetOpenAuth.ApplicationBlock/InMemoryClientAuthorizationTracker.cs @@ -1,5 +1,5 @@ //----------------------------------------------------------------------- -// <copyright file="InMemoryClientTokenManager.cs" company="Andrew Arnott"> +// <copyright file="InMemoryClientAuthorizationTracker.cs" company="Andrew Arnott"> // Copyright (c) Andrew Arnott. All rights reserved. // </copyright> //----------------------------------------------------------------------- @@ -16,7 +16,7 @@ namespace DotNetOpenAuth.ApplicationBlock { using DotNetOpenAuth.OAuth2; #if SAMPLESONLY - internal class InMemoryClientTokenManager : IClientTokenManager { + internal class InMemoryClientAuthorizationTracker : IClientAuthorizationTracker { private readonly Dictionary<int, IAuthorizationState> savedStates = new Dictionary<int, IAuthorizationState>(); private int stateCounter; diff --git a/samples/DotNetOpenAuth.ApplicationBlock/TokenManager.cs b/samples/DotNetOpenAuth.ApplicationBlock/TokenManager.cs index 842045c..50ff85b 100644 --- a/samples/DotNetOpenAuth.ApplicationBlock/TokenManager.cs +++ b/samples/DotNetOpenAuth.ApplicationBlock/TokenManager.cs @@ -8,7 +8,7 @@ namespace DotNetOpenAuth.ApplicationBlock { using System; using DotNetOpenAuth.OAuth2; - public class TokenManager : IClientTokenManager { + public class TokenManager : IClientAuthorizationTracker { public IAuthorizationState GetAuthorizationState(Uri callbackUrl, string clientState) { return new AuthorizationState { Callback = callbackUrl, diff --git a/samples/OAuthConsumer/OAuthConsumer.csproj b/samples/OAuthConsumer/OAuthConsumer.csproj index 4f01f97..fa5acff 100644 --- a/samples/OAuthConsumer/OAuthConsumer.csproj +++ b/samples/OAuthConsumer/OAuthConsumer.csproj @@ -83,9 +83,6 @@ </None> </ItemGroup> <ItemGroup> - <Compile Include="..\DotNetOpenAuth.ApplicationBlock\InMemoryClientTokenManager.cs"> - <Link>Code\InMemoryClientTokenManager.cs</Link> - </Compile> <Compile Include="..\DotNetOpenAuth.ApplicationBlock\InMemoryTokenManager.cs"> <Link>Code\InMemoryTokenManager.cs</Link> </Compile> diff --git a/samples/OAuthConsumer/SampleWcf2.aspx.cs b/samples/OAuthConsumer/SampleWcf2.aspx.cs index a4f0f1c..1f0c291 100644 --- a/samples/OAuthConsumer/SampleWcf2.aspx.cs +++ b/samples/OAuthConsumer/SampleWcf2.aspx.cs @@ -15,17 +15,25 @@ using OAuthConsumer.SampleServiceProvider; public partial class SampleWcf2 : System.Web.UI.Page { - private static InMemoryClientTokenManager tokenManager = new InMemoryClientTokenManager(); + private static AuthorizationServerDescription AuthServerDescription = new AuthorizationServerDescription { + TokenEndpoint = new Uri("http://localhost:65169/OAuth2.ashx/token"), + AuthorizationEndpoint = new Uri("http://localhost:65169/OAuth2.ashx/auth"), + }; private static IAuthorizationState Authorization { get { return (AuthorizationState)HttpContext.Current.Session["Authorization"]; } set { HttpContext.Current.Session["Authorization"] = value; } } + private static WebServerClient Client; + + static SampleWcf2() { + Client = new WebServerClient(AuthServerDescription, "sampleconsumer", "samplesecret"); + } + protected void Page_Load(object sender, EventArgs e) { - var client = CreateClient(); if (!IsPostBack) { - var authorization = client.ProcessUserAuthorization(); + var authorization = Client.ProcessUserAuthorization(); if (authorization != null) { Authorization = authorization; } @@ -33,7 +41,7 @@ // Refresh the access token if it expires and if its lifetime is too short to be of use. if (Authorization != null && Authorization.AccessTokenExpirationUtc.HasValue) { - client.RefreshToken(Authorization, TimeSpan.FromMinutes(1)); + Client.RefreshToken(Authorization, TimeSpan.FromMinutes(1)); } } @@ -43,11 +51,8 @@ select item.Value).ToArray(); string scope = string.Join(" ", scopes); - var client = CreateClient(); - string clientState; - var response = client.PrepareRequestUserAuthorization(tokenManager.NewAuthorization(scope, out clientState)); - response.ClientState = clientState; - client.Channel.Send(response); + var response = Client.PrepareRequestUserAuthorization(scope); + Client.Channel.Send(response); } protected void getNameButton_Click(object sender, EventArgs e) { @@ -76,21 +81,6 @@ } } - private static WebServerClient CreateClient() { - var authServerDescription = new AuthorizationServerDescription { - TokenEndpoint = new Uri("http://localhost:65169/OAuth2.ashx/token"), - AuthorizationEndpoint = new Uri("http://localhost:65169/OAuth2.ashx/auth"), - }; - - var client = new WebServerClient(authServerDescription) { - ClientIdentifier = "sampleconsumer", - ClientSecret = "samplesecret", - TokenManager = tokenManager, - }; - - return client; - } - private T CallService<T>(Func<DataApiClient, T> predicate) { DataApiClient client = new DataApiClient(); ////var serviceEndpoint = new MessageReceivingEndpoint(client.Endpoint.Address.Uri, HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.PostRequest); @@ -99,8 +89,7 @@ } var httpRequest = (HttpWebRequest)WebRequest.Create(client.Endpoint.Address.Uri); - var oauthClient = CreateClient(); - oauthClient.AuthorizeRequest(httpRequest, Authorization.AccessToken); + Client.AuthorizeRequest(httpRequest, Authorization.AccessToken); var httpDetails = new HttpRequestMessageProperty(); httpDetails.Headers[HttpRequestHeader.Authorization] = httpRequest.Headers[HttpRequestHeader.Authorization]; diff --git a/src/DotNetOpenAuth/DotNetOpenAuth.csproj b/src/DotNetOpenAuth/DotNetOpenAuth.csproj index 2b2a069..af24fa3 100644 --- a/src/DotNetOpenAuth/DotNetOpenAuth.csproj +++ b/src/DotNetOpenAuth/DotNetOpenAuth.csproj @@ -329,7 +329,7 @@ http://opensource.org/licenses/ms-pl.html <Compile Include="OAuth2\IAccessTokenAnalyzer.cs" /> <Compile Include="OAuth2\IAuthorizationServer.cs" /> <Compile Include="OAuth2\IAuthorizationState.cs" /> - <Compile Include="OAuth2\IClientTokenManager.cs" /> + <Compile Include="OAuth2\IClientAuthorizationTracker.cs" /> <Compile Include="OAuth2\IConsumerDescription.cs" /> <Compile Include="OAuth2\Messages\AccessProtectedResourceRequest.cs" /> <Compile Include="OAuth2\Messages\AccessTokenAssertionRequest.cs" /> diff --git a/src/DotNetOpenAuth/OAuth2/AuthorizationState.cs b/src/DotNetOpenAuth/OAuth2/AuthorizationState.cs index b6d2ee5..907f6e7 100644 --- a/src/DotNetOpenAuth/OAuth2/AuthorizationState.cs +++ b/src/DotNetOpenAuth/OAuth2/AuthorizationState.cs @@ -10,6 +10,7 @@ namespace DotNetOpenAuth.OAuth2 { /// <summary> /// A simple memory-only copy of an authorization state. /// </summary> + [Serializable] public class AuthorizationState : IAuthorizationState { /// <summary> /// Initializes a new instance of the <see cref="AuthorizationState"/> class. diff --git a/src/DotNetOpenAuth/OAuth2/ClientBase.cs b/src/DotNetOpenAuth/OAuth2/ClientBase.cs index 9be1cee..a63fab3 100644 --- a/src/DotNetOpenAuth/OAuth2/ClientBase.cs +++ b/src/DotNetOpenAuth/OAuth2/ClientBase.cs @@ -24,10 +24,14 @@ namespace DotNetOpenAuth.OAuth2 { /// Initializes a new instance of the <see cref="ClientBase"/> class. /// </summary> /// <param name="authorizationServer">The token issuer.</param> - protected ClientBase(AuthorizationServerDescription authorizationServer) { + /// <param name="clientIdentifier">The client identifier.</param> + /// <param name="clientSecret">The client secret.</param> + protected ClientBase(AuthorizationServerDescription authorizationServer, string clientIdentifier = null, string clientSecret = null) { Contract.Requires<ArgumentNullException>(authorizationServer != null); this.AuthorizationServer = authorizationServer; this.Channel = new OAuthWrapAuthorizationServerChannel(); + this.ClientIdentifier = clientIdentifier; + this.ClientSecret = clientSecret; } /// <summary> diff --git a/src/DotNetOpenAuth/OAuth2/IClientTokenManager.cs b/src/DotNetOpenAuth/OAuth2/IClientAuthorizationTracker.cs index 776d691..97294e6 100644 --- a/src/DotNetOpenAuth/OAuth2/IClientTokenManager.cs +++ b/src/DotNetOpenAuth/OAuth2/IClientAuthorizationTracker.cs @@ -1,5 +1,5 @@ //----------------------------------------------------------------------- -// <copyright file="IClientTokenManager.cs" company="Andrew Arnott"> +// <copyright file="IClientAuthorizationTracker.cs" company="Andrew Arnott"> // Copyright (c) Andrew Arnott. All rights reserved. // </copyright> //----------------------------------------------------------------------- @@ -11,8 +11,8 @@ namespace DotNetOpenAuth.OAuth2 { /// <summary> /// A token manager implemented by some clients to assist in tracking authorization state. /// </summary> - [ContractClass(typeof(IClientTokenManagerContract))] - public interface IClientTokenManager { + [ContractClass(typeof(IClientAuthorizationTrackerContract))] + public interface IClientAuthorizationTracker { /// <summary> /// Gets the state of the authorization for a given callback URL and client state. /// </summary> @@ -23,14 +23,14 @@ namespace DotNetOpenAuth.OAuth2 { } /// <summary> - /// Contract class for the <see cref="IClientTokenManager"/> interface. + /// Contract class for the <see cref="IClientAuthorizationTracker"/> interface. /// </summary> - [ContractClassFor(typeof(IClientTokenManager))] - internal abstract class IClientTokenManagerContract : IClientTokenManager { + [ContractClassFor(typeof(IClientAuthorizationTracker))] + internal abstract class IClientAuthorizationTrackerContract : IClientAuthorizationTracker { /// <summary> - /// Prevents a default instance of the <see cref="IClientTokenManagerContract"/> class from being created. + /// Prevents a default instance of the <see cref="IClientAuthorizationTrackerContract"/> class from being created. /// </summary> - private IClientTokenManagerContract() { + private IClientAuthorizationTrackerContract() { } #region IClientTokenManager Members @@ -43,7 +43,7 @@ namespace DotNetOpenAuth.OAuth2 { /// <returns> /// The authorization state; may be <c>null</c> if no authorization state matches. /// </returns> - IAuthorizationState IClientTokenManager.GetAuthorizationState(Uri callbackUrl, string clientState) { + IAuthorizationState IClientAuthorizationTracker.GetAuthorizationState(Uri callbackUrl, string clientState) { Contract.Requires<ArgumentNullException>(callbackUrl != null); throw new NotImplementedException(); } diff --git a/src/DotNetOpenAuth/OAuth2/WebServerClient.cs b/src/DotNetOpenAuth/OAuth2/WebServerClient.cs index 1d98d7c..467d7d3 100644 --- a/src/DotNetOpenAuth/OAuth2/WebServerClient.cs +++ b/src/DotNetOpenAuth/OAuth2/WebServerClient.cs @@ -23,22 +23,25 @@ namespace DotNetOpenAuth.OAuth2 { /// Initializes a new instance of the <see cref="WebServerClient"/> class. /// </summary> /// <param name="authorizationServer">The authorization server.</param> - public WebServerClient(AuthorizationServerDescription authorizationServer) - : base(authorizationServer) { + /// <param name="clientIdentifier">The client identifier.</param> + /// <param name="clientSecret">The client secret.</param> + public WebServerClient(AuthorizationServerDescription authorizationServer, string clientIdentifier = null, string clientSecret = null) + : base(authorizationServer, clientIdentifier, clientSecret) { } /// <summary> /// Gets or sets the token manager. /// </summary> /// <value>The token manager.</value> - public IClientTokenManager TokenManager { get; set; } + public IClientAuthorizationTracker TokenManager { get; set; } /// <summary> /// Prepares a request for user authorization from an authorization server. /// </summary> /// <returns>The authorization request.</returns> - public EndUserAuthorizationRequest PrepareRequestUserAuthorization() { - return this.PrepareRequestUserAuthorization(new AuthorizationState()); + public EndUserAuthorizationRequest PrepareRequestUserAuthorization(string scope = null) { + var authorizationState = new AuthorizationState { Scope = scope }; + return this.PrepareRequestUserAuthorization(authorizationState); } /// <summary> @@ -78,7 +81,6 @@ namespace DotNetOpenAuth.OAuth2 { public IAuthorizationState ProcessUserAuthorization(HttpRequestInfo request = null) { Contract.Requires<InvalidOperationException>(!string.IsNullOrEmpty(this.ClientIdentifier)); Contract.Requires<InvalidOperationException>(!string.IsNullOrEmpty(this.ClientSecret)); - Contract.Requires<InvalidOperationException>(this.TokenManager != null); if (request == null) { request = this.Channel.GetRequestFromContext(); @@ -87,8 +89,13 @@ namespace DotNetOpenAuth.OAuth2 { IMessageWithClientState response; if (this.Channel.TryReadFromRequest<IMessageWithClientState>(request, out response)) { Uri callback = MessagingUtilities.StripMessagePartsFromQueryString(request.UrlBeforeRewriting, this.Channel.MessageDescriptions.Get(response)); - IAuthorizationState authorizationState = this.TokenManager.GetAuthorizationState(callback, response.ClientState); - ErrorUtilities.VerifyProtocol(authorizationState != null, "Unexpected OAuth authorization response received with callback and client state that does not match an expected value."); + IAuthorizationState authorizationState; + if (this.TokenManager != null) { + authorizationState = this.TokenManager.GetAuthorizationState(callback, response.ClientState); + ErrorUtilities.VerifyProtocol(authorizationState != null, "Unexpected OAuth authorization response received with callback and client state that does not match an expected value."); + } else { + authorizationState = new AuthorizationState { Callback = callback }; + } var success = response as EndUserAuthorizationSuccessResponse; var failure = response as EndUserAuthorizationFailedResponse; ErrorUtilities.VerifyProtocol(success != null || failure != null, MessagingStrings.UnexpectedMessageReceivedOfMany); |