Browse Source

Update the console client sandbox to use Spectre.Console and add back Twitter

pull/1705/head
Kévin Chalet 3 years ago
parent
commit
d99a95628b
  1. 2
      Directory.Packages.props
  2. 39
      sandbox/OpenIddict.Sandbox.Console.Client/InteractiveService.cs
  3. 1
      sandbox/OpenIddict.Sandbox.Console.Client/OpenIddict.Sandbox.Console.Client.csproj
  4. 19
      sandbox/OpenIddict.Sandbox.Console.Client/Program.cs

2
Directory.Packages.props

@ -202,6 +202,7 @@
<PackageVersion Include="Microsoft.Owin.Security.OAuth" Version="4.2.2" />
<PackageVersion Include="Microsoft.Web.Infrastructure" Version="2.0.1" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.2" />
<PackageVersion Include="Spectre.Console" Version="0.46.0" />
<PackageVersion Include="WebGrease" Version="1.6.0" />
<!--
@ -355,6 +356,7 @@
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
<PackageVersion Include="Quartz.Extensions.Hosting" Version="3.5.0" />
<PackageVersion Include="Spectre.Console" Version="0.46.0" />
<!--
Note: OpenIddict uses PolySharp to dynamically generate polyfills for types that are not available on

39
sandbox/OpenIddict.Sandbox.Console.Client/InteractiveService.cs

@ -1,5 +1,6 @@
using Microsoft.Extensions.Hosting;
using OpenIddict.Client;
using Spectre.Console;
using static OpenIddict.Abstractions.OpenIddictConstants;
using static OpenIddict.Abstractions.OpenIddictExceptions;
using static OpenIddict.Client.WebIntegration.OpenIddictClientWebIntegrationConstants;
@ -10,8 +11,6 @@ using IHostApplicationLifetime = Microsoft.Extensions.Hosting.IApplicationLifeti
namespace OpenIddict.Sandbox.Console.Client;
using Console = System.Console;
public class InteractiveService : BackgroundService
{
private readonly IHostApplicationLifetime _lifetime;
@ -36,23 +35,9 @@ public class InteractiveService : BackgroundService
while (!stoppingToken.IsCancellationRequested)
{
string? provider;
do
{
Console.WriteLine("Type '1' + ENTER to log in using the local server or '2' + ENTER to log in using GitHub.");
provider = await ReadLineAsync(stoppingToken) switch
{
"1" => "Local",
"2" => Providers.GitHub,
_ => null
};
}
var provider = await GetSelectedProviderAsync(stoppingToken);
while (string.IsNullOrEmpty(provider));
Console.WriteLine("Launching the system browser.");
AnsiConsole.MarkupLine("[cyan]Launching the system browser.[/]");
try
{
@ -61,31 +46,35 @@ public class InteractiveService : BackgroundService
var (_, _, principal) = await _service.AuthenticateInteractivelyAsync(
provider, cancellationToken: stoppingToken);
Console.WriteLine($"Welcome, {principal.FindFirst(Claims.Name)!.Value}.");
AnsiConsole.MarkupLineInterpolated($"[green]Welcome, {principal.FindFirst(Claims.Name)!.Value}.[/]");
}
catch (OperationCanceledException)
{
Console.WriteLine("The authentication process was aborted.");
AnsiConsole.MarkupLine("[red]The authentication process was aborted.[/]");
}
catch (ProtocolException exception) when (exception.Error is Errors.AccessDenied)
{
Console.WriteLine("The authorization was denied by the end user.");
AnsiConsole.MarkupLine("[yellow]The authorization was denied by the end user.[/]");
}
catch
{
Console.WriteLine("An error occurred while trying to authenticate the user.");
AnsiConsole.MarkupLine("[red]An error occurred while trying to authenticate the user.[/]");
}
}
static async Task<string?> ReadLineAsync(CancellationToken cancellationToken)
static async Task<string> GetSelectedProviderAsync(CancellationToken cancellationToken)
{
static string Prompt() => AnsiConsole.Prompt(new SelectionPrompt<string>()
.Title("Select the authentication provider you'd like to log in with.")
.AddChoices("Local", Providers.GitHub, Providers.Twitter));
#if SUPPORTS_TASK_WAIT_ASYNC
return await Task.Run(Console.ReadLine, cancellationToken).WaitAsync(cancellationToken);
return await Task.Run(Prompt, cancellationToken).WaitAsync(cancellationToken);
#else
var task = Task.Run(Console.ReadLine, cancellationToken);
var task = Task.Run(Prompt, cancellationToken);
var source = new TaskCompletionSource<bool>(TaskCreationOptions.None);
using (cancellationToken.Register(static state => ((TaskCompletionSource<bool>) state!).SetResult(true), source))

1
sandbox/OpenIddict.Sandbox.Console.Client/OpenIddict.Sandbox.Console.Client.csproj

@ -18,6 +18,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" />
<PackageReference Include="Microsoft.Extensions.Hosting" />
<PackageReference Include="Spectre.Console" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETFramework' ">

19
sandbox/OpenIddict.Sandbox.Console.Client/Program.cs

@ -53,7 +53,8 @@ var host = new HostBuilder()
.DisablePipeServer()
.EnableEmbeddedWebServer()
.UseSystemBrowser()
.SetApplicationDiscriminator("0XP3WQ07VVMCVBJ");
.SetApplicationDiscriminator("0XP3WQ07VVMCVBJ")
.SetAllowedEmbeddedWebServerPorts(49152, 49153, 49154);
// Register the System.Net.Http integration and use the identity of the current
// assembly as a more specific user agent, which can be useful when dealing with
@ -78,11 +79,17 @@ var host = new HostBuilder()
// address per provider, unless all the registered providers support returning an "iss"
// parameter containing their URL as part of authorization responses. For more information,
// see https://datatracker.ietf.org/doc/html/draft-ietf-oauth-security-topics#section-4.4.
options.UseWebProviders()
.UseGitHub()
.SetClientId("992372d088f8676a7945")
.SetClientSecret("1f18c22f766e44d7bd4ea4a6510b9e337d48ab38")
.SetRedirectUri("callback/login/github");
options.UseWebProviders(options =>
{
options.UseGitHub()
.SetClientId("992372d088f8676a7945")
.SetClientSecret("1f18c22f766e44d7bd4ea4a6510b9e337d48ab38")
.SetRedirectUri("callback/login/github");
options.UseTwitter()
.SetClientId("bXgwc0U3N3A3YWNuaWVsdlRmRWE6MTpjaQ")
.SetRedirectUri("callback/login/twitter");
});
});
// Register the worker responsible for creating the database used to store tokens

Loading…
Cancel
Save