Skip to content

feat: support short-lived token endpoint #369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Deepgram/AuthClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

using Deepgram.Clients.Auth.v1;
using Deepgram.Models.Authenticate.v1;

namespace Deepgram;

/// <summary>
/// Implements the latest supported version of the Auth Client.
/// </summary>
public class AuthClient : Client
{
public AuthClient(string apiKey = "", DeepgramHttpClientOptions? deepgramClientOptions = null,
string? httpId = null) : base(apiKey, deepgramClientOptions, httpId)
{
}
}
12 changes: 12 additions & 0 deletions Deepgram/ClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@
return new ListenWebSocketClient(apiKey, options);
}

/// <summary>
/// Create a new AuthClient
/// </summary>
/// <param name="apiKey"></param>
/// <param name="options"></param>
/// <param name="httpId"></param>
/// <returns></returns>
public static V1.IAuthClient CreateAuthClient(string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null)
{
return new AuthClient(apiKey, options, httpId);
}

/// <summary>
/// Create a new ManageClient
/// </summary>
Expand Down Expand Up @@ -160,7 +172,7 @@
case 1:
Log.Information("ClientFactory", $"Version 1 of the ListenWebSocketClient is being deprecated in the next major version.");
Log.Information("ClientFactory", $"Transition to the latest version at your earliest convenience.");
return new ListenV1.Client(apiKey, options);

Check warning on line 175 in Deepgram/ClientFactory.cs

View workflow job for this annotation

GitHub Actions / build

'Client' is obsolete: 'Please use Deepgram.Clients.Listen.v2.WebSocket instead'

Check warning on line 175 in Deepgram/ClientFactory.cs

View workflow job for this annotation

GitHub Actions / test (8.0.x)

'Client' is obsolete: 'Please use Deepgram.Clients.Listen.v2.WebSocket instead'
case 2:
return new ListenV2.Client(apiKey, options);
default:
Expand Down Expand Up @@ -210,7 +222,7 @@
case 1:
Log.Information("ClientFactory", $"Version 1 of the ListenWebSocketClient is being deprecated in the next major version.");
Log.Information("ClientFactory", $"Transition to the latest version at your earliest convenience.");
return new SpeakV1.Client(apiKey, options);

Check warning on line 225 in Deepgram/ClientFactory.cs

View workflow job for this annotation

GitHub Actions / build

'Client' is obsolete: 'Please use Deepgram.Clients.Speak.v2.WebSocket instead'

Check warning on line 225 in Deepgram/ClientFactory.cs

View workflow job for this annotation

GitHub Actions / test (8.0.x)

'Client' is obsolete: 'Please use Deepgram.Clients.Speak.v2.WebSocket instead'
case 2:
return new SpeakV2.Client(apiKey, options);
default:
Expand Down
38 changes: 38 additions & 0 deletions Deepgram/Clients/Auth/v1/Client.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

using Deepgram.Models.Authenticate.v1;
using Deepgram.Models.Auth.v1;
using Deepgram.Clients.Interfaces.v1;
using Deepgram.Abstractions.v1;

namespace Deepgram.Clients.Auth.v1;

/// <summary>
/// Implements version 1 of the Models Client.
/// </summary>
/// <param name="apiKey">Required DeepgramApiKey</param>
/// <param name="deepgramClientOptions"><see cref="DeepgramHttpClientOptions"/> for HttpClient Configuration</param>
public class Client(string? apiKey = null, IDeepgramClientOptions? deepgramClientOptions = null, string? httpId = null)
: AbstractRestClient(apiKey, deepgramClientOptions, httpId), IAuthClient
{
/// <summary>
/// Gets a temporary JWT for the Deepgram API.
/// </summary>
/// <returns><see cref="GrantTokenResponse"/></returns>
public async Task<GrantTokenResponse> GrantToken(CancellationTokenSource? cancellationToken = default,
Dictionary<string, string>? addons = null, Dictionary<string, string>? headers = null)
{
Log.Verbose("AuthClient.GrantToken", "ENTER");

var uri = GetUri(_options, $"auth/{UriSegments.GRANTTOKEN}");
var result = await PostAsync<object, GrantTokenResponse>(uri, null, cancellationToken, addons, headers);

Log.Information("GrantToken", $"{uri} Succeeded");
Log.Debug("GrantToken", $"result: {result}");
Log.Verbose("AuthClient.GrantToken", "LEAVE");

return result;
}
}
12 changes: 12 additions & 0 deletions Deepgram/Clients/Auth/v1/UriSegments.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

namespace Deepgram.Clients.Auth.v1;

public static class UriSegments
{
//using constants instead of inline value(magic strings) make consistence
//across SDK And Test Projects Simpler and Easier to change
public const string GRANTTOKEN = "grant";
}
22 changes: 22 additions & 0 deletions Deepgram/Clients/Interfaces/v1/IAuthClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

using Deepgram.Models.Auth.v1;

namespace Deepgram.Clients.Interfaces.v1;

/// <summary>
/// Implements version 1 of the Auth Client.
/// </summary>
/// <param name="apiKey">Required DeepgramApiKey</param>
/// <param name="deepgramClientOptions"><see cref="DeepgramHttpClientOptions"/> for HttpClient Configuration</param>
public interface IAuthClient
{
/// <summary>
/// Gets a short-lived JWT for the Deepgram API.
/// </summary>
/// <returns><see cref="GrantTokenResponse"/></returns>
public Task<GrantTokenResponse> GrantToken(CancellationTokenSource? cancellationToken = default,
Dictionary<string, string>? addons = null, Dictionary<string, string>? headers = null);
}
30 changes: 30 additions & 0 deletions Deepgram/Models/Auth/v1/GrantTokenResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

namespace Deepgram.Models.Auth.v1;

public record GrantTokenResponse
{
/// <summary>
/// Access token for the Deepgram API
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("access_token")]
public string? AccessToken { get; set; }

/// <summary>
/// TTL of the access token in seconds
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("expires_in")]
public decimal? ExpiresIn { get; set; }

/// <summary>
/// Override ToString method to serialize the object
/// </summary>
public override string ToString()
{
return Regex.Unescape(JsonSerializer.Serialize(this, JsonSerializeOptions.DefaultOptions));
}
}
22 changes: 22 additions & 0 deletions examples/auth/grant-token/Auth.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Deepgram\Deepgram.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Deepgram" />
</ItemGroup>

</Project>
40 changes: 40 additions & 0 deletions examples/auth/grant-token/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

using Deepgram.Models.Auth.v1;

namespace SampleApp
{
class Program
{
static async Task Main(string[] args)
{
// Initialize Library with default logging
// Normal logging is "Info" level
Library.Initialize();

// use the client factory with a API Key set with the "DEEPGRAM_API_KEY" environment variable
var deepgramClient = ClientFactory.CreateAuthClient();

// generate token
var tokenResp = await deepgramClient.GrantToken();
if (tokenResp == null)
{
Console.WriteLine("GrantToken failed.");
Environment.Exit(1);
}

string token = tokenResp.AccessToken;
string ttl = tokenResp.ExpiresIn.ToString();
Console.WriteLine($"Token: {token}");
Console.WriteLine($"TTL: {ttl}");

Console.WriteLine("\n\nPress any key to exit.");
Console.ReadKey();

// Teardown Library
Library.Terminate();
}
}
}
Loading