Repository navigation
Expand file tree
/
Copy pathAuthorizationCodeAuthenticator.cs
More file actions
75 lines (63 loc) · 2.67 KB
/
Copy pathAuthorizationCodeAuthenticator.cs
File metadata and controls
75 lines (63 loc) · 2.67 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
using System;
using System.Threading.Tasks;
using SpotifyAPI.Web.Http;
namespace SpotifyAPI.Web
{
/// <summary>
/// This Authenticator requests new credentials token on demand and stores them into memory.
/// It is unable to query user specifc details.
/// </summary>
public class AuthorizationCodeAuthenticator : IAuthenticator
{
/// <summary>
/// Initiate a new instance. The token will be refreshed once it expires.
/// The initialToken will be updated with the new values on refresh!
/// </summary>
public AuthorizationCodeAuthenticator(string clientId, string clientSecret, AuthorizationCodeTokenResponse initialToken)
{
Ensure.ArgumentNotNull(clientId, nameof(clientId));
Ensure.ArgumentNotNull(clientSecret, nameof(clientSecret));
Ensure.ArgumentNotNull(initialToken, nameof(initialToken));
InitialToken = initialToken;
ClientId = clientId;
ClientSecret = clientSecret;
}
/// <summary>
/// This event is called once a new refreshed token was aquired
/// </summary>
public event EventHandler<AuthorizationCodeTokenResponse>? TokenRefreshed;
/// <summary>
/// The ClientID, defined in a spotify application in your Spotify Developer Dashboard
/// </summary>
public string ClientId { get; }
/// <summary>
/// The ClientID, defined in a spotify application in your Spotify Developer Dashboard
/// </summary>
public string ClientSecret { get; }
/// <summary>
/// The inital token passed to the authenticator. Fields will be updated on refresh.
/// </summary>
/// <value></value>
public AuthorizationCodeTokenResponse InitialToken { get; }
public async Task Apply(IRequest request, IAPIConnector apiConnector)
{
Ensure.ArgumentNotNull(request, nameof(request));
if (InitialToken.IsExpired)
{
AuthorizationCodeRefreshRequest? tokenRequest = new(ClientId, ClientSecret, InitialToken.RefreshToken);
AuthorizationCodeRefreshResponse? refreshedToken = await OAuthClient.RequestToken(tokenRequest, apiConnector).ConfigureAwait(false);
InitialToken.AccessToken = refreshedToken.AccessToken;
InitialToken.CreatedAt = refreshedToken.CreatedAt;
InitialToken.ExpiresIn = refreshedToken.ExpiresIn;
InitialToken.Scope = refreshedToken.Scope;
InitialToken.TokenType = refreshedToken.TokenType;
if (refreshedToken.RefreshToken != null)
{
InitialToken.RefreshToken = refreshedToken.RefreshToken;
}
TokenRefreshed?.Invoke(this, InitialToken);
}
request.Headers["Authorization"] = $"{InitialToken.TokenType} {InitialToken.AccessToken}";
}
}
}