From 9961647adca6602d0ed9be0a0bd6fe6d9fb286dd Mon Sep 17 00:00:00 2001 From: Pete Gautier Date: Fri, 8 Sep 2023 13:59:03 -0700 Subject: [PATCH] chore: adding disposable token generation example --- .../DisposableTokens/DisposableTokens.csproj | 21 ++++++++++ examples/DisposableTokens/Program.cs | 42 +++++++++++++++++++ examples/DisposableTokens/README.md | 3 ++ 3 files changed, 66 insertions(+) create mode 100644 examples/DisposableTokens/DisposableTokens.csproj create mode 100644 examples/DisposableTokens/Program.cs create mode 100644 examples/DisposableTokens/README.md diff --git a/examples/DisposableTokens/DisposableTokens.csproj b/examples/DisposableTokens/DisposableTokens.csproj new file mode 100644 index 00000000..50fc400d --- /dev/null +++ b/examples/DisposableTokens/DisposableTokens.csproj @@ -0,0 +1,21 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + diff --git a/examples/DisposableTokens/Program.cs b/examples/DisposableTokens/Program.cs new file mode 100644 index 00000000..b901bf3d --- /dev/null +++ b/examples/DisposableTokens/Program.cs @@ -0,0 +1,42 @@ +using System; +using Momento.Sdk; +using Momento.Sdk.Auth; +using Momento.Sdk.Auth.AccessControl; +using Momento.Sdk.Config; +using Momento.Sdk.Responses; + +ICredentialProvider authProvider = new EnvMomentoTokenProvider("MOMENTO_AUTH_TOKEN"); + +IAuthClient client = new AuthClient(AuthConfigurations.Default.Latest(), authProvider); +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 client.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); +} diff --git a/examples/DisposableTokens/README.md b/examples/DisposableTokens/README.md new file mode 100644 index 00000000..afffe023 --- /dev/null +++ b/examples/DisposableTokens/README.md @@ -0,0 +1,3 @@ +logo + +# Disposable Tokens Example