Skip to content

Commit

Permalink
Merge pull request #57 from box/accept-tokens
Browse files Browse the repository at this point in the history
Added new --token feature, fixed collaboration command bugs, fixed pr…
  • Loading branch information
allenmichael authored Dec 5, 2017
2 parents 27dda21 + a006a0a commit 10a1524
Show file tree
Hide file tree
Showing 118 changed files with 398 additions and 284 deletions.
2 changes: 1 addition & 1 deletion BoxCLI/BoxCLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<PackageId>BoxCLI</PackageId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Box.V2.Core" Version="3.1.0" />
<PackageReference Include="Box.V2.Core" Version="3.3.0" />
<PackageReference Include="CsvHelper" Version="2.16.3.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="1.1.0" />
<PackageReference Include="Microsoft.Extensions.CommandLineUtils" Version="1.1.1" />
Expand Down
2 changes: 1 addition & 1 deletion BoxCLI/BoxCLIInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ public class BoxCLIInfo
{
public const string ProductTitle = "Box CLI";

public const string Version = "1.0.2";
public const string Version = "1.1.0";
}
}
68 changes: 47 additions & 21 deletions BoxCLI/BoxHome/BoxHomeFiles/BoxEnvironments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ public BoxEnvironments(string fileName, IBoxHome home)
_boxHome = home;
_boxHomeEnvironmentsFileName = fileName;
}
public bool VerifyBoxConfigFile(string filePath)
public bool VerifyBoxConfigFile(string filePath, bool ignoreFilePathTranslation = false)
{
filePath = GeneralUtilities.TranslatePath(filePath);
if (!ignoreFilePathTranslation)
{
filePath = GeneralUtilities.TranslatePath(filePath);
}
Reporter.WriteInformation($"Looking for file at this path {filePath}");
if (File.Exists(filePath))
{
Expand Down Expand Up @@ -71,6 +74,7 @@ public bool VerifyBoxConfigFile(string filePath)
}
else
{
Reporter.WriteError($"Couldn't open config file at this path: {filePath}");
return false;
}
}
Expand All @@ -95,28 +99,35 @@ public BoxHomeConfigModel RevalidateExistingConfigFile(string filePath, string p
{
return TranslateConfigFileToEnvironment(filePath, privateKeyPath);
}
public BoxHomeConfigModel TranslateConfigFileToEnvironment(string filePath, string privateKeyPath = "")
public BoxHomeConfigModel TranslateConfigFileToEnvironment(string filePath, string privateKeyPath = "", bool ignoreFilePathTranslation = false)
{
filePath = GeneralUtilities.TranslatePath(filePath);
if (!ignoreFilePathTranslation)
{
filePath = GeneralUtilities.TranslatePath(filePath);
}
var translatedConfig = new BoxHomeConfigModel();
if (File.Exists(filePath))
{
var config = DeserializeBoxConfigFile(filePath);
if (!string.IsNullOrEmpty(privateKeyPath))
{
var potentialPathFromOptions = GeneralUtilities.TranslatePath(privateKeyPath);
if (File.Exists(potentialPathFromOptions))
if (!ignoreFilePathTranslation)
{
translatedConfig.PrivateKeyPath = potentialPathFromOptions;
privateKeyPath = GeneralUtilities.TranslatePath(privateKeyPath);
}
if (File.Exists(privateKeyPath))
{
translatedConfig.PrivateKeyPath = privateKeyPath;
}
else
{
throw new Exception("Couldn't access the private key file from the path provided.");
throw new Exception($"Couldn't access the private key file from the path provided: {privateKeyPath}.");
}
}
else if (!string.IsNullOrEmpty(config.AppSettings.AppAuth.PrivateKey))
{
Reporter.WriteInformation("Detected private key value in config...");
Reporter.WriteInformation("Calculating between in-line private key or separate private key file...");
var pattern = @"^-----BEGIN ENCRYPTED PRIVATE KEY-----\n";
var regex = new Regex(pattern);
if (regex.IsMatch(config.AppSettings.AppAuth.PrivateKey))
Expand All @@ -127,11 +138,19 @@ public BoxHomeConfigModel TranslateConfigFileToEnvironment(string filePath, stri
else
{
Reporter.WriteInformation("Attempting to resolve file path for private key.");
var potentialPath = GeneralUtilities.TranslateDependentPath(config.AppSettings.AppAuth.PrivateKey, filePath);
Reporter.WriteInformation($"Found {potentialPath}.");
if (File.Exists(potentialPath))
var privateKeyPathInline = config.AppSettings.AppAuth.PrivateKey;
if (!ignoreFilePathTranslation)
{
privateKeyPathInline = GeneralUtilities.TranslateDependentPath(privateKeyPathInline, filePath);
}
Reporter.WriteInformation($"Path to private key file identified: {privateKeyPathInline}.");
if (File.Exists(privateKeyPathInline))
{
translatedConfig.PrivateKeyPath = potentialPath;
translatedConfig.PrivateKeyPath = privateKeyPathInline;
}
else
{
throw new Exception($"Unable to open private key file at {privateKeyPathInline}");
}
}
}
Expand All @@ -145,7 +164,7 @@ public BoxHomeConfigModel TranslateConfigFileToEnvironment(string filePath, stri
}
else
{
Reporter.WriteError("Couldn't open file...");
throw new Exception($"Couldn't open config file at {filePath}");
}
return translatedConfig;
}
Expand Down Expand Up @@ -303,7 +322,8 @@ public bool UpdatePrivateKeyPath(string existingName, string newPath)
this.SerializeBoxEnvironmentFile(environments);
return true;
}
public bool UpdateConfigFilePath(string existingName, string newPath, string newPemPath = "")
public bool UpdateConfigFilePath(string existingName, string newPath, string newPemPath = "",
bool ignoreFilePathTranslation = false)
{
var environments = DeserializeBoxEnvironmentFile();
var foundEnv = new BoxHomeConfigModel();
Expand All @@ -312,18 +332,24 @@ public bool UpdateConfigFilePath(string existingName, string newPath, string new
{
throw new Exception("Couldn't find that environment");
}
var translatePath = GeneralUtilities.TranslatePath(newPath);
if (this.VerifyBoxConfigFile(translatePath))
if (!ignoreFilePathTranslation)
{
newPath = GeneralUtilities.TranslatePath(newPath);
}
if (this.VerifyBoxConfigFile(newPath))
{
BoxHomeConfigModel env;
if (!string.IsNullOrEmpty(newPemPath))
{
var translatePemPath = GeneralUtilities.TranslatePath(newPemPath);
env = this.TranslateConfigFileToEnvironment(translatePath, translatePemPath);
if (!ignoreFilePathTranslation)
{
newPemPath = GeneralUtilities.TranslatePath(newPemPath);
}
env = this.TranslateConfigFileToEnvironment(newPath, newPemPath, ignoreFilePathTranslation: ignoreFilePathTranslation);
}
else
{
env = this.TranslateConfigFileToEnvironment(translatePath);
env = this.TranslateConfigFileToEnvironment(newPath, ignoreFilePathTranslation: ignoreFilePathTranslation);
}
foundEnv.BoxConfigFilePath = env.BoxConfigFilePath;
foundEnv.ClientId = env.ClientId;
Expand Down Expand Up @@ -500,7 +526,7 @@ public bool DeleteEnvironment(string name)
throw new Exception("Couldn't find this environment.");
}
}
public bool UpdateEnvironmentFilePath(string path, string envName)
public bool UpdateEnvironmentFilePath(string path, string envName, bool ignoreFilePathTranslation = false)
{
var environments = DeserializeBoxEnvironmentFile();
var foundEnv = new BoxHomeConfigModel();
Expand All @@ -511,7 +537,7 @@ public bool UpdateEnvironmentFilePath(string path, string envName)
}
if (this.VerifyBoxConfigFile(path))
{
var newEnv = this.TranslateConfigFileToEnvironment(path);
var newEnv = this.TranslateConfigFileToEnvironment(path, ignoreFilePathTranslation: ignoreFilePathTranslation);
newEnv.AdminAsUserId = (string.IsNullOrEmpty(foundEnv.AdminAsUserId)) ? "" : foundEnv.AdminAsUserId;
newEnv.DefaultAsUserId = (string.IsNullOrEmpty(foundEnv.DefaultAsUserId)) ? "" : foundEnv.DefaultAsUserId;
newEnv.UseDefaultAsUser = foundEnv.UseDefaultAsUser;
Expand Down
2 changes: 1 addition & 1 deletion BoxCLI/BoxHome/Models/BoxHomeDefaultSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public class BoxHomeDefaultSettings
[JsonProperty(PropertyName = "autoSave")]
public bool AutoSave { get; set; } = false;
[JsonProperty(PropertyName = "outputJson")]
public bool OutputJson { get; set; } = false;
public bool OutputJson { get; set; } = true;
}
}
4 changes: 2 additions & 2 deletions BoxCLI/BoxPlatform/Service/BoxPlatformService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public BoxClient AsUserClient(string asUserId)
}
public BoxClient ClientFromToken(string token)
{
var auth = new OAuthSession(token, "", 3600, "bearer");
return new BoxClient(this.BoxPlatformConfig, auth);
var auth = new OAuthSession(token, "1", 3600, "bearer");
return new BoxClient(new BoxConfig("", "", new Uri("http://localhost")), auth);
}

public async Task<bool> BustCache()
Expand Down
13 changes: 8 additions & 5 deletions BoxCLI/BoxPlatform/Service/BoxPlatformServiceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,15 @@ public void SetIterators()
BoxService.BoxCollectionsIterators = BoxIterators;
}

public IBoxPlatformService Build()
public IBoxPlatformService Build(bool isTokenCall = false)
{
SetConfig();
SetCache();
SetAuthorizedClient();
SetIterators();
if (!isTokenCall)
{
SetConfig();
SetCache();
SetAuthorizedClient();
SetIterators();
}
return BoxService;
}
}
Expand Down
2 changes: 1 addition & 1 deletion BoxCLI/BoxPlatform/Service/IBoxPlatformServiceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace BoxCLI.BoxPlatform.Service
{
public interface IBoxPlatformServiceBuilder
{
IBoxPlatformService Build();
IBoxPlatformService Build(bool isTokenCall = false);
}
}
11 changes: 11 additions & 0 deletions BoxCLI/CommandUtilities/CommandOptions/ProvideTokenOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using Microsoft.Extensions.CommandLineUtils;

namespace BoxCLI.CommandUtilities.CommandOptions
{
public static class ProvideTokenOption
{
public static CommandOption ConfigureOption(CommandLineApplication command)
=> command.Option("--token <TOKEN>", "Provide a token to perform this call", CommandOptionType.SingleValue);
}
}
10 changes: 10 additions & 0 deletions BoxCLI/CommandUtilities/GeneralUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ public static string TranslateDependentPath(string path, string containerPath)
path = path.Substring(match.Length - 1);
}

if (path.StartsWith("/"))
{
return path;
}

if (!path.StartsWith(Path.DirectorySeparatorChar.ToString()) && !path.StartsWith("~") && !path.StartsWith("..") &&
!path.StartsWith($".{Path.DirectorySeparatorChar}") && !winDirectoryRegex.IsMatch(path))
{
Expand Down Expand Up @@ -188,6 +193,11 @@ public static string TranslatePath(string path)
path = path.Substring(match.Length - 1);
}

if (path.StartsWith("/"))
{
return path;
}

if (!path.StartsWith(Path.DirectorySeparatorChar.ToString()) && !path.StartsWith("~") && !path.StartsWith("..") &&
!path.StartsWith($".{Path.DirectorySeparatorChar}") && !winDirectoryRegex.IsMatch(path))
{
Expand Down
5 changes: 4 additions & 1 deletion BoxCLI/CommandUtilities/ProgressBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ public static void UpdateProgress(string item, int progress, int total)
lock (_sync)
{
Console.CursorLeft = 0;
Console.Write(item + " [" + new string('=', percentage / 2) + "] " + percentage + "%");
var spaces = new string(' ', 100 - percentage);
var equals = new string('=', percentage);
var output = $"{item} [{equals}{spaces}] {percentage}%";
Console.Write(output);
}
}
}
Expand Down
20 changes: 13 additions & 7 deletions BoxCLI/Commands/BoxBaseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public abstract class BoxBaseCommand : HelpCommandBaseAsync
protected readonly IBoxPlatformServiceBuilder _boxPlatformBuilder;
protected readonly LocalizedStringsResource _names;
protected CommandOption _json;
protected CommandOption _oneUseToken;

public BoxBaseCommand(IBoxPlatformServiceBuilder boxPlatformBuilder, IBoxHome boxHome, LocalizedStringsResource names)
{
Expand All @@ -48,6 +49,7 @@ public override void Configure(CommandLineApplication command)
{
base.Configure(command);
_json = OutputJsonOption.ConfigureOption(command);
_oneUseToken = ProvideTokenOption.ConfigureOption(command);
}

protected virtual void CheckForId(string id, CommandLineApplication app, string message = "")
Expand Down Expand Up @@ -132,22 +134,26 @@ protected virtual string ConstructDownloadsPath(string fileName, string filePath
}

protected virtual BoxClient ConfigureBoxClient(string oneCallAsUserId = null,
bool returnServiceAccount = false, bool asAdmin = false)
bool asAdmin = false, string oneCallWithToken = null)
{
var Box = _boxPlatformBuilder.Build();
if (!string.IsNullOrEmpty(oneCallAsUserId) && !returnServiceAccount)
var Box = _boxPlatformBuilder.Build(!string.IsNullOrEmpty(oneCallWithToken));
if (!string.IsNullOrEmpty(oneCallWithToken))
{
return Box.ClientFromToken(oneCallWithToken);
}
else if (!string.IsNullOrEmpty(oneCallAsUserId))
{
return Box.AsUserClient(oneCallAsUserId);
}
else if (asAdmin && !returnServiceAccount)
else if (asAdmin)
{
if (string.IsNullOrEmpty(this._environments.GetAdminAsUserIdSetting()))
{
throw new Exception("Admin User ID is not currently set. Use the box configure environments set-admin-user command to use this feature.");
}
return Box.AsUserClient(this._environments.GetAdminAsUserIdSetting());
}
else if (this._environments.GetUserSessionEnabledSetting() && !returnServiceAccount)
else if (this._environments.GetUserSessionEnabledSetting())
{
Reporter.WriteInformation("User session enabled...");
var expiresAt = this._environments.GetUserSessionExpirationSetting();
Expand Down Expand Up @@ -175,9 +181,9 @@ protected virtual BoxClient ConfigureBoxClient(string oneCallAsUserId = null,
}
}

protected virtual IBoxCollectionsIterators GetIterators()
protected virtual IBoxCollectionsIterators GetIterators(bool isTokenCall = false)
{
var box = _boxPlatformBuilder.Build();
var box = _boxPlatformBuilder.Build(isTokenCall);
return box.BoxCollectionsIterators;
}

Expand Down
Loading

0 comments on commit 10a1524

Please sign in to comment.