-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
194 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Remove="Momento.Sdk" /> | ||
<None Remove="Microsoft.SourceLink.GitHub" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="Momento.Sdk" Version="1.20.0" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<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 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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<img src="https://docs.momentohq.com/img/logo.svg" alt="logo" width="400"/> | ||
|
||
# Disposable Tokens Example | ||
|
||
This example program demonstrates how to generate disposable Momento auth tokens. | ||
|
||
# Usage | ||
|
||
The program assumes that your Momento auth token is available in the `MOMENTO_AUTH_TOKEN` environment variable: | ||
|
||
```bash | ||
MOMENTO_AUTH_TOKEN=<YOUR_AUTH_TOKEN> dotnet run | ||
``` | ||
|
||
The example generates a disposable expiring auth token using the enumerated permissions and expiry defined in the program and prints its attributes to the console. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters