2017-07-20 11:21:28 +02:00
using System ;
2018-03-21 09:06:32 +01:00
using System.Web ;
2016-08-12 12:20:00 +02:00
using Microsoft.AspNet.Identity.Owin ;
using Microsoft.Owin ;
2018-04-05 23:10:51 +10:00
using Microsoft.Owin.Security ;
2018-08-29 01:15:46 +10:00
using Umbraco.Core ;
2016-08-12 12:20:00 +02:00
using Umbraco.Core.Models.Identity ;
2018-08-29 01:15:46 +10:00
using Umbraco.Web.Security ;
2016-08-12 12:20:00 +02:00
2018-08-29 01:15:46 +10:00
namespace Umbraco.Web
2016-08-12 12:20:00 +02:00
{
public static class OwinExtensions
{
2020-08-11 16:06:37 +10:00
/// <summary>
/// Used by external login providers to set any errors that occur during the OAuth negotiation
/// </summary>
/// <param name="owinContext"></param>
/// <param name="errors"></param>
2020-06-19 11:24:03 +10:00
public static void SetExternalLoginProviderErrors ( this IOwinContext owinContext , BackOfficeExternalLoginProviderErrors errors )
2020-08-11 16:06:37 +10:00
= > owinContext . Set ( errors ) ;
2020-06-19 11:24:03 +10:00
2020-08-11 16:06:37 +10:00
/// <summary>
/// Retrieve any errors set by external login providers during OAuth negotiation
/// </summary>
/// <param name="owinContext"></param>
/// <returns></returns>
2020-06-19 11:24:03 +10:00
internal static BackOfficeExternalLoginProviderErrors GetExternalLoginProviderErrors ( this IOwinContext owinContext )
= > owinContext . Get < BackOfficeExternalLoginProviderErrors > ( ) ;
2018-08-29 01:15:46 +10:00
/// <summary>
/// Gets the <see cref="ISecureDataFormat{AuthenticationTicket}"/> for the Umbraco back office cookie
/// </summary>
/// <param name="owinContext"></param>
/// <returns></returns>
internal static ISecureDataFormat < AuthenticationTicket > GetUmbracoAuthTicketDataProtector ( this IOwinContext owinContext )
{
var found = owinContext . Get < UmbracoAuthTicketDataProtector > ( ) ;
return found ? . Protector ;
}
2018-03-21 09:06:32 +01:00
public static string GetCurrentRequestIpAddress ( this IOwinContext owinContext )
{
if ( owinContext = = null )
{
return "Unknown, owinContext is null" ;
}
if ( owinContext . Request = = null )
{
return "Unknown, owinContext.Request is null" ;
}
var httpContext = owinContext . TryGetHttpContext ( ) ;
if ( httpContext = = false )
{
return "Unknown, cannot resolve HttpContext from owinContext" ;
}
return httpContext . Result . GetCurrentRequestIpAddress ( ) ;
}
/// <summary>
2019-01-26 10:52:19 -05:00
/// Nasty little hack to get HttpContextBase from an owin context
2018-03-21 09:06:32 +01:00
/// </summary>
/// <param name="owinContext"></param>
/// <returns></returns>
internal static Attempt < HttpContextBase > TryGetHttpContext ( this IOwinContext owinContext )
{
var ctx = owinContext . Get < HttpContextBase > ( typeof ( HttpContextBase ) . FullName ) ;
return ctx = = null ? Attempt < HttpContextBase > . Fail ( ) : Attempt . Succeed ( ctx ) ;
}
2016-08-12 12:20:00 +02:00
/// <summary>
/// Gets the back office sign in manager out of OWIN
/// </summary>
/// <param name="owinContext"></param>
2017-07-20 11:21:28 +02:00
/// <returns></returns>
2016-08-12 12:20:00 +02:00
public static BackOfficeSignInManager GetBackOfficeSignInManager ( this IOwinContext owinContext )
{
2017-09-22 15:23:46 +02:00
return owinContext . Get < BackOfficeSignInManager > ( )
? ? throw new NullReferenceException ( $"Could not resolve an instance of {typeof (BackOfficeSignInManager)} from the {typeof(IOwinContext)}." ) ;
2016-08-12 12:20:00 +02:00
}
/// <summary>
/// Gets the back office user manager out of OWIN
/// </summary>
/// <param name="owinContext"></param>
/// <returns></returns>
/// <remarks>
2017-07-20 11:21:28 +02:00
/// This is required because to extract the user manager we need to user a custom service since owin only deals in generics and
2016-08-12 12:20:00 +02:00
/// developers could register their own user manager types
2017-07-20 11:21:28 +02:00
/// </remarks>
2016-08-12 12:20:00 +02:00
public static BackOfficeUserManager < BackOfficeIdentityUser > GetBackOfficeUserManager ( this IOwinContext owinContext )
{
2017-09-22 15:23:46 +02:00
var marker = owinContext . Get < IBackOfficeUserManagerMarker > ( BackOfficeUserManager . OwinMarkerKey )
? ? throw new NullReferenceException ( $"No {typeof (IBackOfficeUserManagerMarker)}, i.e. no Umbraco back-office, has been registered with Owin." ) ;
2016-08-12 12:20:00 +02:00
2017-09-22 15:23:46 +02:00
return marker . GetManager ( owinContext )
? ? throw new NullReferenceException ( $"Could not resolve an instance of {typeof (BackOfficeUserManager<BackOfficeIdentityUser>)} from the {typeof (IOwinContext)}." ) ;
2016-08-12 12:20:00 +02:00
}
}
2018-04-05 23:10:51 +10:00
2017-07-20 11:21:28 +02:00
}