Spotify.NET/SpotifyAPI.Web.Auth/EmbedIOAuthServer.cs

101 lines
2.9 KiB
C#
Raw Permalink Normal View History

2020-05-15 19:06:24 +01:00
using System.Reflection;
using System.Threading;
using System.Web;
using System.Globalization;
using System.Text;
using System;
using System.Threading.Tasks;
using EmbedIO;
using EmbedIO.Actions;
namespace SpotifyAPI.Web.Auth
{
public class EmbedIOAuthServer : IAuthServer
{
2020-05-25 17:00:38 +01:00
public event Func<object, AuthorizationCodeResponse, Task>? AuthorizationCodeReceived;
public event Func<object, ImplictGrantResponse, Task>? ImplictGrantReceived;
public event Func<object, string, string?, Task>? ErrorReceived;
2020-05-15 19:06:24 +01:00
2020-05-15 20:41:53 +01:00
private const string AssetsResourcePath = "SpotifyAPI.Web.Auth.Resources.auth_assets";
private const string DefaultResourcePath = "SpotifyAPI.Web.Auth.Resources.default_site";
2020-05-15 19:06:24 +01:00
2020-05-25 17:00:38 +01:00
private CancellationTokenSource? _cancelTokenSource;
2020-05-15 19:06:24 +01:00
private readonly WebServer _webServer;
2020-05-15 20:41:53 +01:00
public EmbedIOAuthServer(Uri baseUri, int port)
: this(baseUri, port, Assembly.GetExecutingAssembly(), DefaultResourcePath) { }
public EmbedIOAuthServer(Uri baseUri, int port, Assembly resourceAssembly, string resourcePath)
2020-05-15 19:06:24 +01:00
{
Ensure.ArgumentNotNull(baseUri, nameof(baseUri));
BaseUri = baseUri;
Port = port;
_webServer = new WebServer(port)
.WithModule(new ActionModule("/", HttpVerbs.Post, (ctx) =>
{
var query = ctx.Request.QueryString;
2020-11-13 13:43:00 +00:00
var error = query["error"];
if (error != null)
2020-05-15 19:06:24 +01:00
{
ErrorReceived?.Invoke(this, error, query["state"]);
2020-11-13 13:43:00 +00:00
throw new AuthException(error, query["state"]);
2020-05-15 19:06:24 +01:00
}
var requestType = query.Get("request_type");
if (requestType == "token")
{
ImplictGrantReceived?.Invoke(this, new ImplictGrantResponse(
2020-11-13 13:43:00 +00:00
query["access_token"]!, query["token_type"]!, int.Parse(query["expires_in"]!)
2020-05-15 19:06:24 +01:00
)
{
State = query["state"]
});
}
if (requestType == "code")
{
2020-11-13 13:43:00 +00:00
AuthorizationCodeReceived?.Invoke(this, new AuthorizationCodeResponse(query["code"]!)
2020-05-15 19:06:24 +01:00
{
State = query["state"]
});
}
return ctx.SendStringAsync("OK", "text/plain", Encoding.UTF8);
}))
2020-05-15 20:41:53 +01:00
.WithEmbeddedResources("/auth_assets", Assembly.GetExecutingAssembly(), AssetsResourcePath)
2020-05-15 21:02:54 +01:00
.WithEmbeddedResources(baseUri.AbsolutePath, resourceAssembly, resourcePath);
2020-05-15 19:06:24 +01:00
}
public Uri BaseUri { get; }
public int Port { get; }
public Task Start()
{
_cancelTokenSource = new CancellationTokenSource();
_webServer.Start(_cancelTokenSource.Token);
return Task.CompletedTask;
}
public Task Stop()
{
2020-05-25 17:00:38 +01:00
_cancelTokenSource?.Cancel();
2020-05-15 19:06:24 +01:00
return Task.CompletedTask;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_webServer?.Dispose();
}
}
}
}