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
|
|
|
|
{
|
2019-03-18 20:24:09 +00:00
|
|
|
public class ImplicitGrantAuth : SpotifyAuthServer<Token>
|
2018-08-24 13:10:13 +01:00
|
|
|
{
|
2019-03-18 20:24:09 +00:00
|
|
|
public ImplicitGrantAuth(string clientId, string redirectUri, string serverUri, Scope scope = Scope.None, string state = "") :
|
2018-08-24 13:10:13 +01:00
|
|
|
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
|
|
|
{
|
2019-03-18 20:24:09 +00:00
|
|
|
webServer.Module<WebApiModule>().RegisterController<ImplicitGrantAuthController>();
|
2018-08-24 13:10:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 20:24:09 +00:00
|
|
|
public class ImplicitGrantAuthController : WebApiController
|
2018-08-24 13:10:13 +01:00
|
|
|
{
|
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"];
|
2019-03-18 20:24:09 +00:00
|
|
|
SpotifyAuthServer<Token> auth = ImplicitGrantAuth.GetByState(state);
|
2018-08-24 13:10:13 +01:00
|
|
|
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
|
|
|
|
{
|
2019-03-18 20:24:09 +00:00
|
|
|
token = new Token
|
2018-08-24 13:10:13 +01:00
|
|
|
{
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2019-03-18 20:24:09 +00:00
|
|
|
public ImplicitGrantAuthController(IHttpContext context) : base(context)
|
2018-12-22 20:12:57 +00:00
|
|
|
{
|
2018-08-24 13:10:13 +01:00
|
|
|
}
|
|
|
|
}
|
2018-09-04 13:39:07 +01:00
|
|
|
}
|