public static bool IsInStoryblokEditor(this IQueryCollection query, StoryblokOptions settings) { if (string.IsNullOrWhiteSpace(query["_storyblok_tk[space_id]"])) // fast check to return false immediately without calculating any hashes { return(false); } var validationString = $"{query["_storyblok_tk[space_id]"]}:{settings.ApiKeyPreview}:{query["_storyblok_tk[timestamp]"]}"; using (var sha1 = SHA1.Create()) { var hashBytes = sha1.ComputeHash(Encoding.UTF8.GetBytes(validationString)); var sb = new StringBuilder(); foreach (var b in hashBytes) { var hex = b.ToString("x2"); sb.Append(hex); } var validationToken = sb.ToString(); var timestamp = (int)Math.Floor((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds) - 3600; return(query["_storyblok_tk[token]"] == validationToken && int.Parse(query["_storyblok_tk[timestamp]"]) > timestamp); } }
protected StoryblokBaseClient(IOptions <StoryblokOptions> settings, IHttpClientFactory clientFactory, IHttpContextAccessor?httpContext, IMemoryCache memoryCache, ILogger <StoryblokBaseClient> logger) { Client = clientFactory.CreateClient(); MemoryCache = memoryCache; Logger = logger; Settings = settings.Value; IsInEditor = httpContext?.HttpContext?.Request?.Query?.IsInStoryblokEditor(Settings) ?? false; ValidateSettings(); }
public static StoryblokDatasourceClient GetDatasourceClient() { var options = new StoryblokOptions { // set view environment variable, useful in unit tests on build server, where the key comes from build secrets ApiKeyPublic = Environment.GetEnvironmentVariable("Adliance_Storyblok_Tests__ApiKeyPublic"), SupportedCultures = new[] { "de", "en" } }; return(new StoryblokDatasourceClient( Options.Create(options), new MockedHttpClientFactory(), null, new MemoryCache(Options.Create(new MemoryCacheOptions())), NullLogger <StoryblokBaseClient> .Instance)); }
public static IServiceCollection AddStoryblok(this IServiceCollection services, IConfigurationSection?configurationSection, Action <StoryblokOptions>?configure = null) { var options = new StoryblokOptions(); configurationSection?.Bind(options); services.Configure <StoryblokOptions>(storyblokOptions => { if (storyblokOptions != null) { configurationSection?.Bind(storyblokOptions); } if (configure != null && storyblokOptions != null) { configure.Invoke(storyblokOptions); } }); return(AddStoryblok(services)); }