public async Task <ActionResult <string> > Callback([FromQuery] SpotifyLogin login) { if (login == null) { // If login is nil, throw a nil arg expection. _logger.LogError("Spotify Login Model is NULL", login); return(BadRequest("Input body is null")); } AccessAuthToken tok = await _auth.TransmutAuthCode(login.Code).ConfigureAwait(false); // Change that login code to an access token and refresh token. tok = await _auth.AssertValidLogin(tok, false).ConfigureAwait(false); // Make sure its valid. using HttpClient c = _cf.CreateClient("spotify"); // Create a new client to spotify. c.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( "Bearer", tok.AccessToken ); // Now you are thinking with Bearers. HttpResponseMessage me = await c.GetAsync(new Uri("me", UriKind.Relative)).ConfigureAwait(false); // Get the Auth Users information. string response = await me.Content.ReadAsStringAsync().ConfigureAwait(false); // Make it a string. SpotifyIdentity identify = JsonConvert.DeserializeObject <SpotifyIdentity>(response); // Make it a SpotifyIdentity. User host = await _userManager.GetUserAsync(HttpContext.User).ConfigureAwait(false); if (host == null) { // If not, reject return(BadRequest(new { Success = false, Error = "User has not yet registered with the Identity Provider." })); } // If so, update the tokens. host.Token = tok; if (host.IsHost()) { if (host.Party == null) { host.Party = _context.Parties.Where(p => p.PartyHost == host || p.PartyHostId == host.Id).FirstOrDefault(); } // Disband Party. host.Party.PartyMembers.Clear(); host.Party.KickedMembers.Clear(); _context.Parties.Remove(host.Party); host.Party = null; host.PartyId = null; } if (host.IsInParty()) { // Leave Party. host.Party.PartyMembers.Remove(host); host.Party = null; host.PartyId = null; } Party party = new Party { // Create a new party with this user as a host. JoinCode = _rand.Next(0, 100000).ToString(CultureInfo.InvariantCulture).PadLeft(6, '0'), PartyHost = host, AllowExplicit = true // PartyMembers = new List<User> { host }, }; host.Party = party; host.PartyId = party.Id; _context.Parties.Add(party); _context.SaveChanges(); // Kommit to DB. if (!await _playback.BeginPlayback(host).ConfigureAwait(false)) { return(Ok(new { Success = false, Error = "Cannot start the party on Spotify" })); } return(Ok(new { Success = true, party.JoinCode })); // Return Party Join Code. }
public void AssertThatAssertValidLoginHandlesNull() { Assert.ThrowsAsync <ArgumentNullException>(async() => await authHelper.AssertValidLogin(null, false).ConfigureAwait(false)); }