2020-03-09 19:47:39 +00:00
|
|
|
using System;
|
2018-08-24 13:10:13 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2018-12-22 20:12:57 +00:00
|
|
|
using System.Reflection;
|
2018-08-24 13:10:13 +01:00
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
using SpotifyAPI.Web.Enums;
|
|
|
|
using Unosquare.Labs.EmbedIO;
|
|
|
|
using Unosquare.Labs.EmbedIO.Modules;
|
|
|
|
|
|
|
|
namespace SpotifyAPI.Web.Auth
|
|
|
|
{
|
2020-03-09 19:47:39 +00:00
|
|
|
public abstract class SpotifyAuthServer<T>
|
|
|
|
{
|
|
|
|
public string ClientId { get; set; }
|
|
|
|
public string ServerUri { get; set; }
|
|
|
|
public string RedirectUri { get; set; }
|
|
|
|
public string State { get; set; }
|
|
|
|
public Scope Scope { get; set; }
|
|
|
|
public bool ShowDialog { get; set; }
|
2018-08-24 13:10:13 +01:00
|
|
|
|
2020-03-09 19:47:39 +00:00
|
|
|
private readonly string _folder;
|
|
|
|
private readonly string _type;
|
|
|
|
private WebServer _server;
|
|
|
|
protected CancellationTokenSource _serverSource;
|
2018-08-24 13:10:13 +01:00
|
|
|
|
2020-03-09 19:47:39 +00:00
|
|
|
public delegate void OnAuthReceived(object sender, T payload);
|
|
|
|
public event OnAuthReceived AuthReceived;
|
2018-08-24 13:10:13 +01:00
|
|
|
|
2020-03-09 19:47:39 +00:00
|
|
|
internal static readonly Dictionary<string, SpotifyAuthServer<T>> Instances = new Dictionary<string, SpotifyAuthServer<T>>();
|
2018-08-24 13:10:13 +01:00
|
|
|
|
2020-03-09 19:47:39 +00:00
|
|
|
internal SpotifyAuthServer(string type, string folder, string redirectUri, string serverUri, Scope scope = Scope.None, string state = "")
|
|
|
|
{
|
|
|
|
_type = type;
|
|
|
|
_folder = folder;
|
|
|
|
ServerUri = serverUri;
|
|
|
|
RedirectUri = redirectUri;
|
|
|
|
Scope = scope;
|
|
|
|
State = string.IsNullOrEmpty(state) ? string.Join("", Guid.NewGuid().ToString("n").Take(8)) : state;
|
|
|
|
}
|
2018-08-24 13:10:13 +01:00
|
|
|
|
2020-03-09 19:47:39 +00:00
|
|
|
public void Start()
|
|
|
|
{
|
|
|
|
Instances.Add(State, this);
|
|
|
|
_serverSource = new CancellationTokenSource();
|
2018-09-04 13:39:07 +01:00
|
|
|
|
2020-03-09 19:47:39 +00:00
|
|
|
_server = WebServer.Create(ServerUri);
|
|
|
|
_server.RegisterModule(new WebApiModule());
|
|
|
|
AdaptWebServer(_server);
|
|
|
|
_server.RegisterModule(new ResourceFilesModule(Assembly.GetExecutingAssembly(), $"SpotifyAPI.Web.Auth.Resources.{_folder}"));
|
2018-08-24 13:10:13 +01:00
|
|
|
#pragma warning disable 4014
|
2020-03-09 19:47:39 +00:00
|
|
|
_server.RunAsync(_serverSource.Token);
|
2018-08-24 13:10:13 +01:00
|
|
|
#pragma warning restore 4014
|
2020-03-09 19:47:39 +00:00
|
|
|
}
|
2018-08-24 13:10:13 +01:00
|
|
|
|
2020-03-09 19:47:39 +00:00
|
|
|
public virtual string GetUri()
|
|
|
|
{
|
|
|
|
StringBuilder builder = new StringBuilder("https://accounts.spotify.com/authorize/?");
|
|
|
|
builder.Append("client_id=" + ClientId);
|
|
|
|
builder.Append($"&response_type={_type}");
|
|
|
|
builder.Append("&redirect_uri=" + RedirectUri);
|
|
|
|
builder.Append("&state=" + State);
|
|
|
|
builder.Append("&scope=" + Scope.GetStringAttribute(" "));
|
|
|
|
builder.Append("&show_dialog=" + ShowDialog);
|
|
|
|
return Uri.EscapeUriString(builder.ToString());
|
|
|
|
}
|
2018-08-24 13:10:13 +01:00
|
|
|
|
2020-03-09 19:47:39 +00:00
|
|
|
public void Stop(int delay = 2000)
|
|
|
|
{
|
|
|
|
if (_serverSource == null) return;
|
|
|
|
_serverSource.CancelAfter(delay);
|
|
|
|
Instances.Remove(State);
|
|
|
|
}
|
2018-08-24 13:10:13 +01:00
|
|
|
|
2020-03-09 19:47:39 +00:00
|
|
|
public void OpenBrowser()
|
|
|
|
{
|
|
|
|
string uri = GetUri();
|
|
|
|
AuthUtil.OpenBrowser(uri);
|
|
|
|
}
|
2018-08-24 13:10:13 +01:00
|
|
|
|
2020-03-09 19:47:39 +00:00
|
|
|
internal void TriggerAuth(T payload)
|
|
|
|
{
|
|
|
|
AuthReceived?.Invoke(this, payload);
|
|
|
|
}
|
2018-08-24 13:10:13 +01:00
|
|
|
|
2020-03-09 19:47:39 +00:00
|
|
|
internal static SpotifyAuthServer<T> GetByState(string state)
|
|
|
|
{
|
|
|
|
return Instances.TryGetValue(state, out SpotifyAuthServer<T> auth) ? auth : null;
|
2018-08-24 13:10:13 +01:00
|
|
|
}
|
2020-03-09 19:47:39 +00:00
|
|
|
|
|
|
|
protected abstract void AdaptWebServer(WebServer webServer);
|
|
|
|
}
|
2018-08-24 13:10:13 +01:00
|
|
|
}
|