2018-08-24 13:10:13 +01:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using SpotifyAPI.Web.Enums;
|
|
|
|
using SpotifyAPI.Web.Models;
|
|
|
|
using Unosquare.Labs.EmbedIO;
|
|
|
|
using Unosquare.Labs.EmbedIO.Constants;
|
|
|
|
using Unosquare.Labs.EmbedIO.Modules;
|
|
|
|
|
|
|
|
namespace SpotifyAPI.Web.Auth
|
|
|
|
{
|
2020-03-09 19:47:39 +00:00
|
|
|
public class ImplicitGrantAuth : SpotifyAuthServer<Token>
|
|
|
|
{
|
|
|
|
public ImplicitGrantAuth(string clientId, string redirectUri, string serverUri, Scope scope = Scope.None, string state = "") : base("token", "ImplicitGrantAuth", redirectUri, serverUri, scope, state)
|
2018-08-24 13:10:13 +01:00
|
|
|
{
|
2020-03-09 19:47:39 +00:00
|
|
|
ClientId = clientId;
|
2018-08-24 13:10:13 +01:00
|
|
|
}
|
|
|
|
|
2020-03-09 19:47:39 +00:00
|
|
|
protected override void AdaptWebServer(WebServer webServer)
|
2018-08-24 13:10:13 +01:00
|
|
|
{
|
2020-03-09 19:47:39 +00:00
|
|
|
webServer.Module<WebApiModule>().RegisterController<ImplicitGrantAuthController>();
|
|
|
|
}
|
|
|
|
}
|
2018-08-24 13:10:13 +01:00
|
|
|
|
2020-03-09 19:47:39 +00:00
|
|
|
public class ImplicitGrantAuthController : WebApiController
|
|
|
|
{
|
|
|
|
[WebApiHandler(HttpVerbs.Get, "/auth")]
|
|
|
|
public Task<bool> GetAuth()
|
|
|
|
{
|
|
|
|
string state = Request.QueryString["state"];
|
|
|
|
SpotifyAuthServer<Token> auth = ImplicitGrantAuth.GetByState(state);
|
|
|
|
if (auth == null)
|
|
|
|
return HttpContext.StringResponseAsync(
|
|
|
|
$"Failed - Unable to find auth request with state \"{state}\" - Please retry");
|
2018-12-22 20:12:57 +00:00
|
|
|
|
2020-03-09 19:47:39 +00:00
|
|
|
Token token;
|
|
|
|
string error = Request.QueryString["error"];
|
|
|
|
if (error == null)
|
|
|
|
{
|
|
|
|
string accessToken = Request.QueryString["access_token"];
|
|
|
|
string tokenType = Request.QueryString["token_type"];
|
|
|
|
string expiresIn = Request.QueryString["expires_in"];
|
|
|
|
token = new Token
|
|
|
|
{
|
|
|
|
AccessToken = accessToken,
|
|
|
|
ExpiresIn = double.Parse(expiresIn),
|
|
|
|
TokenType = tokenType
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
token = new Token
|
2018-12-22 20:12:57 +00:00
|
|
|
{
|
2020-03-09 19:47:39 +00:00
|
|
|
Error = error
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Task.Factory.StartNew(() => auth.TriggerAuth(token));
|
|
|
|
return HttpContext.HtmlResponseAsync("<html><script type=\"text/javascript\">window.close();</script>OK - This window can be closed now</html>");
|
2018-08-24 13:10:13 +01:00
|
|
|
}
|
2020-03-09 19:47:39 +00:00
|
|
|
|
|
|
|
public ImplicitGrantAuthController(IHttpContext context) : base(context)
|
|
|
|
{ }
|
|
|
|
}
|
|
|
|
}
|