This repository has been archived by the owner on Sep 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
LinuxKeyRingAccessor.cs
230 lines (193 loc) · 8.53 KB
/
LinuxKeyRingAccessor.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Runtime.InteropServices;
namespace Microsoft.Identity.Client.Extensions.Msal
{
internal class LinuxKeyringAccessor : ICacheAccessor
{
private readonly TraceSourceLogger _logger;
private IntPtr _libsecretSchema = IntPtr.Zero;
private readonly string _cacheFilePath;
private readonly string _keyringCollection;
private readonly string _keyringSchemaName;
private readonly string _keyringSecretLabel;
private readonly string _attributeKey1;
private readonly string _attributeValue1;
private readonly string _attributeKey2;
private readonly string _attributeValue2;
public LinuxKeyringAccessor(
string cacheFilePath,
string keyringCollection,
string keyringSchemaName,
string keyringSecretLabel,
string attributeKey1,
string attributeValue1,
string attributeKey2,
string attributeValue2,
TraceSourceLogger logger)
{
if (string.IsNullOrWhiteSpace(cacheFilePath))
{
throw new ArgumentNullException(nameof(cacheFilePath));
}
if (string.IsNullOrWhiteSpace(attributeKey1))
{
throw new ArgumentNullException(nameof(attributeKey1));
}
if (string.IsNullOrWhiteSpace(attributeValue1))
{
throw new ArgumentNullException(nameof(attributeValue1));
}
if (string.IsNullOrWhiteSpace(attributeKey2))
{
throw new ArgumentNullException(nameof(attributeKey2));
}
if (string.IsNullOrWhiteSpace(attributeValue2))
{
throw new ArgumentNullException(nameof(attributeValue2));
}
_cacheFilePath = cacheFilePath;
_keyringCollection = keyringCollection;
_keyringSchemaName = keyringSchemaName;
_keyringSecretLabel = keyringSecretLabel;
_attributeKey1 = attributeKey1;
_attributeValue1 = attributeValue1;
_attributeKey2 = attributeKey2;
_attributeValue2 = attributeValue2;
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public ICacheAccessor CreateForPersistenceValidation()
{
return new LinuxKeyringAccessor(
_cacheFilePath + ".test",
_keyringCollection,
_keyringSchemaName,
"MSAL Persistence Test",
_attributeKey1, "test",
_attributeKey2, "test",
_logger);
}
public void Clear()
{
_logger.LogInformation("Clearing cache");
FileIOWithRetries.DeleteCacheFile(_cacheFilePath, _logger);
_logger.LogInformation($"Before deleting secret from Linux keyring");
IntPtr error = IntPtr.Zero;
Libsecret.secret_password_clear_sync(
schema: GetLibsecretSchema(),
cancellable: IntPtr.Zero,
error: out error,
attribute1Type: _attributeKey1,
attribute1Value: _attributeValue1,
attribute2Type: _attributeKey2,
attribute2Value: _attributeValue2,
end: IntPtr.Zero);
if (error != IntPtr.Zero)
{
try
{
GError err = (GError)Marshal.PtrToStructure(error, typeof(GError));
_logger.LogError($"An error was encountered while clearing secret from keyring in the {nameof(Storage)} domain:'{err.Domain}' code:'{err.Code}' message:'{err.Message}'");
}
catch (Exception e)
{
_logger.LogError($"An exception was encountered while processing libsecret error information during clearing secret in the {nameof(Storage)} ex:'{e}'");
}
}
_logger.LogInformation("After deleting secret from linux keyring");
}
public byte[] Read()
{
_logger.LogInformation("ReadDataCore");
_logger.LogInformation($"ReadDataCore, Before reading from linux keyring");
byte[] fileData = null;
IntPtr error = IntPtr.Zero;
string secret = Libsecret.secret_password_lookup_sync(
schema: GetLibsecretSchema(),
cancellable: IntPtr.Zero,
error: out error,
attribute1Type: _attributeKey1,
attribute1Value: _attributeValue1,
attribute2Type: _attributeKey2,
attribute2Value: _attributeValue2,
end: IntPtr.Zero);
if (error != IntPtr.Zero)
{
try
{
GError err = (GError)Marshal.PtrToStructure(error, typeof(GError));
_logger.LogError($"An error was encountered while reading secret from keyring in the {nameof(Storage)} domain:'{err.Domain}' code:'{err.Code}' message:'{err.Message}'");
}
catch (Exception e)
{
_logger.LogError($"An exception was encountered while processing libsecret error information during reading in the {nameof(Storage)} ex:'{e}'");
}
}
else if (string.IsNullOrEmpty(secret))
{
_logger.LogError("No matching secret found in the keyring");
}
else
{
_logger.LogInformation("Base64 decoding the secret string");
fileData = Convert.FromBase64String(secret);
_logger.LogInformation($"ReadDataCore, read '{fileData?.Length}' bytes from the keyring");
}
return fileData;
}
public void Write(byte[] data)
{
_logger.LogInformation("Before saving to linux keyring");
IntPtr error = IntPtr.Zero;
Libsecret.secret_password_store_sync(
schema: GetLibsecretSchema(),
collection: _keyringCollection,
label: _keyringSecretLabel,
password: Convert.ToBase64String(data),
cancellable: IntPtr.Zero,
error: out error,
attribute1Type: _attributeKey1,
attribute1Value: _attributeValue1,
attribute2Type: _attributeKey2,
attribute2Value: _attributeValue2,
end: IntPtr.Zero);
if (error != IntPtr.Zero)
{
try
{
GError err = (GError)Marshal.PtrToStructure(error, typeof(GError));
_logger.LogError($"An error was encountered while saving secret to keyring in the {nameof(Storage)} domain:'{err.Domain}' code:'{err.Code}' message:'{err.Message}'");
}
catch (Exception e)
{
_logger.LogError($"An exception was encountered while processing libsecret error information during saving in the {nameof(Storage)} ex:'{e}'");
}
}
_logger.LogInformation("After saving to linux keyring");
// Change the "last modified" attribute and trigger file changed events
FileIOWithRetries.TouchFile(_cacheFilePath, _logger);
}
private IntPtr GetLibsecretSchema()
{
if (_libsecretSchema == IntPtr.Zero)
{
_logger.LogInformation("Before creating libsecret schema");
_libsecretSchema = Libsecret.secret_schema_new(
name: _keyringSchemaName,
flags: (int)Libsecret.SecretSchemaFlags.SECRET_SCHEMA_DONT_MATCH_NAME,
attribute1: _attributeKey1,
attribute1Type: (int)Libsecret.SecretSchemaAttributeType.SECRET_SCHEMA_ATTRIBUTE_STRING,
attribute2: _attributeKey2,
attribute2Type: (int)Libsecret.SecretSchemaAttributeType.SECRET_SCHEMA_ATTRIBUTE_STRING,
end: IntPtr.Zero);
if (_libsecretSchema == IntPtr.Zero)
{
_logger.LogError($"Failed to create libsecret schema from the {nameof(Storage)}");
}
_logger.LogInformation("After creating libsecret schema");
}
return _libsecretSchema;
}
}
}