public async Task <StartPlatformOauthConnectionResultViewModel> StartConnectUserToOauthPlatform(
            StartPlatformConnectionOauthModel model, CancellationToken cancellationToken)
        {
            var uniqueUserIdentifier = _httpContextAccessor.HttpContext.User.Identity.Name;

            using var session = _documentStore.OpenAsyncSession();
            var existingUser =
                await _userManager.GetUserByUniqueIdentifier(uniqueUserIdentifier, session, cancellationToken);

            var app = await _appManager.GetAppFromApplicationId(model.ApplicationId, session, cancellationToken);

            var(authorizationResult, platformConnection, platformIntegrationType) = await _platformConnectionManager.StartConnectUserToOauthPlatform(model.PlatformId,
                                                                                                                                                     existingUser, app, model.PlatformDataClaim,
                                                                                                                                                     model.CallbackUri, session, cancellationToken);

            await session.SaveChangesAsync(cancellationToken);

            if (platformConnection != null)
            {
                await _platformManager.TriggerDataFetch(existingUser.Id, platformConnection, platformIntegrationType, _bus);

                await session.SaveChangesAsync(cancellationToken);
            }

            return(new StartPlatformOauthConnectionResultViewModel(authorizationResult.State,
                                                                   authorizationResult.OAuthAuthenticationUrl));
        }
예제 #2
0
        public async Task <IActionResult> InitiateDataFetch(Guid platformId, CancellationToken cancellationToken)
        {
            var uniqueUserIdentifier = _httpContextAccessor.HttpContext.User.Identity.Name;

            using var session = _documentStore.OpenAsyncSession();
            var user = await _userManager.GetUserByUniqueIdentifier(uniqueUserIdentifier, session, cancellationToken);

            var platform = await _platformManager.GetPlatformByExternalId(platformId, session, cancellationToken);

            var platformConnection = user.PlatformConnections.SingleOrDefault(pc => pc.PlatformId == platform.Id);

            if (platformConnection == null)
            {
                throw new UserPlatformConnectionDoesNotExistException(platform.ExternalId,
                                                                      $"User with id {user.ExternalId} does not have access to platform");
            }

            await _platformManager.TriggerDataFetch(user.Id, platformConnection, platform.IntegrationType, _bus);

            await session.SaveChangesAsync(cancellationToken);

            return(Ok("Data fetch initiated"));
        }
예제 #3
0
        public async Task Handle(PlatformDataFetcherTriggerMessage message)
        {
            using var _ = _logger.BeginNamedScopeWithMessage(nameof(DataFetchCompleteHandler),
                                                             _messageContext.Message.GetMessageId());

            _logger.LogInformation("Will check if any platform connection is up for data fetching.");

            var cancellationToken = _messageContext.GetCancellationToken();

            using var session = _documentStore.OpenAsyncSession();
            var platformConnectionsToFetchDataForPerUser =
                await GetPlatformConnectionsReadyForDataFetch(session, cancellationToken);

            var platforms = await _platformManager.GetPlatforms(
                platformConnectionsToFetchDataForPerUser.SelectMany(kvp => kvp.Value).Select(pc => pc.PlatformId).Distinct()
                .ToList(), session, cancellationToken);

            _logger.LogInformation(
                "Found {NoOfUsers} users that have at least one platform connection to trigger data fetch for.", platformConnectionsToFetchDataForPerUser.Count);

            foreach (var kvp in platformConnectionsToFetchDataForPerUser)
            {
                var userId = kvp.Key;
                using var __ = _logger.BeginPropertyScope((LoggerPropertyNames.UserId, userId));

                _logger.LogInformation("Will trigger data fetches for {NoOfPlatformConnections} distinct platforms for user.", kvp.Value.Count());

                foreach (var platformConnection in kvp.Value)
                {
                    using var ___ = _logger.BeginPropertyScope(
                              (LoggerPropertyNames.PlatformId, platformConnection.PlatformId),
                              (LoggerPropertyNames.PlatformName, platformConnection.PlatformName));

                    _logger.LogInformation(
                        "Will trigger data fetch for platform. LastSuccessfulDataFetch: {LastSuccessfulDataFetch}", platformConnection.LastSuccessfulDataFetch);

                    var platform = platforms[platformConnection.PlatformId];
                    await _platformManager.TriggerDataFetch(userId, platformConnection, platform.IntegrationType, _bus);
                }
            }

            await session.SaveChangesAsync(cancellationToken);
        }
예제 #4
0
        public async Task <IActionResult> PromptCallback([FromQuery(Name = "prompt_id")] Guid promptId, [FromQuery(Name = "accept")] bool accept,
                                                         CancellationToken cancellationToken)
        {
            using var session = _documentStore.OpenAsyncSession();
            var prompt = await _emailValidatorManager.CompleteEmailValidation(promptId, accept, session, cancellationToken);

            var user = await session.LoadAsync <User>(prompt.UserId, cancellationToken);

            var platformConnectionInfos = new Dictionary <string, (PlatformConnection PlatformConnection, PlatformIntegrationType PlatformIntegrationType)>();

            if (prompt.Result.HasValue)
            {
                var userEmail = user.UserEmails.Single(ue => ue.Email == prompt.EmailAddress);
                userEmail.SetEmailState(prompt.Result.Value ? UserEmailState.Verified : UserEmailState.Unverified);

                if (userEmail.UserEmailState == UserEmailState.Verified)
                {
                    var appIds = prompt.PlatformIdToAppId.Values.SelectMany(v => v).Distinct();
                    var apps   = await session.LoadAsync <App>(appIds, cancellationToken);

                    foreach (var platformId in prompt.PlatformIdToAppId.Keys)
                    {
                        Core.Entities.Platform platform;
                        if (platformId != "None")
                        {
                            platform = await _platformManager.GetPlatform(platformId, session, cancellationToken);
                        }
                        else
                        {
                            continue;
                        }

                        var appIdsToNotify = new List <string>();
                        foreach (var appId in prompt.PlatformIdToAppId[platformId])
                        {
                            var app = apps[appId];

                            var(_, platformConnection, _) = await _platformConnectionManager.ConnectUserToEmailPlatform(platform.ExternalId, user,
                                                                                                                        app,
                                                                                                                        prompt.EmailAddress, _emailVerificationConfiguration.AcceptUrl,
                                                                                                                        _emailVerificationConfiguration.DeclineUrl, prompt.PlatformDataClaim, session, true,
                                                                                                                        cancellationToken);

                            if (!platformConnectionInfos.ContainsKey(platformId))
                            {
                                platformConnectionInfos.Add(platformId, (platformConnection, platform.IntegrationType));
                            }


                            if (appIdsToNotify.All(aid => aid != appId))
                            {
                                appIdsToNotify.Add(appId);
                            }
                        }

                        if (appIdsToNotify.Any())
                        {
                            await _appNotificationManager.NotifyPlatformConnectionDataUpdate(user.Id, appIdsToNotify, platform.Id,
                                                                                             session, cancellationToken);
                        }
                    }
                }
            }

            await session.SaveChangesAsync(cancellationToken);

            foreach (var platformId in platformConnectionInfos.Keys)
            {
                var info = platformConnectionInfos[platformId];
                if (info.PlatformConnection == null)
                {
                    continue;
                }

                await _platformManager.TriggerDataFetch(user.Id, info.PlatformConnection, info.PlatformIntegrationType, _bus);
            }

            await session.SaveChangesAsync(cancellationToken);

            return(Ok());
        }