-
Notifications
You must be signed in to change notification settings - Fork 1
/
Authentication.cs
130 lines (111 loc) · 4.09 KB
/
Authentication.cs
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using SpotifyAPI.Web;
namespace SpotiSharpBackend;
public delegate void AuthenticationComplete();
public static class Authentication
{
private static string _verifier;
private static PKCETokenResponse _initialResponse;
private static string _clientId;
private static string _clientSecret;
private static SpotifyClient _spotifyClient;
public static SpotifyClient? SpotifyClient { get; private set; }
public static event AuthenticationComplete OnAuthenticate;
static Authentication()
{
_clientId = StorageHandler.ClientId;
string refreshToken = StorageHandler.RefreshToken;
if (_clientId != string.Empty && refreshToken != string.Empty) RefreshAuthentication(refreshToken);
}
public static void Authenticate(string clientId = "")
{
if (clientId != string.Empty) _clientId = clientId;
if (_spotifyClient == null)
{
NewAuthentication();
}
else
{
RefreshAuthentication();
}
}
public static void UserLessAuthenticate()
{
_clientId = StorageHandler.ClientId;
_clientSecret = StorageHandler.ClientSecret;
var config = SpotifyClientConfig
.CreateDefault()
.WithAuthenticator(new ClientCredentialsAuthenticator(_clientId, _clientSecret));
try
{
SpotifyClient = new SpotifyClient(config);
OnAuthenticate?.Invoke();
}
catch (APIException) { }
}
private static void NewAuthentication()
{
// Generates a secure random verifier of length 100 and its challenge
(_verifier, string challenge) = PKCEUtil.GenerateCodes();
var loginRequest = new LoginRequest(
new Uri("http://127.0.0.1:5000/callback"),
_clientId,
LoginRequest.ResponseType.Code
)
{
CodeChallengeMethod = "S256",
CodeChallenge = challenge,
Scope = new[] {
Scopes.Streaming,
Scopes.AppRemoteControl,
Scopes.PlaylistModifyPrivate,
Scopes.PlaylistModifyPublic,
Scopes.PlaylistReadCollaborative,
Scopes.PlaylistReadPrivate,
Scopes.UgcImageUpload,
Scopes.UserFollowModify,
Scopes.UserFollowRead,
Scopes.UserLibraryModify,
Scopes.UserLibraryRead,
Scopes.UserReadEmail,
Scopes.UserReadPrivate,
Scopes.UserTopRead,
Scopes.UserModifyPlaybackState,
Scopes.UserReadCurrentlyPlaying,
Scopes.UserReadPlaybackPosition,
Scopes.UserReadPlaybackState,
Scopes.UserReadRecentlyPlayed
}
};
// start webserver for callback
_ = CallBackListener.Instance;
Uri uri = loginRequest.ToUri();
MauiConnector.TriggerBrowerOpen(uri);
}
private static async void RefreshAuthentication(string refreshToken = null)
{
try
{
if (refreshToken == null) refreshToken = _initialResponse.RefreshToken;
var newResponse = await new OAuthClient().RequestToken(
new PKCETokenRefreshRequest(_clientId, refreshToken)
);
SpotifyClient = new SpotifyClient(newResponse.AccessToken);
StorageHandler.RefreshToken = newResponse.RefreshToken;
OnAuthenticate?.Invoke();
}
catch (APIException)
{
StorageHandler.RefreshToken = string.Empty;
}
}
internal static async Task GetCallback(string code)
{
_initialResponse = await new OAuthClient().RequestToken(
new PKCETokenRequest(_clientId, code, new Uri("http://127.0.0.1:5000/callback"), _verifier)
);
SpotifyClient = new SpotifyClient(_initialResponse.AccessToken);
StorageHandler.ClientId = _clientId;
StorageHandler.RefreshToken = _initialResponse.RefreshToken;
OnAuthenticate?.Invoke();
}
}