예제 #1
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");

            SystemSetting installSetting = new SystemSetting
            {
                Name  = INSTALLED_KEY,
                Value = "true",
            };
            SystemSetting smtpHostnameSetting = new SystemSetting
            {
                Name  = "smtp.hostname",
                Value = request.SmtpSettings.Hostname,
            };
            SystemSetting smtpUsernameSetting = new SystemSetting
            {
                Name  = "smtp.username",
                Value = request.SmtpSettings.Username,
            };
            SystemSetting smtpPasswordSetting = new SystemSetting
            {
                Name  = "smtp.password",
                Value = request.SmtpSettings.Password,
            };
            SystemSetting smtpSenderAddress = new SystemSetting
            {
                Name  = "smtp.senderAddress",
                Value = request.SmtpSettings.SenderAddress,
            };
            SystemSetting smtpPort = new SystemSetting
            {
                Name  = "smtp.port",
                Value = request.SmtpSettings.Port.ToString(),
            };

            SystemSetting tlsCertificateSetting = new SystemSetting
            {
                Name = "tls.acme.support",
            };

            if (request.TlsData != null)
            {
                tlsCertificateSetting.Value = "true";
                BackgroundJob.Enqueue <IRequestAcmeCertificateJob>(job => job.Request(request.TlsData.ContactEmail, request.TlsData.Domain));
            }
            else
            {
                tlsCertificateSetting.Value = "false";
            }

            SystemSetting primaryDomainSetting = new SystemSetting
            {
                Name  = PRIMARY_DOMAIN_KEY,
                Value = (request.PrimaryDomain != null) ? request.PrimaryDomain : context.GetHttpContext().Request.Host.Host,
            };

            _authDbContext.AddRange(installSetting, smtpHostnameSetting, smtpUsernameSetting, smtpPasswordSetting, smtpSenderAddress, tlsCertificateSetting, primaryDomainSetting);
            await _authDbContext.SaveChangesAsync();

            return(new SetupInstanceReply
            {
                Succeeded = 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,
            });
        }