示例#1
0
        public override async Task <ChangeSmtpSettingsReply> ChangeSmtpSettings(SmtpSettingsMessage request, ServerCallContext context)
        {
            SmtpCredentials credentials = new SmtpCredentials
            {
                Hostname      = request.Hostname,
                Password      = request.Password,
                Port          = request.Port,
                SenderAddress = request.SenderAddress,
                Username      = request.Username
            };
            await _smtpCredentialManager.StoreCredentialsAsync(credentials);

            return(new ChangeSmtpSettingsReply
            {
                Success = true,
            });
        }
示例#2
0
        public override async Task <SetupInstanceReply> SetupInstance(SetupInstanceRequest request, ServerCallContext context)
        {
            bool isInstalled = await IsAlreadyInstalled();

            string existingAuthKey = await GetSetupAuthKey() ?? "";

            bool authKeysMatch = CryptographicOperations.FixedTimeEquals(Encoding.ASCII.GetBytes(existingAuthKey), Encoding.ASCII.GetBytes(request.AuthToken));

            if (isInstalled || existingAuthKey == "" || !authKeysMatch)
            {
                return(new SetupInstanceReply
                {
                    ErrorMessage = "Installation failed for security reasons.",
                    Succeeded = false,
                });
            }

            AppUser user = new AppUser
            {
                EmailConfirmed = true,
                UserName       = request.AccountData.Username,
                Email          = request.AccountData.Email,
            };

            await _userManager.CreateAsync(user, request.AccountData.Password);

            await _userManager.AddToRoleAsync(user, "admin");

            SmtpCredentials smtpCredentials = new SmtpCredentials
            {
                Hostname      = request.SmtpSettings.Hostname,
                Username      = request.SmtpSettings.Username,
                Password      = request.SmtpSettings.Password,
                SenderAddress = request.SmtpSettings.SenderAddress,
                Port          = request.SmtpSettings.Port
            };
            await _smtpCredentialManager.StoreCredentialsAsync(smtpCredentials);

            SystemSetting installSetting = new SystemSetting
            {
                Name  = INSTALLED_KEY,
                Value = "true",
            };

            SystemSetting?primaryDomainSetting = await _authDbContext.SystemSettings
                                                 .SingleOrDefaultAsync(s => s.Name == PRIMARY_DOMAIN_KEY);

            if (primaryDomainSetting == null)
            {
                primaryDomainSetting = new SystemSetting
                {
                    Name  = PRIMARY_DOMAIN_KEY,
                    Value = context.GetHttpContext().Request.Host.Host,
                };
                SystemSetting tlsCertificateSetting = new SystemSetting
                {
                    Name  = "tls.acme.support",
                    Value = "false"
                };
                _authDbContext.AddRange(primaryDomainSetting, tlsCertificateSetting);
            }

            string snapFolder = PathProvider.GetApplicationDataFolder();
            string primaryDomainConfigFile = snapFolder + "/primary-domain.txt";
            await File.WriteAllTextAsync(primaryDomainConfigFile, primaryDomainSetting.Value);

            if (!CertificateRepository.TryGetCertificate(primaryDomainSetting.Value, out _))
            {
                ECDsa ecdsa             = ECDsa.Create();
                CertificateRequest req  = new CertificateRequest("cn=" + primaryDomainSetting.Value, ecdsa, HashAlgorithmName.SHA256);
                X509Certificate2   cert = req.CreateSelfSigned(DateTimeOffset.Now, DateTimeOffset.Now.AddYears(15));

                CertificateRepository repository = new CertificateRepository();
                repository.StoreCertificate(primaryDomainSetting.Value, cert.Export(X509ContentType.Pfx));
            }

            _authDbContext.AddRange(installSetting);
            await _authDbContext.SaveChangesAsync();

            return(new SetupInstanceReply
            {
                Succeeded = true,
            });
        }