2021-10-27 17:31:29 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
using SpotifyAPI.Web;
|
|
|
|
|
2021-12-19 18:44:03 +00:00
|
|
|
using Selector.Events;
|
|
|
|
using Selector.Model;
|
|
|
|
|
2021-10-27 17:31:29 +01:00
|
|
|
namespace Selector.Web.Controller
|
|
|
|
{
|
|
|
|
|
|
|
|
[ApiController]
|
|
|
|
[Route("api/[controller]/callback")]
|
|
|
|
public class SpotifyController : BaseAuthController
|
|
|
|
{
|
|
|
|
private readonly RootOptions Config;
|
2021-12-19 18:44:03 +00:00
|
|
|
private readonly UserEventBus UserEvent;
|
2021-10-27 17:31:29 +01:00
|
|
|
private const string ManageSpotifyPath = "/Identity/Account/Manage/Spotify";
|
|
|
|
|
|
|
|
public SpotifyController(
|
|
|
|
ApplicationDbContext context,
|
|
|
|
IAuthorizationService auth,
|
|
|
|
UserManager<ApplicationUser> userManager,
|
|
|
|
ILogger<UsersController> logger,
|
2021-12-19 18:44:03 +00:00
|
|
|
IOptions<RootOptions> config,
|
|
|
|
UserEventBus userEvent
|
2021-10-27 17:31:29 +01:00
|
|
|
) : base(context, auth, userManager, logger)
|
|
|
|
{
|
|
|
|
Config = config.Value;
|
2021-12-19 18:44:03 +00:00
|
|
|
UserEvent = userEvent;
|
2021-10-27 17:31:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
public async Task<RedirectResult> Callback(string code)
|
|
|
|
{
|
|
|
|
if (Config.ClientId is null)
|
|
|
|
{
|
2022-07-01 22:04:17 +01:00
|
|
|
Logger.LogError("Cannot link user, no Spotify client ID");
|
2021-10-27 17:31:29 +01:00
|
|
|
TempData["StatusMessage"] = "Could not link Spotify, no app credentials";
|
|
|
|
return Redirect(ManageSpotifyPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Config.ClientSecret is null)
|
|
|
|
{
|
2022-07-01 22:04:17 +01:00
|
|
|
Logger.LogError("Cannot link user, no Spotify client secret");
|
2021-10-27 17:31:29 +01:00
|
|
|
TempData["StatusMessage"] = "Could not link Spotify, no app credentials";
|
|
|
|
return Redirect(ManageSpotifyPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
var user = await UserManager.GetUserAsync(User);
|
|
|
|
if (user == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException("No user returned");
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Authorise
|
|
|
|
var response = await new OAuthClient()
|
|
|
|
.RequestToken(
|
|
|
|
new AuthorizationCodeTokenRequest(
|
|
|
|
Config.ClientId,
|
|
|
|
Config.ClientSecret,
|
|
|
|
code,
|
|
|
|
new Uri(Config.SpotifyCallback)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
user.SpotifyIsLinked = true;
|
|
|
|
|
|
|
|
user.SpotifyAccessToken = response.AccessToken;
|
|
|
|
user.SpotifyRefreshToken = response.RefreshToken;
|
|
|
|
user.SpotifyLastRefresh = response.CreatedAt;
|
|
|
|
user.SpotifyTokenExpiry = response.ExpiresIn;
|
|
|
|
|
|
|
|
await UserManager.UpdateAsync(user);
|
|
|
|
|
2021-12-19 18:44:03 +00:00
|
|
|
UserEvent.OnSpotifyLinkChange(this, new SpotifyLinkChange { UserId = user.Id, PreviousLinkState = false, NewLinkState = true });
|
|
|
|
|
2021-10-27 17:31:29 +01:00
|
|
|
TempData["StatusMessage"] = "Spotify Linked";
|
|
|
|
return Redirect(ManageSpotifyPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|