From 09c07e5060253f5166816edee461e7f2afc8e6ee Mon Sep 17 00:00:00 2001 From: pavelbannov Date: Wed, 11 Nov 2020 12:11:44 +0300 Subject: [PATCH] Api: ThirdPartyController --- .../Helpers/OAuth20TokenHelper.cs | 32 +++- .../LoginProviders/BaseLoginProvider.cs | 24 ++- .../LoginProviders/DocuSignLoginProvider.cs | 3 +- .../LoginProviders/FacebookLoginProvider.cs | 3 +- .../LoginProviders/GoogleLoginProvider.cs | 6 +- .../LoginProviders/GosUslugiLoginProvider.cs | 3 +- .../LoginProviders/LinkedInLoginProvider.cs | 6 +- .../LoginProviders/MailRuLoginProvider.cs | 6 +- .../LoginProviders/OneDriveLoginProvider.cs | 3 +- .../LoginProviders/VKLoginProvider.cs | 6 +- .../LoginProviders/WordpressLoginProvider.cs | 6 +- .../LoginProviders/YahooLoginProvider.cs | 7 +- .../LoginProviders/YandexLoginProvider.cs | 6 +- .../Controllers/ThirdPartyController.cs | 153 ++++++++++++++++++ web/ASC.Web.Api/Models/ThirdpartyModel.cs | 8 + web/ASC.Web.Api/Startup.cs | 3 +- 16 files changed, 249 insertions(+), 26 deletions(-) create mode 100644 web/ASC.Web.Api/Controllers/ThirdPartyController.cs create mode 100644 web/ASC.Web.Api/Models/ThirdpartyModel.cs diff --git a/common/ASC.FederatedLogin/Helpers/OAuth20TokenHelper.cs b/common/ASC.FederatedLogin/Helpers/OAuth20TokenHelper.cs index 0f15589e26..bf47a68160 100644 --- a/common/ASC.FederatedLogin/Helpers/OAuth20TokenHelper.cs +++ b/common/ASC.FederatedLogin/Helpers/OAuth20TokenHelper.cs @@ -29,18 +29,29 @@ using System.Collections.Generic; using System.Linq; using System.Web; +using ASC.Common; using ASC.Core.Common.Configuration; using ASC.FederatedLogin.LoginProviders; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; namespace ASC.FederatedLogin.Helpers { - public static class OAuth20TokenHelper + public class OAuth20TokenHelper { - public static void RequestCode(HttpContext context, ConsumerFactory consumerFactory, string scope = null, Dictionary additionalArgs = null) where T : Consumer, IOAuthProvider, new() + private IHttpContextAccessor HttpContextAccessor { get; } + private ConsumerFactory ConsumerFactory { get; } + + public OAuth20TokenHelper(IHttpContextAccessor httpContextAccessor, ConsumerFactory consumerFactory) { - var loginProvider = consumerFactory.Get(); + HttpContextAccessor = httpContextAccessor; + ConsumerFactory = consumerFactory; + } + + public RedirectResult RequestCode(string scope = null, Dictionary additionalArgs = null) where T : Consumer, IOAuthProvider, new() + { + var loginProvider = ConsumerFactory.Get(); var requestUrl = loginProvider.CodeUrl; var clientID = loginProvider.ClientID; var redirectUri = loginProvider.RedirectUri; @@ -55,7 +66,7 @@ namespace ASC.FederatedLogin.Helpers if (!string.IsNullOrEmpty(redirectUri)) query += "&redirect_uri=" + HttpUtility.UrlEncode(redirectUri); if (!string.IsNullOrEmpty(scope)) query += "&scope=" + HttpUtility.UrlEncode(scope); - query += "&state=" + HttpUtility.UrlEncode(context.Request.GetUrlRewriter().AbsoluteUri); + query += "&state=" + HttpUtility.UrlEncode(HttpContextAccessor.HttpContext.Request.GetUrlRewriter().AbsoluteUri) + "/code"; if (additionalArgs != null) { @@ -66,7 +77,7 @@ namespace ASC.FederatedLogin.Helpers + "=" + HttpUtility.UrlEncode((additionalArgs[additionalArg] ?? "").Trim())) : null); } - context.Response.Redirect(uriBuilder.Uri + "?" + query, true); + return new RedirectResult(uriBuilder.Uri + "?" + query); } public static OAuth20Token GetAccessToken(ConsumerFactory consumerFactory, string authCode) where T : Consumer, IOAuthProvider, new() @@ -145,4 +156,15 @@ namespace ASC.FederatedLogin.Helpers return !string.IsNullOrEmpty(token.ClientID) && !string.IsNullOrEmpty(token.ClientSecret); } } + + public static class OAuth20TokenHelperExtension + { + public static DIHelper AddOAuth20TokenHelperService(this DIHelper services) + { + services.TryAddScoped(); + + return services + .AddConsumerFactoryService(); + } + } } diff --git a/common/ASC.FederatedLogin/LoginProviders/BaseLoginProvider.cs b/common/ASC.FederatedLogin/LoginProviders/BaseLoginProvider.cs index a04bef43ce..fcbaed5ae0 100644 --- a/common/ASC.FederatedLogin/LoginProviders/BaseLoginProvider.cs +++ b/common/ASC.FederatedLogin/LoginProviders/BaseLoginProvider.cs @@ -40,7 +40,24 @@ using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; namespace ASC.FederatedLogin.LoginProviders -{ +{ + public enum LoginProviderEnum + { + Facebook, + Google, + Dropbox, + Docusign, + Box, + OneDrive, + GosUslugi, + LinkedIn, + MailRu, + VK, + Wordpress, + Yahoo, + Yandex + } + public abstract class BaseLoginProvider : Consumer, ILoginProvider where T : Consumer, ILoginProvider, new() { public T Instance @@ -68,6 +85,7 @@ namespace ASC.FederatedLogin.LoginProviders } } + private OAuth20TokenHelper OAuth20TokenHelper { get; } internal Signature Signature { get; } internal InstanceCrypto InstanceCrypto { get; } @@ -77,6 +95,7 @@ namespace ASC.FederatedLogin.LoginProviders } protected BaseLoginProvider( + OAuth20TokenHelper oAuth20TokenHelper, TenantManager tenantManager, CoreBaseSettings coreBaseSettings, CoreSettings coreSettings, @@ -88,6 +107,7 @@ namespace ASC.FederatedLogin.LoginProviders string name, int order, Dictionary props, Dictionary additional = null) : base(tenantManager, coreBaseSettings, coreSettings, configuration, cache, consumerFactory, name, order, props, additional) { + OAuth20TokenHelper = oAuth20TokenHelper; Signature = signature; InstanceCrypto = instanceCrypto; } @@ -130,7 +150,7 @@ namespace ASC.FederatedLogin.LoginProviders var code = context.Request.Query["code"]; if (string.IsNullOrEmpty(code)) { - OAuth20TokenHelper.RequestCode(context, ConsumerFactory, scopes, additionalArgs); + OAuth20TokenHelper.RequestCode(scopes, additionalArgs); redirect = true; return null; } diff --git a/common/ASC.FederatedLogin/LoginProviders/DocuSignLoginProvider.cs b/common/ASC.FederatedLogin/LoginProviders/DocuSignLoginProvider.cs index 25711f1ad9..55ebce4822 100644 --- a/common/ASC.FederatedLogin/LoginProviders/DocuSignLoginProvider.cs +++ b/common/ASC.FederatedLogin/LoginProviders/DocuSignLoginProvider.cs @@ -40,7 +40,8 @@ namespace ASC.FederatedLogin.LoginProviders { public class DocuSignLoginProvider : Consumer, IOAuthProvider { - public string Scopes { get { return "signature"; } } + public static string DocuSignLoginProviderScopes { get { return "signature"; } } + public string Scopes { get { return DocuSignLoginProviderScopes; } } public string CodeUrl { get { return DocuSignHost + "/oauth/auth"; } } public string AccessTokenUrl { get { return DocuSignHost + "/oauth/token"; } } public string RedirectUri { get { return this["docuSignRedirectUrl"]; } } diff --git a/common/ASC.FederatedLogin/LoginProviders/FacebookLoginProvider.cs b/common/ASC.FederatedLogin/LoginProviders/FacebookLoginProvider.cs index 6e7af66817..86ae3fe71a 100644 --- a/common/ASC.FederatedLogin/LoginProviders/FacebookLoginProvider.cs +++ b/common/ASC.FederatedLogin/LoginProviders/FacebookLoginProvider.cs @@ -54,6 +54,7 @@ namespace ASC.FederatedLogin.LoginProviders public FacebookLoginProvider() { } public FacebookLoginProvider( + OAuth20TokenHelper oAuth20TokenHelper, TenantManager tenantManager, CoreBaseSettings coreBaseSettings, CoreSettings coreSettings, @@ -63,7 +64,7 @@ namespace ASC.FederatedLogin.LoginProviders Signature signature, InstanceCrypto instanceCrypto, string name, int order, Dictionary props, Dictionary additional = null) - : base(tenantManager, coreBaseSettings, coreSettings, configuration, cache, consumerFactory, signature, instanceCrypto, name, order, props, additional) { } + : base(oAuth20TokenHelper, tenantManager, coreBaseSettings, coreSettings, configuration, cache, consumerFactory, signature, instanceCrypto, name, order, props, additional) { } public override LoginProfile GetLoginProfile(string accessToken) { diff --git a/common/ASC.FederatedLogin/LoginProviders/GoogleLoginProvider.cs b/common/ASC.FederatedLogin/LoginProviders/GoogleLoginProvider.cs index 627a5ff4bc..d986ea69df 100644 --- a/common/ASC.FederatedLogin/LoginProviders/GoogleLoginProvider.cs +++ b/common/ASC.FederatedLogin/LoginProviders/GoogleLoginProvider.cs @@ -69,7 +69,9 @@ namespace ASC.FederatedLogin.LoginProviders public override string Scopes { get { return "https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email"; } } public GoogleLoginProvider() { } - public GoogleLoginProvider(TenantManager tenantManager, + public GoogleLoginProvider( + OAuth20TokenHelper oAuth20TokenHelper, + TenantManager tenantManager, CoreBaseSettings coreBaseSettings, CoreSettings coreSettings, IConfiguration configuration, @@ -78,7 +80,7 @@ namespace ASC.FederatedLogin.LoginProviders Signature signature, InstanceCrypto instanceCrypto, string name, int order, Dictionary props, Dictionary additional = null) - : base(tenantManager, coreBaseSettings, coreSettings, configuration, cache, consumerFactory, signature, instanceCrypto, name, order, props, additional) { } + : base(oAuth20TokenHelper, tenantManager, coreBaseSettings, coreSettings, configuration, cache, consumerFactory, signature, instanceCrypto, name, order, props, additional) { } public override LoginProfile GetLoginProfile(string accessToken) { diff --git a/common/ASC.FederatedLogin/LoginProviders/GosUslugiLoginProvider.cs b/common/ASC.FederatedLogin/LoginProviders/GosUslugiLoginProvider.cs index 5b5a37bbbd..55724013e3 100644 --- a/common/ASC.FederatedLogin/LoginProviders/GosUslugiLoginProvider.cs +++ b/common/ASC.FederatedLogin/LoginProviders/GosUslugiLoginProvider.cs @@ -97,6 +97,7 @@ namespace ASC.FederatedLogin.LoginProviders } public GosUslugiLoginProvider( + OAuth20TokenHelper oAuth20TokenHelper, TenantManager tenantManager, CoreBaseSettings coreBaseSettings, CoreSettings coreSettings, @@ -106,7 +107,7 @@ namespace ASC.FederatedLogin.LoginProviders Signature signature, InstanceCrypto instanceCrypto, string name, int order, Dictionary props, Dictionary additional = null) - : base(tenantManager, coreBaseSettings, coreSettings, configuration, cache, consumerFactory, signature, instanceCrypto, name, order, props, additional) + : base(oAuth20TokenHelper, tenantManager, coreBaseSettings, coreSettings, configuration, cache, consumerFactory, signature, instanceCrypto, name, order, props, additional) { } diff --git a/common/ASC.FederatedLogin/LoginProviders/LinkedInLoginProvider.cs b/common/ASC.FederatedLogin/LoginProviders/LinkedInLoginProvider.cs index 76a72248bf..555cc1187b 100644 --- a/common/ASC.FederatedLogin/LoginProviders/LinkedInLoginProvider.cs +++ b/common/ASC.FederatedLogin/LoginProviders/LinkedInLoginProvider.cs @@ -77,7 +77,9 @@ namespace ASC.FederatedLogin.LoginProviders } public LinkedInLoginProvider() { } - public LinkedInLoginProvider(TenantManager tenantManager, + public LinkedInLoginProvider( + OAuth20TokenHelper oAuth20TokenHelper, + TenantManager tenantManager, CoreBaseSettings coreBaseSettings, CoreSettings coreSettings, IConfiguration configuration, @@ -86,7 +88,7 @@ namespace ASC.FederatedLogin.LoginProviders Signature signature, InstanceCrypto instanceCrypto, string name, int order, Dictionary props, Dictionary additional = null) - : base(tenantManager, coreBaseSettings, coreSettings, configuration, cache, consumerFactory, signature, instanceCrypto, name, order, props, additional) { } + : base(oAuth20TokenHelper, tenantManager, coreBaseSettings, coreSettings, configuration, cache, consumerFactory, signature, instanceCrypto, name, order, props, additional) { } public override LoginProfile GetLoginProfile(string accessToken) { diff --git a/common/ASC.FederatedLogin/LoginProviders/MailRuLoginProvider.cs b/common/ASC.FederatedLogin/LoginProviders/MailRuLoginProvider.cs index abc17aea49..ab1f5ae419 100644 --- a/common/ASC.FederatedLogin/LoginProviders/MailRuLoginProvider.cs +++ b/common/ASC.FederatedLogin/LoginProviders/MailRuLoginProvider.cs @@ -80,7 +80,9 @@ namespace ASC.FederatedLogin.LoginProviders { } - public MailRuLoginProvider(TenantManager tenantManager, + public MailRuLoginProvider( + OAuth20TokenHelper oAuth20TokenHelper, + TenantManager tenantManager, CoreBaseSettings coreBaseSettings, CoreSettings coreSettings, IConfiguration configuration, @@ -89,7 +91,7 @@ namespace ASC.FederatedLogin.LoginProviders Signature signature, InstanceCrypto instanceCrypto, string name, int order, Dictionary props, Dictionary additional = null) - : base(tenantManager, coreBaseSettings, coreSettings, configuration, cache, consumerFactory, signature, instanceCrypto, name, order, props, additional) + : base(oAuth20TokenHelper, tenantManager, coreBaseSettings, coreSettings, configuration, cache, consumerFactory, signature, instanceCrypto, name, order, props, additional) { } diff --git a/common/ASC.FederatedLogin/LoginProviders/OneDriveLoginProvider.cs b/common/ASC.FederatedLogin/LoginProviders/OneDriveLoginProvider.cs index 2871ca74cf..a2196cac1b 100644 --- a/common/ASC.FederatedLogin/LoginProviders/OneDriveLoginProvider.cs +++ b/common/ASC.FederatedLogin/LoginProviders/OneDriveLoginProvider.cs @@ -40,7 +40,8 @@ namespace ASC.FederatedLogin.LoginProviders private const string OneDriveOauthUrl = "https://login.live.com/"; public const string OneDriveApiUrl = "https://api.onedrive.com"; - public string Scopes { get { return "wl.signin wl.skydrive_update wl.offline_access"; } } + public static string OneDriveLoginProviderScopes { get { return "wl.signin wl.skydrive_update wl.offline_access"; } } + public string Scopes { get { return OneDriveLoginProviderScopes; } } public string CodeUrl { get { return OneDriveOauthUrl + "oauth20_authorize.srf"; } } public string AccessTokenUrl { get { return OneDriveOauthUrl + "oauth20_token.srf"; } } public string RedirectUri { get { return this["skydriveRedirectUrl"]; } } diff --git a/common/ASC.FederatedLogin/LoginProviders/VKLoginProvider.cs b/common/ASC.FederatedLogin/LoginProviders/VKLoginProvider.cs index 5164bf5283..7e60ed86ba 100644 --- a/common/ASC.FederatedLogin/LoginProviders/VKLoginProvider.cs +++ b/common/ASC.FederatedLogin/LoginProviders/VKLoginProvider.cs @@ -84,7 +84,9 @@ namespace ASC.FederatedLogin.LoginProviders { } - public VKLoginProvider(TenantManager tenantManager, + public VKLoginProvider( + OAuth20TokenHelper oAuth20TokenHelper, + TenantManager tenantManager, CoreBaseSettings coreBaseSettings, CoreSettings coreSettings, IConfiguration configuration, @@ -93,7 +95,7 @@ namespace ASC.FederatedLogin.LoginProviders Signature signature, InstanceCrypto instanceCrypto, string name, int order, Dictionary props, Dictionary additional = null) - : base(tenantManager, coreBaseSettings, coreSettings, configuration, cache, consumerFactory, signature, instanceCrypto, name, order, props, additional) + : base(oAuth20TokenHelper, tenantManager, coreBaseSettings, coreSettings, configuration, cache, consumerFactory, signature, instanceCrypto, name, order, props, additional) { } diff --git a/common/ASC.FederatedLogin/LoginProviders/WordpressLoginProvider.cs b/common/ASC.FederatedLogin/LoginProviders/WordpressLoginProvider.cs index a0cc272e98..d2c98db4d4 100644 --- a/common/ASC.FederatedLogin/LoginProviders/WordpressLoginProvider.cs +++ b/common/ASC.FederatedLogin/LoginProviders/WordpressLoginProvider.cs @@ -49,7 +49,9 @@ namespace ASC.FederatedLogin.LoginProviders { } - public WordpressLoginProvider(TenantManager tenantManager, + public WordpressLoginProvider( + OAuth20TokenHelper oAuth20TokenHelper, + TenantManager tenantManager, CoreBaseSettings coreBaseSettings, CoreSettings coreSettings, IConfiguration configuration, @@ -58,7 +60,7 @@ namespace ASC.FederatedLogin.LoginProviders Signature signature, InstanceCrypto instanceCrypto, string name, int order, Dictionary props, Dictionary additional = null) - : base(tenantManager, coreBaseSettings, coreSettings, configuration, cache, consumerFactory, signature, instanceCrypto, name, order, props, additional) + : base(oAuth20TokenHelper, tenantManager, coreBaseSettings, coreSettings, configuration, cache, consumerFactory, signature, instanceCrypto, name, order, props, additional) { } diff --git a/common/ASC.FederatedLogin/LoginProviders/YahooLoginProvider.cs b/common/ASC.FederatedLogin/LoginProviders/YahooLoginProvider.cs index 08960bda57..68b2a3ced4 100644 --- a/common/ASC.FederatedLogin/LoginProviders/YahooLoginProvider.cs +++ b/common/ASC.FederatedLogin/LoginProviders/YahooLoginProvider.cs @@ -31,6 +31,7 @@ using ASC.Common.Caching; using ASC.Common.Utils; using ASC.Core; using ASC.Core.Common.Configuration; +using ASC.FederatedLogin.Helpers; using ASC.FederatedLogin.Profile; using ASC.Security.Cryptography; @@ -52,7 +53,9 @@ namespace ASC.FederatedLogin.LoginProviders public override string Scopes { get { return "sdct-r"; } } public YahooLoginProvider() { } - public YahooLoginProvider(TenantManager tenantManager, + public YahooLoginProvider( + OAuth20TokenHelper oAuth20TokenHelper, + TenantManager tenantManager, CoreBaseSettings coreBaseSettings, CoreSettings coreSettings, IConfiguration configuration, @@ -61,7 +64,7 @@ namespace ASC.FederatedLogin.LoginProviders Signature signature, InstanceCrypto instanceCrypto, string name, int order, Dictionary props, Dictionary additional = null) - : base(tenantManager, coreBaseSettings, coreSettings, configuration, cache, consumerFactory, signature, instanceCrypto, name, order, props, additional) { } + : base(oAuth20TokenHelper, tenantManager, coreBaseSettings, coreSettings, configuration, cache, consumerFactory, signature, instanceCrypto, name, order, props, additional) { } public OAuth20Token Auth(HttpContext context) { diff --git a/common/ASC.FederatedLogin/LoginProviders/YandexLoginProvider.cs b/common/ASC.FederatedLogin/LoginProviders/YandexLoginProvider.cs index 43d4eec182..0014db66db 100644 --- a/common/ASC.FederatedLogin/LoginProviders/YandexLoginProvider.cs +++ b/common/ASC.FederatedLogin/LoginProviders/YandexLoginProvider.cs @@ -77,7 +77,9 @@ namespace ASC.FederatedLogin.LoginProviders { } - public YandexLoginProvider(TenantManager tenantManager, + public YandexLoginProvider( + OAuth20TokenHelper oAuth20TokenHelper, + TenantManager tenantManager, CoreBaseSettings coreBaseSettings, CoreSettings coreSettings, IConfiguration configuration, @@ -86,7 +88,7 @@ namespace ASC.FederatedLogin.LoginProviders Signature signature, InstanceCrypto instanceCrypto, string name, int order, Dictionary props, Dictionary additional = null) - : base(tenantManager, coreBaseSettings, coreSettings, configuration, cache, consumerFactory, signature, instanceCrypto, name, order, props, additional) + : base(oAuth20TokenHelper, tenantManager, coreBaseSettings, coreSettings, configuration, cache, consumerFactory, signature, instanceCrypto, name, order, props, additional) { } diff --git a/web/ASC.Web.Api/Controllers/ThirdPartyController.cs b/web/ASC.Web.Api/Controllers/ThirdPartyController.cs new file mode 100644 index 0000000000..e9da719ae4 --- /dev/null +++ b/web/ASC.Web.Api/Controllers/ThirdPartyController.cs @@ -0,0 +1,153 @@ +/* + * + * (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. + * +*/ + +using System; +using System.Collections.Generic; +using System.Threading; +using System.Web; + +using ASC.Common; +using ASC.Core.Common.Configuration; +using ASC.FederatedLogin.Helpers; +using ASC.FederatedLogin.LoginProviders; +using ASC.Web.Api.Models; +using ASC.Web.Api.Routing; + +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace ASC.Web.Api.Controllers +{ + [DefaultRoute] + [ApiController] + public class ThirdPartyController : ControllerBase + { + private OAuth20TokenHelper OAuth20TokenHelper { get; } + + public ThirdPartyController(OAuth20TokenHelper oAuth20TokenHelper) + { + OAuth20TokenHelper = oAuth20TokenHelper; + } + + [Read("{provider}")] + public RedirectResult Get(LoginProviderEnum provider) + { + switch (provider) + { + case LoginProviderEnum.Google: + return OAuth20TokenHelper.RequestCode( + GoogleLoginProvider.GoogleScopeDrive, + new Dictionary + { + { "access_type", "offline" }, + { "prompt", "consent" } + }); + + case LoginProviderEnum.Dropbox: + return OAuth20TokenHelper.RequestCode( + additionalArgs: new Dictionary + { + { "force_reauthentication", "true" } + }); + + case LoginProviderEnum.Docusign: + return OAuth20TokenHelper.RequestCode(DocuSignLoginProvider.DocuSignLoginProviderScopes, + new Dictionary + { + { "prompt", "login" } + }); + case LoginProviderEnum.Box: + return OAuth20TokenHelper.RequestCode(); + + case LoginProviderEnum.OneDrive: + return OAuth20TokenHelper.RequestCode(OneDriveLoginProvider.OneDriveLoginProviderScopes); + + case LoginProviderEnum.Wordpress: + return OAuth20TokenHelper.RequestCode(); + + } + + return null; + } + + [Read("{provider}/code")] + public object GetCode(string redirect, string code, string error) + { + try + { + if (!string.IsNullOrEmpty(error)) + { + if (error == "access_denied") + { + error = "Canceled at provider"; + } + throw new Exception(error); + } + + if (!string.IsNullOrEmpty(redirect)) + { + return AppendCode(redirect, code); + } + + return code; + } + catch (ThreadAbortException) + { + } + catch (Exception ex) + { + if (!string.IsNullOrEmpty(redirect)) + { + return AppendCode(redirect, error: ex.Message); + } + + return ex.Message; + } + + return null; + } + + + private static string AppendCode(string url, string code = null, string error = null) + { + url += (url.Contains("#") ? "&" : "#") + + (string.IsNullOrEmpty(error) + ? (string.IsNullOrEmpty(code) + ? string.Empty + : "code=" + HttpUtility.UrlEncode(code)) + : ("error/" + HttpUtility.UrlEncode(error))); + + return url; + } + } + + public static class ThirdPartyControllerExtension + { + public static DIHelper AddThirdPartyController(this DIHelper services) + { + return services.AddOAuth20TokenHelperService(); + } + } +} diff --git a/web/ASC.Web.Api/Models/ThirdpartyModel.cs b/web/ASC.Web.Api/Models/ThirdpartyModel.cs new file mode 100644 index 0000000000..9e58080171 --- /dev/null +++ b/web/ASC.Web.Api/Models/ThirdpartyModel.cs @@ -0,0 +1,8 @@ +namespace ASC.Web.Api.Models +{ + public class ThirdpartyModel + { + public string Code { get; set; } + public string Redirect { get; set; } + } +} diff --git a/web/ASC.Web.Api/Startup.cs b/web/ASC.Web.Api/Startup.cs index 5160e18f8b..e005d8b93a 100644 --- a/web/ASC.Web.Api/Startup.cs +++ b/web/ASC.Web.Api/Startup.cs @@ -37,7 +37,8 @@ namespace ASC.Web.Api .AddPortalController() .AddSettingsController() .AddSecurityController() - .AddSmtpSettingsController(); + .AddSmtpSettingsController() + .AddThirdPartyController(); } public void ConfigureContainer(ContainerBuilder builder)