private void btnCreateSignerIdentity_Click(object sender, EventArgs e) { if (ValidateInputs()) { AuthTokenDto auth = null; if (txtTenantName.Enabled) { var auths = SnapInContext.Instance.AuthTokenManager.GetAuthTokens(_serverDto); auth = auths[0]; } else { auth = SnapInContext.Instance.AuthTokenManager.GetAuthToken(_serverDto, _tenantName); } ActionHelper.Execute(delegate { // Get private key var keyText = File.ReadAllText(txtKeyFile.Text); keyText = PrivateKeyHelper.ExtractBase64EncodedPayload(keyText); EncrptionAlgorithm algo; if (cbAlgo.SelectedIndex > -1) { Enum.TryParse(cbAlgo.SelectedItem.ToString(), false, out algo); } else { algo = EncrptionAlgorithm.RSA; } var privatekey = new PrivateKeyDto { Encoded = keyText, Algorithm = algo }; // Get all certificates var certs = new List <CertificateDto>(); foreach (var certificate in GetCertificateChain()) { var cert = new X509Certificate2(); cert.Import(certificate); var certDto = new CertificateDto { Encoded = cert.ExportToPem() }; certs.Add(certDto); } var tenantCredentials = new TenantCredentialsDto { Certificates = certs, PrivateKey = privatekey }; if (txtTenantName.Enabled) { var tenantDto = new TenantDto() { Name = txtTenantName.Text, Credentials = tenantCredentials, Username = txtUsername.Text, Password = txtPassword.Text }; _tenantDto = _service.Tenant.Create(_serverDto, tenantDto, auth.Token); } else { _service.Certificate.SetTenantCredentials(_serverDto, _tenantName, tenantCredentials, auth.Token); } }, auth); this.DialogResult = DialogResult.OK; } }
public override void AwakeFromNib() { base.AwakeFromNib(); _certs = new List <string> (); TxtTenantName.Enabled = !UpdateCredentials; txtUsername.Enabled = !UpdateCredentials; TxtPassword.Enabled = !UpdateCredentials; if (UpdateCredentials) { TxtTenantName.StringValue = TenantDto.Name; } else { TenantDto = new TenantDto(); } TenantDto.Credentials = new TenantCredentialsDto() { Certificates = new List <CertificateDto>() }; BtnAddCertificate.Activated += (object sender, EventArgs e) => { var openPanel = new NSOpenPanel(); openPanel.ReleasedWhenClosed = true; openPanel.Prompt = "Select file"; var result = openPanel.RunModal(); if (result == 1) { var filePath = openPanel.Url.AbsoluteString.Replace("file://", string.Empty); var cert = new X509Certificate2(); ActionHelper.Execute(delegate() { cert.Import(filePath); _certs.Add(filePath); var certfificateDto = new CertificateDto { Encoded = cert.ExportToPem(), }; TenantDto.Credentials.Certificates.Add(certfificateDto); ReloadCertificates(); }); } }; BtnRemoveCertificate.Activated += (object sender, EventArgs e) => { if (CertificateChainTableView.SelectedRows.Count > 0) { foreach (var row in CertificateChainTableView.SelectedRows) { _certs.RemoveAt((int)row); TenantDto.Credentials.Certificates.RemoveAt((int)row); } ReloadCertificates(); } }; BtnBrowsePrivateKey.Activated += (object sender, EventArgs e) => { var openPanel = new NSOpenPanel(); openPanel.ReleasedWhenClosed = true; openPanel.Prompt = "Select file"; var result = openPanel.RunModal(); if (result == 1) { var filePath = openPanel.Url.AbsoluteString.Replace("file://", string.Empty); ActionHelper.Execute(delegate() { if (ShaWithRsaSigner.IsPrivateKeyValid(filePath)) { var text = System.IO.File.ReadAllText(filePath); var privateKey = PrivateKeyHelper.ExtractBase64EncodedPayload(text); TxtPrivateKeyPath.StringValue = filePath; TenantDto.Credentials.PrivateKey = new PrivateKeyDto() { Algorithm = EncrptionAlgorithm.RSA, Encoded = privateKey }; } else { UIErrorHelper.ShowAlert("Selected private key is not valid", "Alert"); } }); } }; BtnClose.Activated += (object sender, EventArgs e) => { TenantDto = null; this.Close(); NSApplication.SharedApplication.StopModalWithCode(0); }; this.BtnSave.Activated += (object sender, EventArgs e) => { if (!UpdateCredentials && string.IsNullOrEmpty(TxtTenantName.StringValue)) { UIErrorHelper.ShowAlert("Please enter valid tenant name", "Alert"); } else if (!UpdateCredentials && string.IsNullOrEmpty(txtUsername.StringValue)) { UIErrorHelper.ShowAlert("Please enter valid username", "Alert"); } else if (!UpdateCredentials && string.IsNullOrEmpty(TxtPassword.StringValue)) { UIErrorHelper.ShowAlert("Please enter valid password", "Alert"); } else if (string.IsNullOrEmpty(TxtPrivateKeyPath.StringValue)) { UIErrorHelper.ShowAlert("Please enter valid private key", "Alert"); } else if (_certs.Count < 2) { UIErrorHelper.ShowAlert("Please enter atleast 2 valid Certificates", "Alert"); } else { TenantDto.Name = TxtTenantName.StringValue; TenantDto.Username = txtUsername.StringValue; TenantDto.Password = TxtPassword.StringValue; this.Close(); NSApplication.SharedApplication.StopModalWithCode(1); } }; }