Skip to content

Commit

Permalink
feat: introduce configurable url for OIDC requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Jun 20, 2024
1 parent 01311c4 commit 3172720
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
15 changes: 15 additions & 0 deletions Vonage.Test/ConfigurationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -94,6 +95,15 @@ public void FromConfiguration_ShouldCreateEmptyConfiguration_GivenConfigurationC
})
.Build()).VonageUrls.Nexmo.Should().Be(new Uri("https://api.vonage.com"));

[Fact]
public void FromConfiguration_ShouldSetOidcUrl_GivenConfigurationContainsOidcUrl() =>
Configuration.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"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()
Expand Down Expand Up @@ -206,6 +216,11 @@ public void FromConfiguration_ShouldCreateEmptyConfiguration_GivenConfigurationC
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")]
Expand Down
16 changes: 11 additions & 5 deletions Vonage.Test/TestHelpers/TestingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> 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());
Expand All @@ -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<string, string>());

public static TestingContext WithBearerCredentials(string appSettingsKey) =>
new TestingContext(appSettingsKey, CreateBearerCredentials(), "Bearer *", new Dictionary<string, string>());
new TestingContext(CreateBearerCredentials(), "Bearer *", new Dictionary<string, string>());

public static TestingContext WithBasicCredentials(string appSettingsKey, Dictionary<string, string> settings) =>
new TestingContext(appSettingsKey, CreateBasicCredentials(), "Basic NzkwZmM1ZTU6QWEzNDU2Nzg5", settings);
new TestingContext(CreateBasicCredentials(), "Basic NzkwZmM1ZTU6QWEzNDU2Nzg5", settings);

private static Credentials CreateBasicCredentials() => Credentials.FromApiKeyAndSecret("790fc5e5", "Aa3456789");

Expand Down
21 changes: 17 additions & 4 deletions Vonage/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public HttpClient Client
/// <returns>The HttpClient.</returns>
public HttpClient BuildHttpClientForNexmo() => this.BuildHttpClient(this.VonageUrls.Nexmo);

private HttpClient BuildHttpClient(Uri baseUri)
internal HttpClient BuildHttpClient(Uri baseUri)
{
var client = new HttpClient(this.ClientHandler)
{
Expand All @@ -185,6 +185,12 @@ private HttpClient BuildHttpClient(Uri baseUri)
/// <returns>The HttpClient.</returns>
public HttpClient BuildHttpClientForVideo() => this.BuildHttpClient(this.VonageUrls.Video);

/// <summary>
/// Build an HttpClient for OIDC requests.
/// </summary>
/// <returns>The HttpClient.</returns>
public HttpClient BuildHttpClientForOidc() => this.BuildHttpClient(this.VonageUrls.Oidc);

/// <summary>
/// Build an HttpClient for a specific region.
/// </summary>
Expand Down Expand Up @@ -254,9 +260,11 @@ private void LogAuthenticationCapabilities(ILogger logger)
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<Region, string> regions = new Dictionary<Region, string>
{
Expand All @@ -269,6 +277,11 @@ private void LogAuthenticationCapabilities(ILogger logger)

private VonageUrls(IConfiguration configuration) => this.configuration = configuration;

/// <summary>
/// The Oidc Url.
/// </summary>
public Uri Oidc => this.Evaluate(OidcApiKey, DefaultOidcUrl);

/// <summary>
/// The Nexmo Api Url.
/// </summary>
Expand Down

0 comments on commit 3172720

Please sign in to comment.