Move AddAuthTokenToQueryString to Helpers

Signed-off-by: Danilo Del Busso <danilo.delbusso@cloud.com>
This commit is contained in:
Danilo Del Busso 2023-04-12 08:31:05 +01:00
parent 16ce163e7f
commit 1b3b4b218d
No known key found for this signature in database
2 changed files with 44 additions and 45 deletions

View File

@ -29,9 +29,7 @@
*/
using System;
using System.Web;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.IO;
using System.Xml;
@ -44,7 +42,6 @@ namespace XenAdmin.Actions
{
public class DownloadUpdatesXmlAction : AsyncAction
{
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod()?.DeclaringType);
private const string ClientVersionsNode = "versions";
private const string XenServerVersionsNode = "serverversions";
@ -353,7 +350,7 @@ namespace XenAdmin.Actions
else
{
var authToken = XenAdminConfigManager.Provider.GetInternalStageAuthToken();
uriBuilder.Query = AddAuthTokenToQueryString(authToken, uriBuilder.Query);
uriBuilder.Query = Helpers.AddAuthTokenToQueryString(authToken, uriBuilder.Query);
var proxy = XenAdminConfigManager.Provider.GetProxyFromSettings(Connection, false);
@ -368,46 +365,5 @@ namespace XenAdmin.Actions
return checkForUpdatesXml;
}
/// <summary>
/// Adds the specified authentication token to the existing query string and returns
/// the modified query string.
/// </summary>
/// <param name="authToken">The authentication token to add to the query string.</param>
/// <param name="existingQueryString">The existing query string to add the token to.</param>
/// <returns>The modified query string.</returns>
private static string AddAuthTokenToQueryString(string authToken, string existingQueryString)
{
var queryString = existingQueryString;
if (string.IsNullOrEmpty(authToken))
{
return queryString;
}
try
{
var query = new NameValueCollection();
if (!string.IsNullOrEmpty(existingQueryString))
{
query.Add(HttpUtility.ParseQueryString(existingQueryString));
}
var tokenQueryString = HttpUtility.ParseQueryString(authToken);
query.Add(tokenQueryString);
queryString = string.Join("&",
query.AllKeys
.Where(key => !string.IsNullOrWhiteSpace(key))
.Select(key => $"{key}={query[key]}")
);
}
catch (Exception ex)
{
Log.Error(ex);
}
return queryString;
}
}
}

View File

@ -30,12 +30,14 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml;
using XenAdmin.Network;
using XenAPI;
@ -1537,5 +1539,46 @@ namespace XenAdmin.Core
return GetCoordinator(connection).external_auth_type != Auth.AUTH_TYPE_NONE;
}
/// <summary>
/// Adds the specified authentication token to the existing query string and returns
/// the modified query string.
/// </summary>
/// <param name="authToken">The authentication token to add to the query string.</param>
/// <param name="existingQueryString">The existing query string to add the token to.</param>
/// <returns>The modified query string.</returns>
public static string AddAuthTokenToQueryString(string authToken, string existingQueryString)
{
var queryString = existingQueryString;
if (string.IsNullOrEmpty(authToken))
{
return queryString;
}
try
{
var query = new NameValueCollection();
if (!string.IsNullOrEmpty(existingQueryString))
{
query.Add(HttpUtility.ParseQueryString(existingQueryString));
}
var tokenQueryString = HttpUtility.ParseQueryString(authToken);
query.Add(tokenQueryString);
queryString = string.Join("&",
query.AllKeys
.Where(key => !string.IsNullOrWhiteSpace(key))
.Select(key => $"{key}={query[key]}")
);
}
catch (Exception ex)
{
log.Error(ex);
}
return queryString;
}
}
}