//----------------------------------------------------------------------- // // Copyright (c) Outercurve Foundation. All rights reserved. // //----------------------------------------------------------------------- namespace DotNetOpenAuth.OAuth { using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using System.Web; using DotNetOpenAuth.Messaging; using DotNetOpenAuth.OAuth.ChannelElements; using DotNetOpenAuth.OAuth.Messages; using Validation; /// /// A website or application that uses OAuth to access the Service Provider on behalf of the User. /// /// /// 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. /// public class WebConsumer : ConsumerBase { /// /// Initializes a new instance of the class. /// /// The endpoints and behavior of the Service Provider. /// The host's method of storing and recalling tokens and secrets. public WebConsumer(ServiceProviderDescription serviceDescription, IConsumerTokenManager tokenManager) : base(serviceDescription, tokenManager) { } /// /// 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. /// /// The cancellation token. /// The pending user agent redirect based message to be sent as an HttpResponse. /// /// Requires HttpContext.Current. /// public Task PrepareRequestUserAuthorizationAsync(CancellationToken cancellationToken = default(CancellationToken)) { Uri callback = this.Channel.GetRequestFromContext().GetPublicFacingUrl().StripQueryArgumentsWithPrefix(Protocol.ParameterPrefix); return this.PrepareRequestUserAuthorizationAsync(callback, null, null, cancellationToken); } /// /// Prepares an OAuth message that begins an authorization request that will /// redirect the user to the Service Provider to provide that authorization. /// /// /// An optional Consumer URL that the Service Provider should redirect the /// User Agent to upon successful authorization. /// /// Extra parameters to add to the request token message. Optional. /// Extra parameters to add to the redirect to Service Provider message. Optional. /// The cancellation token. /// The pending user agent redirect based message to be sent as an HttpResponse. public Task PrepareRequestUserAuthorizationAsync(Uri callback, IDictionary requestParameters, IDictionary redirectParameters, CancellationToken cancellationToken = default(CancellationToken)) { return base.PrepareRequestUserAuthorizationAsync(callback, requestParameters, redirectParameters, cancellationToken); } /// /// Processes an incoming authorization-granted message from an SP and obtains an access token. /// /// The incoming HTTP request. /// The cancellation token. /// /// The access token, or null if no incoming authorization message was recognized. /// public async Task ProcessUserAuthorizationAsync(HttpRequestBase request = null, CancellationToken cancellationToken = default(CancellationToken)) { request = request ?? this.Channel.GetRequestFromContext(); var authorizationMessage = await this.Channel.TryReadFromRequestAsync(cancellationToken, request); if (authorizationMessage != null) { string requestToken = authorizationMessage.RequestToken; string verifier = authorizationMessage.VerificationCode; return await this.ProcessUserAuthorizationAsync(requestToken, verifier, cancellationToken); } else { return null; } } } }