Skip to content

Commit

Permalink
fix login
Browse files Browse the repository at this point in the history
  • Loading branch information
byCrookie committed Oct 28, 2023
1 parent bf373a8 commit 27b5b53
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ public async Task InvokeAsync_ShortFlagsArePassed_FlagsGetParsed()
{
loginArgs.Should().NotBeNull();
loginArgs.Token.Should().Be("token");
loginArgs.DeviceFlowAuth.Should().BeTrue();
loginArgs.DeviceFlowAuth.Should().BeFalse();
},
new LoginArgsBinder(_loginArguments)
);

rootCommand.AddCommand(subCommand);
await TestCommandline.Build(rootCommand).InvokeAsync("sub -t token -dfa");
await TestCommandline.Build(rootCommand).InvokeAsync("sub -t token");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
Go to verificationUri
and enter userCode
You have 60 seconds to authenticate before the code expires.
verificationUri - userCode
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ internal static class LoginArgDescriptions
public static readonly Description Token = new("Token", "Token",
"""
If not provided, the token will be aquired from the environment variable GITHUB_BACKUP_TOKEN.
If a home directory is configured, the token will be stored in the file {home}/github-backup/token.
If provided, device flow authentication will be ignored. Recommended for use on servers.");
"""
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class LoginArguments
) { IsRequired = false };

public Option<bool> DeviceFlowAuthOption { get; } = new(
aliases: new[] { "-dfa", "--device-flow-auth" },
aliases: new[] { "-d", "--device-flow-auth" },
getDefaultValue: () => false,
description: LoginArgDescriptions.DeviceFlowAuth.Long
) { IsRequired = false };
Expand Down
15 changes: 12 additions & 3 deletions GithubBackup/GithubBackup.Cli/Commands/Github/Login/LoginRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,18 @@ private async Task<User> LoginAsync(CancellationToken ct)
private async Task<string> GetOAuthTokenAsync(CancellationToken ct)
{
var deviceAndUserCodes = await _authenticationService.RequestDeviceAndUserCodesAsync(ct);
_ansiConsole.WriteLine(
$"Go to {deviceAndUserCodes.VerificationUri}{Environment.NewLine}and enter {deviceAndUserCodes.UserCode}");
_ansiConsole.WriteLine($"You have {deviceAndUserCodes.ExpiresIn} seconds to authenticate before the code expires.");

if (!_globalArgs.Quiet)
{
_ansiConsole.WriteLine(
$"Go to {deviceAndUserCodes.VerificationUri}{Environment.NewLine}and enter {deviceAndUserCodes.UserCode}");
_ansiConsole.WriteLine($"You have {deviceAndUserCodes.ExpiresIn} seconds to authenticate before the code expires.");
}
else
{
_ansiConsole.WriteLine($"{deviceAndUserCodes.VerificationUri} - {deviceAndUserCodes.UserCode}");
}

var accessToken = await _authenticationService.PollForAccessTokenAsync(
deviceAndUserCodes.DeviceCode,
deviceAndUserCodes.Interval,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public async Task<AccessToken> PollForAccessTokenAsync(string deviceCode, int in
)
.ReceiveJson<AccessTokenResponse>(), ct);

if (string.IsNullOrWhiteSpace(response.AccessToken))
{
throw new Exception("Authentication failed. No access token received. Please try again.");
}

return new AccessToken(response.AccessToken!, response.TokenType!, response.Scope!);
}

Expand Down

0 comments on commit 27b5b53

Please sign in to comment.