Skip to content

Commit 466e615

Browse files
committed
VSCode Formatter - Also bumped tests to net core 3.1
1 parent 307d699 commit 466e615

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+2480
-2496
lines changed

SpotifyAPI.Web.Auth/AuthUtil.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
using System.Diagnostics;
1+
using System.Diagnostics;
22
using System.Runtime.InteropServices;
33

44
namespace SpotifyAPI.Web.Auth
55
{
6-
internal static class AuthUtil
6+
internal static class AuthUtil
7+
{
8+
public static void OpenBrowser(string url)
79
{
8-
public static void OpenBrowser(string url)
9-
{
1010
#if NETSTANDARD2_0
11-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
12-
{
13-
url = url.Replace("&", "^&");
14-
Process.Start(new ProcessStartInfo("cmd", $"/c start {url}"));
15-
}
16-
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
17-
{
18-
Process.Start("xdg-open", url);
19-
}
20-
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
21-
{
22-
Process.Start("open", url);
23-
}
11+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
12+
{
13+
url = url.Replace("&", "^&");
14+
Process.Start(new ProcessStartInfo("cmd", $"/c start {url}"));
15+
}
16+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
17+
{
18+
Process.Start("xdg-open", url);
19+
}
20+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
21+
{
22+
Process.Start("open", url);
23+
}
2424
#else
25-
url = url.Replace("&", "^&");
26-
Process.Start(new ProcessStartInfo("cmd", $"/c start {url}"));
25+
url = url.Replace("&", "^&");
26+
Process.Start(new ProcessStartInfo("cmd", $"/c start {url}"));
2727
#endif
28-
}
2928
}
29+
}
3030
}

SpotifyAPI.Web.Auth/AuthorizationCodeAuth.cs

Lines changed: 109 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -12,125 +12,121 @@
1212

1313
namespace SpotifyAPI.Web.Auth
1414
{
15-
public class AuthorizationCodeAuth : SpotifyAuthServer<AuthorizationCode>
15+
public class AuthorizationCodeAuth : SpotifyAuthServer<AuthorizationCode>
16+
{
17+
public string SecretId { get; set; }
18+
19+
public ProxyConfig ProxyConfig { get; set; }
20+
21+
public AuthorizationCodeAuth(string redirectUri, string serverUri, Scope scope = Scope.None, string state = "") : base("code", "AuthorizationCodeAuth", redirectUri, serverUri, scope, state)
22+
{ }
23+
24+
public AuthorizationCodeAuth(string clientId, string secretId, string redirectUri, string serverUri, Scope scope = Scope.None, string state = "") : this(redirectUri, serverUri, scope, state)
1625
{
17-
public string SecretId { get; set; }
18-
19-
public ProxyConfig ProxyConfig { get; set; }
20-
21-
public AuthorizationCodeAuth(string redirectUri, string serverUri, Scope scope = Scope.None, string state = "")
22-
: base("code", "AuthorizationCodeAuth", redirectUri, serverUri, scope, state)
23-
{
24-
}
25-
26-
public AuthorizationCodeAuth(string clientId, string secretId, string redirectUri, string serverUri, Scope scope = Scope.None, string state = "")
27-
: this(redirectUri, serverUri, scope, state)
28-
{
29-
ClientId = clientId;
30-
SecretId = secretId;
31-
}
32-
33-
private bool ShouldRegisterNewApp()
34-
{
35-
return string.IsNullOrEmpty(SecretId) || string.IsNullOrEmpty(ClientId);
36-
}
37-
38-
public override string GetUri()
39-
{
40-
return ShouldRegisterNewApp() ? $"{RedirectUri}/start.html#{State}" : base.GetUri();
41-
}
42-
43-
protected override void AdaptWebServer(WebServer webServer)
44-
{
45-
webServer.Module<WebApiModule>().RegisterController<AuthorizationCodeAuthController>();
46-
}
47-
48-
private string GetAuthHeader() => $"Basic {Convert.ToBase64String(Encoding.UTF8.GetBytes(ClientId + ":" + SecretId))}";
49-
50-
public async Task<Token> RefreshToken(string refreshToken)
51-
{
52-
List<KeyValuePair<string, string>> args = new List<KeyValuePair<string, string>>
53-
{
54-
new KeyValuePair<string, string>("grant_type", "refresh_token"),
55-
new KeyValuePair<string, string>("refresh_token", refreshToken)
56-
};
57-
58-
return await GetToken(args);
59-
}
60-
61-
public async Task<Token> ExchangeCode(string code)
62-
{
63-
List<KeyValuePair<string, string>> args = new List<KeyValuePair<string, string>>
64-
{
65-
new KeyValuePair<string, string>("grant_type", "authorization_code"),
66-
new KeyValuePair<string, string>("code", code),
67-
new KeyValuePair<string, string>("redirect_uri", RedirectUri)
68-
};
69-
70-
return await GetToken(args);
71-
}
72-
73-
private async Task<Token> GetToken(IEnumerable<KeyValuePair<string, string>> args)
74-
{
75-
HttpClientHandler handler = ProxyConfig.CreateClientHandler(ProxyConfig);
76-
HttpClient client = new HttpClient(handler);
77-
client.DefaultRequestHeaders.Add("Authorization", GetAuthHeader());
78-
HttpContent content = new FormUrlEncodedContent(args);
79-
80-
HttpResponseMessage resp = await client.PostAsync("https://accounts.spotify.com/api/token", content);
81-
string msg = await resp.Content.ReadAsStringAsync();
82-
83-
return JsonConvert.DeserializeObject<Token>(msg);
84-
}
26+
ClientId = clientId;
27+
SecretId = secretId;
8528
}
8629

87-
public class AuthorizationCode
30+
private bool ShouldRegisterNewApp()
8831
{
89-
public string Code { get; set; }
32+
return string.IsNullOrEmpty(SecretId) || string.IsNullOrEmpty(ClientId);
33+
}
9034

91-
public string Error { get; set; }
35+
public override string GetUri()
36+
{
37+
return ShouldRegisterNewApp() ? $"{RedirectUri}/start.html#{State}" : base.GetUri();
9238
}
9339

94-
internal class AuthorizationCodeAuthController : WebApiController
40+
protected override void AdaptWebServer(WebServer webServer)
9541
{
96-
[WebApiHandler(HttpVerbs.Get, "/")]
97-
public Task<bool> GetEmpty()
98-
{
99-
string state = Request.QueryString["state"];
100-
AuthorizationCodeAuth.Instances.TryGetValue(state, out SpotifyAuthServer<AuthorizationCode> auth);
101-
102-
string code = null;
103-
string error = Request.QueryString["error"];
104-
if (error == null)
105-
code = Request.QueryString["code"];
106-
107-
Task.Factory.StartNew(() => auth?.TriggerAuth(new AuthorizationCode
108-
{
109-
Code = code,
110-
Error = error
111-
}));
112-
113-
return HttpContext.HtmlResponseAsync("<html><script type=\"text/javascript\">window.close();</script>OK - This window can be closed now</html>");
114-
}
115-
116-
[WebApiHandler(HttpVerbs.Post, "/")]
117-
public async Task<bool> PostValues()
118-
{
119-
Dictionary<string, object> formParams = await HttpContext.RequestFormDataDictionaryAsync();
120-
121-
string state = (string) formParams["state"];
122-
AuthorizationCodeAuth.Instances.TryGetValue(state, out SpotifyAuthServer<AuthorizationCode> authServer);
123-
124-
AuthorizationCodeAuth auth = (AuthorizationCodeAuth) authServer;
125-
auth.ClientId = (string) formParams["clientId"];
126-
auth.SecretId = (string) formParams["secretId"];
127-
128-
string uri = auth.GetUri();
129-
return HttpContext.Redirect(uri, false);
130-
}
131-
132-
public AuthorizationCodeAuthController(IHttpContext context) : base(context)
133-
{
134-
}
42+
webServer.Module<WebApiModule>().RegisterController<AuthorizationCodeAuthController>();
13543
}
136-
}
44+
45+
private string GetAuthHeader() => $"Basic {Convert.ToBase64String(Encoding.UTF8.GetBytes(ClientId + ":" + SecretId))}";
46+
47+
public async Task<Token> RefreshToken(string refreshToken)
48+
{
49+
List<KeyValuePair<string, string>> args = new List<KeyValuePair<string, string>>
50+
{
51+
new KeyValuePair<string, string>("grant_type", "refresh_token"),
52+
new KeyValuePair<string, string>("refresh_token", refreshToken)
53+
};
54+
55+
return await GetToken(args);
56+
}
57+
58+
public async Task<Token> ExchangeCode(string code)
59+
{
60+
List<KeyValuePair<string, string>> args = new List<KeyValuePair<string, string>>
61+
{
62+
new KeyValuePair<string, string>("grant_type", "authorization_code"),
63+
new KeyValuePair<string, string>("code", code),
64+
new KeyValuePair<string, string>("redirect_uri", RedirectUri)
65+
};
66+
67+
return await GetToken(args);
68+
}
69+
70+
private async Task<Token> GetToken(IEnumerable<KeyValuePair<string, string>> args)
71+
{
72+
HttpClientHandler handler = ProxyConfig.CreateClientHandler(ProxyConfig);
73+
HttpClient client = new HttpClient(handler);
74+
client.DefaultRequestHeaders.Add("Authorization", GetAuthHeader());
75+
HttpContent content = new FormUrlEncodedContent(args);
76+
77+
HttpResponseMessage resp = await client.PostAsync("https://accounts.spotify.com/api/token", content);
78+
string msg = await resp.Content.ReadAsStringAsync();
79+
80+
return JsonConvert.DeserializeObject<Token>(msg);
81+
}
82+
}
83+
84+
public class AuthorizationCode
85+
{
86+
public string Code { get; set; }
87+
88+
public string Error { get; set; }
89+
}
90+
91+
internal class AuthorizationCodeAuthController : WebApiController
92+
{
93+
[WebApiHandler(HttpVerbs.Get, "/")]
94+
public Task<bool> GetEmpty()
95+
{
96+
string state = Request.QueryString["state"];
97+
AuthorizationCodeAuth.Instances.TryGetValue(state, out SpotifyAuthServer<AuthorizationCode> auth);
98+
99+
string code = null;
100+
string error = Request.QueryString["error"];
101+
if (error == null)
102+
code = Request.QueryString["code"];
103+
104+
Task.Factory.StartNew(() => auth?.TriggerAuth(new AuthorizationCode
105+
{
106+
Code = code,
107+
Error = error
108+
}));
109+
110+
return HttpContext.HtmlResponseAsync("<html><script type=\"text/javascript\">window.close();</script>OK - This window can be closed now</html>");
111+
}
112+
113+
[WebApiHandler(HttpVerbs.Post, "/")]
114+
public async Task<bool> PostValues()
115+
{
116+
Dictionary<string, object> formParams = await HttpContext.RequestFormDataDictionaryAsync();
117+
118+
string state = (string) formParams["state"];
119+
AuthorizationCodeAuth.Instances.TryGetValue(state, out SpotifyAuthServer<AuthorizationCode> authServer);
120+
121+
AuthorizationCodeAuth auth = (AuthorizationCodeAuth) authServer;
122+
auth.ClientId = (string) formParams["clientId"];
123+
auth.SecretId = (string) formParams["secretId"];
124+
125+
string uri = auth.GetUri();
126+
return HttpContext.Redirect(uri, false);
127+
}
128+
129+
public AuthorizationCodeAuthController(IHttpContext context) : base(context)
130+
{ }
131+
}
132+
}
Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Net.Http;
44
using System.Text;
@@ -8,38 +8,37 @@
88

99
namespace SpotifyAPI.Web.Auth
1010
{
11-
public class CredentialsAuth
12-
{
13-
public string ClientSecret { get; set; }
11+
public class CredentialsAuth
12+
{
13+
public string ClientSecret { get; set; }
1414

15-
public string ClientId { get; set; }
16-
17-
public ProxyConfig ProxyConfig { get; set; }
15+
public string ClientId { get; set; }
1816

19-
public CredentialsAuth(string clientId, string clientSecret)
20-
{
21-
ClientId = clientId;
22-
ClientSecret = clientSecret;
23-
}
17+
public ProxyConfig ProxyConfig { get; set; }
2418

25-
public async Task<Token> GetToken()
26-
{
27-
string auth = Convert.ToBase64String(Encoding.UTF8.GetBytes(ClientId + ":" + ClientSecret));
19+
public CredentialsAuth(string clientId, string clientSecret)
20+
{
21+
ClientId = clientId;
22+
ClientSecret = clientSecret;
23+
}
24+
25+
public async Task<Token> GetToken()
26+
{
27+
string auth = Convert.ToBase64String(Encoding.UTF8.GetBytes(ClientId + ":" + ClientSecret));
2828

29-
List<KeyValuePair<string, string>> args = new List<KeyValuePair<string, string>>
30-
{
31-
new KeyValuePair<string, string>("grant_type", "client_credentials")
32-
};
29+
List<KeyValuePair<string, string>> args = new List<KeyValuePair<string, string>>
30+
{new KeyValuePair<string, string>("grant_type", "client_credentials")
31+
};
3332

34-
HttpClientHandler handler = ProxyConfig.CreateClientHandler(ProxyConfig);
35-
HttpClient client = new HttpClient(handler);
36-
client.DefaultRequestHeaders.Add("Authorization", $"Basic {auth}");
37-
HttpContent content = new FormUrlEncodedContent(args);
33+
HttpClientHandler handler = ProxyConfig.CreateClientHandler(ProxyConfig);
34+
HttpClient client = new HttpClient(handler);
35+
client.DefaultRequestHeaders.Add("Authorization", $"Basic {auth}");
36+
HttpContent content = new FormUrlEncodedContent(args);
3837

39-
HttpResponseMessage resp = await client.PostAsync("https://accounts.spotify.com/api/token", content);
40-
string msg = await resp.Content.ReadAsStringAsync();
38+
HttpResponseMessage resp = await client.PostAsync("https://accounts.spotify.com/api/token", content);
39+
string msg = await resp.Content.ReadAsStringAsync();
4140

42-
return JsonConvert.DeserializeObject<Token>(msg);
43-
}
41+
return JsonConvert.DeserializeObject<Token>(msg);
4442
}
43+
}
4544
}

0 commit comments

Comments
 (0)