12
12
13
13
namespace SpotifyAPI . Web . Auth
14
14
{
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 )
16
25
{
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 ;
85
28
}
86
29
87
- public class AuthorizationCode
30
+ private bool ShouldRegisterNewApp ( )
88
31
{
89
- public string Code { get ; set ; }
32
+ return string . IsNullOrEmpty ( SecretId ) || string . IsNullOrEmpty ( ClientId ) ;
33
+ }
90
34
91
- public string Error { get ; set ; }
35
+ public override string GetUri ( )
36
+ {
37
+ return ShouldRegisterNewApp ( ) ? $ "{ RedirectUri } /start.html#{ State } " : base . GetUri ( ) ;
92
38
}
93
39
94
- internal class AuthorizationCodeAuthController : WebApiController
40
+ protected override void AdaptWebServer ( WebServer webServer )
95
41
{
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 > ( ) ;
135
43
}
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
+ }
0 commit comments