From 231d75060e6c352186cbf49233504dfe1e12bf11 Mon Sep 17 00:00:00 2001 From: anitarua Date: Mon, 11 Sep 2023 16:00:44 -0700 Subject: [PATCH] chore: add disposable token code snippet for dev docs --- examples/DocExampleApis/DocExampleApis.csproj | 2 +- examples/DocExampleApis/Program.cs | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/examples/DocExampleApis/DocExampleApis.csproj b/examples/DocExampleApis/DocExampleApis.csproj index 19b3ed86..fe4d87f8 100644 --- a/examples/DocExampleApis/DocExampleApis.csproj +++ b/examples/DocExampleApis/DocExampleApis.csproj @@ -8,7 +8,7 @@ - + diff --git a/examples/DocExampleApis/Program.cs b/examples/DocExampleApis/Program.cs index 87ca1d50..f2d9f19d 100644 --- a/examples/DocExampleApis/Program.cs +++ b/examples/DocExampleApis/Program.cs @@ -1,5 +1,6 @@ using Momento.Sdk; using Momento.Sdk.Auth; +using Momento.Sdk.Auth.AccessControl; using Momento.Sdk.Config; using Momento.Sdk.Responses; @@ -12,6 +13,10 @@ public static async Task Main(string[] args) var client = new CacheClient(config, new EnvMomentoTokenProvider("MOMENTO_AUTH_TOKEN"), TimeSpan.FromSeconds(10)); + IAuthClient authClient = new AuthClient( + AuthConfigurations.Default.Latest(), + new EnvMomentoTokenProvider("MOMENTO_AUTH_TOKEN") + ); await Example_API_CreateCache(client); await Example_API_FlushCache(client); @@ -22,6 +27,8 @@ public static async Task Main(string[] args) await Example_API_Set(client); await Example_API_Get(client); await Example_API_Delete(client); + + await Example_API_GenerateDisposableToken(authClient); } public static async Task Example_API_CreateCache(CacheClient cacheClient) @@ -122,4 +129,40 @@ public static async Task Example_API_Delete(CacheClient cacheClient) throw new Exception($"An error occurred while attempting to delete key 'test-key' from cache 'test-cache': {error.ErrorCode}: {error}"); } } + + public static async Task Example_API_GenerateDisposableToken(IAuthClient authClient) + { + var scope = new DisposableTokenScope(Permissions: new List + { + new DisposableToken.CacheItemPermission( + CacheRole.ReadWrite, + CacheSelector.ByName("cache"), + CacheItemSelector.AllCacheItems + ), + new DisposableToken.CachePermission( + CacheRole.ReadOnly, + CacheSelector.ByName("topsecret") + ), + new DisposableToken.TopicPermission( + TopicRole.PublishSubscribe, + CacheSelector.ByName("cache"), + TopicSelector.ByName("example-topic") + ) + }); + var tokenResponse = await authClient.GenerateDisposableTokenAsync( + scope, + ExpiresIn.Minutes(5) + ); + + if (tokenResponse is GenerateDisposableTokenResponse.Success token) + { + Console.WriteLine("The generated disposable token is: " + token.AuthToken); + Console.WriteLine("The token endpoint is: " + token.Endpoint); + Console.WriteLine("The token expires at (epoch timestamp): " + token.ExpiresAt.Epoch()); + } + else if (tokenResponse is GenerateDisposableTokenResponse.Error err) + { + Console.WriteLine("Error generating disposable token: " + err.Message); + } + } }