/// <summary>
        /// This method retrieves token string for a specific <paramref name="siteId" />.
        /// Token is retrieved from cache if it exists there and is valid, otherwise provided by the remote API
        /// </summary>
        /// <returns>
        /// Token string for Consultix authorization
        /// </returns>
        /// <param name="siteId">Site Id Guid associated to the token</param>
        /// <seealso cref="GetClientId(Guid)"/>
        /// <seealso cref="RefreshToken(string)"/>
        public string GetToken(Guid siteId)
        {
            var clientId = GetClientId(siteId);

            // Cache key is related to client Id. Client Id is related to site Id
            var cacheKey = "{{ClientId_CacheKey}}";

            // Get from remote API if not existing in cache
            if (!_cacheProvider.ContainsKey(cacheKey))
            {
                var bearer = RefreshToken(clientId);
                _cacheProvider.Add(cacheKey, bearer, bearer.ExpiryStamp);
                return(bearer?.AccessToken);
            }
            else
            {
                var bearer = _cacheProvider.Get <BearerToken>(cacheKey);

                // Check token validity if from cache, otherwise get from remote
                if (AuthenticationHelper.IsValidToken(bearer))
                {
                    return(bearer.AccessToken);
                }
                else
                {
                    var newBearer = RefreshToken(clientId);
                    _cacheProvider.Add(cacheKey, newBearer, newBearer.ExpiryStamp);
                    return(bearer?.AccessToken);
                }
            }
        }
示例#2
0
 /// <summary>
 /// Check's if a node's alias is cached.
 /// </summary>
 public static bool ContainsAlias(this ICacheProvider cache, int id)
 {
     return(cache.ContainsKey(string.Format(_aliasFormat, id)));
 }
示例#3
0
 /// <summary>
 /// Check's if a model's property value is cached.
 /// </summary>
 public static bool ContainsPropertyValue(this ICacheProvider cache, int id, string propertyName)
 {
     return(cache.ContainsKey(string.Format(_propertyValueFormat, id, propertyName)));
 }
 private void AndItemExistsInCache(string key)
 {
     _cache.ContainsKey(key).ShouldBe(true);
 }