From 3172720a0470114ad9f9c23942cf40554d2723a3 Mon Sep 17 00:00:00 2001 From: tr00d Date: Thu, 20 Jun 2024 07:43:53 +0200 Subject: [PATCH] feat: introduce configurable url for OIDC requests --- Vonage.Test/ConfigurationTest.cs | 15 +++++++++++++++ Vonage.Test/TestHelpers/TestingContext.cs | 16 +++++++++++----- Vonage/Configuration.cs | 21 +++++++++++++++++---- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/Vonage.Test/ConfigurationTest.cs b/Vonage.Test/ConfigurationTest.cs index c401a394..a98c2f8b 100644 --- a/Vonage.Test/ConfigurationTest.cs +++ b/Vonage.Test/ConfigurationTest.cs @@ -43,6 +43,7 @@ public void FromConfiguration_ShouldCreateEmptyConfiguration_GivenConfigurationC configuration.VonageUrls.Nexmo.Should().Be(new Uri("https://api.nexmo.com")); configuration.VonageUrls.Rest.Should().Be(new Uri("https://rest.nexmo.com")); configuration.VonageUrls.Video.Should().Be(new Uri("https://video.api.vonage.com")); + configuration.VonageUrls.Oidc.Should().Be("https://oidc.idp.vonage.com"); configuration.VonageUrls.Get(VonageUrls.Region.EU).Should().Be(new Uri("https://api-eu.vonage.com")); configuration.VonageUrls.Get(VonageUrls.Region.APAC).Should().Be(new Uri("https://api-ap.vonage.com")); configuration.VonageUrls.Get(VonageUrls.Region.US).Should().Be(new Uri("https://api-us.vonage.com")); @@ -94,6 +95,15 @@ public void FromConfiguration_ShouldSetNexmoApiUrl_GivenConfigurationContainsNex }) .Build()).VonageUrls.Nexmo.Should().Be(new Uri("https://api.vonage.com")); + [Fact] + public void FromConfiguration_ShouldSetOidcUrl_GivenConfigurationContainsOidcUrl() => + Configuration.FromConfiguration(new ConfigurationBuilder() + .AddInMemoryCollection(new Dictionary + { + {"vonage:Url.OIDC", "https://api.vonage.com"}, + }) + .Build()).VonageUrls.Oidc.Should().Be(new Uri("https://api.vonage.com")); + [Fact] public void FromConfiguration_ShouldSetRequestTimeout_GivenConfigurationContainsRequestTimeout() => Configuration.FromConfiguration(new ConfigurationBuilder() @@ -206,6 +216,11 @@ public void VonageUrl_ShouldReturnDefaultVideoUrl() => public void VonageUrl_ShouldReturnNexmoUrl() => Configuration.FromConfiguration(new ConfigurationBuilder().Build()) .VonageUrls.Nexmo.Should().Be(new Uri("https://api.nexmo.com")); + + [Fact] + public void VonageUrl_ShouldReturnOidcUrl() => + Configuration.FromConfiguration(new ConfigurationBuilder().Build()) + .VonageUrls.Oidc.Should().Be(new Uri("https://oidc.idp.vonage.com")); } [Trait("Category", "HttpConnectionPool")] diff --git a/Vonage.Test/TestHelpers/TestingContext.cs b/Vonage.Test/TestHelpers/TestingContext.cs index 5e712885..0108bdd8 100644 --- a/Vonage.Test/TestHelpers/TestingContext.cs +++ b/Vonage.Test/TestHelpers/TestingContext.cs @@ -10,12 +10,18 @@ namespace Vonage.Test.TestHelpers; internal class TestingContext : IDisposable { - private TestingContext(string appSettingsKey, Credentials credentials, string authorizationHeaderValue, + private TestingContext(Credentials credentials, string authorizationHeaderValue, Dictionary settings) { this.ExpectedAuthorizationHeaderValue = authorizationHeaderValue; this.Server = WireMockServer.Start(); - settings.Add($"vonage:{appSettingsKey}", this.Server.Url); + settings.Add(VonageUrls.NexmoApiKey, this.Server.Url); + settings.Add($"{VonageUrls.NexmoApiKey}.EMEA", this.Server.Url); + settings.Add($"{VonageUrls.NexmoApiKey}.APAC", this.Server.Url); + settings.Add($"{VonageUrls.NexmoApiKey}.AMER", this.Server.Url); + settings.Add(VonageUrls.NexmoRestKey, this.Server.Url); + settings.Add(VonageUrls.VideoApiKey, this.Server.Url); + settings.Add(VonageUrls.OidcApiKey, this.Server.Url); var configuration = Configuration.FromConfiguration(new ConfigurationBuilder().AddInMemoryCollection(settings).Build()); this.VonageClient = new VonageClient(credentials, configuration, new TimeProvider()); @@ -32,14 +38,14 @@ public void Dispose() } public static TestingContext WithBasicCredentials(string appSettingsKey) => - new TestingContext(appSettingsKey, CreateBasicCredentials(), "Basic NzkwZmM1ZTU6QWEzNDU2Nzg5", + new TestingContext(CreateBasicCredentials(), "Basic NzkwZmM1ZTU6QWEzNDU2Nzg5", new Dictionary()); public static TestingContext WithBearerCredentials(string appSettingsKey) => - new TestingContext(appSettingsKey, CreateBearerCredentials(), "Bearer *", new Dictionary()); + new TestingContext(CreateBearerCredentials(), "Bearer *", new Dictionary()); public static TestingContext WithBasicCredentials(string appSettingsKey, Dictionary settings) => - new TestingContext(appSettingsKey, CreateBasicCredentials(), "Basic NzkwZmM1ZTU6QWEzNDU2Nzg5", settings); + new TestingContext(CreateBasicCredentials(), "Basic NzkwZmM1ZTU6QWEzNDU2Nzg5", settings); private static Credentials CreateBasicCredentials() => Credentials.FromApiKeyAndSecret("790fc5e5", "Aa3456789"); diff --git a/Vonage/Configuration.cs b/Vonage/Configuration.cs index 3d81ad80..cbed574b 100644 --- a/Vonage/Configuration.cs +++ b/Vonage/Configuration.cs @@ -168,7 +168,7 @@ public Credentials BuildCredentials() => /// The HttpClient. public HttpClient BuildHttpClientForNexmo() => this.BuildHttpClient(this.VonageUrls.Nexmo); - private HttpClient BuildHttpClient(Uri baseUri) + internal HttpClient BuildHttpClient(Uri baseUri) { var client = new HttpClient(this.ClientHandler) { @@ -185,6 +185,12 @@ private HttpClient BuildHttpClient(Uri baseUri) /// The HttpClient. public HttpClient BuildHttpClientForVideo() => this.BuildHttpClient(this.VonageUrls.Video); + /// + /// Build an HttpClient for OIDC requests. + /// + /// The HttpClient. + public HttpClient BuildHttpClientForOidc() => this.BuildHttpClient(this.VonageUrls.Oidc); + /// /// Build an HttpClient for a specific region. /// @@ -254,9 +260,11 @@ public readonly struct VonageUrls private const string DefaultNexmoApiUrl = "https://api.nexmo.com"; private const string DefaultRestApiUrl = "https://rest.nexmo.com"; private const string DefaultVideoApiUrl = "https://video.api.vonage.com"; - private const string NexmoApiKey = "vonage:Url.Api"; - private const string NexmoRestKey = "vonage:Url.Rest"; - private const string VideoApiKey = "vonage:Url.Api.Video"; + private const string DefaultOidcUrl = "https://oidc.idp.vonage.com"; + internal const string NexmoApiKey = "vonage:Url.Api"; + internal const string NexmoRestKey = "vonage:Url.Rest"; + internal const string VideoApiKey = "vonage:Url.Api.Video"; + internal const string OidcApiKey = "vonage:Url.OIDC"; private readonly Dictionary regions = new Dictionary { @@ -269,6 +277,11 @@ public readonly struct VonageUrls private VonageUrls(IConfiguration configuration) => this.configuration = configuration; + /// + /// The Oidc Url. + /// + public Uri Oidc => this.Evaluate(OidcApiKey, DefaultOidcUrl); + /// /// The Nexmo Api Url. ///