-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Adding Tidal API auth workflow.
Added OVR Toolkit connection status to HomePage.
- Loading branch information
Showing
12 changed files
with
362 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
VXMusic/Connections/Tidal/Authentication/TidalOAuthClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System.Text; | ||
using SpotifyAPI.Web; | ||
using SpotifyAPI.Web.Http; | ||
using VXMusic.Connections.Tidal; | ||
|
||
namespace VXMusic.Tidal.Authentication; | ||
|
||
public class TidalOAuthClient : APIClient, IOAuthClient | ||
{ | ||
public Task<PKCETokenResponse> RequestToken(PKCETokenRequest request, CancellationToken cancel = default (CancellationToken)) => RequestToken(request, this.API, cancel); | ||
|
||
public static Task<PKCETokenResponse> RequestToken( | ||
PKCETokenRequest request, | ||
IAPIConnector apiConnector, | ||
CancellationToken cancel = default (CancellationToken)) | ||
{ | ||
TidalUtil.Ensure.ArgumentNotNull((object) request, nameof (request)); | ||
TidalUtil.Ensure.ArgumentNotNull((object) apiConnector, nameof (apiConnector)); | ||
List<KeyValuePair<string, string>> form = new List<KeyValuePair<string, string>>() | ||
{ | ||
new KeyValuePair<string, string>("client_id", request.ClientId), | ||
new KeyValuePair<string, string>("grant_type", "authorization_code"), | ||
new KeyValuePair<string, string>("code", request.Code), | ||
new KeyValuePair<string, string>("redirect_uri", request.RedirectUri.ToString()), | ||
new KeyValuePair<string, string>("code_verifier", request.CodeVerifier) | ||
}; | ||
return SendOAuthRequest<PKCETokenResponse>(apiConnector, form, (string) null, (string) null, cancel); | ||
} | ||
|
||
private static Task<T> SendOAuthRequest<T>( | ||
IAPIConnector apiConnector, | ||
List<KeyValuePair<string?, string?>> form, | ||
string? clientId, | ||
string? clientSecret, | ||
CancellationToken cancel = default (CancellationToken)) | ||
{ | ||
// TODO Inject creds here | ||
Dictionary<string, string> headers = BuildAuthHeader(clientId, clientSecret); | ||
return apiConnector.Post<T>(TidalAuthentication.TidalAuthApiUrl, (IDictionary<string, string>) null, (object) new FormUrlEncodedContent((IEnumerable<KeyValuePair<string, string>>) form), headers, cancel); | ||
Check failure on line 39 in VXMusic/Connections/Tidal/Authentication/TidalOAuthClient.cs GitHub Actions / build
|
||
} | ||
|
||
private static Dictionary<string, string> BuildAuthHeader(string? clientId, string? clientSecret) | ||
{ | ||
if (clientId == null || clientSecret == null) | ||
return new Dictionary<string, string>(); | ||
return new Dictionary<string, string>() | ||
{ | ||
{ | ||
"Authorization", | ||
"Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(clientId + ":" + clientSecret)) | ||
} | ||
}; | ||
} | ||
|
||
public TidalOAuthClient(SpotifyClientConfig config) | ||
: base((IAPIConnector) ValidateConfig(config)) | ||
{ | ||
} | ||
|
||
private static APIConnector ValidateConfig(SpotifyClientConfig config) | ||
{ | ||
TidalUtil.Ensure.ArgumentNotNull((object) config, nameof (config)); | ||
return new APIConnector(config.BaseAddress, config.Authenticator, config.JSONSerializer, config.HTTPClient, config.RetryHandler, config.HTTPLogger); | ||
} | ||
|
||
public Task<ClientCredentialsTokenResponse> RequestToken(ClientCredentialsRequest request, CancellationToken cancel = new CancellationToken()) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public Task<AuthorizationCodeRefreshResponse> RequestToken(AuthorizationCodeRefreshRequest request, CancellationToken cancel = new CancellationToken()) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public Task<AuthorizationCodeTokenResponse> RequestToken(AuthorizationCodeTokenRequest request, CancellationToken cancel = new CancellationToken()) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public Task<AuthorizationCodeTokenResponse> RequestToken(TokenSwapTokenRequest request, CancellationToken cancel = new CancellationToken()) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public Task<AuthorizationCodeRefreshResponse> RequestToken(TokenSwapRefreshRequest request, CancellationToken cancel = new CancellationToken()) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace VXMusic.Tidal.Model; | ||
|
||
public class TidalClient | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace VXMusic.Tidal.Model; | ||
|
||
public static class TidalScopes | ||
{ | ||
public const string PlaylistRead = "playlists.read"; | ||
public const string PlaylistWrite = "playlists.write"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
namespace VXMusic.Connections.Tidal; | ||
|
||
public class TidalUtil | ||
{ | ||
internal static class Ensure | ||
{ | ||
/// <summary>Checks an argument to ensure it isn't null.</summary> | ||
/// <param name="value">The argument value to check</param> | ||
/// <param name="name">The name of the argument</param> | ||
public static void ArgumentNotNull(object value, string name) | ||
{ | ||
if (value == null) | ||
throw new ArgumentNullException(name); | ||
} | ||
|
||
/// <summary> | ||
/// Checks an argument to ensure it isn't null or an empty string | ||
/// </summary> | ||
/// <param name="value">The argument value to check</param> | ||
/// <param name="name">The name of the argument</param> | ||
public static void ArgumentNotNullOrEmptyString(string value, string name) | ||
{ | ||
if (string.IsNullOrEmpty(value)) | ||
throw new ArgumentException("String is empty or null", name); | ||
} | ||
|
||
public static void ArgumentNotNullOrEmptyList<T>(IEnumerable<T> value, string name) | ||
{ | ||
if (value == null || !value.Any<T>()) | ||
throw new ArgumentException("List is empty or null", name); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.