2019-06-06 13:34:46 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* (c) Copyright Ascensio System Limited 2010-2018
|
|
|
|
*
|
|
|
|
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
|
|
|
|
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
|
|
|
|
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
|
|
|
|
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
|
|
|
|
*
|
|
|
|
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
|
|
|
|
*
|
|
|
|
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
|
|
|
|
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
|
|
|
|
*
|
|
|
|
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
|
|
|
|
* relevant author attributions when distributing the software. If the display of the logo in its graphic
|
|
|
|
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
|
|
|
|
* in every copy of the program you distribute.
|
|
|
|
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2019-07-12 13:23:39 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text;
|
2020-07-28 08:13:49 +00:00
|
|
|
using System.Text.Json;
|
2019-07-12 13:23:39 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Web;
|
2019-12-20 11:17:01 +00:00
|
|
|
|
2020-07-28 08:13:49 +00:00
|
|
|
using ASC.Common;
|
2019-10-09 15:04:46 +00:00
|
|
|
using ASC.Common.Utils;
|
2019-06-06 13:34:46 +00:00
|
|
|
using ASC.FederatedLogin.Helpers;
|
|
|
|
using ASC.FederatedLogin.LoginProviders;
|
|
|
|
using ASC.FederatedLogin.Profile;
|
2019-10-09 15:04:46 +00:00
|
|
|
using ASC.Security.Cryptography;
|
2019-12-20 11:17:01 +00:00
|
|
|
|
2020-07-28 08:13:49 +00:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
2019-06-06 13:34:46 +00:00
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.AspNetCore.WebUtilities;
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
2020-07-28 08:13:49 +00:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2019-06-06 13:34:46 +00:00
|
|
|
|
|
|
|
namespace ASC.FederatedLogin
|
|
|
|
{
|
|
|
|
public class Login
|
|
|
|
{
|
|
|
|
private Dictionary<string, string> _params;
|
|
|
|
|
2019-07-12 13:23:39 +00:00
|
|
|
private IWebHostEnvironment WebHostEnvironment { get; }
|
|
|
|
private IMemoryCache MemoryCache { get; }
|
2020-08-12 09:58:08 +00:00
|
|
|
private Signature Signature { get; }
|
|
|
|
private InstanceCrypto InstanceCrypto { get; }
|
|
|
|
private ProviderManager ProviderManager { get; }
|
2019-07-12 13:23:39 +00:00
|
|
|
|
2019-12-20 11:17:01 +00:00
|
|
|
public Login(
|
|
|
|
IWebHostEnvironment webHostEnvironment,
|
|
|
|
IMemoryCache memoryCache,
|
|
|
|
Signature signature,
|
|
|
|
InstanceCrypto instanceCrypto,
|
2020-07-28 08:13:49 +00:00
|
|
|
ProviderManager providerManager)
|
2019-07-12 13:23:39 +00:00
|
|
|
{
|
|
|
|
WebHostEnvironment = webHostEnvironment;
|
|
|
|
MemoryCache = memoryCache;
|
2019-10-09 15:04:46 +00:00
|
|
|
Signature = signature;
|
|
|
|
InstanceCrypto = instanceCrypto;
|
2020-07-28 08:13:49 +00:00
|
|
|
ProviderManager = providerManager;
|
2019-07-12 13:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-06 13:34:46 +00:00
|
|
|
public async Task Invoke(HttpContext context)
|
|
|
|
{
|
2020-07-28 08:13:49 +00:00
|
|
|
var scope = context.PushRewritenUri();
|
2019-06-06 13:34:46 +00:00
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(context.Request.Query["p"]))
|
|
|
|
{
|
|
|
|
_params = new Dictionary<string, string>(context.Request.Query.Count);
|
|
|
|
//Form params and redirect
|
|
|
|
foreach (var key in context.Request.Query.Keys)
|
|
|
|
{
|
|
|
|
_params.Add(key, context.Request.Query[key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Pack and redirect
|
|
|
|
var uriBuilder = new UriBuilder(context.Request.GetUrlRewriter());
|
2020-07-28 08:13:49 +00:00
|
|
|
var token = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(JsonSerializer.Serialize(_params)));
|
2019-06-06 13:34:46 +00:00
|
|
|
uriBuilder.Query = "p=" + token;
|
|
|
|
context.Response.Redirect(uriBuilder.Uri.ToString(), true);
|
2020-07-28 08:13:49 +00:00
|
|
|
await context.Response.CompleteAsync();
|
|
|
|
return;
|
2019-06-06 13:34:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-28 08:13:49 +00:00
|
|
|
_params = JsonSerializer.Deserialize<Dictionary<string, string>>(Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(context.Request.Query["p"])));
|
2019-06-06 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(Auth))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-07-28 08:13:49 +00:00
|
|
|
var profile = ProviderManager.Process(Auth, context, _params);
|
2019-06-06 13:34:46 +00:00
|
|
|
if (profile != null)
|
|
|
|
{
|
|
|
|
await SendClientData(context, profile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (ThreadAbortException)
|
|
|
|
{
|
|
|
|
//Thats is responce ending
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2019-10-09 15:04:46 +00:00
|
|
|
await SendClientData(context, LoginProfile.FromError(Signature, InstanceCrypto, ex));
|
2019-06-06 13:34:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Render xrds
|
|
|
|
await RenderXrds(context);
|
|
|
|
}
|
|
|
|
context.PopRewritenUri();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected bool Minimal
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (_params.ContainsKey("min"))
|
|
|
|
{
|
2019-08-15 13:05:50 +00:00
|
|
|
bool.TryParse(_params.Get("min"), out var result);
|
2019-06-06 13:34:46 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected string Callback
|
|
|
|
{
|
|
|
|
get { return _params.Get("callback") ?? "loginCallback"; }
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task RenderXrds(HttpContext context)
|
|
|
|
{
|
2019-07-12 13:23:39 +00:00
|
|
|
var xrdsloginuri = new Uri(context.Request.GetUrlRewriter(), new Uri(context.Request.GetUrlRewriter().AbsolutePath, UriKind.Relative)) + "?auth=openid&returnurl=" + ReturnUrl;
|
|
|
|
var xrdsimageuri = new Uri(context.Request.GetUrlRewriter(), new Uri(WebHostEnvironment.WebRootPath, UriKind.Relative)) + "openid.gif";
|
2019-06-06 13:34:46 +00:00
|
|
|
await XrdsHelper.RenderXrds(context.Response, xrdsloginuri, xrdsimageuri);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected LoginMode Mode
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(_params.Get("mode")))
|
|
|
|
{
|
2019-08-15 12:04:42 +00:00
|
|
|
return (LoginMode)Enum.Parse(typeof(LoginMode), _params.Get("mode"), true);
|
2019-06-06 13:34:46 +00:00
|
|
|
}
|
|
|
|
return LoginMode.Popup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected string ReturnUrl
|
|
|
|
{
|
|
|
|
get { return _params.Get("returnurl"); } //TODO?? FormsAuthentication.LoginUrl; }
|
|
|
|
}
|
|
|
|
|
|
|
|
protected string Auth
|
|
|
|
{
|
|
|
|
get { return _params.Get("auth"); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsReusable
|
|
|
|
{
|
|
|
|
get { return false; }
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task SendClientData(HttpContext context, LoginProfile profile)
|
|
|
|
{
|
|
|
|
switch (Mode)
|
|
|
|
{
|
|
|
|
case LoginMode.Redirect:
|
2020-07-28 08:13:49 +00:00
|
|
|
await RedirectToReturnUrl(context, profile);
|
2019-06-06 13:34:46 +00:00
|
|
|
break;
|
|
|
|
case LoginMode.Popup:
|
|
|
|
await SendJsCallback(context, profile);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task SendJsCallback(HttpContext context, LoginProfile profile)
|
|
|
|
{
|
|
|
|
//Render a page
|
|
|
|
context.Response.ContentType = "text/html";
|
|
|
|
await context.Response.WriteAsync(JsCallbackHelper.GetCallbackPage().Replace("%PROFILE%", profile.ToJson()).Replace("%CALLBACK%", Callback));
|
|
|
|
}
|
|
|
|
|
2020-07-28 08:13:49 +00:00
|
|
|
private async Task RedirectToReturnUrl(HttpContext context, LoginProfile profile)
|
2019-06-06 13:34:46 +00:00
|
|
|
{
|
|
|
|
var useMinimalProfile = Minimal;
|
|
|
|
if (useMinimalProfile)
|
|
|
|
profile = profile.GetMinimalProfile(); //Only id and provider
|
|
|
|
|
|
|
|
if (context.Session != null && !useMinimalProfile)
|
|
|
|
{
|
|
|
|
//Store in session
|
|
|
|
context.Response.Redirect(new Uri(ReturnUrl, UriKind.Absolute).AddProfileSession(profile, context).ToString(), true);
|
|
|
|
}
|
2019-07-12 13:23:39 +00:00
|
|
|
else if (MemoryCache != null && !useMinimalProfile)
|
2019-06-06 13:34:46 +00:00
|
|
|
{
|
2019-07-12 13:23:39 +00:00
|
|
|
context.Response.Redirect(new Uri(ReturnUrl, UriKind.Absolute).AddProfileCache(profile, MemoryCache).ToString(), true);
|
2019-06-06 13:34:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
context.Response.Redirect(new Uri(ReturnUrl, UriKind.Absolute).AddProfile(profile).ToString(), true);
|
|
|
|
}
|
2020-07-28 08:13:49 +00:00
|
|
|
|
|
|
|
await context.Response.CompleteAsync();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class LoginHandler
|
|
|
|
{
|
2020-08-12 09:58:08 +00:00
|
|
|
private RequestDelegate Next { get; }
|
|
|
|
private IServiceProvider ServiceProvider { get; }
|
2020-07-28 08:13:49 +00:00
|
|
|
|
|
|
|
public LoginHandler(RequestDelegate next, IServiceProvider serviceProvider)
|
|
|
|
{
|
|
|
|
Next = next;
|
|
|
|
ServiceProvider = serviceProvider;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Invoke(HttpContext context)
|
|
|
|
{
|
|
|
|
using var scope = ServiceProvider.CreateScope();
|
|
|
|
var login = scope.ServiceProvider.GetService<Login>();
|
|
|
|
await login.Invoke(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class LoginHandlerExtensions
|
|
|
|
{
|
|
|
|
public static DIHelper AddLoginHandlerService(this DIHelper services)
|
|
|
|
{
|
|
|
|
if (services.TryAddScoped<Login>())
|
|
|
|
{
|
|
|
|
return services
|
|
|
|
.AddSignatureService()
|
|
|
|
.AddInstanceCryptoService()
|
|
|
|
.AddProviderManagerService();
|
|
|
|
}
|
|
|
|
|
|
|
|
return services;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static IApplicationBuilder UseLoginHandler(this IApplicationBuilder builder)
|
|
|
|
{
|
|
|
|
return builder.UseMiddleware<LoginHandler>();
|
2019-06-06 13:34:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|