-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Sinch dashboard credentials
- Loading branch information
Showing
6 changed files
with
264 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/Sinch.ServerSdk/ApiFilters/AccessCredentialsSigningFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Sinch.ServerSdk.ApiFilters | ||
{ | ||
internal class AccessCredentialsSigningFilter : SinchSigningFilterBase | ||
{ | ||
private readonly AuthenticationHeaderValue _authHeader; | ||
private readonly string _applicationKey; | ||
|
||
public AccessCredentialsSigningFilter(SinchAccessCredentials credentials) | ||
{ | ||
if (credentials == null) | ||
throw new ArgumentNullException(nameof(credentials)); | ||
|
||
_applicationKey = credentials.ApplicationKey; | ||
|
||
_authHeader = new AuthenticationHeaderValue("Basic", | ||
Convert.ToBase64String(Encoding.ASCII.GetBytes($"{credentials.AccessKeyId}:{credentials.KeySecret}"))); | ||
} | ||
|
||
public override Task OnActionExecuting(HttpRequestMessage requestMessage) | ||
{ | ||
requestMessage.Headers.Authorization = _authHeader; | ||
requestMessage.Headers.Add("X-Sinch-AuthType", "zap"); | ||
requestMessage.Headers.Add("X-Sinch-ApplicationKey", _applicationKey); | ||
|
||
// net45 does not have Task.CompletedTask | ||
return Task.FromResult(false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json; | ||
using Sinch.WebApiClient; | ||
|
||
namespace Sinch.ServerSdk.ApiFilters | ||
{ | ||
internal abstract class SinchSigningFilterBase : IActionFilter | ||
{ | ||
public abstract Task OnActionExecuting(HttpRequestMessage requestMessage); | ||
|
||
public async Task OnActionExecuted(HttpResponseMessage responseMessage) | ||
{ | ||
if (responseMessage.StatusCode != HttpStatusCode.OK && | ||
responseMessage.StatusCode != HttpStatusCode.NoContent) | ||
{ | ||
var value = await responseMessage.Content.ReadAsStringAsync(); | ||
ApiError error; | ||
try | ||
{ | ||
error = JsonConvert.DeserializeObject<ApiError>(value) ?? | ||
new ApiError { ErrorCode = (int)responseMessage.StatusCode, Message = "Unable to deserialize exception (because it seems to be empty): " + value }; | ||
} | ||
catch (JsonSerializationException) | ||
{ | ||
error = new ApiError { ErrorCode = (int)responseMessage.StatusCode, Message = "Unable to deserialize exception: " + value }; | ||
} | ||
|
||
throw new ApiException(error); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
|
||
namespace Sinch.ServerSdk | ||
{ | ||
/// <summary> | ||
/// Sinch project credentials | ||
/// </summary> | ||
public class SinchAccessCredentials | ||
{ | ||
/// <summary> | ||
/// Project access key ID | ||
/// </summary> | ||
public readonly string AccessKeyId; | ||
|
||
/// <summary> | ||
/// Project key secret | ||
/// </summary> | ||
public readonly string KeySecret; | ||
|
||
/// <summary> | ||
/// Application identifier to be associated with the requests | ||
/// </summary> | ||
public readonly string ApplicationKey; | ||
|
||
public SinchAccessCredentials(string accessKeyId, string keySecret, string applicationKey) | ||
{ | ||
if (accessKeyId == null) | ||
throw new ArgumentNullException(nameof(accessKeyId)); | ||
|
||
if (string.Empty.Equals(accessKeyId)) | ||
throw new ArgumentException($"{nameof(accessKeyId)} must be a non-empty string", nameof(accessKeyId)); | ||
|
||
if (keySecret == null) | ||
throw new ArgumentNullException(nameof(keySecret)); | ||
|
||
if (string.Empty.Equals(keySecret)) | ||
throw new ArgumentException($"{nameof(keySecret)} must be a non-empty string", nameof(keySecret)); | ||
|
||
if (applicationKey == null) | ||
throw new ArgumentNullException(nameof(applicationKey)); | ||
|
||
if (string.Empty.Equals(applicationKey)) | ||
throw new ArgumentException($"{nameof(applicationKey)} must be a non-empty string", nameof(applicationKey)); | ||
|
||
AccessKeyId = accessKeyId; | ||
KeySecret = keySecret; | ||
ApplicationKey = applicationKey; | ||
} | ||
|
||
public static SinchAccessCredentials Create(string accessKeyId, string keySecret, string applicationKey) => | ||
new SinchAccessCredentials(accessKeyId, keySecret, applicationKey); | ||
} | ||
} |
Oops, something went wrong.