Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
Improve handling for unauthorized API calls when response is null or …
Browse files Browse the repository at this point in the history
…user provides invalid token
  • Loading branch information
lorddev committed Sep 28, 2015
1 parent c0088f0 commit 7595c39
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 9 additions & 1 deletion SnapMD.ConnectedCare.Sdk/ApiCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ public ApiCall(string baseUrl,
string apiKey = null)
{
_baseUri = new Uri(baseUrl);

if (bearerToken == string.Empty)
{
// Prevent users from bypassing null checks by setting empty strings instead.
throw new ArgumentException("Invalid value supplied for bearer token.", "bearerToken");
}

_bearerToken = bearerToken;

_developerId = developerId;
_apiKey = apiKey;
RequiresAuthentication = true;
Expand Down Expand Up @@ -108,7 +116,7 @@ protected JObject MakeCall(Func<IWebClient, string> executeFunc)

private void SetHeaders(IWebClient wc)
{
if (RequiresAuthentication || _bearerToken != null)
if (RequiresAuthentication || !string.IsNullOrEmpty(_bearerToken))
{
AddHeader(wc, "Authorization", "Bearer " + _bearerToken);
}
Expand Down
4 changes: 4 additions & 0 deletions SnapMD.ConnectedCare.Sdk/UserApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public UserApi(string baseUrl, string bearerToken, string developerId, string ap
{
var response = MakeCall("v2/account/user");

if (response == null)
{
return null;
}

var dataEnumerator = response.ToObject<ApiResponseV2<SerializableUser>>();
if (dataEnumerator.Data != null)
Expand Down

0 comments on commit 7595c39

Please sign in to comment.