public static void AddRefreshTokenToCache(ITokenCacheAccessor accessor, string uid, string utid, string name) { var rtItem = new MsalRefreshTokenCacheItem (CoreTestConstants.ProductionPrefCacheEnvironment, CoreTestConstants.ClientId, "someRT", MockHelpers.CreateClientInfo(uid, utid)); accessor.SaveRefreshToken(rtItem); }
public IDictionary <string, JToken> Deserialize(byte[] bytes, bool clearExistingCacheData) { CacheSerializationContract cache; try { cache = CacheSerializationContract.FromJsonString(CoreHelpers.ByteArrayToString(bytes)); } catch (Exception ex) { throw new MsalClientException(MsalError.JsonParseError, MsalErrorMessage.TokenCacheJsonSerializerFailedParse, ex); } if (clearExistingCacheData) { _accessor.Clear(); } if (cache.AccessTokens != null) { foreach (var atItem in cache.AccessTokens.Values) { _accessor.SaveAccessToken(atItem); } } if (cache.RefreshTokens != null) { foreach (var rtItem in cache.RefreshTokens.Values) { _accessor.SaveRefreshToken(rtItem); } } if (cache.IdTokens != null) { foreach (var idItem in cache.IdTokens.Values) { _accessor.SaveIdToken(idItem); } } if (cache.Accounts != null) { foreach (var account in cache.Accounts.Values) { _accessor.SaveAccount(account); } } if (cache.AppMetadata != null) { foreach (var appMetadata in cache.AppMetadata.Values) { _accessor.SaveAppMetadata(appMetadata); } } return(cache.UnknownNodes); }
public IDictionary <string, JToken> Deserialize(byte[] bytes, bool clearExistingCacheData) { List <KeyValuePair <string, IEnumerable <string> > > cacheKvpList; try { cacheKvpList = JsonHelper.DeserializeFromJson <List <KeyValuePair <string, IEnumerable <string> > > >(bytes); } catch (Exception ex) { throw new MsalClientException(MsalError.JsonParseError, MsalErrorMessage.TokenCacheDictionarySerializerFailedParse, ex); } var cacheDict = cacheKvpList.ToDictionary(x => x.Key, x => x.Value); if (clearExistingCacheData) { _accessor.Clear(); } if (cacheKvpList == null || cacheKvpList.Count == 0) { return(null); } if (cacheDict.ContainsKey(AccessTokenKey)) { foreach (var atItem in cacheDict[AccessTokenKey]) { _accessor.SaveAccessToken(MsalAccessTokenCacheItem.FromJsonString(atItem)); } } if (cacheDict.ContainsKey(RefreshTokenKey)) { foreach (var rtItem in cacheDict[RefreshTokenKey]) { _accessor.SaveRefreshToken(MsalRefreshTokenCacheItem.FromJsonString(rtItem)); } } if (cacheDict.ContainsKey(IdTokenKey)) { foreach (var idItem in cacheDict[IdTokenKey]) { _accessor.SaveIdToken(MsalIdTokenCacheItem.FromJsonString(idItem)); } } if (cacheDict.ContainsKey(AccountKey)) { foreach (var account in cacheDict[AccountKey]) { _accessor.SaveAccount(MsalAccountCacheItem.FromJsonString(account)); } } return(null); }
public static void AddRefreshTokenToCache( ITokenCacheAccessor accessor, string uid, string utid, string clientId = TestConstants.ClientId, string environment = TestConstants.ProductionPrefCacheEnvironment, string rtSecret = TestConstants.RTSecret) { var rtItem = new MsalRefreshTokenCacheItem (environment, clientId, rtSecret, MockHelpers.CreateClientInfo(uid, utid), null, $"{uid}.{utid}"); accessor.SaveRefreshToken(rtItem); }
public static void WriteMsalRefreshToken(ITokenCacheAccessor tokenCacheAccessor, AdalResultWrapper resultWrapper, string authority, string clientId, string displayableId, string givenName, string familyName, string objectId) { if (string.IsNullOrEmpty(resultWrapper.RawClientInfo)) { MsalLogger.Default.Info("Client Info is missing. Skipping MSAL refresh token cache write"); return; } if (string.IsNullOrEmpty(resultWrapper.RefreshToken)) { MsalLogger.Default.Info("Refresh Token is missing. Skipping MSAL refresh token cache write"); return; } if (string.IsNullOrEmpty(resultWrapper.Result.IdToken)) { MsalLogger.Default.Info("Id Token is missing. Skipping MSAL refresh token cache write"); return; } try { var rtItem = new MsalRefreshTokenCacheItem (new Uri(authority).Host, clientId, resultWrapper.RefreshToken, resultWrapper.RawClientInfo); tokenCacheAccessor.SaveRefreshToken(rtItem); MsalAccountCacheItem accountCacheItem = new MsalAccountCacheItem (new Uri(authority).Host, objectId, resultWrapper.RawClientInfo, null, displayableId, resultWrapper.Result.TenantId, givenName, familyName); tokenCacheAccessor.SaveAccount(accountCacheItem); } catch (Exception ex) { MsalLogger.Default.WarningPiiWithPrefix( ex, "An error occurred while writing ADAL refresh token to the cache in MSAL format. " + "For details please see https://aka.ms/net-cache-persistence-errors. "); } }
/// <summary> /// Deserializes the token cache from a serialization blob /// </summary> /// <param name="tokenCacheAccessor">Token cache accessor to perform cache write operations (to fill-in from the state)</param> /// <param name="unifiedState">Array of bytes containing serialized unified cache data</param> /// <param name="requestContext">call state to pass correlation id and logger instance</param> internal static void DeserializeUnifiedCache(ITokenCacheAccessor tokenCacheAccessor, byte[] unifiedState, RequestContext requestContext) { tokenCacheAccessor.Clear(); Dictionary <string, IEnumerable <string> > cacheDict = JsonHelper .DeserializeFromJson <Dictionary <string, IEnumerable <string> > >(unifiedState); if (cacheDict == null || cacheDict.Count == 0) { MsalLogger.Default.Info("Msal Cache is empty."); return; } if (cacheDict.ContainsKey(AccessTokenKey)) { foreach (var atItem in cacheDict[AccessTokenKey]) { var msalAccessTokenCacheItem = JsonHelper.TryToDeserializeFromJson <MsalAccessTokenCacheItem>(atItem, requestContext); if (msalAccessTokenCacheItem != null) { tokenCacheAccessor.SaveAccessToken(msalAccessTokenCacheItem); } } } if (cacheDict.ContainsKey(RefreshTokenKey)) { foreach (var rtItem in cacheDict[RefreshTokenKey]) { var msalRefreshTokenCacheItem = JsonHelper.TryToDeserializeFromJson <MsalRefreshTokenCacheItem>(rtItem, requestContext); if (msalRefreshTokenCacheItem != null) { tokenCacheAccessor.SaveRefreshToken(msalRefreshTokenCacheItem); } } } if (cacheDict.ContainsKey(IdTokenKey)) { foreach (var idItem in cacheDict[IdTokenKey]) { var msalIdTokenCacheItem = JsonHelper.TryToDeserializeFromJson <MsalIdTokenCacheItem>(idItem, requestContext); if (msalIdTokenCacheItem != null) { tokenCacheAccessor.SaveIdToken(msalIdTokenCacheItem); } } } if (cacheDict.ContainsKey(AccountKey)) { foreach (var account in cacheDict[AccountKey]) { var msalAccountCacheItem = JsonHelper.TryToDeserializeFromJson <MsalAccountCacheItem>(account, requestContext); if (msalAccountCacheItem != null) { tokenCacheAccessor.SaveAccount(msalAccountCacheItem); } } } }
/// <inheritdoc /> public void SaveRefreshToken(MsalRefreshTokenCacheItem item) { _tokenCacheAccessor.SaveRefreshToken(item); }