forked from JohnnyCrazy/SpotifyAPI-NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmbedIOAuthServer.cs
More file actions
100 lines (86 loc) · 2.91 KB
/
EmbedIOAuthServer.cs
File metadata and controls
100 lines (86 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
{
public event Func<object, AuthorizationCodeResponse, Task>? AuthorizationCodeReceived;
public event Func<object, ImplictGrantResponse, Task>? ImplictGrantReceived;
public event Func<object, string, string?, Task>? ErrorReceived;
private const string AssetsResourcePath = "SpotifyAPI.Web.Auth.Resources.auth_assets";
private const string DefaultResourcePath = "SpotifyAPI.Web.Auth.Resources.default_site";
private CancellationTokenSource? _cancelTokenSource;
private readonly WebServer _webServer;
public EmbedIOAuthServer(Uri baseUri, int port)
: this(baseUri, port, Assembly.GetExecutingAssembly(), DefaultResourcePath) { }
public EmbedIOAuthServer(Uri baseUri, int port, Assembly resourceAssembly, string resourcePath)
{
Ensure.ArgumentNotNull(baseUri, nameof(baseUri));
BaseUri = baseUri;
Port = port;
_webServer = new WebServer(port)
.WithModule(new ActionModule("/", HttpVerbs.Post, (ctx) =>
{
var query = ctx.Request.QueryString;
var error = query["error"];
if (error != null)
{
ErrorReceived?.Invoke(this, error, query["state"]);
throw new AuthException(error, query["state"]);
}
var requestType = query.Get("request_type");
if (requestType == "token")
{
ImplictGrantReceived?.Invoke(this, new ImplictGrantResponse(
query["access_token"]!, query["token_type"]!, int.Parse(query["expires_in"]!)
)
{
State = query["state"]
});
}
if (requestType == "code")
{
AuthorizationCodeReceived?.Invoke(this, new AuthorizationCodeResponse(query["code"]!)
{
State = query["state"]
});
}
return ctx.SendStringAsync("OK", "text/plain", Encoding.UTF8);
}))
.WithEmbeddedResources("/auth_assets", Assembly.GetExecutingAssembly(), AssetsResourcePath)
.WithEmbeddedResources(baseUri.AbsolutePath, resourceAssembly, resourcePath);
}
public Uri BaseUri { get; }
public int Port { get; }
public Task Start()
{
_cancelTokenSource = new CancellationTokenSource();
_webServer.Start(_cancelTokenSource.Token);
return Task.CompletedTask;
}
public Task Stop()
{
_cancelTokenSource?.Cancel();
return Task.CompletedTask;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_webServer?.Dispose();
}
}
}
}