Authenticate.cpp 14 KB

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