Spotify.NET/SpotifyAPI.Web.Auth/ImplicitGrantAuth.cs
dependabot-preview[bot] 94d84e1f9d Bump EmbedIO from 2.2.7 to 2.7.4 (#344)
* Bump EmbedIO from 2.2.7 to 2.7.4

Bumps [EmbedIO](https://github.com/unosquare/embedio) from 2.2.7 to 2.7.4.
- [Release notes](https://github.com/unosquare/embedio/releases)
- [Commits](https://github.com/unosquare/embedio/commits/2.7.4)

* Update SpotifyAPI.Web.Auth.csproj

* Updated code to be compatible with EmbedIO 2.8
2019-06-03 15:18:10 +02:00

66 lines
2.3 KiB
C#

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
{
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)
{
ClientId = clientId;
}
protected override void AdaptWebServer(WebServer webServer)
{
webServer.Module<WebApiModule>().RegisterController<ImplicitGrantAuthController>();
}
}
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");
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
{
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>");
}
public ImplicitGrantAuthController(IHttpContext context) : base(context)
{
}
}
}