Exemple #1
0
        public async Task <bool> PopulateModelAsync(ModelStateDictionary modelState, SamlUpParty mp)
        {
            var isValid = true;

            try
            {
                if (mp.UpdateState != PartyUpdateStates.Manual)
                {
                    await samlMetadataReadLogic.PopulateModelAsync(mp);

                    if (mp.UpdateState == PartyUpdateStates.AutomaticStopped)
                    {
                        mp.UpdateState = PartyUpdateStates.Automatic;
                    }
                }
            }
            catch (Exception ex)
            {
                isValid = false;
                logger.Warning(ex);
                modelState.TryAddModelError(nameof(mp.MetadataUrl).ToCamelCase(), ex.GetAllMessagesJoined());
            }
            return(isValid);
        }
        public async Task CheckMetadataAndUpdateUpPartyAsync(SamlUpParty party)
        {
            if (party.UpdateState != PartyUpdateStates.Automatic)
            {
                return;
            }

            var lastUpdated = DateTimeOffset.FromUnixTimeSeconds(party.LastUpdated);

            if (lastUpdated.AddSeconds(party.MetadataUpdateRate.Value) >= DateTimeOffset.UtcNow)
            {
                return;
            }

            var db  = redisConnectionMultiplexer.GetDatabase();
            var key = UpdateUpPartyWaitPeriodKey(party.Id);

            if (await db.KeyExistsAsync(key))
            {
                logger.ScopeTrace(() => $"Up party '{party.Id}' not updated with SAML 2.0 metadata because another update is in progress.");
                return;
            }
            else
            {
                await db.StringSetAsync(key, true, TimeSpan.FromSeconds(settings.UpPartyUpdateWaitPeriod));
            }

            var failingUpdateCount = (long?)await db.StringGetAsync(FailingUpdateUpPartyCountKey(party.Id));

            if (failingUpdateCount.HasValue && failingUpdateCount.Value >= settings.UpPartyMaxFailingUpdate)
            {
                party.UpdateState = PartyUpdateStates.AutomaticStopped;
                await tenantRepository.SaveAsync(party);

                await db.KeyDeleteAsync(FailingUpdateUpPartyCountKey(party.Id));

                return;
            }

            try
            {
                try
                {
                    await samlMetadataReadLogic.PopulateModelAsync(party);
                }
                catch (Exception ex)
                {
                    throw new EndpointException("Failed to read SAML 2.0 metadata.", ex)
                          {
                              RouteBinding = RouteBinding
                          };
                }

                await tenantRepository.SaveAsync(party);

                logger.ScopeTrace(() => $"Up party '{party.Id}' updated by SAML 2.0 metadata.", triggerEvent: true);

                await db.KeyDeleteAsync(FailingUpdateUpPartyCountKey(party.Id));
            }
            catch (Exception ex)
            {
                await db.StringIncrementAsync(FailingUpdateUpPartyCountKey(party.Id));

                logger.Warning(ex);
            }
        }