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

hotfix(voice, auth): handle zero byte array in app signature #71

Merged
merged 1 commit into from
May 13, 2024
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
2 changes: 1 addition & 1 deletion src/Sinch/Auth/ApplicationSignedAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public string GetSignedAuth(byte[] jsonBodyBytes, string httpVerb,
public Task<string> GetAuthToken(bool force = false)
{
var encodedBody = string.Empty;
if (_jsonBodyInBytes is not null)
if (_jsonBodyInBytes is not null && _jsonBodyInBytes.Length > 0)
{
var md5Bytes = MD5.HashData(_jsonBodyInBytes);
encodedBody = Convert.ToBase64String(md5Bytes);
Expand Down
7 changes: 6 additions & 1 deletion src/Sinch/SinchClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ public class SinchClient : ISinchClient
private const string SmsApiUrlTemplate = "https://zt.{0}.sms.api.sinch.com";
private const string SmsApiServicePlanIdUrlTemplate = "https://{0}.sms.api.sinch.com";
private const string ConversationApiUrlTemplate = "https://{0}.conversation.api.sinch.com/";

private const string VoiceApiUrlTemplate = "https://{0}.api.sinch.com/";

// apparently, management api for applications have a different set url
private const string VoiceApiApplicationManagementUrl = "https://callingapi.sinch.com/";
private const string AuthApiUrl = "https://auth.sinch.com";
private const string TemplatesApiUrlTemplate = "https://{0}.template.api.sinch.com/";

Expand Down Expand Up @@ -281,7 +285,8 @@ public ISinchVoiceClient Voice(string appKey, string appSecret,
var http = new Http(auth, _httpClient, _loggerFactory?.Create<IHttp>(), JsonNamingPolicy.CamelCase);
return new SinchVoiceClient(
new Uri(_apiUrlOverrides?.VoiceUrl ?? string.Format(VoiceApiUrlTemplate, voiceRegion.Value)),
_loggerFactory, http, (auth as ApplicationSignedAuth)!);
_loggerFactory, http, (auth as ApplicationSignedAuth)!,
new Uri(_apiUrlOverrides?.VoiceUrl ?? VoiceApiApplicationManagementUrl));
}

private void ValidateCommonCredentials()
Expand Down
5 changes: 3 additions & 2 deletions src/Sinch/Voice/SinchVoiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ internal class SinchVoiceClient : ISinchVoiceClient
private readonly ILoggerAdapter<ISinchVoiceClient>? _logger;

public SinchVoiceClient(Uri baseAddress, LoggerFactory? loggerFactory,
IHttp http, ApplicationSignedAuth applicationSignedAuth)
IHttp http, ApplicationSignedAuth applicationSignedAuth, Uri applicationManagementBaseAddress)
{
_applicationSignedAuth = applicationSignedAuth;
_logger = loggerFactory?.Create<ISinchVoiceClient>();
Callouts = new SinchCallout(loggerFactory?.Create<ISinchVoiceCallout>(), baseAddress, http);
Calls = new SinchCalls(loggerFactory?.Create<ISinchVoiceCalls>(), baseAddress, http);
Conferences = new SinchConferences(loggerFactory?.Create<ISinchVoiceConferences>(), baseAddress, http);
Applications = new SinchApplications(loggerFactory?.Create<ISinchVoiceApplications>(), baseAddress, http);
Applications = new SinchApplications(loggerFactory?.Create<ISinchVoiceApplications>(),
applicationManagementBaseAddress, http);
}

/// <inheritdoc />
Expand Down
8 changes: 5 additions & 3 deletions tests/Sinch.Tests/AuthTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,14 @@ public void GenerateCorrectApplicationSignature()
.BeEquivalentTo("669E367E-6BBA-48AB-AF15-266871C28135:h6rXrFayOoggyHW4ymnLlfSDkZZPg6j98lHzvOXVjvw=");
}

[Fact]
public void AppSignatureWithEmptyBody()
[Theory]
[InlineData(new byte[0])]
[InlineData(null)]
public void AppSignatureWithEmptyBody(byte[] data)
{
var auth = new ApplicationSignedAuth("669E367E-6BBA-48AB-AF15-266871C28135", "BeIukql3pTKJ8RGL5zo0DA==");

var result = auth.GetSignedAuth(null, "POST", "/verification/v1/verifications",
var result = auth.GetSignedAuth(data, "POST", "/verification/v1/verifications",
"x-timestamp:2014-06-04T13:41:58Z",
"application/json");

Expand Down
Loading