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

Commit

Permalink
Added logic for OAuth token in the authorization header
Browse files Browse the repository at this point in the history
Also added unit test
  • Loading branch information
assislucas authored and lucasamaral committed Sep 6, 2018
1 parent 1a5b39e commit d5658b1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
8 changes: 6 additions & 2 deletions Shippo/APIResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,12 @@ protected virtual WebRequest SetupRequest(string method, string url)
req.Credentials = credential;
req.PreAuthenticate = true; */

// Disable line below for basic auth
req.Headers.Add("Authorization", "ShippoToken " + accessToken);
// Disable lines below for basic auth
string tokenType = "ShippoToken";
if (accessToken.StartsWith("oauth.")) {
tokenType = "Bearer";
}
req.Headers.Add("Authorization", string.Format("{0} {1}", tokenType, accessToken));
if (apiVersion != null) {
req.Headers.Add("Shippo-API-Version", apiVersion);
}
Expand Down
4 changes: 2 additions & 2 deletions Shippo/Shippo.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
<package>
<metadata>
<id>Shippo</id>
<version>2.1.1</version>
<version>2.1.3</version>
<title>Shippo client library</title>
<authors>Shippo</authors>
<owners>Shippo</owners>
<description>Shippo Shipping API client Library (USPS, FedEx, UPS, and more)</description>
<releaseNotes>Added support for the new Shippo API Version - for full changelog visit goshippo.com/docs/changelog</releaseNotes>
<releaseNotes>Added support for OAuth Token</releaseNotes>
<copyright></copyright>
<tags>USPS Fedex UPS API Shippo</tags>
<dependencies>
Expand Down
13 changes: 13 additions & 0 deletions ShippoTesting/APIResourceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ public void TestValidHeader() {
Assert.AreEqual("ShippoToken " + dummyApiToken, request.Headers.Get("Authorization"));
Assert.AreEqual(dummyApiVersion, request.Headers.Get("Shippo-API-Version"));
}

[Test]
public void TestValidOAuthHeader() {
String dummyMethod = "post";
String dummyUrl = "http://example.com";
String dummyApiToken = "oauth.abcdefffff.yyyyassaldjf=";
String dummyApiVersion = "2018-02-08";
MockAPIResource resource = new MockAPIResource(dummyApiToken);
resource.SetApiVersion(dummyApiVersion);
WebRequest request = resource.SetupRequestTest(dummyMethod, dummyUrl);
Assert.AreEqual("Bearer " + dummyApiToken, request.Headers.Get("Authorization"));
Assert.AreEqual(dummyApiVersion, request.Headers.Get("Shippo-API-Version"));
}
}

}

0 comments on commit d5658b1

Please sign in to comment.