Skip to content

Commit

Permalink
chore: add disposable token code snippet for dev docs
Browse files Browse the repository at this point in the history
  • Loading branch information
anitarua committed Sep 11, 2023
1 parent 2d0e0d3 commit 231d750
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/DocExampleApis/DocExampleApis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Momento.Sdk" Version="1.19.0" />
<PackageReference Include="Momento.Sdk" Version="1.21.1" />
</ItemGroup>

</Project>
43 changes: 43 additions & 0 deletions examples/DocExampleApis/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Momento.Sdk;
using Momento.Sdk.Auth;
using Momento.Sdk.Auth.AccessControl;
using Momento.Sdk.Config;
using Momento.Sdk.Responses;

Expand All @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -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<DisposableTokenPermission>
{
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);
}
}
}

0 comments on commit 231d750

Please sign in to comment.