示例#1
0
        public async Task <IActionResult> Authorized(string code)
        {
            try
            {
                PartyGoerDetails partyGoerDetails = await _authenticationService.AuthenticateUserWithAccessCodeAsync(code);

                _partyGoerService.SavePartyGoer(partyGoerDetails);

                // Get details from spotify of user
                var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
                identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, partyGoerDetails.Id));

                var principal = new ClaimsPrincipal(identity);

                await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);

                await _logService.LogUserActivityAsync(new PartyGoer(partyGoerDetails.Id), "Successfully authenticated through Spotify");

                return(RedirectToAction("Index", "Dashboard"));
            }
            catch (Exception)
            {
                //  TODO: CHANGE THIS TO THE IDNEX PAGE ON HOME
                return(RedirectToAction("Index", "Dashboard"));
            }
        }
示例#2
0
 public void SavePartyGoer(PartyGoerDetails partyGoerDetails)
 {
     if (_partyGoerCache.ContainsKey(partyGoerDetails.Id))
     {
         _partyGoerCache[partyGoerDetails.Id] = new PartyGoer(partyGoerDetails);
     }
     else
     {
         _partyGoerCache.Add(partyGoerDetails.Id, new PartyGoer(partyGoerDetails));
     }
 }
示例#3
0
        public async Task <PartyGoer> GetCurrentPartyGoerAsync()
        {
            string partyGoerId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;

            if (_partyGoerCache.ContainsKey(partyGoerId))
            {
                return(_partyGoerCache[partyGoerId]);
            }
            else
            {
                PartyGoerDetails partyGoerDetails = await _spotifyHttpClient.GetUserDetailsAsync(partyGoerId);

                PartyGoer newPartyGoer = new PartyGoer(partyGoerDetails);
                _partyGoerCache.Add(partyGoerId, newPartyGoer);

                return(newPartyGoer);
            }
        }
示例#4
0
        public async Task <PartyGoerDetails> RequestAccessAndRefreshTokenFromSpotifyAsync(string code)
        {
            List <KeyValuePair <string, string> > properties = new List <KeyValuePair <string, string> > {
                new KeyValuePair <string, string>("grant_type", "authorization_code"),
                new KeyValuePair <string, string>("code", code),
                new KeyValuePair <string, string>("redirect_uri", _spotifyAuthentication.RedirectUrl),
                new KeyValuePair <string, string>("client_id", _spotifyAuthentication.ClientId),
                new KeyValuePair <string, string>("client_secret", _spotifyAuthentication.ClientSecret)
            };

            HttpResponseMessage response = null;

            using (var requestMessage = new HttpRequestMessage(_apiEndpoints[ApiEndpointType.Token].HttpMethod, _apiEndpoints[ApiEndpointType.Token].EndpointUrl))
            {
                requestMessage.Content = new FormUrlEncodedContent(properties);

                response = await _httpClient.SendAsync(requestMessage);
            }

            if (response is null)
            {
                throw new Exception("The response from requesting the access and refresh token was null");
            }
            // TODO: Add logic to know if it spotifys problem or ours
            if (response.IsSuccessStatusCode)
            {
                JObject accessTokenBody = JObject.Parse(await response.Content.ReadAsStringAsync());

                string accessToken = accessTokenBody["access_token"].ToString();

                PartyGoerDetails details = await GetCurrentUserIdAsync(accessToken);

                await _spotifyAuthentication.AddAuthenticatedPartyGoerAsync(details.Id, accessToken,
                                                                            accessTokenBody["refresh_token"].ToString(),
                                                                            Convert.ToInt32(accessTokenBody["expires_in"])
                                                                            );

                return(details);
            }

            return(null);
        }
示例#5
0
        private async Task RequestNewAccessToken(string refreshToken)
        {
            List <KeyValuePair <string, string> > properties = new List <KeyValuePair <string, string> > {
                new KeyValuePair <string, string>("grant_type", "refresh_token"),
                new KeyValuePair <string, string>("refresh_token", refreshToken),
                new KeyValuePair <string, string>("client_id", _spotifyAuthentication.ClientId),
                new KeyValuePair <string, string>("client_secret", _spotifyAuthentication.ClientSecret)
            };

            HttpResponseMessage response = null;

            using (var requestMessage = new HttpRequestMessage(_apiEndpoints[ApiEndpointType.Token].HttpMethod, _apiEndpoints[ApiEndpointType.Token].EndpointUrl))
            {
                requestMessage.Content = new FormUrlEncodedContent(properties);

                response = await _httpClient.SendAsync(requestMessage);
            }

            if (response is null)
            {
                throw new Exception("The response from request a new access token with a refresh token was null");
            }

            if (response.IsSuccessStatusCode)
            {
                JObject accessTokenBody = JObject.Parse(await response.Content.ReadAsStringAsync());

                string accessToken = accessTokenBody["access_token"].ToString();

                PartyGoerDetails details = await GetCurrentUserIdAsync(accessToken);

                await _spotifyAuthentication.RefreshAccessTokenForPartyGoerAsync(details.Id, accessToken,
                                                                                 Convert.ToInt32(accessTokenBody["expires_in"])
                                                                                 );
            }
        }