-
Notifications
You must be signed in to change notification settings - Fork 1
/
CacheHelper.cs
87 lines (72 loc) · 3.35 KB
/
CacheHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.Identity.Client.Extensions.Msal;
namespace Wox.Plugin.MSToDo
{
public class CacheHelper
{
private const string TraceSourceName = "MSAL.Contoso.CacheExtension";
public static async Task<MsalCacheHelper> CreateCacheHelperAsync()
{
StorageCreationProperties storageProperties;
MsalCacheHelper cacheHelper;
try
{
storageProperties = ConfigureSecureStorage(usePlaintextFileOnLinux: false);
cacheHelper = await MsalCacheHelper.CreateAsync(
storageProperties,
new TraceSource(TraceSourceName))
.ConfigureAwait(false);
// the underlying persistence mechanism might not be usable
// this typically happens on Linux over SSH
cacheHelper.VerifyPersistence();
return cacheHelper;
}
catch (MsalCachePersistenceException ex)
{
Console.WriteLine("Cannot persist data securely. ");
Console.WriteLine("Details: " + ex);
if (SharedUtilities.IsLinuxPlatform())
{
storageProperties = ConfigureSecureStorage(usePlaintextFileOnLinux: true);
Console.WriteLine($"Falling back on using a plaintext " +
$"file located at {storageProperties.CacheFilePath} Users are responsible for securing this file!");
cacheHelper = await MsalCacheHelper.CreateAsync(
storageProperties,
new TraceSource(TraceSourceName))
.ConfigureAwait(false);
return cacheHelper;
}
throw;
}
}
private static StorageCreationProperties ConfigureSecureStorage(bool usePlaintextFileOnLinux)
{
if (!usePlaintextFileOnLinux)
{
return new StorageCreationPropertiesBuilder(
Config.CacheFileName,
Config.CacheDir)
.WithLinuxKeyring(
Config.LinuxKeyRingSchema,
Config.LinuxKeyRingCollection,
Config.LinuxKeyRingLabel,
Config.LinuxKeyRingAttr1,
Config.LinuxKeyRingAttr2)
.WithMacKeyChain(
Config.KeyChainServiceName,
Config.KeyChainAccountName)
.Build();
}
return new StorageCreationPropertiesBuilder(
Config.CacheFileName,
Config.CacheDir)
.WithLinuxUnprotectedFile()
.WithMacKeyChain(
Config.KeyChainServiceName,
Config.KeyChainAccountName)
.Build();
}
}
}