summaryrefslogtreecommitdiffstats
path: root/src/DotNetOAuth/Consumer.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/DotNetOAuth/Consumer.cs')
-rw-r--r--src/DotNetOAuth/Consumer.cs49
1 files changed, 37 insertions, 12 deletions
diff --git a/src/DotNetOAuth/Consumer.cs b/src/DotNetOAuth/Consumer.cs
index be5d4a5..1a99f2e 100644
--- a/src/DotNetOAuth/Consumer.cs
+++ b/src/DotNetOAuth/Consumer.cs
@@ -19,11 +19,22 @@ namespace DotNetOAuth {
/// <summary>
/// Initializes a new instance of the <see cref="Consumer"/> class.
/// </summary>
- public Consumer() {
+ /// <param name="endpoints">The endpoints on the Service Provider.</param>
+ /// <param name="tokenManager">The host's method of storing and recalling tokens and secrets.</param>
+ public Consumer(ServiceProviderEndpoints endpoints, ITokenManager tokenManager) {
+ if (endpoints == null) {
+ throw new ArgumentNullException("endpoints");
+ }
+ if (tokenManager == null) {
+ throw new ArgumentNullException("tokenManager");
+ }
+
this.WebRequestHandler = new StandardWebRequestHandler();
SigningBindingElementBase signingElement = new PlainTextSigningBindingElement();
INonceStore store = new NonceMemoryStore(StandardExpirationBindingElement.DefaultMaximumMessageAge);
this.Channel = new OAuthChannel(signingElement, store, new OAuthMessageTypeProvider(), this.WebRequestHandler);
+ this.ServiceProvider = endpoints;
+ this.TokenManager = tokenManager;
}
/// <summary>
@@ -37,9 +48,14 @@ namespace DotNetOAuth {
public string ConsumerSecret { get; set; }
/// <summary>
- /// Gets or sets the Service Provider that will be accessed.
+ /// Gets the Service Provider that will be accessed.
+ /// </summary>
+ public ServiceProviderEndpoints ServiceProvider { get; private set; }
+
+ /// <summary>
+ /// Gets the persistence store for tokens and secrets.
/// </summary>
- public ServiceProviderEndpoints ServiceProvider { get; set; }
+ public ITokenManager TokenManager { get; private set; }
/// <summary>
/// Gets the pending user agent redirect based message to be sent as an HttpResponse.
@@ -64,37 +80,46 @@ namespace DotNetOAuth {
/// Begins an OAuth authorization request and redirects the user to the Service Provider
/// to provide that authorization.
/// </summary>
- /// <returns>The Request Token Secret.</returns>
- public string RequestUserAuthorization(Uri callback) {
+ /// <param name="callback">
+ /// An optional Consumer URL that the Service Provider should redirect the
+ /// User Agent to upon successful authorization.
+ /// </param>
+ public void RequestUserAuthorization(Uri callback) {
// Obtain an unauthorized request token.
- var requestToken = new RequestTokenMessage(ServiceProvider.RequestTokenEndpoint) {
+ var requestToken = new RequestTokenMessage(this.ServiceProvider.RequestTokenEndpoint) {
ConsumerKey = this.ConsumerKey,
ConsumerSecret = this.ConsumerSecret,
};
var requestTokenResponse = this.Channel.Request<UnauthorizedRequestTokenMessage>(requestToken);
+ this.TokenManager.StoreNewRequestToken(this.ConsumerKey, requestTokenResponse.RequestToken, requestTokenResponse.TokenSecret, null/*TODO*/);
// Request user authorization.
- var requestAuthorization = new DirectUserToServiceProviderMessage(ServiceProvider.UserAuthorizationEndpoint) {
+ var requestAuthorization = new DirectUserToServiceProviderMessage(this.ServiceProvider.UserAuthorizationEndpoint) {
Callback = callback,
RequestToken = requestTokenResponse.RequestToken,
};
this.Channel.Send(requestAuthorization);
this.PendingRequest = this.Channel.DequeueIndirectOrResponseMessage();
- return requestTokenResponse.TokenSecret;
}
- internal GrantAccessTokenMessage ProcessUserAuthorization(string requestTokenSecret) {
+ /// <summary>
+ /// Processes an incoming authorization-granted message from an SP and obtains an access token.
+ /// </summary>
+ /// <returns>The access token.</returns>
+ internal string ProcessUserAuthorization() {
var authorizationMessage = this.Channel.ReadFromRequest<DirectUserToConsumerMessage>();
-
+
// Exchange request token for access token.
- var requestAccess = new RequestAccessTokenMessage(ServiceProvider.AccessTokenEndpoint) {
+ string requestTokenSecret = this.TokenManager.GetTokenSecret(authorizationMessage.RequestToken);
+ var requestAccess = new RequestAccessTokenMessage(this.ServiceProvider.AccessTokenEndpoint) {
RequestToken = authorizationMessage.RequestToken,
TokenSecret = requestTokenSecret,
ConsumerKey = this.ConsumerKey,
ConsumerSecret = this.ConsumerSecret,
};
var grantAccess = this.Channel.Request<GrantAccessTokenMessage>(requestAccess);
- return grantAccess;
+ this.TokenManager.ExpireRequestTokenAndStoreNewAccessToken(this.ConsumerKey, authorizationMessage.RequestToken, grantAccess.AccessToken, grantAccess.TokenSecret);
+ return grantAccess.AccessToken;
}
}
}