diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2009-09-15 21:26:15 -0700 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2009-09-15 21:26:15 -0700 |
commit | 77df177bea564d0ef7c8eb9497b2f6cb2ffcbf2d (patch) | |
tree | 9a6aa0a83e99783f07847945435a53b2c9a71930 /src | |
parent | 5f0bea6265bfae7f0c134173cabb5293375eaee4 (diff) | |
download | DotNetOpenAuth-77df177bea564d0ef7c8eb9497b2f6cb2ffcbf2d.zip DotNetOpenAuth-77df177bea564d0ef7c8eb9497b2f6cb2ffcbf2d.tar.gz DotNetOpenAuth-77df177bea564d0ef7c8eb9497b2f6cb2ffcbf2d.tar.bz2 |
Work on a WebConsumer class for Simple API Auth.
Diffstat (limited to 'src')
6 files changed, 136 insertions, 9 deletions
diff --git a/src/DotNetOpenAuth/DotNetOpenAuth.csproj b/src/DotNetOpenAuth/DotNetOpenAuth.csproj index 5a0de45..ec3b3ad 100644 --- a/src/DotNetOpenAuth/DotNetOpenAuth.csproj +++ b/src/DotNetOpenAuth/DotNetOpenAuth.csproj @@ -530,6 +530,8 @@ <DesignTime>True</DesignTime> <DependentUpon>SimpleAuthStrings.resx</DependentUpon> </Compile> + <Compile Include="SimpleAuth\TokenIssuerDescription.cs" /> + <Compile Include="SimpleAuth\WebConsumer.cs" /> <Compile Include="Util.cs" /> <Compile Include="OAuth\Protocol.cs" /> <Compile Include="OAuth\ServiceProvider.cs" /> diff --git a/src/DotNetOpenAuth/SimpleAuth/ConsumerBase.cs b/src/DotNetOpenAuth/SimpleAuth/ConsumerBase.cs index 02582dc..e36a006 100644 --- a/src/DotNetOpenAuth/SimpleAuth/ConsumerBase.cs +++ b/src/DotNetOpenAuth/SimpleAuth/ConsumerBase.cs @@ -19,14 +19,44 @@ namespace DotNetOpenAuth.SimpleAuth { /// <summary> /// Initializes a new instance of the <see cref="ConsumerBase"/> class. /// </summary> - protected ConsumerBase() { + protected ConsumerBase(TokenIssuerDescription tokenIssuer) { + ErrorUtilities.VerifyArgumentNotNull(tokenIssuer, "tokenIssuer"); + this.TokenIssuer = tokenIssuer; } /// <summary> + /// Initializes a new instance of the <see cref="ConsumerBase"/> class. + /// </summary> + /// <param name="tokenIssuerEndpoint">The token issuer endpoint.</param> + protected ConsumerBase(Uri tokenIssuerEndpoint) + : this(new TokenIssuerDescription(tokenIssuerEndpoint)) { + } + + /// <summary> + /// Initializes a new instance of the <see cref="ConsumerBase"/> class. + /// </summary> + /// <param name="tokenIssuerEndpoint">The token issuer endpoint.</param> + protected ConsumerBase(string tokenIssuerEndpoint) + : this(new Uri(tokenIssuerEndpoint)) { + } + + /// <summary> + /// Gets or sets the token issuer. + /// </summary> + /// <value>The token issuer.</value> + public TokenIssuerDescription TokenIssuer { get; private set; } + + /// <summary> + /// Gets or sets the channel. + /// </summary> + /// <value>The channel.</value> + public Channel Channel { get; private set; } + + /// <summary> /// Adds the necessary HTTP header to an HTTP request for protected resources /// so that the Service Provider will allow the request through. /// </summary> - /// <param name="request">The request for protected resources.</param> + /// <param name="request">The request for protected resources from the service provider.</param> /// <param name="accessToken">The access token previously obtained from the Token Issuer.</param> public static void AuthorizeRequest(HttpWebRequest request, string accessToken) { ErrorUtilities.VerifyArgumentNotNull(request, "request"); diff --git a/src/DotNetOpenAuth/SimpleAuth/Messages/UserAuthorizationInUserAgentDeniedResponse.cs b/src/DotNetOpenAuth/SimpleAuth/Messages/UserAuthorizationInUserAgentDeniedResponse.cs index bbbdc60..68c9deb 100644 --- a/src/DotNetOpenAuth/SimpleAuth/Messages/UserAuthorizationInUserAgentDeniedResponse.cs +++ b/src/DotNetOpenAuth/SimpleAuth/Messages/UserAuthorizationInUserAgentDeniedResponse.cs @@ -13,7 +13,7 @@ namespace DotNetOpenAuth.SimpleAuth.Messages { /// following the user's denial to grant Consumer with authorization of /// access to requested resources. /// </summary> - internal class UserAuthorizationInUserAgentDeniedResponse : MessageBase, IDirectedProtocolMessage { + public class UserAuthorizationInUserAgentDeniedResponse : MessageBase, IDirectedProtocolMessage { /// <summary> /// A constant parameter that indicates the user refused to grant the requested authorization. /// </summary> @@ -39,6 +39,6 @@ namespace DotNetOpenAuth.SimpleAuth.Messages { /// If this value is present, the Token Issuer MUST return it to the Consumer's callback URL. /// </remarks> [MessagePart(Protocol.sa_consumer_state, IsRequired = false, AllowEmpty = true)] - internal string ConsumerState { get; set; } + public string ConsumerState { get; set; } } } diff --git a/src/DotNetOpenAuth/SimpleAuth/Messages/UserAuthorizationInUserAgentRequest.cs b/src/DotNetOpenAuth/SimpleAuth/Messages/UserAuthorizationInUserAgentRequest.cs index fb30fcf..dde764a 100644 --- a/src/DotNetOpenAuth/SimpleAuth/Messages/UserAuthorizationInUserAgentRequest.cs +++ b/src/DotNetOpenAuth/SimpleAuth/Messages/UserAuthorizationInUserAgentRequest.cs @@ -14,13 +14,13 @@ namespace DotNetOpenAuth.SimpleAuth.Messages { /// to get the Token Issuer to obtain authorization from the user and prepare /// to issue an access token to the Consumer if permission is granted. /// </summary> - internal class UserAuthorizationInUserAgentRequest : MessageBase, IDirectedProtocolMessage { + public class UserAuthorizationInUserAgentRequest : MessageBase, IDirectedProtocolMessage { /// <summary> /// Initializes a new instance of the <see cref="UserAuthorizationInUserAgentRequest"/> class. /// </summary> /// <param name="tokenIssuer">The token issuer URL to direct the user to.</param> /// <param name="version">The protocol version.</param> - internal UserAuthorizationInUserAgentRequest(Uri tokenIssuer, Version version) + public UserAuthorizationInUserAgentRequest(Uri tokenIssuer, Version version) : base(version, MessageTransport.Indirect, tokenIssuer) { } @@ -29,7 +29,7 @@ namespace DotNetOpenAuth.SimpleAuth.Messages { /// </summary> /// <value>The consumer key.</value> [MessagePart(Protocol.sa_consumer_key, IsRequired = true, AllowEmpty = false)] - internal string ConsumerKey { get; set; } + public string ConsumerKey { get; set; } /// <summary> /// Gets or sets the callback URL. @@ -43,7 +43,7 @@ namespace DotNetOpenAuth.SimpleAuth.Messages { /// will receive the Delegation Code out of band. /// </remarks> [MessagePart(Protocol.sa_callback, IsRequired = true, AllowEmpty = false, Encoder = typeof(UriOrOutOfBandEncoding))] - internal Uri Callback { get; set; } + public Uri Callback { get; set; } /// <summary> /// Gets or sets the state of the consumer. @@ -55,6 +55,6 @@ namespace DotNetOpenAuth.SimpleAuth.Messages { /// If this value is present, the Token Issuer MUST return it to the Consumer's callback URL. /// </remarks> [MessagePart(Protocol.sa_consumer_state, IsRequired = false, AllowEmpty = true)] - internal string ConsumerState { get; set; } + public string ConsumerState { get; set; } } } diff --git a/src/DotNetOpenAuth/SimpleAuth/TokenIssuerDescription.cs b/src/DotNetOpenAuth/SimpleAuth/TokenIssuerDescription.cs new file mode 100644 index 0000000..fd8912b --- /dev/null +++ b/src/DotNetOpenAuth/SimpleAuth/TokenIssuerDescription.cs @@ -0,0 +1,45 @@ +//----------------------------------------------------------------------- +// <copyright file="TokenIssuerDescription.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.SimpleAuth { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + + /// <summary> + /// A description of a Simple API Auth Token Issuer. + /// </summary> + public class TokenIssuerDescription { + /// <summary> + /// Initializes a new instance of the <see cref="TokenIssuerDescription"/> class. + /// </summary> + public TokenIssuerDescription() { + this.Version = Protocol.DefaultVersion; + } + + /// <summary> + /// Initializes a new instance of the <see cref="TokenIssuerDescription"/> class. + /// </summary> + /// <param name="endpointUrl">The endpoint URL of the Token Issuer.</param> + public TokenIssuerDescription(Uri endpointUrl) + : this() { + this.EndpointUrl = endpointUrl; + } + + /// <summary> + /// Gets or sets the endpoint URL of the Token Issuer. + /// </summary> + /// <value>The endpoint URL.</value> + public Uri EndpointUrl { get; set; } + + /// <summary> + /// Gets or sets the version of the Simple API Auth protocol to use with this Token Issuer. + /// </summary> + /// <value>The version.</value> + public Version Version { get; set; } + } +} diff --git a/src/DotNetOpenAuth/SimpleAuth/WebConsumer.cs b/src/DotNetOpenAuth/SimpleAuth/WebConsumer.cs new file mode 100644 index 0000000..6eb4713 --- /dev/null +++ b/src/DotNetOpenAuth/SimpleAuth/WebConsumer.cs @@ -0,0 +1,50 @@ +//----------------------------------------------------------------------- +// <copyright file="WebConsumer.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.SimpleAuth { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using DotNetOpenAuth.SimpleAuth.Messages; + using DotNetOpenAuth.Messaging; + + public class WebConsumer : ConsumerBase { + public WebConsumer(TokenIssuerDescription tokenIssuer) + : base(tokenIssuer) { + } + + public WebConsumer(Uri tokenIssuerEndpoint) + : base(tokenIssuerEndpoint) { + } + + public WebConsumer(string tokenIssuerEndpoint) + : base(tokenIssuerEndpoint) { + } + + public UserAuthorizationInUserAgentRequest PrepareRequestUserAuthorization(string consumerKey) { + var request = new UserAuthorizationInUserAgentRequest(this.TokenIssuer.EndpointUrl, this.TokenIssuer.Version); + request.ConsumerKey = consumerKey; + request.Callback = this.Channel.GetRequestFromContext().UrlBeforeRewriting; + return request; + } + + public IDirectedProtocolMessage ProcessUserAuthorization() { + return this.ProcessUserAuthorization(this.Channel.GetRequestFromContext()); + } + + public IDirectedProtocolMessage ProcessUserAuthorization(HttpRequestInfo request) { + ErrorUtilities.VerifyArgumentNotNull(request, "request"); + IDirectedProtocolMessage message = this.Channel.ReadFromRequest(); + if (message != null) { + ErrorUtilities.VerifyProtocol( + message is UserAuthorizationInUserAgentGrantedResponse || message is UserAuthorizationInUserAgentDeniedResponse, + MessagingStrings.UnexpectedMessageReceivedOfMany); + } + return message; + } + } +} |