-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to using new identity server auth flow (#212)
- Loading branch information
Showing
14 changed files
with
244 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
Source/GenerateSharp/PackageManager/Soup.Build.PackageManager.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFrameworks>net7.0;net7.0-windows10.0.17763.0</TargetFrameworks> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<PublishSingleFile>true</PublishSingleFile> | ||
<SelfContained>true</SelfContained> | ||
<PublishReadyToRun>true</PublishReadyToRun> | ||
<PublishTrimmed>false</PublishTrimmed> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Identity.Client" Version="4.54.1" /> | ||
<PackageReference Include="System.Security.Cryptography.ProtectedData" Version="7.0.1" /> | ||
<FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
<PackageReference Include="IdentityModel" Version="6.1.0" /> | ||
<PackageReference Include="IdentityModel.OidcClient" Version="5.2.1" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<TrimmableAssembly Include="Microsoft.Identity.Client" /> | ||
<ProjectReference Include="..\PackageManager.Core\Soup.Build.PackageManager.Core.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
// <copyright file="SystemBrowser.cs" company="Soup"> | ||
// Copyright (c) Soup. All rights reserved. | ||
// </copyright | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Net; | ||
using System.Net.Sockets; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using IdentityModel.OidcClient.Browser; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Soup.Build.PackageManager | ||
{ | ||
public class SystemBrowser : IBrowser | ||
{ | ||
public int Port { get; } | ||
private readonly string? _path; | ||
|
||
public SystemBrowser(int? port = null, string? path = null) | ||
{ | ||
_path = path; | ||
|
||
if (!port.HasValue) | ||
{ | ||
Port = GetRandomUnusedPort(); | ||
} | ||
else | ||
{ | ||
Port = port.Value; | ||
} | ||
} | ||
|
||
private int GetRandomUnusedPort() | ||
{ | ||
var listener = new TcpListener(IPAddress.Loopback, 0); | ||
listener.Start(); | ||
var port = ((IPEndPoint)listener.LocalEndpoint).Port; | ||
listener.Stop(); | ||
return port; | ||
} | ||
|
||
public async Task<BrowserResult> InvokeAsync(BrowserOptions options, CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
using (var listener = new LoopbackHttpListener(Port, _path)) | ||
{ | ||
OpenBrowser(options.StartUrl); | ||
|
||
try | ||
{ | ||
var result = await listener.WaitForCallbackAsync(); | ||
if (String.IsNullOrWhiteSpace(result)) | ||
{ | ||
return new BrowserResult { ResultType = BrowserResultType.UnknownError, Error = "Empty response." }; | ||
} | ||
|
||
return new BrowserResult { Response = result, ResultType = BrowserResultType.Success }; | ||
} | ||
catch (TaskCanceledException ex) | ||
{ | ||
return new BrowserResult { ResultType = BrowserResultType.Timeout, Error = ex.Message }; | ||
} | ||
catch (Exception ex) | ||
{ | ||
return new BrowserResult { ResultType = BrowserResultType.UnknownError, Error = ex.Message }; | ||
} | ||
} | ||
} | ||
|
||
public static void OpenBrowser(string url) | ||
{ | ||
try | ||
{ | ||
Process.Start(url); | ||
} | ||
catch | ||
{ | ||
// hack because of this: https://github.com/dotnet/corefx/issues/10361 | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
{ | ||
url = url.Replace("&", "^&"); | ||
Process.Start(new ProcessStartInfo("cmd", $"/c start {url}") { CreateNoWindow = true }); | ||
} | ||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) | ||
{ | ||
Process.Start("xdg-open", url); | ||
} | ||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | ||
{ | ||
Process.Start("open", url); | ||
} | ||
else | ||
{ | ||
throw; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public class LoopbackHttpListener : IDisposable | ||
{ | ||
const int DefaultTimeout = 60 * 5; // 5 mins (in seconds) | ||
|
||
IWebHost _host; | ||
TaskCompletionSource<string> _source = new TaskCompletionSource<string>(); | ||
string _url; | ||
|
||
public string Url => _url; | ||
|
||
public LoopbackHttpListener(int port, string? path = null) | ||
{ | ||
path = path ?? String.Empty; | ||
if (path.StartsWith("/")) path = path.Substring(1); | ||
|
||
_url = $"http://127.0.0.1:{port}/{path}"; | ||
|
||
_host = new WebHostBuilder() | ||
.UseKestrel() | ||
.UseUrls(_url) | ||
.Configure(Configure) | ||
.Build(); | ||
_host.Start(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Task.Run(async () => | ||
{ | ||
await Task.Delay(500); | ||
_host.Dispose(); | ||
}); | ||
} | ||
|
||
void Configure(IApplicationBuilder app) | ||
{ | ||
app.Run(async ctx => | ||
{ | ||
if (ctx.Request.Method == "GET") | ||
{ | ||
await SetResultAsync(ctx.Request.QueryString.Value ?? string.Empty, ctx); | ||
} | ||
else if (ctx.Request.Method == "POST") | ||
{ | ||
if (ctx.Request.ContentType is not null && | ||
!ctx.Request.ContentType.Equals("application/x-www-form-urlencoded", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
ctx.Response.StatusCode = 415; | ||
} | ||
else | ||
{ | ||
using (var sr = new StreamReader(ctx.Request.Body, Encoding.UTF8)) | ||
{ | ||
var body = await sr.ReadToEndAsync(); | ||
await SetResultAsync(body, ctx); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
ctx.Response.StatusCode = 405; | ||
} | ||
}); | ||
} | ||
|
||
private async Task SetResultAsync(string value, HttpContext ctx) | ||
{ | ||
try | ||
{ | ||
ctx.Response.StatusCode = 200; | ||
ctx.Response.ContentType = "text/html"; | ||
await ctx.Response.WriteAsync("<h1>You can now return to the application.</h1>"); | ||
await ctx.Response.Body.FlushAsync(); | ||
|
||
_source.TrySetResult(value); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine(ex.ToString()); | ||
|
||
ctx.Response.StatusCode = 400; | ||
ctx.Response.ContentType = "text/html"; | ||
await ctx.Response.WriteAsync("<h1>Invalid request.</h1>"); | ||
await ctx.Response.Body.FlushAsync(); | ||
} | ||
} | ||
|
||
public Task<string> WaitForCallbackAsync(int timeoutInSeconds = DefaultTimeout) | ||
{ | ||
Task.Run(async () => | ||
{ | ||
await Task.Delay(timeoutInSeconds * 1000); | ||
_source.TrySetCanceled(); | ||
}); | ||
|
||
return _source.Task; | ||
} | ||
} | ||
} |
Oops, something went wrong.