2018-09-04 13:39:07 +01:00
|
|
|
using System;
|
|
|
|
using System.Net;
|
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;
|
2018-09-04 13:41:35 +01:00
|
|
|
#if NETSTANDARD2_0
|
|
|
|
using System.Net.Http;
|
|
|
|
#endif
|
|
|
|
#if NET46
|
|
|
|
using System.Net.Http;
|
|
|
|
using HttpListenerContext = Unosquare.Net.HttpListenerContext;
|
|
|
|
#endif
|
2018-08-24 13:10:13 +01:00
|
|
|
|
|
|
|
namespace SpotifyAPI.Web.Auth
|
|
|
|
{
|
|
|
|
public class ImplictGrantAuth : SpotifyAuthServer<Token>
|
|
|
|
{
|
|
|
|
public ImplictGrantAuth(string clientId, string redirectUri, string serverUri, Scope scope = Scope.None, string state = "") :
|
|
|
|
base("token", "ImplicitGrantAuth", redirectUri, serverUri, scope, state)
|
|
|
|
{
|
|
|
|
ClientId = clientId;
|
|
|
|
}
|
|
|
|
|
2018-12-22 20:12:57 +00:00
|
|
|
protected override void AdaptWebServer(WebServer webServer)
|
2018-08-24 13:10:13 +01:00
|
|
|
{
|
2018-09-04 13:39:07 +01:00
|
|
|
webServer.Module<WebApiModule>().RegisterController<ImplictGrantAuthController>();
|
2018-08-24 13:10:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class ImplictGrantAuthController : WebApiController
|
|
|
|
{
|
2018-09-04 13:41:35 +01:00
|
|
|
[WebApiHandler(HttpVerbs.Get, "/auth")]
|
2018-12-22 20:12:57 +00:00
|
|
|
public Task<bool> GetAuth()
|
2018-08-24 13:10:13 +01:00
|
|
|
{
|
2018-12-22 20:12:57 +00:00
|
|
|
string state = Request.QueryString["state"];
|
2018-08-24 13:10:13 +01:00
|
|
|
SpotifyAuthServer<Token> auth = ImplictGrantAuth.GetByState(state);
|
|
|
|
if (auth == null)
|
2018-12-22 20:12:57 +00:00
|
|
|
return this.StringResponseAsync(
|
2018-09-04 13:41:35 +01:00
|
|
|
$"Failed - Unable to find auth request with state \"{state}\" - Please retry");
|
2018-08-24 13:10:13 +01:00
|
|
|
|
|
|
|
Token token;
|
2018-12-22 20:12:57 +00:00
|
|
|
string error = Request.QueryString["error"];
|
2018-08-24 13:10:13 +01:00
|
|
|
if (error == null)
|
|
|
|
{
|
2018-12-22 20:12:57 +00:00
|
|
|
string accessToken = Request.QueryString["access_token"];
|
|
|
|
string tokenType = Request.QueryString["token_type"];
|
|
|
|
string expiresIn = Request.QueryString["expires_in"];
|
2018-08-24 13:10:13 +01:00
|
|
|
token = new Token
|
|
|
|
{
|
|
|
|
AccessToken = accessToken,
|
|
|
|
ExpiresIn = double.Parse(expiresIn),
|
|
|
|
TokenType = tokenType
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
token = new Token()
|
|
|
|
{
|
|
|
|
Error = error
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-04 13:41:35 +01:00
|
|
|
Task.Factory.StartNew(() => auth.TriggerAuth(token));
|
2018-12-22 20:12:57 +00:00
|
|
|
return this.StringResponseAsync("OK - This window can be closed now");
|
|
|
|
}
|
|
|
|
|
|
|
|
public ImplictGrantAuthController(IHttpContext context) : base(context)
|
|
|
|
{
|
2018-08-24 13:10:13 +01:00
|
|
|
}
|
|
|
|
}
|
2018-09-04 13:39:07 +01:00
|
|
|
}
|