Authenticate.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. LogView->Repaint();
  119. }
  120. //---------------------------------------------------------------------------
  121. void __fastcall TAuthenticateForm::AdjustControls()
  122. {
  123. if (FStatus.IsEmpty())
  124. {
  125. Caption = FSessionData->SessionName;
  126. }
  127. else
  128. {
  129. Caption = FORMAT(L"%s - %s", (FStatus, FSessionData->SessionName));
  130. }
  131. }
  132. //---------------------------------------------------------------------------
  133. TLabel * __fastcall TAuthenticateForm::GenerateLabel(int Current, UnicodeString Caption)
  134. {
  135. TLabel * Result = new TLabel(this);
  136. Result->Parent = FPromptParent;
  137. Result->Anchors = TAnchors() << akLeft << akTop << akRight;
  138. Result->WordWrap = true;
  139. Result->AutoSize = false;
  140. Result->Top = Current;
  141. Result->Left = FPromptLeft;
  142. Result->Caption = Caption;
  143. int Width = FPromptParent->ClientWidth - FPromptLeft - FPromptRight;
  144. Result->Width = Width;
  145. Result->AutoSize = true;
  146. return Result;
  147. }
  148. //---------------------------------------------------------------------------
  149. TCustomEdit * __fastcall TAuthenticateForm::GenerateEdit(int Current, bool Echo, int MaxLen)
  150. {
  151. TCustomEdit * Result = (Echo ? static_cast<TCustomEdit *>(new TEdit(this)) :
  152. static_cast<TCustomEdit *>(new TPasswordEdit(this)));
  153. Result->Parent = FPromptParent;
  154. Result->Anchors = TAnchors() << akLeft << akTop << akRight;
  155. Result->Top = Current;
  156. Result->Left = FPromptLeft;
  157. Result->Width = FPromptParent->ClientWidth - FPromptLeft - FPromptRight;
  158. ((TEdit *)Result)->MaxLength = MaxLen;
  159. return Result;
  160. }
  161. //---------------------------------------------------------------------------
  162. TList * __fastcall TAuthenticateForm::GeneratePrompt(UnicodeString Instructions,
  163. TStrings * Prompts, TStrings * Results)
  164. {
  165. while (FPromptParent->ControlCount > 0)
  166. {
  167. delete FPromptParent->Controls[0];
  168. }
  169. TList * Result = new TList;
  170. int Current = FPromptTop;
  171. if (!Instructions.IsEmpty())
  172. {
  173. TLabel * Label = GenerateLabel(Current, Instructions);
  174. Current += Label->Height + FPromptsGap;
  175. }
  176. assert(Prompts->Count == Results->Count);
  177. for (int Index = 0; Index < Prompts->Count; Index++)
  178. {
  179. if (Index > 0)
  180. {
  181. Current += FPromptEditGap;
  182. }
  183. TLabel * Label = GenerateLabel(Current, Prompts->Strings[Index]);
  184. Current += Label->Height + FPromptEditGap;
  185. TCustomEdit * Edit = GenerateEdit(Current, bool(Prompts->Objects[Index]),
  186. int(Results->Objects[Index]));
  187. Result->Add(Edit);
  188. Label->FocusControl = Edit;
  189. Current += Edit->Height;
  190. }
  191. FPromptParent->ClientHeight = Current;
  192. return Result;
  193. }
  194. //---------------------------------------------------------------------------
  195. bool __fastcall TAuthenticateForm::PromptUser(TPromptKind Kind, UnicodeString Name,
  196. UnicodeString Instructions, TStrings * Prompts, TStrings * Results, bool ForceLog,
  197. bool StoredCredentialsTried)
  198. {
  199. bool Result;
  200. TList * Edits = GeneratePrompt(Instructions, Prompts, Results);
  201. try
  202. {
  203. bool ShowSavePasswordPanel = false;
  204. TSessionData * Data = NULL;
  205. if (((Kind == pkPassword) || (Kind == pkTIS) || (Kind == pkCryptoCard) ||
  206. (Kind == pkKeybInteractive)) &&
  207. (Prompts->Count == 1) && !bool(Prompts->Objects[0]) &&
  208. StoredCredentialsTried)
  209. {
  210. Data = StoredSessions->FindSame(FSessionData);
  211. ShowSavePasswordPanel = (Data != NULL) && !Data->Password.IsEmpty();
  212. }
  213. SavePasswordCheck->Checked = false;
  214. SavePasswordPanel->Visible = ShowSavePasswordPanel;
  215. if (PasswordPanel->AutoSize)
  216. {
  217. PasswordPanel->AutoSize = false;
  218. PasswordPanel->AutoSize = true;
  219. }
  220. PasswordPanel->Realign();
  221. assert(Results->Count == Edits->Count);
  222. for (int Index = 0; Index < Edits->Count; Index++)
  223. {
  224. TCustomEdit * Edit = reinterpret_cast<TCustomEdit *>(Edits->Items[Index]);
  225. Edit->Text = Results->Strings[Index];
  226. }
  227. Result = Execute(Name, PasswordPanel,
  228. ((Edits->Count > 0) ?
  229. reinterpret_cast<TWinControl *>(Edits->Items[0]) :
  230. static_cast<TWinControl *>(PasswordOKButton)),
  231. PasswordOKButton, PasswordCancelButton, true, false, ForceLog);
  232. if (Result)
  233. {
  234. for (int Index = 0; Index < Edits->Count; Index++)
  235. {
  236. TCustomEdit * Edit = reinterpret_cast<TCustomEdit *>(Edits->Items[Index]);
  237. Results->Strings[Index] = Edit->Text;
  238. }
  239. if (SavePasswordCheck->Checked)
  240. {
  241. assert(Data != NULL);
  242. assert(Results->Count >= 1);
  243. Data->Password = Results->Strings[0];
  244. // modified only, explicit
  245. StoredSessions->Save(false, true);
  246. }
  247. }
  248. }
  249. __finally
  250. {
  251. delete Edits;
  252. }
  253. return Result;
  254. }
  255. //---------------------------------------------------------------------------
  256. void __fastcall TAuthenticateForm::Banner(const UnicodeString & Banner,
  257. bool & NeverShowAgain, int Options)
  258. {
  259. BannerMemo->Lines->Text = Banner;
  260. NeverShowAgainCheck->Visible = FLAGCLEAR(Options, boDisableNeverShowAgain);
  261. NeverShowAgainCheck->Checked = NeverShowAgain;
  262. bool Result = Execute(LoadStr(AUTHENTICATION_BANNER), BannerPanel, BannerCloseButton,
  263. BannerCloseButton, BannerCloseButton, false, true, false);
  264. if (Result)
  265. {
  266. NeverShowAgain = NeverShowAgainCheck->Checked;
  267. }
  268. }
  269. //---------------------------------------------------------------------------
  270. bool __fastcall TAuthenticateForm::Execute(UnicodeString Status, TPanel * Panel,
  271. TWinControl * FocusControl, TButton * DefaultButton, TButton * CancelButton,
  272. bool FixHeight, bool Zoom, bool ForceLog)
  273. {
  274. TAlign Align = Panel->Align;
  275. try
  276. {
  277. assert(FStatus.IsEmpty());
  278. FStatus = Status;
  279. DefaultButton->Default = true;
  280. CancelButton->Cancel = true;
  281. if (Zoom)
  282. {
  283. Panel->Align = alClient;
  284. }
  285. if (ForceLog || Visible)
  286. {
  287. if (ClientHeight < Panel->Height)
  288. {
  289. ClientHeight = Panel->Height;
  290. }
  291. // Panel being hidden gets not realigned automatically, even if it
  292. // has Align property set
  293. Panel->Top = ClientHeight - Panel->Height;
  294. Panel->Show();
  295. TCursor PrevCursor = Screen->Cursor;
  296. try
  297. {
  298. if (Zoom)
  299. {
  300. LogView->Hide();
  301. }
  302. else
  303. {
  304. if (LogView->Items->Count > 0)
  305. {
  306. TListItem * Item = LogView->ItemFocused;
  307. if (Item == NULL)
  308. {
  309. Item = LogView->Items->Item[LogView->Items->Count - 1];
  310. }
  311. Item->MakeVisible(false);
  312. }
  313. }
  314. Screen->Cursor = crDefault;
  315. if (!Visible)
  316. {
  317. assert(ForceLog);
  318. ShowAsModal();
  319. }
  320. ActiveControl = FocusControl;
  321. ModalResult = mrNone;
  322. AdjustControls();
  323. do
  324. {
  325. Application->HandleMessage();
  326. }
  327. while (!Application->Terminated && (ModalResult == mrNone));
  328. }
  329. __finally
  330. {
  331. Panel->Hide();
  332. Screen->Cursor = PrevCursor;
  333. if (Zoom)
  334. {
  335. LogView->Show();
  336. }
  337. Repaint();
  338. }
  339. }
  340. else
  341. {
  342. int PrevHeight = ClientHeight;
  343. int PrevMinHeight = Constraints->MinHeight;
  344. int PrevMaxHeight = Constraints->MaxHeight;
  345. try
  346. {
  347. Constraints->MinHeight = 0;
  348. ClientHeight = Panel->Height;
  349. if (FixHeight)
  350. {
  351. Constraints->MinHeight = Height;
  352. Constraints->MaxHeight = Height;
  353. }
  354. LogView->Hide();
  355. Panel->Show();
  356. FFocusControl = FocusControl;
  357. ShowModal();
  358. }
  359. __finally
  360. {
  361. FFocusControl = NULL;
  362. ClientHeight = PrevHeight;
  363. Constraints->MinHeight = PrevMinHeight;
  364. Constraints->MaxHeight = PrevMaxHeight;
  365. Panel->Hide();
  366. LogView->Show();
  367. }
  368. }
  369. }
  370. __finally
  371. {
  372. Panel->Align = Align;
  373. DefaultButton->Default = false;
  374. CancelButton->Cancel = false;
  375. FStatus = L"";
  376. AdjustControls();
  377. }
  378. bool Result = (ModalResult != mrCancel);
  379. if (!Result)
  380. {
  381. DoCancel();
  382. }
  383. return Result;
  384. }
  385. //---------------------------------------------------------------------------
  386. void __fastcall TAuthenticateForm::HelpButtonClick(TObject * /*Sender*/)
  387. {
  388. FormHelp(this);
  389. }
  390. //---------------------------------------------------------------------------