Skip to content

Commit f83b5c4

Browse files
authored
Merge pull request #369 from deepgram/feat/auth
feat: support short-lived token endpoint
2 parents 86c1716 + 5ba2038 commit f83b5c4

File tree

8 files changed

+195
-0
lines changed

8 files changed

+195
-0
lines changed

Deepgram/AuthClient.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved.
2+
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
3+
// SPDX-License-Identifier: MIT
4+
5+
using Deepgram.Clients.Auth.v1;
6+
using Deepgram.Models.Authenticate.v1;
7+
8+
namespace Deepgram;
9+
10+
/// <summary>
11+
/// Implements the latest supported version of the Auth Client.
12+
/// </summary>
13+
public class AuthClient : Client
14+
{
15+
public AuthClient(string apiKey = "", DeepgramHttpClientOptions? deepgramClientOptions = null,
16+
string? httpId = null) : base(apiKey, deepgramClientOptions, httpId)
17+
{
18+
}
19+
}

Deepgram/ClientFactory.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ public static V2.IListenWebSocketClient CreateListenWebSocketClient(string apiKe
4949
return new ListenWebSocketClient(apiKey, options);
5050
}
5151

52+
/// <summary>
53+
/// Create a new AuthClient
54+
/// </summary>
55+
/// <param name="apiKey"></param>
56+
/// <param name="options"></param>
57+
/// <param name="httpId"></param>
58+
/// <returns></returns>
59+
public static V1.IAuthClient CreateAuthClient(string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null)
60+
{
61+
return new AuthClient(apiKey, options, httpId);
62+
}
63+
5264
/// <summary>
5365
/// Create a new ManageClient
5466
/// </summary>

Deepgram/Clients/Auth/v1/Client.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved.
2+
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
3+
// SPDX-License-Identifier: MIT
4+
5+
using Deepgram.Models.Authenticate.v1;
6+
using Deepgram.Models.Auth.v1;
7+
using Deepgram.Clients.Interfaces.v1;
8+
using Deepgram.Abstractions.v1;
9+
10+
namespace Deepgram.Clients.Auth.v1;
11+
12+
/// <summary>
13+
/// Implements version 1 of the Models Client.
14+
/// </summary>
15+
/// <param name="apiKey">Required DeepgramApiKey</param>
16+
/// <param name="deepgramClientOptions"><see cref="DeepgramHttpClientOptions"/> for HttpClient Configuration</param>
17+
public class Client(string? apiKey = null, IDeepgramClientOptions? deepgramClientOptions = null, string? httpId = null)
18+
: AbstractRestClient(apiKey, deepgramClientOptions, httpId), IAuthClient
19+
{
20+
/// <summary>
21+
/// Gets a temporary JWT for the Deepgram API.
22+
/// </summary>
23+
/// <returns><see cref="GrantTokenResponse"/></returns>
24+
public async Task<GrantTokenResponse> GrantToken(CancellationTokenSource? cancellationToken = default,
25+
Dictionary<string, string>? addons = null, Dictionary<string, string>? headers = null)
26+
{
27+
Log.Verbose("AuthClient.GrantToken", "ENTER");
28+
29+
var uri = GetUri(_options, $"auth/{UriSegments.GRANTTOKEN}");
30+
var result = await PostAsync<object, GrantTokenResponse>(uri, null, cancellationToken, addons, headers);
31+
32+
Log.Information("GrantToken", $"{uri} Succeeded");
33+
Log.Debug("GrantToken", $"result: {result}");
34+
Log.Verbose("AuthClient.GrantToken", "LEAVE");
35+
36+
return result;
37+
}
38+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved.
2+
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
3+
// SPDX-License-Identifier: MIT
4+
5+
namespace Deepgram.Clients.Auth.v1;
6+
7+
public static class UriSegments
8+
{
9+
//using constants instead of inline value(magic strings) make consistence
10+
//across SDK And Test Projects Simpler and Easier to change
11+
public const string GRANTTOKEN = "grant";
12+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved.
2+
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
3+
// SPDX-License-Identifier: MIT
4+
5+
using Deepgram.Models.Auth.v1;
6+
7+
namespace Deepgram.Clients.Interfaces.v1;
8+
9+
/// <summary>
10+
/// Implements version 1 of the Auth Client.
11+
/// </summary>
12+
/// <param name="apiKey">Required DeepgramApiKey</param>
13+
/// <param name="deepgramClientOptions"><see cref="DeepgramHttpClientOptions"/> for HttpClient Configuration</param>
14+
public interface IAuthClient
15+
{
16+
/// <summary>
17+
/// Gets a short-lived JWT for the Deepgram API.
18+
/// </summary>
19+
/// <returns><see cref="GrantTokenResponse"/></returns>
20+
public Task<GrantTokenResponse> GrantToken(CancellationTokenSource? cancellationToken = default,
21+
Dictionary<string, string>? addons = null, Dictionary<string, string>? headers = null);
22+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved.
2+
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
3+
// SPDX-License-Identifier: MIT
4+
5+
namespace Deepgram.Models.Auth.v1;
6+
7+
public record GrantTokenResponse
8+
{
9+
/// <summary>
10+
/// Access token for the Deepgram API
11+
/// </summary>
12+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
13+
[JsonPropertyName("access_token")]
14+
public string? AccessToken { get; set; }
15+
16+
/// <summary>
17+
/// TTL of the access token in seconds
18+
/// </summary>
19+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
20+
[JsonPropertyName("expires_in")]
21+
public decimal? ExpiresIn { get; set; }
22+
23+
/// <summary>
24+
/// Override ToString method to serialize the object
25+
/// </summary>
26+
public override string ToString()
27+
{
28+
return Regex.Unescape(JsonSerializer.Serialize(this, JsonSerializeOptions.DefaultOptions));
29+
}
30+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\..\..\Deepgram\Deepgram.csproj" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<Using Include="Deepgram" />
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved.
2+
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
3+
// SPDX-License-Identifier: MIT
4+
5+
using Deepgram.Models.Auth.v1;
6+
7+
namespace SampleApp
8+
{
9+
class Program
10+
{
11+
static async Task Main(string[] args)
12+
{
13+
// Initialize Library with default logging
14+
// Normal logging is "Info" level
15+
Library.Initialize();
16+
17+
// use the client factory with a API Key set with the "DEEPGRAM_API_KEY" environment variable
18+
var deepgramClient = ClientFactory.CreateAuthClient();
19+
20+
// generate token
21+
var tokenResp = await deepgramClient.GrantToken();
22+
if (tokenResp == null)
23+
{
24+
Console.WriteLine("GrantToken failed.");
25+
Environment.Exit(1);
26+
}
27+
28+
string token = tokenResp.AccessToken;
29+
string ttl = tokenResp.ExpiresIn.ToString();
30+
Console.WriteLine($"Token: {token}");
31+
Console.WriteLine($"TTL: {ttl}");
32+
33+
Console.WriteLine("\n\nPress any key to exit.");
34+
Console.ReadKey();
35+
36+
// Teardown Library
37+
Library.Terminate();
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)