2017-07-20 11:21:28 +02:00
|
|
|
|
using System;
|
2015-04-02 14:46:53 +11:00
|
|
|
|
using Microsoft.Owin;
|
|
|
|
|
|
using Microsoft.Owin.Security;
|
|
|
|
|
|
using Umbraco.Core;
|
2017-05-30 15:56:27 +02:00
|
|
|
|
using Umbraco.Core.Composing;
|
2017-05-31 09:18:09 +02:00
|
|
|
|
using Umbraco.Core.Exceptions;
|
2015-04-02 14:46:53 +11:00
|
|
|
|
|
2018-08-29 01:15:46 +10:00
|
|
|
|
namespace Umbraco.Web.Security
|
2015-04-02 14:46:53 +11:00
|
|
|
|
{
|
|
|
|
|
|
public static class AuthenticationOptionsExtensions
|
|
|
|
|
|
{
|
2016-09-08 09:30:13 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// When trying to implement an Azure AD B2C provider or other OAuth provider that requires a customized Challenge Result in order to work then
|
|
|
|
|
|
/// this must be used.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="authOptions"></param>
|
|
|
|
|
|
/// <param name="authProperties"></param>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// See: http://issues.umbraco.org/issue/U4-7353
|
|
|
|
|
|
/// </remarks>
|
2015-11-09 10:42:15 +01:00
|
|
|
|
public static void SetSignInChallengeResultCallback(
|
|
|
|
|
|
this AuthenticationOptions authOptions,
|
|
|
|
|
|
Func<IOwinContext, AuthenticationProperties> authProperties)
|
2017-07-20 11:21:28 +02:00
|
|
|
|
{
|
2015-11-09 10:42:15 +01:00
|
|
|
|
authOptions.Description.Properties["ChallengeResultCallback"] = authProperties;
|
|
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
|
2015-11-09 10:42:15 +01:00
|
|
|
|
public static AuthenticationProperties GetSignInChallengeResult(this AuthenticationDescription authenticationDescription, IOwinContext ctx)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (authenticationDescription.Properties.ContainsKey("ChallengeResultCallback") == false) return null;
|
|
|
|
|
|
var cb = authenticationDescription.Properties["ChallengeResultCallback"] as Func<IOwinContext, AuthenticationProperties>;
|
|
|
|
|
|
if (cb == null) return null;
|
|
|
|
|
|
return cb(ctx);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-06-26 16:59:40 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Used during the External authentication process to assign external sign-in options
|
|
|
|
|
|
/// that are used by the Umbraco authentication process.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="authOptions"></param>
|
|
|
|
|
|
/// <param name="options"></param>
|
2015-06-26 17:04:40 +02:00
|
|
|
|
public static void SetExternalSignInAutoLinkOptions(
|
2015-06-26 16:59:40 +02:00
|
|
|
|
this AuthenticationOptions authOptions,
|
|
|
|
|
|
ExternalSignInAutoLinkOptions options)
|
|
|
|
|
|
{
|
|
|
|
|
|
authOptions.Description.Properties["ExternalSignInAutoLinkOptions"] = options;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Used during the External authentication process to retrieve external sign-in options
|
|
|
|
|
|
/// that have been set with SetExternalAuthenticationOptions
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="authenticationDescription"></param>
|
|
|
|
|
|
public static ExternalSignInAutoLinkOptions GetExternalAuthenticationOptions(this AuthenticationDescription authenticationDescription)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (authenticationDescription.Properties.ContainsKey("ExternalSignInAutoLinkOptions") == false) return null;
|
|
|
|
|
|
var options = authenticationDescription.Properties["ExternalSignInAutoLinkOptions"] as ExternalSignInAutoLinkOptions;
|
|
|
|
|
|
return options;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-04-02 14:46:53 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Configures the properties of the authentication description instance for use with Umbraco back office
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="options"></param>
|
|
|
|
|
|
/// <param name="style"></param>
|
|
|
|
|
|
/// <param name="icon"></param>
|
|
|
|
|
|
/// <param name="callbackPath">
|
|
|
|
|
|
/// This is important if the identity provider is to be able to authenticate when upgrading Umbraco. We will try to extract this from
|
|
|
|
|
|
/// any options passed in via reflection since none of the default OWIN providers inherit from a base class but so far all of them have a consistent
|
|
|
|
|
|
/// name for the 'CallbackPath' property which is of type PathString. So we'll try to extract it if it's not found or supplied.
|
2017-07-20 11:21:28 +02:00
|
|
|
|
///
|
2015-04-02 14:46:53 +11:00
|
|
|
|
/// If a value is extracted or supplied, this will be added to an internal list which the UmbracoModule will use to allow the request to pass
|
|
|
|
|
|
/// through without redirecting to the installer.
|
|
|
|
|
|
/// </param>
|
|
|
|
|
|
public static void ForUmbracoBackOffice(this AuthenticationOptions options, string style, string icon, string callbackPath = null)
|
|
|
|
|
|
{
|
2017-05-31 09:18:09 +02:00
|
|
|
|
if (string.IsNullOrEmpty(options.AuthenticationType)) throw new ArgumentNullOrEmptyException("options.AuthenticationType");
|
2015-04-02 14:46:53 +11:00
|
|
|
|
|
|
|
|
|
|
//Ensure the prefix is set
|
|
|
|
|
|
if (options.AuthenticationType.StartsWith(Constants.Security.BackOfficeExternalAuthenticationTypePrefix) == false)
|
|
|
|
|
|
{
|
2017-07-20 11:21:28 +02:00
|
|
|
|
options.AuthenticationType = Constants.Security.BackOfficeExternalAuthenticationTypePrefix + options.AuthenticationType;
|
2015-04-02 14:46:53 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
options.Description.Properties["SocialStyle"] = style;
|
|
|
|
|
|
options.Description.Properties["SocialIcon"] = icon;
|
|
|
|
|
|
|
|
|
|
|
|
//flag for use in back office
|
|
|
|
|
|
options.Description.Properties["UmbracoBackOffice"] = true;
|
|
|
|
|
|
|
|
|
|
|
|
if (callbackPath.IsNullOrWhiteSpace())
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
//try to get it with reflection
|
|
|
|
|
|
var prop = options.GetType().GetProperty("CallbackPath");
|
|
|
|
|
|
if (prop != null && TypeHelper.IsTypeAssignableFrom<PathString>(prop.PropertyType))
|
|
|
|
|
|
{
|
|
|
|
|
|
//get the value
|
|
|
|
|
|
var path = (PathString) prop.GetValue(options);
|
|
|
|
|
|
if (path.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
UmbracoModule.ReservedPaths.TryAdd(path.ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (System.Exception ex)
|
|
|
|
|
|
{
|
2018-08-17 15:41:58 +01:00
|
|
|
|
Current.Logger.Error(typeof (AuthenticationOptionsExtensions), ex, "Could not read AuthenticationOptions properties");
|
2015-04-02 14:46:53 +11:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
UmbracoModule.ReservedPaths.TryAdd(callbackPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|