Skip to content

Commit

Permalink
feat: add possibility to set auth token uri in BoxConfig (#794)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwwoda committed Jan 14, 2022
1 parent 2766a91 commit ae8cd8b
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 2 deletions.
15 changes: 15 additions & 0 deletions Box.V2.Test/BoxConfigTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,20 @@ public void BoxConfig_CreateFromString()
Assert.AreEqual(config.JWTPrivateKey, "DUMMY");
Assert.AreEqual(config.EnterpriseId, "eid-123");
}

[TestMethod]
[TestCategory("CI-UNIT-TEST")]
public void BoxConfig_SetAuthTokenUriString()
{
var boxConfig = new BoxConfigBuilder("", "", "", "", "", "")
.Build();
Assert.AreEqual(boxConfig.BoxAuthTokenApiUri, new System.Uri(Constants.BoxAuthTokenApiUriString));

var exampleUri = new System.Uri("https://example.com/");
var newConfig = new BoxConfigBuilder("", "", "", "", "", "")
.SetBoxTokenApiUri(exampleUri)
.Build();
Assert.AreEqual(newConfig.BoxAuthTokenApiUri, exampleUri);
}
}
}
2 changes: 2 additions & 0 deletions Box.V2.Test/BoxJWTAuthTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Threading.Tasks;
using Box.V2.Auth;
using Box.V2.Config;
Expand Down Expand Up @@ -25,6 +26,7 @@ public BoxJWTAuthTest()
_service = new BoxService(_handler.Object);
_boxConfig = new Mock<IBoxConfig>();
_boxConfig.SetupGet(x => x.EnterpriseId).Returns("12345");
_boxConfig.SetupGet(x => x.BoxAuthTokenApiUri).Returns(new Uri(Constants.BoxAuthTokenApiUriString));
_jwtAuth = new BoxJWTAuth(_boxConfig.Object, _service);
}

Expand Down
2 changes: 2 additions & 0 deletions Box.V2/Config/BoxConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public BoxConfig(BoxConfigBuilder builder)
BoxAccountApiHostUri = builder.BoxAccountApiHostUri;
BoxApiUri = builder.BoxApiUri;
BoxUploadApiUri = builder.BoxUploadApiUri;
BoxAuthTokenApiUri = builder.BoxAuthTokenApiUri;
RedirectUri = builder.RedirectUri;
DeviceId = builder.DeviceId;
DeviceName = builder.DeviceName;
Expand Down Expand Up @@ -147,6 +148,7 @@ public static IBoxConfig CreateFromJsonString(string jsonString)
public Uri BoxAccountApiHostUri { get; private set; } = new Uri(Constants.BoxAccountApiHostUriString);
public Uri BoxApiUri { get; private set; } = new Uri(Constants.BoxApiUriString);
public Uri BoxUploadApiUri { get; private set; } = new Uri(Constants.BoxUploadApiUriString);
public Uri BoxAuthTokenApiUri { get; private set; } = new Uri(Constants.BoxAuthTokenApiUriString);

public string ClientId { get; private set; }
public string ConsumerKey { get; private set; }
Expand Down
12 changes: 12 additions & 0 deletions Box.V2/Config/BoxConfigBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ public BoxConfigBuilder SetBoxUploadApiUri(Uri boxUploadApiUri)
return this;
}

/// <summary>
/// Sets BoxAPI auth token uri.
/// </summary>
/// <param name="boxAuthTokenApiUri">BoxAPI auth token uri.</param>
/// <returns>this BoxConfigBuilder object for chaining</returns>
public BoxConfigBuilder SetBoxTokenApiUri(Uri boxAuthTokenApiUri)
{
BoxAuthTokenApiUri = boxAuthTokenApiUri;
return this;
}

/// <summary>
/// Sets redirect uri.
/// </summary>
Expand Down Expand Up @@ -227,6 +238,7 @@ public BoxConfigBuilder SetTimeout(TimeSpan timeout)
public Uri BoxAccountApiHostUri { get; private set; } = new Uri(Constants.BoxAccountApiHostUriString);
public Uri BoxApiUri { get; private set; } = new Uri(Constants.BoxApiUriString);
public Uri BoxUploadApiUri { get; private set; } = new Uri(Constants.BoxUploadApiUriString);
public Uri BoxAuthTokenApiUri { get; private set; } = new Uri(Constants.BoxAuthTokenApiUriString);

public Uri RedirectUri { get; private set; }
public string DeviceId { get; private set; }
Expand Down
1 change: 1 addition & 0 deletions Box.V2/Config/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static class Constants
public const string BoxAccountApiHostUriString = "https://account.box.com/api/";
public const string BoxApiUriString = "https://api.box.com/2.0/";
public const string BoxUploadApiUriString = "https://upload.box.com/api/2.0/";
public const string BoxAuthTokenApiUriString = "https://api.box.com/oauth2/token";


/*** API Endpoints ***/
Expand Down
1 change: 1 addition & 0 deletions Box.V2/Config/IBoxConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public interface IBoxConfig
Uri BoxAccountApiHostUri { get; }
Uri BoxApiUri { get; }
Uri BoxUploadApiUri { get; }
Uri BoxAuthTokenApiUri { get; }

string ClientId { get; }
string ConsumerKey { get; }
Expand Down
3 changes: 1 addition & 2 deletions Box.V2/JWTAuth/BoxJWTAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ namespace Box.V2.JWTAuth
///</remarks>
public class BoxJWTAuth
{
private const string AUTH_URL = "https://api.box.com/oauth2/token";
private const string ENTERPRISE_SUB_TYPE = "enterprise";
private const string USER_SUB_TYPE = "user";
private const string TOKEN_TYPE = "bearer";
Expand Down Expand Up @@ -252,7 +251,7 @@ private string ConstructJWTAssertion(string sub, string boxSubType, DateTimeOffs
expireTime = nowOverride.Value.AddSeconds(30);
}

var payload = new JwtPayload(_boxConfig.ClientId, AUTH_URL, claims, null, expireTime.LocalDateTime);
var payload = new JwtPayload(_boxConfig.ClientId, _boxConfig.BoxAuthTokenApiUri.ToString(), claims, null, expireTime.LocalDateTime);

var header = new JwtHeader(signingCredentials: _credentials);
if (_boxConfig.JWTPublicKeyId != null)
Expand Down

0 comments on commit ae8cd8b

Please sign in to comment.