diff --git a/Deepgram/AuthClient.cs b/Deepgram/AuthClient.cs new file mode 100644 index 00000000..3bf7a268 --- /dev/null +++ b/Deepgram/AuthClient.cs @@ -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; + +/// +/// Implements the latest supported version of the Auth Client. +/// +public class AuthClient : Client +{ + public AuthClient(string apiKey = "", DeepgramHttpClientOptions? deepgramClientOptions = null, + string? httpId = null) : base(apiKey, deepgramClientOptions, httpId) + { + } +} diff --git a/Deepgram/ClientFactory.cs b/Deepgram/ClientFactory.cs index 18c5bb65..a5f0b684 100644 --- a/Deepgram/ClientFactory.cs +++ b/Deepgram/ClientFactory.cs @@ -49,6 +49,18 @@ public static V2.IListenWebSocketClient CreateListenWebSocketClient(string apiKe return new ListenWebSocketClient(apiKey, options); } + /// + /// Create a new AuthClient + /// + /// + /// + /// + /// + public static V1.IAuthClient CreateAuthClient(string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) + { + return new AuthClient(apiKey, options, httpId); + } + /// /// Create a new ManageClient /// diff --git a/Deepgram/Clients/Auth/v1/Client.cs b/Deepgram/Clients/Auth/v1/Client.cs new file mode 100644 index 00000000..d6424e2a --- /dev/null +++ b/Deepgram/Clients/Auth/v1/Client.cs @@ -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; + +/// +/// Implements version 1 of the Models Client. +/// +/// Required DeepgramApiKey +/// for HttpClient Configuration +public class Client(string? apiKey = null, IDeepgramClientOptions? deepgramClientOptions = null, string? httpId = null) + : AbstractRestClient(apiKey, deepgramClientOptions, httpId), IAuthClient +{ + /// + /// Gets a temporary JWT for the Deepgram API. + /// + /// + public async Task GrantToken(CancellationTokenSource? cancellationToken = default, + Dictionary? addons = null, Dictionary? headers = null) + { + Log.Verbose("AuthClient.GrantToken", "ENTER"); + + var uri = GetUri(_options, $"auth/{UriSegments.GRANTTOKEN}"); + var result = await PostAsync(uri, null, cancellationToken, addons, headers); + + Log.Information("GrantToken", $"{uri} Succeeded"); + Log.Debug("GrantToken", $"result: {result}"); + Log.Verbose("AuthClient.GrantToken", "LEAVE"); + + return result; + } +} diff --git a/Deepgram/Clients/Auth/v1/UriSegments.cs b/Deepgram/Clients/Auth/v1/UriSegments.cs new file mode 100644 index 00000000..be70f5fb --- /dev/null +++ b/Deepgram/Clients/Auth/v1/UriSegments.cs @@ -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"; +} diff --git a/Deepgram/Clients/Interfaces/v1/IAuthClient.cs b/Deepgram/Clients/Interfaces/v1/IAuthClient.cs new file mode 100644 index 00000000..1e05979c --- /dev/null +++ b/Deepgram/Clients/Interfaces/v1/IAuthClient.cs @@ -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; + +/// +/// Implements version 1 of the Auth Client. +/// +/// Required DeepgramApiKey +/// for HttpClient Configuration +public interface IAuthClient +{ + /// + /// Gets a short-lived JWT for the Deepgram API. + /// + /// + public Task GrantToken(CancellationTokenSource? cancellationToken = default, + Dictionary? addons = null, Dictionary? headers = null); +} diff --git a/Deepgram/Models/Auth/v1/GrantTokenResponse.cs b/Deepgram/Models/Auth/v1/GrantTokenResponse.cs new file mode 100644 index 00000000..312f7006 --- /dev/null +++ b/Deepgram/Models/Auth/v1/GrantTokenResponse.cs @@ -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 +{ + /// + /// Access token for the Deepgram API + /// + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + [JsonPropertyName("access_token")] + public string? AccessToken { get; set; } + + /// + /// TTL of the access token in seconds + /// + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + [JsonPropertyName("expires_in")] + public decimal? ExpiresIn { get; set; } + + /// + /// Override ToString method to serialize the object + /// + public override string ToString() + { + return Regex.Unescape(JsonSerializer.Serialize(this, JsonSerializeOptions.DefaultOptions)); + } +} diff --git a/examples/auth/grant-token/Auth.csproj b/examples/auth/grant-token/Auth.csproj new file mode 100644 index 00000000..2b2ca5d4 --- /dev/null +++ b/examples/auth/grant-token/Auth.csproj @@ -0,0 +1,22 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + + + + + + + + + diff --git a/examples/auth/grant-token/Program.cs b/examples/auth/grant-token/Program.cs new file mode 100644 index 00000000..9aa2c244 --- /dev/null +++ b/examples/auth/grant-token/Program.cs @@ -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(); + } + } +} \ No newline at end of file