Skip to content

Commit

Permalink
moved from cachesettings.json to settings.json
Browse files Browse the repository at this point in the history
  • Loading branch information
erwinvanhunen committed Dec 4, 2024
1 parent 3e92ad3 commit 6745d9a
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 44 deletions.
66 changes: 22 additions & 44 deletions src/Commands/Base/PnPConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,32 +1046,28 @@ private static async Task MSALCacheHelper(ITokenCache tokenCache, string url, st

internal static bool CacheEnabled(string url, string clientid)
{
var configFile = Path.Combine(MsalCacheHelper.UserRootDirectory, ".m365pnppowershell", "cachesettings.json");
if (System.IO.File.Exists(configFile))
var settings = Settings.Current;

var cacheEntries = settings.Cache;
var urls = GetCheckUrls(url);
var entry = settings.Cache?.FirstOrDefault(c => urls.Contains(c.Url) && c.ClientId == clientid);
if (entry != null && entry.Enabled)
{
var configs = JsonSerializer.Deserialize<List<TokenCacheConfiguration>>(System.IO.File.ReadAllText(configFile));
var urls = GetCheckUrls(url);
var entry = configs.FirstOrDefault(c => urls.Contains(c.Url) && c.ClientId == clientid);
if (entry != null && entry.Enabled)
{
return true;
}
return true;
}
return false;
}

internal static string GetCacheClientId(string url)
{
var configFile = Path.Combine(MsalCacheHelper.UserRootDirectory, ".m365pnppowershell", "cachesettings.json");
if (System.IO.File.Exists(configFile))
var settings = Settings.Current;

var cacheEntries = settings.Cache;
var urls = GetCheckUrls(url);
var entry = settings.Cache?.FirstOrDefault(c => urls.Contains(c.Url));
if (entry != null && entry.Enabled)
{
var configs = JsonSerializer.Deserialize<List<TokenCacheConfiguration>>(System.IO.File.ReadAllText(configFile));
var urls = GetCheckUrls(url);
var entry = configs.FirstOrDefault(c => urls.Contains(c.Url));
if (entry != null && entry.Enabled)
{
return entry.ClientId;
}
return entry.ClientId;
}
return null;
}
Expand All @@ -1091,21 +1087,8 @@ private static List<string> GetCheckUrls(string url)

private static void EnableCaching(string url, string clientid)
{
bool folderExists = System.IO.Directory.Exists(Path.Combine(MsalCacheHelper.UserRootDirectory, ".m365pnppowershell"));
if (!folderExists)
{
System.IO.Directory.CreateDirectory(Path.Combine(MsalCacheHelper.UserRootDirectory, ".m365pnppowershell"));
}

var configFile = Path.Combine(MsalCacheHelper.UserRootDirectory, ".m365pnppowershell", "cachesettings.json");
var configs = new List<TokenCacheConfiguration>();
if (System.IO.File.Exists(configFile))
{
configs = JsonSerializer.Deserialize<List<TokenCacheConfiguration>>(System.IO.File.ReadAllText(configFile));
}

var urls = GetCheckUrls(url);
var entry = configs.FirstOrDefault(c => urls.Contains(c.Url) && c.ClientId == clientid);
var entry = Settings.Current.Cache?.FirstOrDefault(c => urls.Contains(c.Url) && c.ClientId == clientid);
if (entry != null)
{
entry.Enabled = true;
Expand All @@ -1114,9 +1097,9 @@ private static void EnableCaching(string url, string clientid)
{
var baseAuthority = new Uri(url).Authority.Replace("-admin.sharepoint.com", ".sharepoint.com").Replace("-my.sharepoint.com", ".sharepoint.com");
var baseUrl = $"https://{baseAuthority}";
configs.Add(new TokenCacheConfiguration() { ClientId = clientid, Url = baseUrl, Enabled = true });
Settings.Current.Cache.Add(new TokenCacheConfiguration() { ClientId = clientid, Url = baseUrl, Enabled = true });
}
System.IO.File.WriteAllText(configFile, JsonSerializer.Serialize(configs));
Settings.Current.Save();
}

private static void WriteCacheEnabledMessage(PSHost host)
Expand All @@ -1126,17 +1109,12 @@ private static void WriteCacheEnabledMessage(PSHost host)

internal static void ClearCache(PnPConnection connection)
{
var configFile = Path.Combine(MsalCacheHelper.UserRootDirectory, ".m365pnppowershell", "cachesettings.json");
if (System.IO.File.Exists(configFile))
var urls = GetCheckUrls(connection.Url);
var entry = Settings.Current.Cache?.FirstOrDefault(c => urls.Contains(c.Url) && c.ClientId == connection.ClientId);
if (entry != null)
{
var configs = JsonSerializer.Deserialize<List<TokenCacheConfiguration>>(System.IO.File.ReadAllText(configFile));
var urls = GetCheckUrls(connection.Url);
var entry = configs.FirstOrDefault(c => urls.Contains(c.Url) && c.ClientId == connection.ClientId);
if (entry != null)
{
configs.Remove(entry);
System.IO.File.WriteAllText(configFile, JsonSerializer.Serialize(configs));
}
Settings.Current.Cache.Remove(entry);
Settings.Current.Save();
}
if (connection.AuthenticationManager != null)
{
Expand Down
69 changes: 69 additions & 0 deletions src/Commands/Model/Settings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Graph;
using Microsoft.Identity.Client.Extensions.Msal;

namespace PnP.PowerShell.Commands.Model
{
public class Settings
{
private static Settings _settings;

[JsonPropertyName("Cache")]
private List<TokenCacheConfiguration> _cache { get; set; }
public List<TokenCacheConfiguration> Cache
{
get
{
if (_cache == null)
{
_cache = new List<TokenCacheConfiguration>();
}
return _cache;
}
set
{
_cache = value;
}
}

//public string LastUsedTenant { get; set; }

public static Settings Current
{
get
{
if (_settings == null)
{
// try to load settings
var settingsFile = Path.Combine(MsalCacheHelper.UserRootDirectory, ".m365pnppowershell", "settings.json");
if (System.IO.File.Exists(settingsFile))
{
_settings = JsonSerializer.Deserialize<Settings>(System.IO.File.ReadAllText(settingsFile)); ;
}
else
{
_settings = new Settings();
}
}
return _settings;
}
}

public void Save()
{
if (_settings != null)
{
var settingsFile = Path.Combine(MsalCacheHelper.UserRootDirectory, ".m365pnppowershell", "settings.json");
if (!System.IO.Directory.Exists(Path.Combine(MsalCacheHelper.UserRootDirectory, ".m365pnppowershell")))
{
System.IO.Directory.CreateDirectory(Path.Combine(MsalCacheHelper.UserRootDirectory, ".m365pnppowershell"));
}
var json = JsonSerializer.Serialize(_settings);
System.IO.File.WriteAllText(settingsFile, json);
}
}
}
}

0 comments on commit 6745d9a

Please sign in to comment.