Skip to content

Commit

Permalink
Add clipboard service + Argument parser
Browse files Browse the repository at this point in the history
  • Loading branch information
SaifAqqad committed Jan 28, 2023
1 parent 0da2003 commit c21cd65
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 11 deletions.
37 changes: 37 additions & 0 deletions Arguments.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.ComponentModel;
using Ookii.CommandLine;

namespace SteamAuth
{
[ParseOptions(
Mode = ParsingMode.LongShort,
CaseSensitive = false,
ArgumentNameTransform = NameTransform.DashCase,
ValueDescriptionTransform = NameTransform.DashCase
)]
[Description("Generates a steam TOTP code using a provided secret.")]
public class Arguments
{

[CommandLineArgument(Position = 0, IsRequired = false)]
[Description("Steam TOTP secret")]
public string? Secret { get; set; }

[CommandLineArgument(IsRequired = false, ShortName = 's')]
[Description("Whether to save the encrypted secret to a config file")]
public bool Save { get; set; }

internal bool IsInvalid { get; private set; }

public Arguments()
{
Secret = null;
Save = false;
}

internal Arguments(bool isInvalid) : this()
{
IsInvalid = isInvalid;
}
}
}
33 changes: 24 additions & 9 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using SteamGuardTotp = SteamGuard.TOTP.SteamGuard;
using SteamAuth.Utils;
using System.Security.Cryptography;
using TextCopy;
using Ookii.CommandLine;
using Ookii.CommandLine.Terminal;

namespace SteamAuth;

Expand All @@ -10,33 +13,39 @@ class Program
const string STEAMGUARD_FILE = "steamauth.json";
private static readonly SteamGuardTotp steamGuard = new();
private static readonly string steamGuardPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, STEAMGUARD_FILE);
private static readonly ConsoleWriter console = new();

private static int Main(string[] args)
private static int Main()
{
var result = args.Length == 0 ? ProcessFile() : ProcessArgs(args);
var args = CommandLineParser.Parse<Arguments>() ?? new(isInvalid: true);
if (args.IsInvalid)
{
return (int)Status.InvalidArguments;
}

var result = string.IsNullOrWhiteSpace(args.Secret) ? ProcessFile() : ProcessArgs(args);

if (result != Status.Success)
{
Console.Error.WriteLine(result.GetMessage());
console.WriteLine(result.GetMessage(), TextFormat.ForegroundRed, TextFormat.BoldBright);
}

return (int)result;
}

private static Status ProcessArgs(string[] args)
private static Status ProcessArgs(Arguments args)
{
var secret = args[0];
var shouldSave = args.Length > 1 && args[1].Contains("-s", StringComparison.InvariantCultureIgnoreCase);
var secret = args.Secret ?? string.Empty;

var code = steamGuard.GenerateAuthenticationCode(secret);
if (code is null)
{
return Status.TotpGenerationFailure;
}

Console.WriteLine(code);
OutputTotpCode(code);

if (shouldSave)
if (args.Save)
{
var entropy = new byte[20];
RandomNumberGenerator.Fill(entropy);
Expand Down Expand Up @@ -84,10 +93,16 @@ private static Status ProcessFile()
var code = steamGuard.GenerateAuthenticationCode(secret);
if (code is not null)
{
Console.WriteLine(code);
OutputTotpCode(code);
return Status.Success;
}

return Status.TotpGenerationFailure;
}

private static void OutputTotpCode(string code)
{
console.WriteLine(code, TextFormat.ForegroundGreen, TextFormat.BoldBright);
ClipboardService.SetText(code);
}
}
3 changes: 2 additions & 1 deletion Utils/Status.cs → Status.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
namespace SteamAuth.Utils;
namespace SteamAuth;
enum Status
{
Success = 0,
InvalidArguments = -1,
PlatformNotSupported = -2,
ConfigFileNotFound = -3,
EncryptionFailure = -4,
Expand Down
7 changes: 6 additions & 1 deletion SteamAuth.csproj
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>0.0.2</Version>
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>SteamAuth</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<WarningsAsErrors>Nullable</WarningsAsErrors>
</PropertyGroup>

<PropertyGroup>
<OutputType>Exe</OutputType>
<PublishSingleFile>true</PublishSingleFile>
<PublishReadyToRun>true</PublishReadyToRun>
<PublishTrimmed>true</PublishTrimmed>
<SuppressTrimAnalysisWarnings>true</SuppressTrimAnalysisWarnings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CrossPlatformProtectedData" Version="1.0.3" />
<PackageReference Include="Ookii.CommandLine" Version="3.0.0" />
<PackageReference Include="SteamGuard.TOTP" Version="1.0.0" />
<PackageReference Include="TextCopy" Version="6.2.1" />
</ItemGroup>

</Project>
</Project>
42 changes: 42 additions & 0 deletions Utils/ConsoleWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Ookii.CommandLine.Terminal;

namespace SteamAuth.Utils
{
public class ConsoleWriter : IDisposable
{
private readonly VirtualTerminalSupport virtualTerminal;
private readonly bool _formatSupported = false;

public ConsoleWriter()
{
virtualTerminal = VirtualTerminal.EnableColor(StandardStream.Output);
_formatSupported = virtualTerminal.IsSupported;
}

public void Write(string text, params string[] formats)
{
if (_formatSupported)
{
Console.Write(formats);
}

Console.Write(text);
}

public void WriteLine(string text, params string[] formats)
{
if (_formatSupported)
{
Console.Write(string.Concat(formats));
}

Console.WriteLine(text);
}

public void Dispose()
{
virtualTerminal.Dispose();
GC.SuppressFinalize(this);
}
}
}

0 comments on commit c21cd65

Please sign in to comment.