2015-10-16 23:44:35 +01:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using SpotifyAPI.Web.Enums;
|
|
|
|
|
using SpotifyAPI.Web.Models;
|
|
|
|
|
using System;
|
2015-07-07 17:11:11 +01:00
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
namespace SpotifyAPI.Web.Auth
|
|
|
|
|
{
|
|
|
|
|
public class AutorizationCodeAuth
|
|
|
|
|
{
|
|
|
|
|
public delegate void OnResponseReceived(AutorizationCodeAuthResponse response);
|
|
|
|
|
|
|
|
|
|
private SimpleHttpServer _httpServer;
|
|
|
|
|
private Thread _httpThread;
|
2016-03-31 11:08:23 +01:00
|
|
|
|
public string ClientId { get; set; }
|
|
|
|
|
public string RedirectUri { get; set; }
|
|
|
|
|
public string State { get; set; }
|
2015-07-07 17:11:11 +01:00
|
|
|
|
public Scope Scope { get; set; }
|
|
|
|
|
public Boolean ShowDialog { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Will be fired once the user authenticated
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event OnResponseReceived OnResponseReceivedEvent;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Start the auth process (Make sure the internal HTTP-Server ist started)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void DoAuth()
|
|
|
|
|
{
|
2016-03-31 11:08:23 +01:00
|
|
|
|
string uri = GetUri();
|
2015-07-07 17:11:11 +01:00
|
|
|
|
Process.Start(uri);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Refreshes auth by providing the clientsecret (Don't use this if you're on a client)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="refreshToken">The refresh-token of the earlier gathered token</param>
|
|
|
|
|
/// <param name="clientSecret">Your Client-Secret, don't provide it if this is running on a client!</param>
|
|
|
|
|
public Token RefreshToken(string refreshToken, string clientSecret)
|
|
|
|
|
{
|
|
|
|
|
using (WebClient wc = new WebClient())
|
|
|
|
|
{
|
|
|
|
|
wc.Proxy = null;
|
|
|
|
|
wc.Headers.Add("Authorization",
|
|
|
|
|
"Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(ClientId + ":" + clientSecret)));
|
|
|
|
|
NameValueCollection col = new NameValueCollection
|
|
|
|
|
{
|
2015-10-16 23:44:35 +01:00
|
|
|
|
{"grant_type", "refresh_token"},
|
2015-07-07 17:11:11 +01:00
|
|
|
|
{"refresh_token", refreshToken}
|
|
|
|
|
};
|
|
|
|
|
|
2016-03-31 11:08:23 +01:00
|
|
|
|
string response;
|
2015-07-07 17:11:11 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
byte[] data = wc.UploadValues("https://accounts.spotify.com/api/token", "POST", col);
|
|
|
|
|
response = Encoding.UTF8.GetString(data);
|
|
|
|
|
}
|
|
|
|
|
catch (WebException e)
|
|
|
|
|
{
|
|
|
|
|
using (StreamReader reader = new StreamReader(e.Response.GetResponseStream()))
|
|
|
|
|
{
|
|
|
|
|
response = reader.ReadToEnd();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return JsonConvert.DeserializeObject<Token>(response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 11:08:23 +01:00
|
|
|
|
private string GetUri()
|
2015-07-07 17:11:11 +01:00
|
|
|
|
{
|
|
|
|
|
StringBuilder builder = new StringBuilder("https://accounts.spotify.com/authorize/?");
|
|
|
|
|
builder.Append("client_id=" + ClientId);
|
|
|
|
|
builder.Append("&response_type=code");
|
|
|
|
|
builder.Append("&redirect_uri=" + RedirectUri);
|
|
|
|
|
builder.Append("&state=" + State);
|
|
|
|
|
builder.Append("&scope=" + Scope.GetStringAttribute(" "));
|
|
|
|
|
builder.Append("&show_dialog=" + ShowDialog);
|
|
|
|
|
return builder.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Start the internal HTTP-Server
|
|
|
|
|
/// </summary>
|
2015-09-08 14:35:07 +01:00
|
|
|
|
public void StartHttpServer(int port = 80)
|
2015-07-07 17:11:11 +01:00
|
|
|
|
{
|
2015-09-08 14:35:07 +01:00
|
|
|
|
_httpServer = new SimpleHttpServer(port, AuthType.Authorization);
|
2015-07-07 17:11:11 +01:00
|
|
|
|
_httpServer.OnAuth += HttpServerOnOnAuth;
|
|
|
|
|
|
|
|
|
|
_httpThread = new Thread(_httpServer.Listen);
|
|
|
|
|
_httpThread.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HttpServerOnOnAuth(AuthEventArgs e)
|
|
|
|
|
{
|
2015-10-28 16:05:09 +00:00
|
|
|
|
OnResponseReceivedEvent?.Invoke(new AutorizationCodeAuthResponse()
|
|
|
|
|
{
|
|
|
|
|
Code = e.Code,
|
|
|
|
|
State = e.State,
|
|
|
|
|
Error = e.Error
|
|
|
|
|
});
|
2015-07-07 17:11:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This will stop the internal HTTP-Server (Should be called after you got the Token)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void StopHttpServer()
|
|
|
|
|
{
|
2015-12-08 01:10:41 +00:00
|
|
|
|
_httpServer.Dispose();
|
2015-07-07 17:11:11 +01:00
|
|
|
|
_httpServer = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Exchange a code for a Token (Don't use this if you're on a client)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="code">The gathered code from the response</param>
|
|
|
|
|
/// <param name="clientSecret">Your Client-Secret, don't provide it if this is running on a client!</param>
|
|
|
|
|
/// <returns></returns>
|
2016-03-31 11:08:23 +01:00
|
|
|
|
public Token ExchangeAuthCode(string code, string clientSecret)
|
2015-07-07 17:11:11 +01:00
|
|
|
|
{
|
|
|
|
|
using (WebClient wc = new WebClient())
|
|
|
|
|
{
|
|
|
|
|
wc.Proxy = null;
|
|
|
|
|
|
|
|
|
|
NameValueCollection col = new NameValueCollection
|
|
|
|
|
{
|
|
|
|
|
{"grant_type", "authorization_code"},
|
|
|
|
|
{"code", code},
|
|
|
|
|
{"redirect_uri", RedirectUri},
|
|
|
|
|
{"client_id", ClientId},
|
|
|
|
|
{"client_secret", clientSecret}
|
|
|
|
|
};
|
|
|
|
|
|
2016-03-31 11:08:23 +01:00
|
|
|
|
string response;
|
2015-07-07 17:11:11 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
byte[] data = wc.UploadValues("https://accounts.spotify.com/api/token", "POST", col);
|
|
|
|
|
response = Encoding.UTF8.GetString(data);
|
|
|
|
|
}
|
|
|
|
|
catch (WebException e)
|
|
|
|
|
{
|
|
|
|
|
using (StreamReader reader = new StreamReader(e.Response.GetResponseStream()))
|
|
|
|
|
{
|
|
|
|
|
response = reader.ReadToEnd();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return JsonConvert.DeserializeObject<Token>(response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public struct AutorizationCodeAuthResponse
|
|
|
|
|
{
|
2016-03-31 11:08:23 +01:00
|
|
|
|
public string Code { get; set; }
|
|
|
|
|
public string State { get; set; }
|
|
|
|
|
public string Error { get; set; }
|
2015-07-07 17:11:11 +01:00
|
|
|
|
}
|
2015-12-08 01:10:41 +00:00
|
|
|
|
}
|