118 lines
5.5 KiB
C#
118 lines
5.5 KiB
C#
// Copyright (c) .NET Foundation. All rights reserved.
|
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
|
|
using System;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Globalization;
|
|
using System.Net.Http;
|
|
using Microsoft.AspNet.Builder;
|
|
using Microsoft.AspNet.DataProtection;
|
|
using Microsoft.Framework.Internal;
|
|
using Microsoft.Framework.Logging;
|
|
using Microsoft.Framework.OptionsModel;
|
|
using Microsoft.Framework.WebEncoders;
|
|
|
|
namespace Microsoft.AspNet.Authentication.Twitter
|
|
{
|
|
/// <summary>
|
|
/// ASP.NET middleware for authenticating users using Twitter
|
|
/// </summary>
|
|
[SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable", Justification = "Middleware are not disposable.")]
|
|
public class TwitterAuthenticationMiddleware : AuthenticationMiddleware<TwitterAuthenticationOptions>
|
|
{
|
|
private readonly HttpClient _httpClient;
|
|
|
|
/// <summary>
|
|
/// Initializes a <see cref="TwitterAuthenticationMiddleware"/>
|
|
/// </summary>
|
|
/// <param name="next">The next middleware in the HTTP pipeline to invoke</param>
|
|
/// <param name="dataProtectionProvider"></param>
|
|
/// <param name="loggerFactory"></param>
|
|
/// <param name="encoder"></param>
|
|
/// <param name="sharedOptions"></param>
|
|
/// <param name="options">Configuration options for the middleware</param>
|
|
/// <param name="configureOptions"></param>
|
|
public TwitterAuthenticationMiddleware(
|
|
[NotNull] RequestDelegate next,
|
|
[NotNull] IDataProtectionProvider dataProtectionProvider,
|
|
[NotNull] ILoggerFactory loggerFactory,
|
|
[NotNull] IUrlEncoder encoder,
|
|
[NotNull] IOptions<SharedAuthenticationOptions> sharedOptions,
|
|
[NotNull] IOptions<TwitterAuthenticationOptions> options,
|
|
ConfigureOptions<TwitterAuthenticationOptions> configureOptions = null)
|
|
: base(next, options, loggerFactory, encoder, configureOptions)
|
|
{
|
|
if (string.IsNullOrEmpty(Options.ConsumerSecret))
|
|
{
|
|
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.Exception_OptionMustBeProvided, nameof(Options.ConsumerSecret)));
|
|
}
|
|
if (string.IsNullOrEmpty(Options.ConsumerKey))
|
|
{
|
|
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.Exception_OptionMustBeProvided, nameof(Options.ConsumerKey)));
|
|
}
|
|
|
|
if (Options.Notifications == null)
|
|
{
|
|
Options.Notifications = new TwitterAuthenticationNotifications();
|
|
}
|
|
if (Options.StateDataFormat == null)
|
|
{
|
|
var dataProtector = dataProtectionProvider.CreateProtector(
|
|
typeof(TwitterAuthenticationMiddleware).FullName, Options.AuthenticationScheme, "v1");
|
|
Options.StateDataFormat = new SecureDataFormat<RequestToken>(
|
|
Serializers.RequestToken,
|
|
dataProtector,
|
|
TextEncodings.Base64Url);
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(Options.SignInScheme))
|
|
{
|
|
Options.SignInScheme = sharedOptions.Value.SignInScheme;
|
|
}
|
|
if (string.IsNullOrEmpty(Options.SignInScheme))
|
|
{
|
|
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.Exception_OptionMustBeProvided, "SignInScheme"));
|
|
}
|
|
|
|
_httpClient = new HttpClient(ResolveHttpMessageHandler(Options));
|
|
_httpClient.Timeout = Options.BackchannelTimeout;
|
|
_httpClient.MaxResponseContentBufferSize = 1024 * 1024 * 10; // 10 MB
|
|
_httpClient.DefaultRequestHeaders.Accept.ParseAdd("*/*");
|
|
_httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Microsoft ASP.NET Twitter middleware");
|
|
_httpClient.DefaultRequestHeaders.ExpectContinue = false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Provides the <see cref="AuthenticationHandler"/> object for processing authentication-related requests.
|
|
/// </summary>
|
|
/// <returns>An <see cref="AuthenticationHandler"/> configured with the <see cref="TwitterAuthenticationOptions"/> supplied to the constructor.</returns>
|
|
protected override AuthenticationHandler<TwitterAuthenticationOptions> CreateHandler()
|
|
{
|
|
return new TwitterAuthenticationHandler(_httpClient);
|
|
}
|
|
|
|
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Managed by caller")]
|
|
private static HttpMessageHandler ResolveHttpMessageHandler(TwitterAuthenticationOptions options)
|
|
{
|
|
var handler = options.BackchannelHttpHandler ??
|
|
#if DNX451
|
|
new WebRequestHandler();
|
|
// If they provided a validator, apply it or fail.
|
|
if (options.BackchannelCertificateValidator != null)
|
|
{
|
|
// Set the cert validate callback
|
|
var webRequestHandler = handler as WebRequestHandler;
|
|
if (webRequestHandler == null)
|
|
{
|
|
throw new InvalidOperationException(Resources.Exception_ValidatorHandlerMismatch);
|
|
}
|
|
webRequestHandler.ServerCertificateValidationCallback = options.BackchannelCertificateValidator.Validate;
|
|
}
|
|
#else
|
|
new WinHttpHandler();
|
|
#endif
|
|
return handler;
|
|
}
|
|
}
|
|
}
|