81 lines
3.4 KiB
C#
81 lines
3.4 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using System.Net.Http;
|
|
using Microsoft.Owin;
|
|
using Microsoft.Owin.Logging;
|
|
using Microsoft.Owin.Security;
|
|
using Microsoft.Owin.Security.DataHandler;
|
|
using Microsoft.Owin.Security.DataProtection;
|
|
using Microsoft.Owin.Security.Infrastructure;
|
|
using Owin.Security.Providers.Properties;
|
|
using Owin.Security.Providers.SoundCloud.Provider;
|
|
|
|
namespace Owin.Security.Providers.SoundCloud
|
|
{
|
|
public class SoundCloudAuthenticationMiddleware : AuthenticationMiddleware<SoundCloudAuthenticationOptions>
|
|
{
|
|
private readonly HttpClient httpClient;
|
|
private readonly ILogger logger;
|
|
|
|
public SoundCloudAuthenticationMiddleware(
|
|
OwinMiddleware next,
|
|
IAppBuilder app,
|
|
SoundCloudAuthenticationOptions options) : base(next, options)
|
|
{
|
|
if (String.IsNullOrWhiteSpace(Options.ClientId))
|
|
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture,
|
|
Resources.Exception_OptionMustBeProvided, "ClientId"));
|
|
if (String.IsNullOrWhiteSpace(Options.ClientSecret))
|
|
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture,
|
|
Resources.Exception_OptionMustBeProvided, "ClientSecret"));
|
|
|
|
logger = app.CreateLogger<SoundCloudAuthenticationProvider>();
|
|
|
|
if (Options.Provider == null)
|
|
Options.Provider = new SoundCloudAuthenticationProvider();
|
|
|
|
if (Options.StateDataFormat == null)
|
|
{
|
|
var dataProtector = app.CreateDataProtector(
|
|
typeof (SoundCloudAuthenticationMiddleware).FullName,
|
|
Options.AuthenticationType, "v1");
|
|
Options.StateDataFormat = new PropertiesDataFormat(dataProtector);
|
|
}
|
|
|
|
if (String.IsNullOrEmpty(Options.SignInAsAuthenticationType))
|
|
Options.SignInAsAuthenticationType = app.GetDefaultSignInAsAuthenticationType();
|
|
|
|
httpClient = new HttpClient(ResolveHttpMessageHandler(Options))
|
|
{
|
|
Timeout = Options.BackchannelTimeout,
|
|
MaxResponseContentBufferSize = 1024*1024*10
|
|
};
|
|
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Microsoft Owin SoundCloud middleware");
|
|
httpClient.DefaultRequestHeaders.ExpectContinue = false;
|
|
}
|
|
|
|
protected override AuthenticationHandler<SoundCloudAuthenticationOptions> CreateHandler()
|
|
{
|
|
return new SoundCloudAuthenticationHandler(httpClient, logger);
|
|
}
|
|
|
|
private HttpMessageHandler ResolveHttpMessageHandler(SoundCloudAuthenticationOptions options)
|
|
{
|
|
var handler = options.BackchannelHttpHandler ?? 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;
|
|
}
|
|
|
|
return handler;
|
|
}
|
|
}
|
|
} |