Skip to content
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

feat: initialize credentials from Configuration #491

Merged
merged 1 commit into from
Aug 23, 2023
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
78 changes: 78 additions & 0 deletions Vonage.Test.Unit/Request/CredentialsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,93 @@
using System.Collections.Generic;
using System.Text;
using FluentAssertions;
using Microsoft.Extensions.Configuration;
using Vonage.Common.Failures;
using Vonage.Common.Test.Extensions;
using Vonage.Cryptography;
using Vonage.Request;
using Xunit;

namespace Vonage.Test.Unit.Request
{
public class CredentialsTest
{
[Fact] public void FromConfiguration_ShouldLoadEmptyCredentials_GivenConfigurationContainsNoElement()
{
var credentials = Credentials.FromConfiguration(new ConfigurationBuilder().Build());
credentials.ApiKey.Should().BeNull();
credentials.ApiSecret.Should().BeNull();
credentials.ApplicationId.Should().BeNull();
credentials.ApplicationKey.Should().BeNull();
credentials.Method.Should().Be(SmsSignatureGenerator.Method.md5hash);
credentials.SecuritySecret.Should().BeNull();
credentials.AppUserAgent.Should().BeNull();
}

[Fact] public void FromConfiguration_ShouldSetApiKey_GivenConfigurationContainsApiKey() =>
Credentials.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage_key", "testKey"},
})
.Build()).ApiKey.Should().Be("testKey");

[Fact] public void FromConfiguration_ShouldSetApiKSecret_GivenConfigurationContainsApiSecret() =>
Credentials.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage_secret", "testSecret"},
})
.Build()).ApiSecret.Should().Be("testSecret");

[Fact] public void FromConfiguration_ShouldSetApplicationId_GivenConfigurationContainsApplicationId() =>
Credentials.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage.Application.Id", "testApplicationId"},
})
.Build()).ApplicationId.Should().Be("testApplicationId");

[Fact] public void FromConfiguration_ShouldSetApplicationKey_GivenConfigurationContainsApplicationKey() =>
Credentials.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage.Application.Key", "testApplicationKey"},
})
.Build()).ApplicationKey.Should().Be("testApplicationKey");

[Fact] public void FromConfiguration_ShouldSetSecuritySecret_GivenConfigurationContainsSecuritySecret() =>
Credentials.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage.security_secret", "testSecuritySecret"},
})
.Build()).SecuritySecret.Should().Be("testSecuritySecret");

[Fact] public void FromConfiguration_ShouldSetUserAgent_GivenConfigurationContainsUserAgent() =>
Credentials.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage.UserAgent", "testUserAgent"},
})
.Build()).AppUserAgent.Should().Be("testUserAgent");

[Fact] public void FromConfiguration_ShouldSetSigningMethod_GivenConfigurationContainsSigningMethod() =>
Credentials.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage.signing_method", "sha512"},
})
.Build()).Method.Should().Be(SmsSignatureGenerator.Method.sha512);

[Fact] public void FromConfiguration_ShouldSetDefaultSigningMethod_GivenConfigurationContainsInvalidSigningMethod() =>
Credentials.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage.signing_method", "invalid"},
})
.Build()).Method.Should().Be(SmsSignatureGenerator.Method.md5hash);

[Fact]
public void GetAuthenticationHeader_ReturnsBasicScheme_GivenContainsApiKeyAndApiSecret() =>
BuildBasicCredentials()
Expand Down
1 change: 1 addition & 0 deletions Vonage.Test.Unit/Vonage.Test.Unit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@
<None Update="ProactiveConnect\Events\Data\ShouldDeserialize200-response.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<Folder Include="ConfigurationData\" />
<Folder Include="Messages\Messenger\Data" />
<Folder Include="Messages\Mms\Data" />
<Folder Include="Messages\Sms\Data" />
Expand Down
21 changes: 21 additions & 0 deletions Vonage/Request/Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.Net.Http.Headers;
using System.Text;
using Microsoft.Extensions.Configuration;
using Vonage.Common.Failures;
using Vonage.Common.Monads;
using Vonage.Cryptography;
Expand Down Expand Up @@ -147,6 +148,26 @@ public static Credentials FromAppIdAndPrivateKeyPath(string appId, string privat
return new Credentials {ApplicationId = appId, ApplicationKey = privateKey};
}

/// <summary>
/// Initializes a Credentials from configuration.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <returns>The Credentials.</returns>
public static Credentials FromConfiguration(IConfiguration configuration) =>
new()
{
ApiKey = configuration["appSettings:Vonage_key"],
ApiSecret = configuration["appSettings:Vonage_secret"],
ApplicationId = configuration["appSettings:Vonage.Application.Id"],
ApplicationKey = configuration["appSettings:Vonage.Application.Key"],
SecuritySecret = configuration["appSettings:Vonage.security_secret"],
AppUserAgent = configuration["appSettings:Vonage.UserAgent"],
Method = Enum.TryParse(configuration["appSettings:Vonage.signing_method"],
out SmsSignatureGenerator.Method result)
? result
: default,
};

/// <summary>
/// Provides the preferred authentication based on authentication type.
/// </summary>
Expand Down