Пример #1
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);
            }
        }
Пример #2
0
        private async Task OnSamlUpPartyCertificateFileSelectedAsync(GeneralSamlUpPartyViewModel generalSamlUpParty, IFileListEntry[] files)
        {
            if (generalSamlUpParty.Form.Model.Keys == null)
            {
                generalSamlUpParty.Form.Model.Keys = new List <JwtWithCertificateInfo>();
            }
            generalSamlUpParty.Form.ClearFieldError(nameof(generalSamlUpParty.Form.Model.Keys));
            foreach (var file in files)
            {
                if (file.Size > GeneralSamlUpPartyViewModel.CertificateMaxFileSize)
                {
                    generalSamlUpParty.Form.SetFieldError(nameof(generalSamlUpParty.Form.Model.Keys), $"That's too big. Max size: {GeneralSamlUpPartyViewModel.CertificateMaxFileSize} bytes.");
                    return;
                }

                generalSamlUpParty.CertificateFileStatus = "Loading...";

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

                    try
                    {
                        var base64UrlEncodeCertificate = WebEncoders.Base64UrlEncode(memoryStream.ToArray());
                        var jwtWithCertificateInfo     = await HelpersService.ReadCertificateAsync(new CertificateAndPassword { EncodeCertificate = base64UrlEncodeCertificate });

                        if (generalSamlUpParty.Form.Model.Keys.Any(k => k.X5t.Equals(jwtWithCertificateInfo.X5t, StringComparison.OrdinalIgnoreCase)))
                        {
                            generalSamlUpParty.Form.SetFieldError(nameof(generalSamlUpParty.Form.Model.Keys), "Signature validation keys (certificates) has duplicates.");
                            return;
                        }

                        generalSamlUpParty.KeyInfoList.Add(new KeyInfoViewModel
                        {
                            Subject    = jwtWithCertificateInfo.CertificateInfo.Subject,
                            ValidFrom  = jwtWithCertificateInfo.CertificateInfo.ValidFrom,
                            ValidTo    = jwtWithCertificateInfo.CertificateInfo.ValidTo,
                            IsValid    = jwtWithCertificateInfo.CertificateInfo.IsValid(),
                            Thumbprint = jwtWithCertificateInfo.CertificateInfo.Thumbprint,
                            Key        = jwtWithCertificateInfo
                        });
                        generalSamlUpParty.Form.Model.Keys.Add(jwtWithCertificateInfo);
                    }
                    catch (Exception ex)
                    {
                        generalSamlUpParty.Form.SetFieldError(nameof(generalSamlUpParty.Form.Model.Keys), ex.Message);
                    }
                }

                generalSamlUpParty.CertificateFileStatus = GeneralSamlUpPartyViewModel.DefaultCertificateFileStatus;
            }
        }