126 lines
5.1 KiB
C#
126 lines
5.1 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 System.Text.Encodings.Web;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace Microsoft.AspNetCore.Authentication.Twitter
|
|
{
|
|
/// <summary>
|
|
/// ASP.NET middleware for authenticating users using Twitter
|
|
/// </summary>
|
|
[SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable", Justification = "Middleware are not disposable.")]
|
|
public class TwitterMiddleware : AuthenticationMiddleware<TwitterOptions>
|
|
{
|
|
private readonly HttpClient _httpClient;
|
|
|
|
/// <summary>
|
|
/// Initializes a <see cref="TwitterMiddleware"/>
|
|
/// </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>
|
|
public TwitterMiddleware(
|
|
RequestDelegate next,
|
|
IDataProtectionProvider dataProtectionProvider,
|
|
ILoggerFactory loggerFactory,
|
|
UrlEncoder encoder,
|
|
IOptions<SharedAuthenticationOptions> sharedOptions,
|
|
IOptions<TwitterOptions> options)
|
|
: base(next, options, loggerFactory, encoder)
|
|
{
|
|
if (next == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(next));
|
|
}
|
|
|
|
if (dataProtectionProvider == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(dataProtectionProvider));
|
|
}
|
|
|
|
if (loggerFactory == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(loggerFactory));
|
|
}
|
|
|
|
if (encoder == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(encoder));
|
|
}
|
|
|
|
if (sharedOptions == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(sharedOptions));
|
|
}
|
|
|
|
if (options == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(options));
|
|
}
|
|
|
|
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.CallbackPath.HasValue)
|
|
{
|
|
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.Exception_OptionMustBeProvided, nameof(Options.CallbackPath)));
|
|
}
|
|
|
|
if (Options.Events == null)
|
|
{
|
|
Options.Events = new TwitterEvents();
|
|
}
|
|
if (Options.StateDataFormat == null)
|
|
{
|
|
var dataProtector = dataProtectionProvider.CreateProtector(
|
|
typeof(TwitterMiddleware).FullName, Options.AuthenticationScheme, "v1");
|
|
Options.StateDataFormat = new SecureDataFormat<RequestToken>(
|
|
new RequestTokenSerializer(),
|
|
dataProtector);
|
|
}
|
|
|
|
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(Options.BackchannelHttpHandler ?? new HttpClientHandler());
|
|
_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{T}"/> object for processing authentication-related requests.
|
|
/// </summary>
|
|
/// <returns>An <see cref="AuthenticationHandler{T}"/> configured with the <see cref="TwitterOptions"/> supplied to the constructor.</returns>
|
|
protected override AuthenticationHandler<TwitterOptions> CreateHandler()
|
|
{
|
|
return new TwitterHandler(_httpClient);
|
|
}
|
|
}
|
|
}
|