DocSpace-buildtools/common/ASC.FederatedLogin/Helpers/OAuth20TokenHelper.cs

172 lines
7.9 KiB
C#
Raw Normal View History

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.
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
2019-12-20 11:17:01 +00:00
2020-11-11 09:11:44 +00:00
using ASC.Common;
2019-06-06 13:34:46 +00:00
using ASC.Core.Common.Configuration;
using ASC.FederatedLogin.LoginProviders;
2019-12-20 11:17:01 +00:00
2019-06-06 13:34:46 +00:00
using Microsoft.AspNetCore.Http;
namespace ASC.FederatedLogin.Helpers
{
[Scope]
2020-11-11 09:11:44 +00:00
public class OAuth20TokenHelper
2019-06-06 13:34:46 +00:00
{
2022-03-18 19:34:01 +00:00
private readonly RequestHelper _requestHelper;
2020-11-11 09:11:44 +00:00
private IHttpContextAccessor HttpContextAccessor { get; }
private ConsumerFactory ConsumerFactory { get; }
2022-03-18 19:34:01 +00:00
public OAuth20TokenHelper(IHttpContextAccessor httpContextAccessor, ConsumerFactory consumerFactory, RequestHelper requestHelper)
2019-06-06 13:34:46 +00:00
{
2020-11-11 09:11:44 +00:00
HttpContextAccessor = httpContextAccessor;
ConsumerFactory = consumerFactory;
2022-03-18 19:34:01 +00:00
_requestHelper = requestHelper;
2020-11-11 09:11:44 +00:00
}
2021-10-15 08:34:34 +00:00
public string RequestCode<T>(string scope = null, IDictionary<string, string> additionalArgs = null, IDictionary<string, string> additionalStateArgs = null) where T : Consumer, IOAuthProvider, new()
2020-11-11 09:11:44 +00:00
{
var loginProvider = ConsumerFactory.Get<T>();
2019-06-06 13:34:46 +00:00
var requestUrl = loginProvider.CodeUrl;
var clientID = loginProvider.ClientID;
var redirectUri = loginProvider.RedirectUri;
var uriBuilder = new UriBuilder(requestUrl);
var query = uriBuilder.Query;
if (!string.IsNullOrEmpty(query)) query += "&";
query += "response_type=code";
2021-01-13 13:30:32 +00:00
if (!string.IsNullOrEmpty(clientID)) query += $"&client_id={HttpUtility.UrlEncode(clientID)}";
if (!string.IsNullOrEmpty(redirectUri)) query += $"&redirect_uri={HttpUtility.UrlEncode(redirectUri)}";
if (!string.IsNullOrEmpty(scope)) query += $"&scope={HttpUtility.UrlEncode(scope)}";
2019-06-06 13:34:46 +00:00
2021-01-13 13:30:32 +00:00
var u = HttpContextAccessor.HttpContext.Request.GetUrlRewriter();
2021-10-15 08:34:34 +00:00
2021-10-13 07:25:19 +00:00
var stateUriBuilder = new UriBuilder(u.Scheme, u.Host, u.Port, $"thirdparty/{loginProvider.Name.ToLower()}/code");
2021-10-15 08:34:34 +00:00
2022-01-18 13:54:24 +00:00
if (additionalStateArgs != null && additionalStateArgs.Count > 0)
2021-10-13 07:25:19 +00:00
{
2021-10-15 08:34:34 +00:00
var stateQuery = "";
stateQuery = additionalStateArgs.Keys
.Where(a => a != null)
.Aggregate(stateQuery, (current, a) => a != null ? $"{current}&{a.Trim()}={additionalStateArgs[a] ?? "".Trim()}" : null);
stateUriBuilder.Query = stateQuery.Substring(1);
2021-10-13 07:25:19 +00:00
}
var state = HttpUtility.UrlEncode(stateUriBuilder.Uri.AbsoluteUri);
2021-01-13 13:30:32 +00:00
query += $"&state={state}";
2019-06-06 13:34:46 +00:00
if (additionalArgs != null)
{
query = additionalArgs.Keys.Where(additionalArg => additionalArg != null)
.Aggregate(query, (current, additionalArg) =>
additionalArg != null ? current
2022-01-21 13:46:30 +00:00
+ "&" + HttpUtility.UrlEncode(additionalArg.Trim())
+ "=" + HttpUtility.UrlEncode((additionalArgs[additionalArg] ?? "").Trim()) : null);
2019-06-06 13:34:46 +00:00
}
2020-11-12 08:24:36 +00:00
return uriBuilder.Uri + "?" + query;
2019-06-06 13:34:46 +00:00
}
2022-03-18 19:34:01 +00:00
public OAuth20Token GetAccessToken<T>(ConsumerFactory consumerFactory, string authCode) where T : Consumer, IOAuthProvider, new()
2019-06-06 13:34:46 +00:00
{
2019-12-20 11:17:01 +00:00
var loginProvider = consumerFactory.Get<T>();
2019-06-06 13:34:46 +00:00
var requestUrl = loginProvider.AccessTokenUrl;
var clientID = loginProvider.ClientID;
var clientSecret = loginProvider.ClientSecret;
var redirectUri = loginProvider.RedirectUri;
2022-01-24 10:49:00 +00:00
if (string.IsNullOrEmpty(authCode)) throw new ArgumentNullException(nameof(authCode));
2019-08-15 13:16:39 +00:00
if (string.IsNullOrEmpty(clientID)) throw new ArgumentNullException("clientID");
if (string.IsNullOrEmpty(clientSecret)) throw new ArgumentNullException("clientSecret");
2019-06-06 13:34:46 +00:00
2022-01-14 13:12:37 +00:00
var data = $"code={HttpUtility.UrlEncode(authCode)}&client_id={HttpUtility.UrlEncode(clientID)}&client_secret={HttpUtility.UrlEncode(clientSecret)}";
2019-06-06 13:34:46 +00:00
2019-08-15 13:16:39 +00:00
if (!string.IsNullOrEmpty(redirectUri))
2019-06-06 13:34:46 +00:00
data += "&redirect_uri=" + HttpUtility.UrlEncode(redirectUri);
data += "&grant_type=authorization_code";
2022-03-18 19:34:01 +00:00
var json = _requestHelper.PerformRequest(requestUrl, "application/x-www-form-urlencoded", "POST", data);
2019-06-06 13:34:46 +00:00
if (json != null)
{
2022-01-18 12:40:28 +00:00
if (!json.StartsWith('{'))
2019-06-06 13:34:46 +00:00
{
json = "{\"" + json.Replace("=", "\":\"").Replace("&", "\",\"") + "\"}";
}
var token = OAuth20Token.FromJson(json);
if (token == null) return null;
token.ClientID = clientID;
token.ClientSecret = clientSecret;
token.RedirectUri = redirectUri;
return token;
}
return null;
}
2022-03-18 19:34:01 +00:00
public OAuth20Token RefreshToken<T>(ConsumerFactory consumerFactory, OAuth20Token token) where T : Consumer, IOAuthProvider, new()
2019-06-06 13:34:46 +00:00
{
2019-12-20 11:17:01 +00:00
var loginProvider = consumerFactory.Get<T>();
2019-06-06 13:34:46 +00:00
return RefreshToken(loginProvider.AccessTokenUrl, token);
}
2022-03-18 19:34:01 +00:00
public OAuth20Token RefreshToken(string requestUrl, OAuth20Token token)
2019-06-06 13:34:46 +00:00
{
2022-01-24 10:49:00 +00:00
if (token == null || !CanRefresh(token)) throw new ArgumentException("Can not refresh given token", nameof(token));
2019-06-06 13:34:46 +00:00
2022-01-14 13:12:37 +00:00
var data = $"client_id={HttpUtility.UrlEncode(token.ClientID)}&client_secret={HttpUtility.UrlEncode(token.ClientSecret)}&refresh_token={HttpUtility.UrlEncode(token.RefreshToken)}&grant_type=refresh_token";
2019-06-06 13:34:46 +00:00
2022-03-18 19:34:01 +00:00
var json = _requestHelper.PerformRequest(requestUrl, "application/x-www-form-urlencoded", "POST", data);
2019-06-06 13:34:46 +00:00
if (json != null)
{
var refreshed = OAuth20Token.FromJson(json);
refreshed.ClientID = token.ClientID;
refreshed.ClientSecret = token.ClientSecret;
refreshed.RedirectUri = token.RedirectUri;
2019-08-15 14:48:55 +00:00
refreshed.RefreshToken ??= token.RefreshToken;
2019-06-06 13:34:46 +00:00
return refreshed;
}
return token;
}
private static bool CanRefresh(OAuth20Token token)
{
2019-08-15 13:16:39 +00:00
return !string.IsNullOrEmpty(token.ClientID) && !string.IsNullOrEmpty(token.ClientSecret);
2019-06-06 13:34:46 +00:00
}
}
}