2015-06-18 19:16:49 +02:00
|
|
|
|
using System;
|
2015-11-27 16:25:39 +01:00
|
|
|
|
using Microsoft.Owin;
|
|
|
|
|
|
using Microsoft.Owin.Security;
|
2015-02-06 13:47:00 +11:00
|
|
|
|
using Microsoft.Owin.Security.Cookies;
|
2015-02-06 14:05:29 +11:00
|
|
|
|
using Umbraco.Core;
|
2020-02-13 07:46:49 +01:00
|
|
|
|
using Umbraco.Core.Cache;
|
2015-02-06 13:47:00 +11:00
|
|
|
|
using Umbraco.Core.Configuration;
|
|
|
|
|
|
using Umbraco.Core.Configuration.UmbracoSettings;
|
2020-04-03 11:03:06 +11:00
|
|
|
|
using Umbraco.Core.Hosting;
|
2019-12-04 14:03:39 +01:00
|
|
|
|
using Umbraco.Core.IO;
|
2015-02-06 13:47:00 +11:00
|
|
|
|
|
2018-08-29 01:15:46 +10:00
|
|
|
|
namespace Umbraco.Web.Security
|
2015-02-06 13:47:00 +11:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Umbraco auth cookie options
|
|
|
|
|
|
/// </summary>
|
2015-04-10 16:55:04 +10:00
|
|
|
|
public sealed class UmbracoBackOfficeCookieAuthOptions : CookieAuthenticationOptions
|
2015-02-06 13:47:00 +11:00
|
|
|
|
{
|
2018-04-05 23:10:51 +10:00
|
|
|
|
public int LoginTimeoutMinutes { get; }
|
2019-11-05 12:54:22 +01:00
|
|
|
|
|
2015-12-15 16:44:03 +01:00
|
|
|
|
public UmbracoBackOfficeCookieAuthOptions(
|
|
|
|
|
|
string[] explicitPaths,
|
2018-04-06 13:51:54 +10:00
|
|
|
|
IUmbracoContextAccessor umbracoContextAccessor,
|
2020-03-12 14:36:25 +01:00
|
|
|
|
ISecuritySettings securitySettings,
|
2018-04-06 13:51:54 +10:00
|
|
|
|
IGlobalSettings globalSettings,
|
2020-04-03 11:03:06 +11:00
|
|
|
|
IHostingEnvironment hostingEnvironment,
|
2018-04-06 13:51:54 +10:00
|
|
|
|
IRuntimeState runtimeState,
|
2019-12-04 14:03:39 +01:00
|
|
|
|
ISecureDataFormat<AuthenticationTicket> secureDataFormat,
|
2020-02-13 07:46:49 +01:00
|
|
|
|
IRequestCache requestCache)
|
2015-12-15 16:44:03 +01:00
|
|
|
|
{
|
2018-04-05 23:10:51 +10:00
|
|
|
|
var secureDataFormat1 = secureDataFormat ?? throw new ArgumentNullException(nameof(secureDataFormat));
|
2018-04-06 13:51:54 +10:00
|
|
|
|
LoginTimeoutMinutes = globalSettings.TimeOutInMinutes;
|
2019-11-05 13:45:42 +01:00
|
|
|
|
AuthenticationType = Constants.Security.BackOfficeAuthenticationType;
|
2019-11-05 12:54:22 +01:00
|
|
|
|
|
2015-12-15 16:44:03 +01:00
|
|
|
|
SlidingExpiration = true;
|
|
|
|
|
|
ExpireTimeSpan = TimeSpan.FromMinutes(LoginTimeoutMinutes);
|
2020-03-12 14:36:25 +01:00
|
|
|
|
CookieDomain = securitySettings.AuthCookieDomain;
|
|
|
|
|
|
CookieName = securitySettings.AuthCookieName;
|
2015-12-15 16:44:03 +01:00
|
|
|
|
CookieHttpOnly = true;
|
2018-04-06 13:51:54 +10:00
|
|
|
|
CookieSecure = globalSettings.UseHttps ? CookieSecureOption.Always : CookieSecureOption.SameAsRequest;
|
2017-07-20 11:21:28 +02:00
|
|
|
|
CookiePath = "/";
|
2015-12-15 16:44:03 +01:00
|
|
|
|
|
2018-04-05 23:10:51 +10:00
|
|
|
|
TicketDataFormat = new UmbracoSecureDataFormat(LoginTimeoutMinutes, secureDataFormat1);
|
|
|
|
|
|
|
2015-12-15 16:44:03 +01:00
|
|
|
|
//Custom cookie manager so we can filter requests
|
2020-04-03 11:03:06 +11:00
|
|
|
|
CookieManager = new BackOfficeCookieManager(umbracoContextAccessor, runtimeState, hostingEnvironment, globalSettings, requestCache, explicitPaths);
|
2015-12-15 16:44:03 +01:00
|
|
|
|
}
|
2019-11-05 12:54:22 +01:00
|
|
|
|
|
2015-12-15 16:44:03 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Creates the cookie options for saving the auth cookie
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="ctx"></param>
|
|
|
|
|
|
/// <param name="ticket"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2015-11-27 16:25:39 +01:00
|
|
|
|
public CookieOptions CreateRequestCookieOptions(IOwinContext ctx, AuthenticationTicket ticket)
|
2016-01-06 10:46:38 +01:00
|
|
|
|
{
|
2018-04-05 23:10:51 +10:00
|
|
|
|
if (ctx == null) throw new ArgumentNullException(nameof(ctx));
|
|
|
|
|
|
if (ticket == null) throw new ArgumentNullException(nameof(ticket));
|
2016-01-06 10:46:38 +01:00
|
|
|
|
|
|
|
|
|
|
var issuedUtc = ticket.Properties.IssuedUtc ?? SystemClock.UtcNow;
|
|
|
|
|
|
var expiresUtc = ticket.Properties.ExpiresUtc ?? issuedUtc.Add(ExpireTimeSpan);
|
|
|
|
|
|
|
|
|
|
|
|
var cookieOptions = new CookieOptions
|
|
|
|
|
|
{
|
|
|
|
|
|
Path = "/",
|
|
|
|
|
|
Domain = this.CookieDomain ?? null,
|
|
|
|
|
|
HttpOnly = true,
|
|
|
|
|
|
Secure = this.CookieSecure == CookieSecureOption.Always
|
|
|
|
|
|
|| (this.CookieSecure == CookieSecureOption.SameAsRequest && ctx.Request.IsSecure),
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (ticket.Properties.IsPersistent)
|
|
|
|
|
|
{
|
2016-02-02 12:12:51 +01:00
|
|
|
|
cookieOptions.Expires = expiresUtc.UtcDateTime;
|
2016-01-06 10:46:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return cookieOptions;
|
|
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
|
2015-02-06 13:47:00 +11:00
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|