Exemple #1
0
        public IActionResult Purge()
        {
            int   athleteId = HttpContext.Session.GetInt32("AthleteId") ?? 0;
            Token token     = _context.Athletes
                              .Include(athlete => athlete.Token)
                              .FirstOrDefault(athlete => athlete.AthleteId == athleteId)
                              .Token;

            // If token expires with one hour, refresh token
            if (token.ExpiresAt < DateTime.Now.AddSeconds(3600))
            {
                AuthorizationModel tokenModel = StravaController
                                                .loadNewToken(token.RefreshToken).Result;
                token.AccessToken  = tokenModel.Access_Token;
                token.RefreshToken = tokenModel.Refresh_Token;
                token.ExpiresAt    = DateTime.Now.AddSeconds(tokenModel.Expires_In);
                token.ExpiresIn    = TimeSpan.FromSeconds(tokenModel.Expires_In);
            }
            Processor.Deauthorization(token.AccessToken);
            Athlete athleteToDelete = _context.Athletes
                                      .FirstOrDefault(athlete => athlete.AthleteId == athleteId);

            _context.Remove(athleteToDelete);
            _context.Remove(token);
            _context.SaveChanges();
            HttpContext.Session.Clear();
            return(RedirectToAction("Index", "Home"));
        }
        public IActionResult ExchangeToken(string state, string code, string scope)
        {
            Console.WriteLine($"Exchange Token State: {state}");
            Console.WriteLine($"Exchange Token Code: {code}");
            Console.WriteLine($"Exchange Token Scope: {scope}");

            // send post request to strava api with code
            // to receive the access token and refresh token
            AuthorizationModel authModel = loadAuthorization(code).Result;

            // Extract token info from authModel
            var newToken = new Token();

            newToken.ExpiresAt    = DateTime.Now.AddSeconds(authModel.Expires_In);
            newToken.ExpiresIn    = TimeSpan.FromSeconds(authModel.Expires_In);
            newToken.RefreshToken = authModel.Refresh_Token;
            newToken.AccessToken  = authModel.Access_Token;

            // Query database for athlete that has a matching Strava Id
            Athlete dbAthlete = _context.Athletes
                                .FirstOrDefault(athlete => athlete.Id == authModel.Athlete.Id);
            int athleteId;

            if (dbAthlete == null)
            {
                athleteId = CreateAthlete(authModel.Athlete, newToken);
            }
            else
            {
                athleteId = dbAthlete.AthleteId;
                Token dbToken = _context.Tokens
                                .FirstOrDefault(token => token.TokenId == dbAthlete.TokenId);
                dbToken.ExpiresAt    = newToken.ExpiresAt;
                dbToken.ExpiresIn    = newToken.ExpiresIn;
                dbToken.RefreshToken = newToken.RefreshToken;
                dbToken.AccessToken  = newToken.AccessToken;
                _context.SaveChanges();
            }
            HttpContext.Session.SetInt32("AthleteId", athleteId);

            return(RedirectToAction("Home", "Banana"));
        }