source: trunk/CrypPlugins/P2PEditor/GUI/Controls/GetNewCertificate.xaml.cs @ 2276

Last change on this file since 2276 was 2276, checked in by kopal, 11 years ago
  • created gui to request a certificate for p2p
  • fixed a small bug not checking the password/worldname fields correctly before connecting to p2p
File size: 7.3 KB
Line 
1using System;
2using System.ComponentModel;
3using System.IO;
4using System.Windows;
5using System.Windows.Forms;
6using Cryptool.P2PEditor.Distributed;
7using Cryptool.P2PEditor.Worker;
8using Cryptool.PluginBase;
9using PeersAtPlay.CertificateLibrary.Network;
10using System.Threading;
11using PeersAtPlay.CertificateLibrary.Util;
12using System.Windows.Threading;
13
14namespace Cryptool.P2PEditor.GUI.Controls
15{
16    public partial class GetNewCertificate
17    {
18        public static string WorldName = ".*";
19
20        public GetNewCertificate()
21        {
22            InitializeComponent();
23        }
24
25        private void Retrieve_Click(object sender, RoutedEventArgs e)
26        {
27            if (!Verification.IsValidAvatar(this.UsernameField.Text))
28            {
29                System.Windows.MessageBox.Show("Username is not valid.", "Username is not valid");               
30                this.UsernameField.Focus();
31                this.UsernameField.SelectAll();
32                return;
33            }
34
35            if (!Verification.IsValidEmailAddress(this.EmailField.Text))
36            {
37                System.Windows.MessageBox.Show("Email is not valid.", "Email is not valid");
38                this.EmailField.Focus();
39                this.EmailField.SelectAll();
40                return;
41            }
42           
43            if (this.PasswordField.Password != this.ConfirmField.Password)
44            {
45                System.Windows.MessageBox.Show("Passwords did not match", "Passwords did not match");
46                this.PasswordField.Password = "";
47                this.ConfirmField.Password = "";
48                this.PasswordField.Focus();
49                return;
50            }   
51
52            if (!Verification.IsValidPassword(this.PasswordField.Password))
53            {
54                System.Windows.MessageBox.Show("Password is not valid.", "Password is not valid");
55                this.PasswordField.Password = "";
56                this.ConfirmField.Password = "";
57                this.PasswordField.Focus();
58                return;
59            }         
60
61            Thread thread = new Thread(new ParameterizedThreadStart(RetrieveCertificate));
62            object[] array = new object[3];
63            array[0] = this.UsernameField.Text;
64            array[1] = this.EmailField.Text;
65            array[2] = this.PasswordField.Password;
66            thread.Start(array);
67        }
68
69        public void RetrieveCertificate(object o)
70        {
71            object[] array = (object[])o;
72            string username = (string)array[0];
73            string email = (string)array[1];
74            string password = (string)array[2];
75
76            try
77            {
78                CertificateClient certificateClient = new CertificateClient();
79                certificateClient.CertificateAuthorizationDenied += new EventHandler<EventArgs>(delegate
80                {
81                    System.Windows.MessageBox.Show("Certificate authorization denied", "Certificate authorization denied");
82                });
83
84                certificateClient.CertificateAuthorizationRequired += new EventHandler<EventArgs>(delegate
85                {
86                    System.Windows.MessageBox.Show("Certificate authorization required", "Certificate authorization required");
87                });
88
89                certificateClient.CertificateNotYetAuthorized += new EventHandler<EventArgs>(delegate
90                {
91                    System.Windows.MessageBox.Show("Certificate not yet authorized", "Certificate not yet authorized");
92                });
93
94                certificateClient.CertificateReceived += CertificateReceived;               
95
96                certificateClient.InvalidCertificateRequest += InvalidCertificateRequest;
97
98                certificateClient.InvalidEmailCheck += new EventHandler<InvalidRequestEventArgs>(delegate
99                {
100                    System.Windows.MessageBox.Show("Invalid email address", "Invalid email address");
101                });
102
103                certificateClient.ServerErrorOccurred += new EventHandler<EventArgs>(delegate
104                {
105                    System.Windows.MessageBox.Show("Server error occurred. Please try again later", "Server error occurred");
106                });
107
108                certificateClient.NewProtocolVersion += new EventHandler<EventArgs>(delegate
109                {
110                    System.Windows.MessageBox.Show("The protocol of the certificate server is different from the clients one. Please update.", "Protocol mismatch");
111                });               
112
113                certificateClient.RequestCertificate(username,
114                                                     email,
115                                                     WorldName,
116                                                     password);
117            }
118            catch (Exception ex)
119            {
120                System.Windows.MessageBox.Show("An exception occured: " + ex.Message, "Exception occured");
121            }
122        }
123
124        public void InvalidCertificateRequest(object sender, InvalidRequestEventArgs args)
125        {
126            switch(args.ErrorType){
127                case RespondType.AvatarAlreadyExists:
128                    System.Windows.MessageBox.Show("The username already exists. Please chose another one.", "Username already exists");
129                    break;               
130                case RespondType.EmailAlreadyExists:
131                    System.Windows.MessageBox.Show("The email already exists. Please chose another ones.", "Email already exists");
132                    break;
133                case RespondType.AvatarEmailAlreadyExists:
134                    System.Windows.MessageBox.Show("The username and email already exist but the entered password was wrong. Either enter a new username and email combination or enter the correct password to receive the certificate again", "Username and email already exist");
135                    break;
136                default:
137                    System.Windows.MessageBox.Show("Invalid certificate request: " + args.ErrorType, "Invalid certificate request");
138                    break;
139            }           
140        }
141
142        public void CertificateReceived(object sender, CertificateReceivedEventArgs args)
143        {
144            try
145            {
146                args.Certificate.SaveCrtToAppData();
147                args.Certificate.SavePkcs12ToAppData(args.Certificate.Password);
148                System.Windows.MessageBox.Show("Certificate received and stored.", "Certificate received and stored");
149                this.Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback)delegate
150                {
151                    this.UsernameField.Text = "";
152                    this.EmailField.Text = "";
153                    this.PasswordField.Password = "";
154                    this.ConfirmField.Password = "";
155                }, null);
156            }
157            catch (Exception ex)
158            {
159                System.Windows.MessageBox.Show("Could not save the received certificate to your AppData folder:\n\n" +
160                       (ex.GetBaseException() != null && ex.GetBaseException().Message != null ? ex.GetBaseException().Message : ex.Message),
161                       "Error while saving the certificate");
162            }
163        }
164    }
165}
Note: See TracBrowser for help on using the repository browser.