-
Notifications
You must be signed in to change notification settings - Fork 354
/
Copy pathiOSTokenCacheAccessor.cs
372 lines (309 loc) · 12.6 KB
/
iOSTokenCacheAccessor.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Foundation;
using Microsoft.Identity.Client.Cache;
using Microsoft.Identity.Client.Cache.Items;
using Microsoft.Identity.Client.Cache.Keys;
using Microsoft.Identity.Client.Core;
using Security;
namespace Microsoft.Identity.Client.Platforms.iOS
{
internal class iOSTokenCacheAccessor : ITokenCacheAccessor
{
public const string CacheKeyDelimiter = "-";
private const bool _defaultSyncSetting = false;
private const SecAccessible _defaultAccessiblityPolicy = SecAccessible.AfterFirstUnlockThisDeviceOnly;
// Identifier for the keychain item used to retrieve current team ID
private const string TeamIdKey = "DotNetTeamIDHint";
private const string DefaultKeychainAccessGroup = "com.microsoft.adalcache";
private string _keychainGroup;
private readonly RequestContext _requestContext;
public void SetiOSKeychainSecurityGroup(string keychainSecurityGroup)
{
if (string.IsNullOrEmpty(keychainSecurityGroup))
{
_keychainGroup = GetTeamId() + '.' + DefaultKeychainAccessGroup;
}
else
{
_keychainGroup = GetTeamId() + '.' + keychainSecurityGroup;
}
}
private string GetTeamId()
{
var queryRecord = new SecRecord(SecKind.GenericPassword)
{
Service = "",
Account = TeamIdKey,
Accessible = SecAccessible.Always
};
SecRecord match = SecKeyChain.QueryAsRecord(queryRecord, out SecStatusCode resultCode);
if (resultCode == SecStatusCode.ItemNotFound)
{
SecKeyChain.Add(queryRecord);
match = SecKeyChain.QueryAsRecord(queryRecord, out resultCode);
}
if (resultCode == SecStatusCode.Success)
{
return match.AccessGroup.Split('.')[0];
}
throw new MsalClientException(
MsalError.CannotAccessPublisherKeyChain,
MsalErrorMessage.CannotAccessPublisherKeyChain);
}
public iOSTokenCacheAccessor()
{
SetiOSKeychainSecurityGroup(null);
}
public iOSTokenCacheAccessor(RequestContext requestContext) : this()
{
_requestContext = requestContext;
}
public void SaveAccessToken(MsalAccessTokenCacheItem item)
{
IiOSKey key = item.GetKey();
Save(key, item.ToJsonString());
}
public void SaveRefreshToken(MsalRefreshTokenCacheItem item)
{
Save(item.GetKey(), item.ToJsonString());
}
public void SaveIdToken(MsalIdTokenCacheItem item)
{
Save(item.GetKey(), item.ToJsonString());
}
public void SaveAccount(MsalAccountCacheItem item)
{
Save(item.GetKey(), item.ToJsonString());
}
public void DeleteAccessToken(MsalAccessTokenCacheKey cacheKey)
{
Remove(cacheKey);
}
public void DeleteRefreshToken(MsalRefreshTokenCacheKey cacheKey)
{
Remove(cacheKey);
}
public void DeleteIdToken(MsalIdTokenCacheKey cacheKey)
{
Remove(cacheKey);
}
public void DeleteAccount(MsalAccountCacheKey cacheKey)
{
Remove(cacheKey);
}
public IEnumerable<MsalAccessTokenCacheItem> GetAllAccessTokens()
{
return GetPayloadAsString((int)MsalCacheKeys.iOSCredentialAttrType.AccessToken)
.Select(x => MsalAccessTokenCacheItem.FromJsonString(x))
.ToList();
}
public IEnumerable<MsalRefreshTokenCacheItem> GetAllRefreshTokens()
{
return GetPayloadAsString((int)MsalCacheKeys.iOSCredentialAttrType.RefreshToken)
.Select(x => MsalRefreshTokenCacheItem.FromJsonString(x))
.ToList();
}
public IEnumerable<MsalIdTokenCacheItem> GetAllIdTokens()
{
return GetPayloadAsString((int)MsalCacheKeys.iOSCredentialAttrType.IdToken)
.Select(x => MsalIdTokenCacheItem.FromJsonString(x))
.ToList();
}
public IEnumerable<MsalAccountCacheItem> GetAllAccounts()
{
return GetPayloadAsString(MsalCacheKeys.iOSAuthorityTypeToAttrType[CacheAuthorityType.MSSTS.ToString()])
.Select(x => MsalAccountCacheItem.FromJsonString(x))
.ToList();
}
internal SecStatusCode TryGetBrokerApplicationToken(string clientId, out string appToken)
{
var queryRecord = new SecRecord(SecKind.GenericPassword)
{
AccessGroup = _keychainGroup,
Account = clientId,
Service = iOSBrokerConstants.iOSBroker,
};
SecRecord record = SecKeyChain.QueryAsRecord(queryRecord, out SecStatusCode resultCode);
appToken = null;
if (resultCode == SecStatusCode.Success)
{
appToken = record.ValueData.ToString(NSStringEncoding.UTF8);
}
return resultCode;
}
private string GetPayload(IiOSKey key)
{
var queryRecord = new SecRecord(SecKind.GenericPassword)
{
Account = key.iOSAccount,
Service = key.iOSService,
CreatorType = key.iOSType,
AccessGroup = _keychainGroup
};
var match = SecKeyChain.QueryAsRecord(queryRecord, out SecStatusCode resultCode);
return (resultCode == SecStatusCode.Success)
? match.ValueData.ToString(NSStringEncoding.UTF8)
: string.Empty;
}
private ICollection<string> GetPayloadAsString(int type)
{
var queryRecord = new SecRecord(SecKind.GenericPassword)
{
CreatorType = type,
AccessGroup = _keychainGroup
};
SecRecord[] records = SecKeyChain.QueryAsRecord(queryRecord, int.MaxValue, out SecStatusCode resultCode);
ICollection<string> res = new List<string>();
if (resultCode == SecStatusCode.Success)
{
foreach (var record in records)
{
string str = record.ValueData.ToString(NSStringEncoding.UTF8);
res.Add(str);
}
}
return res;
}
private SecStatusCode Save(IiOSKey key, string payload)
{
var recordToSave = new SecRecord(SecKind.GenericPassword)
{
Account = key.iOSAccount,
Service = key.iOSService,
Generic = key.iOSGeneric,
CreatorType = key.iOSType,
ValueData = NSData.FromString(payload, NSStringEncoding.UTF8),
AccessGroup = _keychainGroup,
Accessible = _defaultAccessiblityPolicy,
Synchronizable = _defaultSyncSetting,
};
var secStatusCode = Update(recordToSave);
if (secStatusCode == SecStatusCode.ItemNotFound)
{
secStatusCode = SecKeyChain.Add(recordToSave);
}
if (secStatusCode == SecStatusCode.MissingEntitlement)
{
throw new MsalClientException(
MsalError.MissingEntitlements,
string.Format(
CultureInfo.InvariantCulture,
MsalErrorMessage.MissingEntitlements,
recordToSave.AccessGroup));
}
return secStatusCode;
}
internal SecStatusCode SaveBrokerApplicationToken(string clientIdAsKey, string applicationToken)
{
// The broker application token is used starting w/iOS broker 6.3.19+ (v3)
// If the application cannot be verified, an additional user consent dialog will be required the first time
// when the application is using the iOS broker.
// After initial user consent, the iOS broker will issue a token to the application that will
// grant application access to its own cache on subsequent broker invocations.
// On subsequent calls, the application presents the application token to the iOS broker, this will prevent
// the showing of the consent dialog.
var recordToSave = new SecRecord(SecKind.GenericPassword)
{
Account = clientIdAsKey,
Service = iOSBrokerConstants.iOSBroker,
ValueData = NSData.FromString(applicationToken, NSStringEncoding.UTF8),
AccessGroup = _keychainGroup,
Accessible = _defaultAccessiblityPolicy,
Synchronizable = _defaultSyncSetting
};
SecStatusCode secStatusCode = Update(recordToSave);
if (secStatusCode == SecStatusCode.ItemNotFound)
{
secStatusCode = SecKeyChain.Add(recordToSave);
}
return secStatusCode;
}
private SecStatusCode Remove(IiOSKey key)
{
var record = new SecRecord(SecKind.GenericPassword)
{
Account = key.iOSAccount,
Service = key.iOSService,
CreatorType = key.iOSType,
AccessGroup = _keychainGroup
};
return SecKeyChain.Remove(record);
}
private SecStatusCode Update(SecRecord updatedRecord)
{
var currentRecord = new SecRecord(SecKind.GenericPassword)
{
Account = updatedRecord.Account,
Service = updatedRecord.Service,
CreatorType = updatedRecord.CreatorType,
AccessGroup = _keychainGroup
};
var attributesToUpdate = new SecRecord()
{
ValueData = updatedRecord.ValueData
};
return SecKeyChain.Update(currentRecord, attributesToUpdate);
}
private void RemoveByType(int type)
{
var queryRecord = new SecRecord(SecKind.GenericPassword)
{
CreatorType = type,
AccessGroup = _keychainGroup
};
SecKeyChain.Remove(queryRecord);
}
public void Clear()
{
RemoveByType((int)MsalCacheKeys.iOSCredentialAttrType.AccessToken);
RemoveByType((int)MsalCacheKeys.iOSCredentialAttrType.RefreshToken);
RemoveByType((int)MsalCacheKeys.iOSCredentialAttrType.IdToken);
RemoveByType(MsalCacheKeys.iOSAuthorityTypeToAttrType[CacheAuthorityType.MSSTS.ToString()]);
}
public MsalAccessTokenCacheItem GetAccessToken(MsalAccessTokenCacheKey accessTokenKey)
{
return MsalAccessTokenCacheItem.FromJsonString(GetPayload(accessTokenKey));
}
public MsalRefreshTokenCacheItem GetRefreshToken(MsalRefreshTokenCacheKey refreshTokenKey)
{
return MsalRefreshTokenCacheItem.FromJsonString(GetPayload(refreshTokenKey));
}
public MsalIdTokenCacheItem GetIdToken(MsalIdTokenCacheKey idTokenKey)
{
return MsalIdTokenCacheItem.FromJsonString(GetPayload(idTokenKey));
}
public MsalAccountCacheItem GetAccount(MsalAccountCacheKey accountKey)
{
return MsalAccountCacheItem.FromJsonString(GetPayload(accountKey));
}
#region AppMetatada - not implemented on iOS
public MsalAppMetadataCacheItem ReadAppMetadata(MsalAppMetadataCacheKey appMetadataKey)
{
//return MsalAppMetadataCacheItem.FromJsonString(GetPayload(appMetadataKey));
throw new NotImplementedException();
}
public void WriteAppMetadata(MsalAppMetadataCacheItem appMetadata)
{
//Save(appMetadata.GetKey(), appMetadata.ToJsonString());
throw new NotImplementedException();
}
public void SaveAppMetadata(MsalAppMetadataCacheItem item)
{
throw new NotImplementedException();
}
public IEnumerable<MsalAppMetadataCacheItem> GetAllAppMetadata()
{
throw new NotImplementedException();
}
public MsalAppMetadataCacheItem GetAppMetadata(MsalAppMetadataCacheKey appMetadataKey)
{
throw new NotImplementedException();
}
#endregion
}
}