Skip to content

Commit

Permalink
Pass credentials into SetUserAgent so when file config isn't being us…
Browse files Browse the repository at this point in the history
…ed the option to send app version is still available
  • Loading branch information
smithrobs committed Sep 29, 2017
1 parent 961dddd commit 7eba9cb
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Nexmo.Api/Client/ShortCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Api.SMS.SMSResponse RequestAlert(Api.ShortCode.AlertRequest request, Dict
sb.AppendFormat("{0}={1}&", System.Net.WebUtility.UrlEncode(key), System.Net.WebUtility.UrlEncode(customValues[key]));
}

var json = ApiRequest.DoRequest(ApiRequest.GetBaseUriFor(typeof(Api.ShortCode), "/sc/us/alert/json?" + sb));
var json = ApiRequest.DoRequest(ApiRequest.GetBaseUriFor(typeof(Api.ShortCode), "/sc/us/alert/json?" + sb), creds);
return JsonConvert.DeserializeObject<Api.SMS.SMSResponse>(json);
}
}
Expand Down
10 changes: 5 additions & 5 deletions Nexmo.Api/Request/ApiRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,24 +110,24 @@ internal static StringBuilder GetQueryStringBuilderFor(object parameters, Creden
public static string DoRequest(Uri uri, Dictionary<string, string> parameters, Credentials creds = null)
{
var sb = BuildQueryString(parameters, creds);
return DoRequest(new Uri(uri, "?" + sb));
return DoRequest(new Uri(uri, "?" + sb), creds);
}

internal static string DoRequest(Uri uri, object parameters, Credentials creds = null)
{
var sb = GetQueryStringBuilderFor(parameters, creds);

return DoRequest(new Uri(uri, "?" + sb));
return DoRequest(new Uri(uri, "?" + sb), creds);
}

internal static string DoRequest(Uri uri)
internal static string DoRequest(Uri uri, Credentials creds)
{
var req = new HttpRequestMessage
{
RequestUri = uri,
Method = HttpMethod.Get
};
VersionedApiRequest.SetUserAgent(ref req);
VersionedApiRequest.SetUserAgent(ref req, creds);

using (Configuration.Instance.ApiLogger.BeginScope("ApiRequest.DoRequest {0}",uri.GetHashCode()))
{
Expand Down Expand Up @@ -169,7 +169,7 @@ public static NexmoResponse DoRequest(string method, Uri uri, Dictionary<string,
RequestUri = uri,
Method = new HttpMethod(method),
};
VersionedApiRequest.SetUserAgent(ref req);
VersionedApiRequest.SetUserAgent(ref req, creds);

var data = Encoding.ASCII.GetBytes(sb.ToString());
req.Content = new ByteArrayContent(data);
Expand Down
3 changes: 3 additions & 0 deletions Nexmo.Api/Request/Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ public class Credentials
public string SecuritySecret { get; set; }
public string ApplicationId { get; set; }
public string ApplicationKey { get; set; }

// Optional app useragent value to pass with every request
public string AppUserAgent { get; set; }
}
}
4 changes: 2 additions & 2 deletions Nexmo.Api/Request/SignatureHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private static bool SlowEquals(byte[] a, byte[] b)
return diff == 0;
}

public static bool IsSignatureValid(IEnumerable<KeyValuePair<string, StringValues>> querystring)
public static bool IsSignatureValid(IEnumerable<KeyValuePair<string, StringValues>> querystring, string securitySecret = null)
{
Action<IDictionary<string, string>, StringBuilder> buildStringFromParams = (param, strings) =>
{
Expand Down Expand Up @@ -50,7 +50,7 @@ public static bool IsSignatureValid(IEnumerable<KeyValuePair<string, StringValue
var sb = new StringBuilder();
buildStringFromParams(sorted, sb);
var queryToSign = "&" + sb;
queryToSign = queryToSign.Remove(queryToSign.Length - 1) + Configuration.Instance.Settings["appSettings:Nexmo.security_secret"].ToUpper();
queryToSign = queryToSign.Remove(queryToSign.Length - 1) + (securitySecret?.ToUpper() ?? Configuration.Instance.Settings["appSettings:Nexmo.security_secret"].ToUpper());
// Generate MD5
var hashgen = MD5.Create();
var hash = hashgen.ComputeHash(Encoding.UTF8.GetBytes(queryToSign));
Expand Down
8 changes: 4 additions & 4 deletions Nexmo.Api/Request/VersionedApiRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private static string DoRequest(Uri uri, Credentials creds = null)
RequestUri = uri,
Method = HttpMethod.Get,
};
SetUserAgent(ref req);
SetUserAgent(ref req, creds);
// attempt bearer token auth
req.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer",
Jwt.CreateToken(appId, appKeyPath));
Expand Down Expand Up @@ -65,7 +65,7 @@ private static string DoRequest(Uri uri, Credentials creds = null)
}

private static string _userAgent;
internal static void SetUserAgent(ref HttpRequestMessage request)
internal static void SetUserAgent(ref HttpRequestMessage request, Credentials creds)
{
if (string.IsNullOrEmpty(_userAgent))
{
Expand All @@ -91,7 +91,7 @@ internal static void SetUserAgent(ref HttpRequestMessage request)

_userAgent = $"nexmo-dotnet/{libraryVersion} dotnet/{runtimeVersion}";

var appVersion = Configuration.Instance.Settings["appSettings:Nexmo.UserAgent"];
var appVersion = creds?.AppUserAgent ?? Configuration.Instance.Settings["appSettings:Nexmo.UserAgent"];
if (!string.IsNullOrWhiteSpace(appVersion))
{
_userAgent += $" {appVersion}";
Expand Down Expand Up @@ -120,7 +120,7 @@ public static NexmoResponse DoRequest(string method, Uri uri, object payload, Cr
RequestUri = uri,
Method = new HttpMethod(method),
};
SetUserAgent(ref req);
SetUserAgent(ref req, creds);
// attempt bearer token auth
req.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer",
Jwt.CreateToken(appId, appKeyPath));
Expand Down
2 changes: 1 addition & 1 deletion Nexmo.Api/ShortCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static SMS.SMSResponse RequestAlert(AlertRequest request, Dictionary<stri
sb.AppendFormat("{0}={1}&", System.Net.WebUtility.UrlEncode(key), System.Net.WebUtility.UrlEncode(customValues[key]));
}

var json = ApiRequest.DoRequest(ApiRequest.GetBaseUriFor(typeof(ShortCode), "/sc/us/alert/json?" + sb));
var json = ApiRequest.DoRequest(ApiRequest.GetBaseUriFor(typeof(ShortCode), "/sc/us/alert/json?" + sb), creds);
return JsonConvert.DeserializeObject<SMS.SMSResponse>(json);
}
}
Expand Down

0 comments on commit 7eba9cb

Please sign in to comment.