From a9c4aba7fabebf3d4f6caa73951a55d131ead708 Mon Sep 17 00:00:00 2001 From: Tim Ittermann Date: Wed, 26 Aug 2020 15:38:00 +0200 Subject: [PATCH] Add interfaces for tokens (#499) * Add interfaces for tokens * Delete UpgradeLog.htm * Changed Namespace Co-authored-by: Jonas Dellinger --- SpotifyAPI.Web/Clients/SpotifyClient.cs | 4 +++ .../AuthorizationCodeRefreshResponse.cs | 2 +- .../AuthorizationCodeTokenResponse.cs | 2 +- .../ClientCredentialsTokenResponse.cs | 3 +- .../Response/Interfaces/IRefreshableToken.cs | 13 ++++++++ .../Models/Response/Interfaces/IToken.cs | 30 +++++++++++++++++++ .../Models/Response/Interfaces/IUserToken.cs | 13 ++++++++ .../Models/Response/PKCETokenResponse.cs | 2 +- 8 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 SpotifyAPI.Web/Models/Response/Interfaces/IRefreshableToken.cs create mode 100644 SpotifyAPI.Web/Models/Response/Interfaces/IToken.cs create mode 100644 SpotifyAPI.Web/Models/Response/Interfaces/IUserToken.cs diff --git a/SpotifyAPI.Web/Clients/SpotifyClient.cs b/SpotifyAPI.Web/Clients/SpotifyClient.cs index ef8ca937..8439ce28 100644 --- a/SpotifyAPI.Web/Clients/SpotifyClient.cs +++ b/SpotifyAPI.Web/Clients/SpotifyClient.cs @@ -11,6 +11,10 @@ namespace SpotifyAPI.Web { private readonly IAPIConnector _apiConnector; + public SpotifyClient(IToken token) : + this(SpotifyClientConfig.CreateDefault(token?.AccessToken ?? throw new ArgumentNullException(nameof(token)), token.TokenType)) + { } + public SpotifyClient(string token, string tokenType = "Bearer") : this(SpotifyClientConfig.CreateDefault(token, tokenType)) { } diff --git a/SpotifyAPI.Web/Models/Response/AuthorizationCodeRefreshResponse.cs b/SpotifyAPI.Web/Models/Response/AuthorizationCodeRefreshResponse.cs index b50b96af..b4dc1c4a 100644 --- a/SpotifyAPI.Web/Models/Response/AuthorizationCodeRefreshResponse.cs +++ b/SpotifyAPI.Web/Models/Response/AuthorizationCodeRefreshResponse.cs @@ -2,7 +2,7 @@ using System; namespace SpotifyAPI.Web { - public class AuthorizationCodeRefreshResponse + public class AuthorizationCodeRefreshResponse: IUserToken { public string AccessToken { get; set; } = default!; public string TokenType { get; set; } = default!; diff --git a/SpotifyAPI.Web/Models/Response/AuthorizationCodeTokenResponse.cs b/SpotifyAPI.Web/Models/Response/AuthorizationCodeTokenResponse.cs index 63198867..05cea2a3 100644 --- a/SpotifyAPI.Web/Models/Response/AuthorizationCodeTokenResponse.cs +++ b/SpotifyAPI.Web/Models/Response/AuthorizationCodeTokenResponse.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; namespace SpotifyAPI.Web { - public class AuthorizationCodeTokenResponse + public class AuthorizationCodeTokenResponse: IRefreshableToken { public string AccessToken { get; set; } = default!; public string TokenType { get; set; } = default!; diff --git a/SpotifyAPI.Web/Models/Response/ClientCredentialsTokenResponse.cs b/SpotifyAPI.Web/Models/Response/ClientCredentialsTokenResponse.cs index 30e00d02..3261c44c 100644 --- a/SpotifyAPI.Web/Models/Response/ClientCredentialsTokenResponse.cs +++ b/SpotifyAPI.Web/Models/Response/ClientCredentialsTokenResponse.cs @@ -1,7 +1,8 @@ using System; + namespace SpotifyAPI.Web { - public class ClientCredentialsTokenResponse + public class ClientCredentialsTokenResponse: IToken { public string AccessToken { get; set; } = default!; public string TokenType { get; set; } = default!; diff --git a/SpotifyAPI.Web/Models/Response/Interfaces/IRefreshableToken.cs b/SpotifyAPI.Web/Models/Response/Interfaces/IRefreshableToken.cs new file mode 100644 index 00000000..ddb83ebf --- /dev/null +++ b/SpotifyAPI.Web/Models/Response/Interfaces/IRefreshableToken.cs @@ -0,0 +1,13 @@ +namespace SpotifyAPI.Web +{ + /// + /// An user token, which can be refreshed + /// + public interface IRefreshableToken : IUserToken + { + /// + /// Refresh token + /// + public string RefreshToken { get; set; } + } +} diff --git a/SpotifyAPI.Web/Models/Response/Interfaces/IToken.cs b/SpotifyAPI.Web/Models/Response/Interfaces/IToken.cs new file mode 100644 index 00000000..e9e6e93e --- /dev/null +++ b/SpotifyAPI.Web/Models/Response/Interfaces/IToken.cs @@ -0,0 +1,30 @@ +using System; + +namespace SpotifyAPI.Web +{ + /// + /// A token to access the Spotify API + /// + public interface IToken + { + /// + /// Access token string + /// + public string AccessToken { get; set; } + + /// + /// Type of this token (eg. Bearer) + /// + public string TokenType { get; set; } + + /// + /// Auto-Initalized to UTC Now + /// + public DateTime CreatedAt { get; set; } + + /// + /// Is the token still valid? + /// + public bool IsExpired { get; } + } +} diff --git a/SpotifyAPI.Web/Models/Response/Interfaces/IUserToken.cs b/SpotifyAPI.Web/Models/Response/Interfaces/IUserToken.cs new file mode 100644 index 00000000..07455619 --- /dev/null +++ b/SpotifyAPI.Web/Models/Response/Interfaces/IUserToken.cs @@ -0,0 +1,13 @@ +namespace SpotifyAPI.Web +{ + /// + /// A token which allows you to access the API as user + /// + public interface IUserToken : IToken + { + /// + /// Comma-Seperated list of scopes + /// + public string Scope { get; set; } + } +} diff --git a/SpotifyAPI.Web/Models/Response/PKCETokenResponse.cs b/SpotifyAPI.Web/Models/Response/PKCETokenResponse.cs index c3fdf291..d2748586 100644 --- a/SpotifyAPI.Web/Models/Response/PKCETokenResponse.cs +++ b/SpotifyAPI.Web/Models/Response/PKCETokenResponse.cs @@ -2,7 +2,7 @@ using System; namespace SpotifyAPI.Web { - public class PKCETokenResponse + public class PKCETokenResponse: IRefreshableToken { public string AccessToken { get; set; } = default!; public string TokenType { get; set; } = default!;