0
|
1 // Filename : auth.cpp
|
8
|
2 // Last Change: 2018-10-29 月 09:09:57.
|
0
|
3 //
|
|
4
|
4
|
5 #include "id.h"
|
0
|
6 #include "auth.h"
|
|
7
|
|
8 AuthDialog::AuthDialog( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style )
|
|
9 : wxDialog( parent, id, title, pos, size, style )
|
|
10 {
|
|
11 CreateControls();
|
|
12 }
|
|
13
|
|
14 AuthDialog::~AuthDialog()
|
|
15 {
|
|
16 }
|
|
17
|
|
18 // Event Table
|
|
19 BEGIN_EVENT_TABLE( AuthDialog, wxDialog )
|
3
|
20 EVT_SHOW( AuthDialog::OnShow )
|
0
|
21 EVT_TEXT( ID_UID, AuthDialog::OnCheckUserID )
|
|
22 EVT_TEXT( ID_PW, AuthDialog::OnCheckPassword )
|
|
23 EVT_TEXT_ENTER( ID_UID, AuthDialog::OnEnter )
|
|
24 EVT_TEXT_ENTER( ID_PW, AuthDialog::OnEnter )
|
3
|
25 EVT_BUTTON( wxID_OK, AuthDialog::OnEnter )
|
0
|
26 END_EVENT_TABLE()
|
|
27
|
|
28 // Event Handler
|
3
|
29 void AuthDialog::OnShow( wxShowEvent& WXUNUSED(event) )
|
|
30 {
|
|
31 }
|
|
32
|
0
|
33 void AuthDialog::OnCheckUserID( wxCommandEvent& WXUNUSED(event) )
|
|
34 {
|
|
35 wxString id = m_textCtrlId->GetValue();
|
3
|
36 for ( int i = 0; i < m_users.GetCount(); i++ ) {
|
|
37 if ( id.IsSameAs( m_users[ i ], true ) ) {
|
|
38 m_staticTextIdmsg->SetLabel( wxT( "ok" ) );
|
|
39 m_staticTextPwmsg->SetLabel( wxT( "← input" ) );
|
|
40 m_textCtrlPw->SetValue( wxEmptyString );
|
|
41 m_textCtrlPw->SetFocus();
|
|
42 return;
|
|
43 } else {
|
|
44 m_staticTextIdmsg->SetLabel( wxT( "← input" ) );
|
|
45 m_staticTextPwmsg->SetLabel( wxEmptyString );
|
|
46 }
|
|
47 }
|
0
|
48 }
|
|
49
|
|
50 void AuthDialog::OnCheckPassword( wxCommandEvent& WXUNUSED(event) )
|
|
51 {
|
|
52 wxString pw = m_textCtrlPw->GetValue();
|
|
53 if ( pw.Len() < 4 ) {
|
3
|
54 m_staticTextPwmsg->SetLabel( wxT( "← too short" ) );
|
0
|
55 }
|
|
56 else {
|
|
57 m_staticTextPwmsg->SetLabel( wxEmptyString );
|
|
58 }
|
|
59 }
|
|
60
|
|
61 void AuthDialog::OnEnter( wxCommandEvent& WXUNUSED(event) )
|
|
62 {
|
3
|
63 m_staticTextPwmsg->SetLabel( wxEmptyString );
|
|
64 if ( IsValidUser() ) {
|
|
65 m_user = m_textCtrlId->GetValue();
|
|
66 EndModal( wxID_OK );
|
|
67 }
|
|
68 m_staticTextPwmsg->SetLabel( wxT( "Bad User/Password !!" ) );
|
|
69 m_textCtrlPw->SelectAll();
|
|
70 return;
|
0
|
71 }
|
|
72
|
|
73 // Functions
|
|
74 void AuthDialog::CreateControls( void )
|
|
75 {
|
|
76 this->SetIcon( wxIcon( wxT( "sample" ) ) );
|
|
77 this->SetSizeHints( wxDefaultSize, wxDefaultSize );
|
|
78 this->SetBackgroundColour( wxColour( 0, 150, 230 ) );
|
|
79
|
|
80 wxGridSizer* gSizer = new wxGridSizer( 0, 3, 0, 0 );
|
|
81
|
|
82 // user id
|
3
|
83 m_staticTextId = new wxStaticText( this, wxID_ANY, wxT( "User ID" ), wxDefaultPosition, wxDefaultSize, 0 );
|
0
|
84 gSizer->Add( m_staticTextId, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );
|
|
85
|
|
86 m_textCtrlId = new wxTextCtrl( this, ID_UID, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER );
|
|
87 gSizer->Add( m_textCtrlId, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
|
|
88
|
|
89 m_staticTextIdmsg = new wxStaticText( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
|
|
90 m_staticTextIdmsg->SetForegroundColour( wxColour( 250, 0, 0 ) );
|
|
91 gSizer->Add( m_staticTextIdmsg, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
|
|
92
|
|
93 // password
|
3
|
94 m_staticTextPw = new wxStaticText( this, wxID_ANY, wxT( "Password" ), wxDefaultPosition, wxDefaultSize, 0 );
|
0
|
95 gSizer->Add( m_staticTextPw, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );
|
|
96
|
|
97 m_textCtrlPw = new wxTextCtrl( this, ID_PW, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_PASSWORD|wxTE_PROCESS_ENTER );
|
|
98 gSizer->Add( m_textCtrlPw, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
|
|
99
|
|
100 m_staticTextPwmsg = new wxStaticText( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
|
|
101 m_staticTextPwmsg->SetForegroundColour( wxColour( 250, 0, 0 ) );
|
|
102 gSizer->Add( m_staticTextPwmsg, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
|
3
|
103
|
|
104 // buttons
|
|
105 gSizer->AddSpacer( 1 );
|
|
106
|
|
107 m_buttonLogin = new wxButton( this, wxID_OK, wxT( "Login" ), wxDefaultPosition, wxDefaultSize, 0 );
|
|
108 gSizer->Add( m_buttonLogin, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
|
|
109
|
|
110 m_buttonCancel = new wxButton( this, wxID_CANCEL, wxT( "Cancel" ), wxDefaultPosition, wxDefaultSize, 0 );
|
|
111 gSizer->Add( m_buttonCancel, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
|
|
112
|
0
|
113 this->SetSizer( gSizer );
|
|
114 this->Layout();
|
|
115 gSizer->Fit( this );
|
|
116
|
|
117 this->Centre( wxBOTH );
|
3
|
118 m_textCtrlId->SetFocus();
|
0
|
119 }
|
|
120
|
8
|
121 /*
|
6
|
122 bool AuthDialog::GetDB( void )
|
|
123 { // http get ( auth.db, hhs.db, index.db )
|
5
|
124 wxArrayString args;
|
|
125 args.Add( wxT( "client.exe" ) );
|
|
126 args.Add( wxT( "-u" ) );
|
|
127 args.Add( m_server );
|
6
|
128 wxExecute( wxJoin( args, ' ', '\\' ), wxEXEC_SYNC|wxEXEC_HIDE_CONSOLE );
|
|
129 }
|
8
|
130 */
|
5
|
131
|
6
|
132 bool AuthDialog::LoadDB( void )
|
|
133 {
|
5
|
134 wxTextFile file;
|
|
135 if ( !file.Open( wxT( "auth.db" ) ) ) return false;
|
|
136 for ( int i = 0; i < file.GetLineCount(); i++ ) {
|
|
137 wxArrayString line = wxSplit( file.GetLine( i ), ' ', '\\' );
|
|
138 m_users.Add( line[ 0 ] );
|
|
139 }
|
|
140 file.Close();
|
|
141 return true;
|
|
142 }
|
|
143
|
3
|
144 bool AuthDialog::IsValidUser( void )
|
|
145 {
|
|
146 wxString id = m_textCtrlId->GetValue();
|
|
147 wxString pw = m_textCtrlPw->GetValue();
|
|
148
|
|
149 wxArrayString args;
|
|
150 args.Add( wxT( "crypto.exe" ) );
|
|
151 args.Add( wxT( "-c" ) );
|
|
152 args.Add( wxT( "auth.db" ) );
|
|
153 args.Add( wxT( "-s" ) );
|
|
154 args.Add( wxT( "@#!;" ) ); // salt
|
|
155 args.Add( id );
|
|
156 args.Add( pw );
|
|
157
|
|
158 int ret = wxExecute( wxJoin( args, ' ', '\\' ), wxEXEC_SYNC|wxEXEC_HIDE_CONSOLE );
|
|
159 if ( ret == 39 ) return true;
|
|
160
|
|
161 return false;
|
|
162 }
|
|
163
|
|
164 void AuthDialog::InDevelop( bool flag )
|
|
165 {
|
|
166 if ( !flag ) return;
|
|
167 m_textCtrlId->SetValue( "test" );
|
|
168 m_textCtrlPw->SetValue( "test" );
|
|
169 m_textCtrlPw->SetFocus();
|
|
170 }
|
|
171
|