Authenticate.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include "Authenticate.h"
  6. #include <VCLCommon.h>
  7. #include <TextsWin.h>
  8. #include <Terminal.h>
  9. #include <CoreMain.h>
  10. //---------------------------------------------------------------------------
  11. #pragma package(smart_init)
  12. #pragma link "PasswordEdit"
  13. #ifndef NO_RESOURCES
  14. #pragma resource "*.dfm"
  15. #endif
  16. //---------------------------------------------------------------------------
  17. __fastcall TAuthenticateForm::TAuthenticateForm(TComponent * Owner)
  18. : TForm(Owner), FSessionData(NULL), FTerminal(NULL)
  19. {
  20. }
  21. //---------------------------------------------------------------------------
  22. void __fastcall TAuthenticateForm::Init(TTerminal * Terminal)
  23. {
  24. FTerminal = Terminal;
  25. FSessionData = Terminal->SessionData;
  26. UseSystemSettings(this);
  27. FShowAsModalStorage = NULL;
  28. FFocusControl = NULL;
  29. FPromptParent = InstructionsLabel->Parent;
  30. FPromptLeft = InstructionsLabel->Left;
  31. FPromptTop = InstructionsLabel->Top;
  32. FPromptRight = FPromptParent->ClientWidth - InstructionsLabel->Width - FPromptLeft;
  33. FPromptEditGap = PromptEdit1->Top - PromptLabel1->Top - PromptLabel1->Height;
  34. FPromptsGap = PromptLabel2->Top - PromptEdit1->Top - PromptEdit1->Height;
  35. ClientHeight = 270;
  36. ClearLog();
  37. }
  38. //---------------------------------------------------------------------------
  39. __fastcall TAuthenticateForm::~TAuthenticateForm()
  40. {
  41. ReleaseAsModal(this, FShowAsModalStorage);
  42. }
  43. //---------------------------------------------------------------------------
  44. void __fastcall TAuthenticateForm::ShowAsModal()
  45. {
  46. ::ShowAsModal(this, FShowAsModalStorage);
  47. }
  48. //---------------------------------------------------------------------------
  49. void __fastcall TAuthenticateForm::HideAsModal()
  50. {
  51. ::HideAsModal(this, FShowAsModalStorage);
  52. }
  53. //---------------------------------------------------------------------------
  54. void __fastcall TAuthenticateForm::WMNCCreate(TWMNCCreate & Message)
  55. {
  56. // bypass TForm::WMNCCreate to prevent disabling "resize"
  57. // (which is done for bsDialog, see comments in CreateParams)
  58. DefaultHandler(&Message);
  59. }
  60. //---------------------------------------------------------------------------
  61. void __fastcall TAuthenticateForm::DoCancel()
  62. {
  63. if (FOnCancel != NULL)
  64. {
  65. FOnCancel(this);
  66. }
  67. }
  68. //---------------------------------------------------------------------------
  69. void __fastcall TAuthenticateForm::Dispatch(void * AMessage)
  70. {
  71. TMessage & Message = *reinterpret_cast<TMessage *>(AMessage);
  72. if (Message.Msg == WM_NCCREATE)
  73. {
  74. WMNCCreate(*reinterpret_cast<TWMNCCreate *>(AMessage));
  75. }
  76. else if (Message.Msg == WM_CLOSE)
  77. {
  78. DoCancel();
  79. TForm::Dispatch(AMessage);
  80. }
  81. else
  82. {
  83. TForm::Dispatch(AMessage);
  84. }
  85. }
  86. //---------------------------------------------------------------------------
  87. void __fastcall TAuthenticateForm::CreateParams(TCreateParams & Params)
  88. {
  89. TForm::CreateParams(Params);
  90. // Allow resizing of the window, even if this is bsDialog.
  91. // This makes it more close to bsSizeable, but bsSizeable cannot for some
  92. // reason receive focus, if window is shown atop non-main window
  93. // (like editor)
  94. Params.Style = Params.Style | WS_THICKFRAME;
  95. }
  96. //---------------------------------------------------------------------------
  97. void __fastcall TAuthenticateForm::FormShow(TObject * /*Sender*/)
  98. {
  99. AdjustControls();
  100. if (FFocusControl != NULL)
  101. {
  102. ActiveControl = FFocusControl;
  103. }
  104. }
  105. //---------------------------------------------------------------------------
  106. void __fastcall TAuthenticateForm::ClearLog()
  107. {
  108. // TListItems::Clear() does nothing without allocated handle
  109. LogView->HandleNeeded();
  110. LogView->Items->Clear();
  111. }
  112. //---------------------------------------------------------------------------
  113. void __fastcall TAuthenticateForm::Log(const UnicodeString Message)
  114. {
  115. TListItem * Item = LogView->Items->Add();
  116. Item->Caption = Message;
  117. Item->MakeVisible(false);
  118. AdjustLogView();
  119. LogView->Repaint();
  120. }
  121. //---------------------------------------------------------------------------
  122. void __fastcall TAuthenticateForm::AdjustControls()
  123. {
  124. if (FStatus.IsEmpty())
  125. {
  126. Caption = FSessionData->SessionName;
  127. }
  128. else
  129. {
  130. Caption = FORMAT(L"%s - %s", (FStatus, FSessionData->SessionName));
  131. }
  132. }
  133. //---------------------------------------------------------------------------
  134. TLabel * __fastcall TAuthenticateForm::GenerateLabel(int Current, UnicodeString Caption)
  135. {
  136. TLabel * Result = new TLabel(this);
  137. Result->Parent = FPromptParent;
  138. Result->Anchors = TAnchors() << akLeft << akTop << akRight;
  139. Result->WordWrap = true;
  140. Result->AutoSize = false;
  141. Result->Top = Current;
  142. Result->Left = FPromptLeft;
  143. Result->Caption = Caption;
  144. int Width = FPromptParent->ClientWidth - FPromptLeft - FPromptRight;
  145. Result->Width = Width;
  146. Result->AutoSize = true;
  147. return Result;
  148. }
  149. //---------------------------------------------------------------------------
  150. TCustomEdit * __fastcall TAuthenticateForm::GenerateEdit(int Current, bool Echo, int MaxLen)
  151. {
  152. TCustomEdit * Result = (Echo ? static_cast<TCustomEdit *>(new TEdit(this)) :
  153. static_cast<TCustomEdit *>(new TPasswordEdit(this)));
  154. Result->Parent = FPromptParent;
  155. Result->Anchors = TAnchors() << akLeft << akTop << akRight;
  156. Result->Top = Current;
  157. Result->Left = FPromptLeft;
  158. Result->Width = FPromptParent->ClientWidth - FPromptLeft - FPromptRight;
  159. ((TEdit *)Result)->MaxLength = MaxLen;
  160. return Result;
  161. }
  162. //---------------------------------------------------------------------------
  163. TList * __fastcall TAuthenticateForm::GeneratePrompt(UnicodeString Instructions,
  164. TStrings * Prompts, TStrings * Results)
  165. {
  166. while (FPromptParent->ControlCount > 0)
  167. {
  168. delete FPromptParent->Controls[0];
  169. }
  170. TList * Result = new TList;
  171. int Current = FPromptTop;
  172. if (!Instructions.IsEmpty())
  173. {
  174. TLabel * Label = GenerateLabel(Current, Instructions);
  175. Current += Label->Height + FPromptsGap;
  176. }
  177. assert(Prompts->Count == Results->Count);
  178. for (int Index = 0; Index < Prompts->Count; Index++)
  179. {
  180. if (Index > 0)
  181. {
  182. Current += FPromptEditGap;
  183. }
  184. TLabel * Label = GenerateLabel(Current, Prompts->Strings[Index]);
  185. Current += Label->Height + FPromptEditGap;
  186. bool Echo = FLAGSET(int(Prompts->Objects[Index]), pupEcho);
  187. TCustomEdit * Edit = GenerateEdit(Current, Echo,
  188. int(Results->Objects[Index]));
  189. Result->Add(Edit);
  190. Label->FocusControl = Edit;
  191. Current += Edit->Height;
  192. }
  193. FPromptParent->ClientHeight = Current;
  194. return Result;
  195. }
  196. //---------------------------------------------------------------------------
  197. bool __fastcall TAuthenticateForm::PromptUser(TPromptKind Kind, UnicodeString Name,
  198. UnicodeString Instructions, TStrings * Prompts, TStrings * Results, bool ForceLog,
  199. bool StoredCredentialsTried)
  200. {
  201. bool Result;
  202. TList * Edits = GeneratePrompt(Instructions, Prompts, Results);
  203. try
  204. {
  205. bool ShowSessionRememberPasswordPanel = false;
  206. bool ShowSavePasswordPanel = false;
  207. TSessionData * Data = NULL;
  208. bool PasswordPrompt =
  209. ((Kind == pkPassword) || (Kind == pkTIS) || (Kind == pkCryptoCard) ||
  210. (Kind == pkKeybInteractive)) &&
  211. (Prompts->Count == 1) && FLAGCLEAR(int(Prompts->Objects[0]), pupEcho);
  212. if (PasswordPrompt && StoredCredentialsTried)
  213. {
  214. Data = StoredSessions->FindSame(FSessionData);
  215. ShowSavePasswordPanel = (Data != NULL) && !Data->Password.IsEmpty();
  216. }
  217. // do not offer to rememeber password,
  218. // if we are offering to save the password to stored session
  219. if (!ShowSavePasswordPanel &&
  220. (Prompts->Count == 1) &&
  221. FLAGSET(int(Prompts->Objects[0]), pupRemember) &&
  222. ALWAYS_TRUE(PasswordPrompt))
  223. {
  224. ShowSessionRememberPasswordPanel = true;
  225. }
  226. SavePasswordCheck->Checked = false;
  227. SavePasswordPanel->Visible = ShowSavePasswordPanel;
  228. SessionRememberPasswordCheck->Checked = false;
  229. SessionRememberPasswordPanel->Visible = ShowSessionRememberPasswordPanel;
  230. if (PasswordPanel->AutoSize)
  231. {
  232. PasswordPanel->AutoSize = false;
  233. PasswordPanel->AutoSize = true;
  234. }
  235. PasswordPanel->Realign();
  236. assert(Results->Count == Edits->Count);
  237. for (int Index = 0; Index < Edits->Count; Index++)
  238. {
  239. TCustomEdit * Edit = reinterpret_cast<TCustomEdit *>(Edits->Items[Index]);
  240. Edit->Text = Results->Strings[Index];
  241. }
  242. Result = Execute(Name, PasswordPanel,
  243. ((Edits->Count > 0) ?
  244. reinterpret_cast<TWinControl *>(Edits->Items[0]) :
  245. static_cast<TWinControl *>(PasswordOKButton)),
  246. PasswordOKButton, PasswordCancelButton, true, false, ForceLog);
  247. if (Result)
  248. {
  249. for (int Index = 0; Index < Edits->Count; Index++)
  250. {
  251. TCustomEdit * Edit = reinterpret_cast<TCustomEdit *>(Edits->Items[Index]);
  252. Results->Strings[Index] = Edit->Text;
  253. Prompts->Objects[Index] = (TObject *)
  254. ((int(Prompts->Objects[Index]) & ~pupRemember) |
  255. FLAGMASK(((Index == 0) && SessionRememberPasswordCheck->Checked), pupRemember));
  256. }
  257. if (SavePasswordCheck->Checked)
  258. {
  259. assert(Data != NULL);
  260. assert(Results->Count >= 1);
  261. FSessionData->Password = Results->Strings[0];
  262. Data->Password = Results->Strings[0];
  263. // modified only, explicit
  264. StoredSessions->Save(false, true);
  265. }
  266. }
  267. }
  268. __finally
  269. {
  270. delete Edits;
  271. }
  272. return Result;
  273. }
  274. //---------------------------------------------------------------------------
  275. void __fastcall TAuthenticateForm::Banner(const UnicodeString & Banner,
  276. bool & NeverShowAgain, int Options)
  277. {
  278. BannerMemo->Lines->Text = Banner;
  279. NeverShowAgainCheck->Visible = FLAGCLEAR(Options, boDisableNeverShowAgain);
  280. NeverShowAgainCheck->Checked = NeverShowAgain;
  281. bool Result = Execute(LoadStr(AUTHENTICATION_BANNER), BannerPanel, BannerCloseButton,
  282. BannerCloseButton, BannerCloseButton, false, true, false);
  283. if (Result)
  284. {
  285. NeverShowAgain = NeverShowAgainCheck->Checked;
  286. }
  287. }
  288. //---------------------------------------------------------------------------
  289. bool __fastcall TAuthenticateForm::Execute(UnicodeString Status, TPanel * Panel,
  290. TWinControl * FocusControl, TButton * DefaultButton, TButton * CancelButton,
  291. bool FixHeight, bool Zoom, bool ForceLog)
  292. {
  293. TAlign Align = Panel->Align;
  294. try
  295. {
  296. assert(FStatus.IsEmpty());
  297. FStatus = Status;
  298. DefaultButton->Default = true;
  299. CancelButton->Cancel = true;
  300. if (Zoom)
  301. {
  302. Panel->Align = alClient;
  303. }
  304. if (ForceLog || Visible)
  305. {
  306. if (ClientHeight < Panel->Height)
  307. {
  308. ClientHeight = Panel->Height;
  309. }
  310. // Panel being hidden gets not realigned automatically, even if it
  311. // has Align property set
  312. Panel->Top = ClientHeight - Panel->Height;
  313. Panel->Show();
  314. TCursor PrevCursor = Screen->Cursor;
  315. try
  316. {
  317. if (Zoom)
  318. {
  319. LogView->Hide();
  320. }
  321. else
  322. {
  323. if (LogView->Items->Count > 0)
  324. {
  325. TListItem * Item = LogView->ItemFocused;
  326. if (Item == NULL)
  327. {
  328. Item = LogView->Items->Item[LogView->Items->Count - 1];
  329. }
  330. Item->MakeVisible(false);
  331. }
  332. }
  333. Screen->Cursor = crDefault;
  334. if (!Visible)
  335. {
  336. assert(ForceLog);
  337. ShowAsModal();
  338. }
  339. ActiveControl = FocusControl;
  340. ModalResult = mrNone;
  341. AdjustControls();
  342. do
  343. {
  344. Application->HandleMessage();
  345. }
  346. while (!Application->Terminated && (ModalResult == mrNone));
  347. }
  348. __finally
  349. {
  350. Panel->Hide();
  351. Screen->Cursor = PrevCursor;
  352. if (Zoom)
  353. {
  354. LogView->Show();
  355. }
  356. Repaint();
  357. }
  358. }
  359. else
  360. {
  361. int PrevHeight = ClientHeight;
  362. int PrevMinHeight = Constraints->MinHeight;
  363. int PrevMaxHeight = Constraints->MaxHeight;
  364. try
  365. {
  366. Constraints->MinHeight = 0;
  367. ClientHeight = Panel->Height;
  368. if (FixHeight)
  369. {
  370. Constraints->MinHeight = Height;
  371. Constraints->MaxHeight = Height;
  372. }
  373. LogView->Hide();
  374. Panel->Show();
  375. FFocusControl = FocusControl;
  376. ShowModal();
  377. }
  378. __finally
  379. {
  380. FFocusControl = NULL;
  381. ClientHeight = PrevHeight;
  382. Constraints->MinHeight = PrevMinHeight;
  383. Constraints->MaxHeight = PrevMaxHeight;
  384. Panel->Hide();
  385. LogView->Show();
  386. }
  387. }
  388. }
  389. __finally
  390. {
  391. Panel->Align = Align;
  392. DefaultButton->Default = false;
  393. CancelButton->Cancel = false;
  394. FStatus = L"";
  395. AdjustControls();
  396. }
  397. bool Result = (ModalResult != mrCancel);
  398. if (!Result)
  399. {
  400. DoCancel();
  401. }
  402. return Result;
  403. }
  404. //---------------------------------------------------------------------------
  405. void __fastcall TAuthenticateForm::HelpButtonClick(TObject * /*Sender*/)
  406. {
  407. FormHelp(this);
  408. }
  409. //---------------------------------------------------------------------------
  410. void __fastcall TAuthenticateForm::AdjustLogView()
  411. {
  412. ListView_SetColumnWidth(LogView->Handle, 0, LVSCW_AUTOSIZE);
  413. }
  414. //---------------------------------------------------------------------------
  415. void __fastcall TAuthenticateForm::FormResize(TObject * /*Sender*/)
  416. {
  417. AdjustLogView();
  418. }
  419. //---------------------------------------------------------------------------