public async Task <IActionResult> RunAsync( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "disconnect")] HttpRequest request, [Table("LinkedAccounts")] CloudTable table, ILogger logger, CancellationToken cancellationToken) { (ClaimsPrincipal user, SecurityToken _) = await _auth0Authenticator.AuthenticateAsync(request, logger, cancellationToken); string userId = _auth0Authenticator.GetUserId(user); logger.LogInformation($"User authenticated, user id='{userId}'"); try { LinkedAccount linkedAccount = await _linkedAccountService.GetLinkedAccountAsync(userId, table, logger); // De-authorize Strava account await DeauthorizeAsync(linkedAccount, logger, cancellationToken); // Delete linked account from table storage TableOperation operation = TableOperation.Delete(linkedAccount); await table.ExecuteAsync(operation); // todo: delete workout files from blob storage return(new OkResult()); } catch (Exception ex) { logger.LogError(ex, "Error occurred while trying to unlink Strava account"); return(new StatusCodeResult(StatusCodes.Status500InternalServerError)); } }
public async Task <IActionResult> RunAsync( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "connect")] [FromBody] Shared.Models.ConnectStravaApp connectStravaApp, HttpRequest request, [Table("LinkedAccounts")] CloudTable table, ILogger logger, CancellationToken cancellationToken) { (ClaimsPrincipal user, SecurityToken _) = await _auth0Authenticator.AuthenticateAsync(request, logger, cancellationToken); string userId = _auth0Authenticator.GetUserId(user); logger.LogInformation($"User authenticated, user id='{userId}'"); // Hit the auth0 user_info API and see what we get back about this user var authorizationHeader = AuthenticationHeaderValue.Parse(request.Headers["Authorization"]); Auth0UserInfo userInfo = await GetAuth0UserinfoAsync(authorizationHeader, logger, cancellationToken); logger.LogInformation($"Got user info from auth0: '{userInfo}'"); AccessTokenModel stravaAccessToken = await PerformCodeExchangeAsync(connectStravaApp.AuthorizationCode, userInfo, logger, cancellationToken); // Save token in the table storage LinkedAccount linkedAccount = new LinkedAccount() { IdpUserId = userInfo.UserId, IdpUserName = userInfo.Name, StravaAccountId = stravaAccessToken.Athlete.Id, FirstName = stravaAccessToken.Athlete.FirstName, LastName = stravaAccessToken.Athlete.LastName, Profile = stravaAccessToken.Athlete.Profile, TokenType = stravaAccessToken.TokenType, AccessToken = stravaAccessToken.AccessToken, ExpiresAt = stravaAccessToken.ExpiresAt, RefreshToken = stravaAccessToken.RefreshToken, }; logger.LogInformation($"Saving access token for user: '******' '{linkedAccount.IdpUserName}'. Strava user:'******' {linkedAccount.FirstName} {linkedAccount.LastName}"); linkedAccount.PartitionKey = Constants.LinkedAccountPartitionKey; linkedAccount.RowKey = linkedAccount.IdpUserId; linkedAccount.ETag = "*"; TableOperation operation = TableOperation.InsertOrMerge(linkedAccount); await table.ExecuteAsync(operation); return(new OkResult()); }
public async Task <IActionResult> RunAsync( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "userinfo")] HttpRequest request, [Table("LinkedAccounts")] CloudTable table, ILogger logger, CancellationToken cancellationToken) { try { // The "user" returned here is an actual ClaimsPrincipal with the claims that were in the access_token. // The "token" is a SecurityToken that can be used to invoke services on the part of the user. E.g., create a Google Calendar event on the user's calendar. (ClaimsPrincipal user, SecurityToken token) = await _auth0Authenticator.AuthenticateAsync(request, logger, cancellationToken); string userId = _auth0Authenticator.GetUserId(user); logger.LogInformation($"User authenticated, user id='{userId}'"); UserInfoModel userInfo = new UserInfoModel(); LinkedAccount linkedAccount = await _linkedAccountService.GetLinkedAccountAsync(userId, table, logger); if (linkedAccount != null) { // validate that the access token has not expired and renew if needed string accessToken = await GetValidStravaAccessTokenAsync(linkedAccount, table, logger, cancellationToken); if (accessToken != null) { AthleteModel athlete = await GetAthleteAsync(accessToken, linkedAccount, table, logger, cancellationToken); if (athlete != null) { logger.LogInformation($"Got athlete info from Strava: '{athlete}'"); userInfo.FirstName = athlete.FirstName; userInfo.LastName = athlete.LastName; userInfo.Country = athlete.Country; userInfo.City = athlete.City; userInfo.PictureUrl = athlete.Profile; userInfo.IsStravaAccountLinked = true; } AthleteStatsModel stats = await GetAthleteStatsAsync(accessToken, linkedAccount, logger, cancellationToken); if (stats != null) { logger.LogInformation($"Got athlete stats from Strava: '{stats}'"); userInfo.Runs = stats.AllRunsTotals.Count; userInfo.Swims = stats.AllSwimsTotals.Count; userInfo.Rides = stats.AllRidesTotals.Count; } } } return(new OkObjectResult(userInfo)); } catch (AuthException) { return(new UnauthorizedResult()); } }