Ejemplo n.º 1
0
        private async Task CleanupSessions(CancellationToken cancellationToken)
        {
            _log.LogInformation("Cleaning up sessions");
            var sessions = await _sessionStorage.GetAllSessions(cancellationToken);

            foreach (var session in sessions)
            {
                try
                {
                    if (session.CreatedUtc < DateTime.UtcNow.AddDays(-10) && !_inUseExternalApiTokens.Contains(session.ExternalToken))
                    {
                        try
                        {
                            await _pexApiClient.DeleteExternalToken(session.ExternalToken, cancellationToken);
                        }
                        catch (PexApiClientException ex) when(ex.Code == HttpStatusCode.Unauthorized)
                        {
                            //Proceed - Token expired or does not exist
                        }
                        catch (PexApiClientException ex) when(ex.Code == HttpStatusCode.Forbidden)
                        {
                            //Proceed - Inactive user / business closed
                        }
                        await _sessionStorage.DeleteBySessionGuidAsync(session.SessionGuid, cancellationToken);
                    }
                }
                catch (Exception e)
                {
                    _log.LogError(e, $"Exception during clean-up of session '{session.SessionGuid}'. {e}");
                }
            }
        }
        public async Task <ActionResult> DeleteSession(string sessionId, CancellationToken cancellationToken)
        {
            if (!Guid.TryParse(sessionId, out var sessionGuid))
            {
                return(BadRequest());
            }
            var modelResult = await _pexOAuthSessionStorage.GetBySessionGuidAsync(sessionGuid, cancellationToken);

            if (modelResult != null)
            {
                var mappingResult = await _pex2AplosMappingStorage.GetByBusinessAcctIdAsync(modelResult.PEXBusinessAcctId, cancellationToken);

                if (mappingResult == null)
                {
                    await _pexApiClient.DeleteExternalToken(modelResult.ExternalToken);
                }
                await _pexOAuthSessionStorage.DeleteBySessionGuidAsync(sessionGuid, cancellationToken);
            }

            return(Ok());
        }