Example #1
0
        protected override void InitializeInner(CancellationToken cancellationToken)
        {
            _logger.Info($"Started {nameof(InitializeInner)}");

            _logger.Info($"There are {_externalIdps?.Length ?? 0} external IdPs");

            foreach (var externalIdp in _externalIdps)
            {
                _logger.Info($"Initializing {JsonConvert.SerializeObject(externalIdp)}");

                try
                {
                    var provider = _dataAccessService.GetExternalIdentityProvider(externalIdp.Name);
                    if (provider == null)
                    {
                        long accountId = CreateIdentityProviderAccount(externalIdp);

                        _dataAccessService.AddExternalIdentityProvider(externalIdp.Name, externalIdp.Alias, externalIdp.Description, accountId);
                        provider = _dataAccessService.GetExternalIdentityProvider(externalIdp.Name);
                    }

                    var accountDescriptor = _accountsService.Authenticate(provider.AccountId, GetDefaultIdpPassword(provider.Name));
                    if (accountDescriptor != null)
                    {
                        _logger.Info($"Account {externalIdp.Name} authenticated successfully");

                        if (externalIdp.AttributeDefinitions != null)
                        {
                            foreach (var item in externalIdp.AttributeDefinitions)
                            {
                                long rootAttributeSchemeId = _dataAccessService.AddAttributeToScheme(accountDescriptor.PublicSpendKey.ToHexString(), item.AttributeName, item.SchemeName, item.Alias, item.Description);
                                if (item.IsRoot)
                                {
                                    _dataAccessService.ToggleOnRootAttributeScheme(rootAttributeSchemeId);
                                }
                            }
                        }

                        _executionContextManager.InitializeStateExecutionServices(accountDescriptor.AccountId, accountDescriptor.SecretSpendKey);
                    }
                    else
                    {
                        _logger.Error($"Authentication of the account {externalIdp.Name} failed");
                    }

                    _logger.Info($"Finished {nameof(InitializeInner)}");
                }
                catch (Exception ex)
                {
                    _logger.Error($"Failed to initialize the External IdP {externalIdp.Name}", ex);
                }
            }
        }