private void AccessToken_GetOrAdd(int userId, int contentId = 0, string feature = null) { var timeout1 = TimeSpan.FromMinutes(3); var timeout2 = TimeSpan.FromMinutes(10); var timeout3 = TimeSpan.FromMinutes(20); // create three different tokens var savedToken1 = AccessTokenVault.CreateToken(userId, timeout1, contentId, feature); var savedToken2 = AccessTokenVault.CreateToken(userId, timeout2, contentId, feature); var savedToken3 = AccessTokenVault.CreateToken(userId, timeout3, contentId, feature); // ACTION: get a token with the same parameters var token = AccessTokenVault.GetOrAddToken(userId, timeout3, contentId, feature); // ASSERT: we should get the last one AssertTokensAreEqual(savedToken3, token); // ACTION: get a token with shorter expiration time token = AccessTokenVault.GetOrAddToken(userId, timeout2, contentId, feature); // ASSERT: we should get the previous one AssertTokensAreEqual(savedToken2, token); // ACTION: get a token with an even shorter expiration time token = AccessTokenVault.GetOrAddToken(userId, TimeSpan.FromMinutes(7), contentId, feature); // ASSERT: we should get a totally new one, because the first // token (savedToken1) expires too soon. Assert.AreNotEqual(savedToken1.Value, token.Value); Assert.AreNotEqual(savedToken2.Value, token.Value); Assert.AreNotEqual(savedToken3.Value, token.Value); Assert.IsTrue(token.ExpirationDate < savedToken2.ExpirationDate); }
public void UT_AccessToken_GetOrAdd_MaxTimeOut() { Providers.Instance = new Providers(new ServiceCollection() .AddSingleton <DataProvider, InMemoryDataProvider>() .AddSingleton <IAccessTokenDataProvider, InMemoryAccessTokenDataProvider>() .BuildServiceProvider()); // ACTION var token = AccessTokenVault.GetOrAddToken(1, TimeSpan.MaxValue); // ASSERT Assert.AreEqual(DateTime.MaxValue, token.ExpirationDate); }
public void UT_AccessToken_GetOrAdd_MaxTimeOut() { var dataProvider = new InMemoryDataProvider(); dataProvider.SetExtension(typeof(IAccessTokenDataProviderExtension), new InMemoryAccessTokenDataProvider()); Providers.Instance.DataProvider = dataProvider; // ACTION var token = AccessTokenVault.GetOrAddToken(1, TimeSpan.MaxValue); // ASSERT Assert.AreEqual(DateTime.MaxValue, token.ExpirationDate); }
public static object GetWopiData(Content content, string action) { if (!(content.ContentHandler is File)) { throw new SnNotSupportedException("Office Online is not supported for this type of content."); } var officeOnlineUrl = Settings.GetValue("OfficeOnline", "OfficeOnlineUrl", content.Path, string.Empty); if (string.IsNullOrEmpty(officeOnlineUrl)) { throw new SnNotSupportedException("Office Online Server setting not found."); } var wd = WopiDiscovery.GetInstance(officeOnlineUrl); if (wd == null || !wd.Zones.Any()) { throw new SnNotSupportedException("Office Online Server not found."); } //TODO: handle internal or external zone urls var wopiApp = wd.Zones["internal-https"]?.GetApp(content.Name, action); var wopiAction = wopiApp?.Actions.GetAction(action, content.Name); if (wopiAction == null) { throw new SnNotSupportedException($"Office Online action '{action}' is not supported on this content."); } // load an existing token or create a new one var token = AccessTokenVault.GetOrAddToken(User.Current.Id, DefaultTokenTimeout, content.Id, WopiHandler.AccessTokenFeatureName); var expiration = Math.Truncate((token.ExpirationDate - new DateTime(1970, 1, 1).ToUniversalTime()).TotalMilliseconds); return(new Dictionary <string, object> { { "accesstoken", token.Value }, { "expiration", expiration }, { "actionUrl", TransformUrl(wopiAction.UrlSrc, content.Id) }, { "faviconUrl", wopiApp.FaviconUrl } }); }