Пример #1
0
        private async Task CreateSelfSignedCertificateAsync(GeneralTrackCertificateViewModel generalCertificate)
        {
            generalCertificate.Form.ClearError();
            try
            {
                var trackKeyResponse = await TrackService.UpdateTrackKeyContainedAsync(generalCertificate.Form.Model.Map <TrackKeyItemContainedRequest>(afterMap: afterMap =>
                {
                    afterMap.CreateSelfSigned = true;
                    afterMap.Key = null;
                }));

                var keyResponse = generalCertificate.Form.Model.IsPrimary ? trackKeyResponse.PrimaryKey : trackKeyResponse.SecondaryKey;

                generalCertificate.Subject    = keyResponse.CertificateInfo.Subject;
                generalCertificate.ValidFrom  = keyResponse.CertificateInfo.ValidFrom;
                generalCertificate.ValidTo    = keyResponse.CertificateInfo.ValidTo;
                generalCertificate.IsValid    = keyResponse.CertificateInfo.IsValid();
                generalCertificate.Thumbprint = keyResponse.CertificateInfo.Thumbprint;
                generalCertificate.CreateMode = false;
                generalCertificate.Edit       = false;
            }
            catch (TokenUnavailableException)
            {
                await(OpenidConnectPkce as TenantOpenidConnectPkce).TenantLoginAsync();
            }
            catch (HttpRequestException ex)
            {
                generalCertificate.Form.SetError(ex.Message);
            }
            catch (FoxIDsApiException aex)
            {
                generalCertificate.Form.SetError(aex.Message);
            }
        }
Пример #2
0
        private async Task OnEditCertificateValidSubmitAsync(GeneralTrackCertificateViewModel generalCertificate, EditContext editContext)
        {
            try
            {
                if (generalCertificate.Form.Model.Key == null)
                {
                    throw new ArgumentNullException("Model.Key");
                }

                _ = await TrackService.UpdateTrackKeyContainedAsync(generalCertificate.Form.Model.Map <TrackKeyItemContainedRequest>());

                generalCertificate.Subject    = generalCertificate.Form.Model.Subject;
                generalCertificate.ValidFrom  = generalCertificate.Form.Model.ValidFrom;
                generalCertificate.ValidTo    = generalCertificate.Form.Model.ValidTo;
                generalCertificate.IsValid    = generalCertificate.Form.Model.IsValid;
                generalCertificate.Thumbprint = generalCertificate.Form.Model.Thumbprint;
                generalCertificate.CreateMode = false;
                generalCertificate.Edit       = false;
            }
            catch (FoxIDsApiException ex)
            {
                if (ex.StatusCode == System.Net.HttpStatusCode.Conflict)
                {
                    generalCertificate.Form.SetFieldError(nameof(generalCertificate.Form.Model.Key), ex.Message);
                }
                else
                {
                    throw;
                }
            }
        }
Пример #3
0
 private void ShowUpdateCertificate(GeneralTrackCertificateViewModel generalCertificate)
 {
     generalCertificate.CreateMode        = false;
     generalCertificate.DeleteAcknowledge = false;
     generalCertificate.ShowAdvanced      = false;
     generalCertificate.Error             = null;
     generalCertificate.Edit = true;
 }
Пример #4
0
        private async Task OnCertificateFileSelectedAsync(GeneralTrackCertificateViewModel generalCertificate, IFileListEntry[] files)
        {
            try
            {
                generalCertificate.Form.ClearFieldError(nameof(generalCertificate.Form.Model.Key));
                foreach (var file in files)
                {
                    if (file.Size > GeneralTrackCertificateViewModel.CertificateMaxFileSize)
                    {
                        generalCertificate.Form.SetFieldError(nameof(generalCertificate.Form.Model.Key), $"That's too big. Max size: {GeneralTrackCertificateViewModel.CertificateMaxFileSize} bytes.");
                        return;
                    }

                    generalCertificate.CertificateFileStatus = "Loading...";

                    byte[] certificateBytes;
                    using (var memoryStream = new MemoryStream())
                    {
                        await file.Data.CopyToAsync(memoryStream);

                        certificateBytes = memoryStream.ToArray();
                    }

                    var base64UrlEncodeCertificate = WebEncoders.Base64UrlEncode(certificateBytes);
                    var jwtWithCertificateInfo     = await HelpersService.ReadCertificateAsync(new CertificateAndPassword { EncodeCertificate = base64UrlEncodeCertificate, Password = generalCertificate.Form.Model.Password });

                    if (!jwtWithCertificateInfo.HasPrivateKey())
                    {
                        generalCertificate.Form.Model.Subject = null;
                        generalCertificate.Form.Model.Key     = null;
                        generalCertificate.Form.SetFieldError(nameof(generalCertificate.Form.Model.Key), "Private key is required. Maybe a password is required to unlock the private key.");
                        generalCertificate.CertificateFileStatus = GeneralTrackCertificateViewModel.DefaultCertificateFileStatus;
                        return;
                    }

                    generalCertificate.Form.Model.Subject    = jwtWithCertificateInfo.CertificateInfo.Subject;
                    generalCertificate.Form.Model.ValidFrom  = jwtWithCertificateInfo.CertificateInfo.ValidFrom;
                    generalCertificate.Form.Model.ValidTo    = jwtWithCertificateInfo.CertificateInfo.ValidTo;
                    generalCertificate.Form.Model.IsValid    = jwtWithCertificateInfo.CertificateInfo.IsValid();
                    generalCertificate.Form.Model.Thumbprint = jwtWithCertificateInfo.CertificateInfo.Thumbprint;
                    generalCertificate.Form.Model.Key        = jwtWithCertificateInfo;

                    generalCertificate.CertificateFileStatus = GeneralTrackCertificateViewModel.DefaultCertificateFileStatus;
                }
            }
            catch (TokenUnavailableException)
            {
                await(OpenidConnectPkce as TenantOpenidConnectPkce).TenantLoginAsync();
            }
            catch (HttpRequestException ex)
            {
                generalCertificate.Form.SetFieldError(nameof(generalCertificate.Form.Model.Key), ex.Message);
            }
            catch (FoxIDsApiException aex)
            {
                generalCertificate.Form.SetFieldError(nameof(generalCertificate.Form.Model.Key), aex.Message);
            }
        }
Пример #5
0
        private void CertificateViewModelAfterInit(GeneralTrackCertificateViewModel generalCertificate, TrackCertificateInfoViewModel model)
        {
            model.IsPrimary = generalCertificate.IsPrimary;

            if (generalCertificate.Edit)
            {
                model.Subject    = generalCertificate.Subject;
                model.ValidFrom  = generalCertificate.ValidFrom;
                model.ValidTo    = generalCertificate.ValidTo;
                model.IsValid    = generalCertificate.IsValid;
                model.Thumbprint = generalCertificate.Thumbprint;
            }
        }
Пример #6
0
        private async Task OnCertificateFileSelectedAsync(GeneralTrackCertificateViewModel generalCertificate, IFileListEntry[] files)
        {
            generalCertificate.Form.ClearFieldError(nameof(generalCertificate.Form.Model.Key));
            foreach (var file in files)
            {
                if (file.Size > GeneralTrackCertificateViewModel.CertificateMaxFileSize)
                {
                    generalCertificate.Form.SetFieldError(nameof(generalCertificate.Form.Model.Key), $"That's too big. Max size: {GeneralTrackCertificateViewModel.CertificateMaxFileSize} bytes.");
                    return;
                }

                generalCertificate.CertificateFileStatus = "Loading...";

                using (var memoryStream = new MemoryStream())
                {
                    await file.Data.CopyToAsync(memoryStream);

                    try
                    {
                        var certificate = new X509Certificate2(memoryStream.ToArray());
                        var jwk         = await certificate.ToFTJsonWebKeyAsync(true);

                        if (!jwk.HasPrivateKey())
                        {
                            generalCertificate.Form.SetFieldError(nameof(generalCertificate.Form.Model.Key), "Private key is required.");
                            return;
                        }

                        generalCertificate.Form.Model.Subject    = certificate.Subject;
                        generalCertificate.Form.Model.ValidFrom  = certificate.NotBefore;
                        generalCertificate.Form.Model.ValidTo    = certificate.NotAfter;
                        generalCertificate.Form.Model.IsValid    = certificate.IsValid();
                        generalCertificate.Form.Model.Thumbprint = certificate.Thumbprint;
                        generalCertificate.Form.Model.Key        = jwk;
                    }
                    catch (Exception ex)
                    {
                        generalCertificate.Form.SetFieldError(nameof(generalCertificate.Form.Model.Key), ex.Message);
                    }
                }

                generalCertificate.CertificateFileStatus = GeneralSamlUpPartyViewModel.DefaultCertificateFileStatus;
            }
        }
Пример #7
0
        private async Task DeleteSecondaryCertificateAsync(GeneralTrackCertificateViewModel generalCertificate)
        {
            try
            {
                await TrackService.DeleteTrackKeyContainedAsync();

                generalCertificate.CreateMode         = true;
                generalCertificate.Edit               = false;
                generalCertificate.Subject            = null;
                generalCertificate.Form.Model.Subject = null;
            }
            catch (TokenUnavailableException)
            {
                await(OpenidConnectPkce as TenantOpenidConnectPkce).TenantLoginAsync();
            }
            catch (Exception ex)
            {
                generalCertificate.Form.SetError(ex.Message);
            }
        }
Пример #8
0
 private void CertificateCancel(GeneralTrackCertificateViewModel generalCertificate)
 {
     generalCertificate.Edit = false;
 }