Exemple #1
0
        public CertificateAuthorityModule()
        {
            this.RequiresAuthentication();

            Get["/ca/ssl/status"] = x => Response.AsJson(ApplicationSetting.Ssl());

            Post["/ca/ssl/toggle"] = x => {
                if (ApplicationSetting.Ssl() == "yes")
                {
                    ApplicationSetting.DisableSsl();
                    return(Response.AsJson(true));
                }
                ApplicationSetting.EnableSsl();
                return(Response.AsJson(true));
            };

            Post["/ca/ssl/enable"] = x => {
                ApplicationSetting.EnableSsl();
                return(Response.AsJson(true));
            };

            Post["/ca/ssl/disable"] = x => {
                ApplicationSetting.DisableSsl();
                return(Response.AsJson(true));
            };

            Post["/ca/setup"] = x => {
                var caDirectory          = (string)Request.Form.CaDirectory;
                var caCountry            = (string)Request.Form.CaCountry;
                var caProvince           = (string)Request.Form.CaProvince;
                var caLocality           = (string)Request.Form.CaLocality;
                var caOrganization       = (string)Request.Form.CaOrganization;
                var caOrganizationalUnit = (string)Request.Form.CaOrganizationalUnit;
                var caCommonName         = (string)Request.Form.CaCommonName;
                var caEmail      = (string)Request.Form.CaEmail;
                var caPassphrase = (string)Request.Form.CaPassphrase;
                CertificateAuthority.Setup(caDirectory, caPassphrase, caCountry, caProvince, caLocality, caOrganization, caOrganizationalUnit, caCommonName, caEmail);
                return(Response.AsJson(true));
            };

            Post["/ca/certificate/new"] = x => {
                var countryName = ((string)Request.Form.CountryName).Length < 1 ? "." : (string)Request.Form.CountryName;
                if (countryName.Length > 2)
                {
                    countryName = countryName.Substring(0, 2).ToUpper();
                }
                var stateProvinceName      = ((string)Request.Form.StateProvinceName).Length < 1 ? "." : (string)Request.Form.StateProvinceName;
                var localityName           = ((string)Request.Form.LocalityName).Length < 1 ? "." : (string)Request.Form.LocalityName;
                var organizationName       = ((string)Request.Form.OrganizationName).Length < 1 ? "." : (string)Request.Form.OrganizationName;
                var organizationalUnitName = ((string)Request.Form.OrganizationalUnitName).Length < 1 ? "." : (string)Request.Form.OrganizationalUnitName;
                var commonName             = ((string)Request.Form.CommonName).Length < 1 ? "*" : (string)Request.Form.CommonName;
                var emailAddress           = ((string)Request.Form.EmailAddress).Length < 1 ? "." : (string)Request.Form.EmailAddress;
                var password     = ((string)Request.Form.Password).Length < 1 ? "" : (string)Request.Form.Password;
                var bytesLength  = ((string)Request.Form.BytesLength).Length < 1 ? "2048" : (string)Request.Form.BytesLength;
                var assignment   = ((string)Request.Form.Assignment.Value).Length < 1 ? CertificateAssignment.User : CertificateAssignmentType.Detect((string)Request.Form.Assignment.Value);
                var userGuid     = ((string)Request.Form.UserGuid).Length < 1 ? "" : (string)Request.Form.UserGuid;
                var serviceGuid  = ((string)Request.Form.ServiceGuid).Length < 1 ? "" : (string)Request.Form.ServiceGuid;
                var serviceAlias = ((string)Request.Form.ServiceAlias).Length < 1 ? "" : (string)Request.Form.ServiceAlias;
                CertificateAuthority.Certificate.Create(countryName, stateProvinceName, localityName, organizationName, organizationalUnitName, commonName, emailAddress, password, assignment, bytesLength, userGuid, serviceGuid, serviceAlias);
                return(Response.AsRedirect("/"));
            };

            Get["/ca/certificate/download/{format}/{guid}"] = x => {
                var guid        = (string)x.guid;
                var certificate = CertificateRepository.GetByGuid(guid);
                if (certificate == null)
                {
                    return(HttpStatusCode.InternalServerError);
                }
                string path;
                var    format = (string)x.format;
                switch (format)
                {
                case "der":
                    path = certificate.CertificateDerPath;
                    break;

                case "pfx":
                    path = certificate.CertificatePfxPath;
                    break;

                default:
                    path = certificate.CertificatePath;
                    break;
                }
                var file     = new FileStream(path, FileMode.Open);
                var fileName = Path.GetFileName(certificate.CertificatePath);
                var response = new StreamResponse(() => file, MimeTypes.GetMimeType(fileName));
                return(response.AsAttachment(fileName));
            };
        }