httpauth.dpr 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2020 Silvio Clecio <[email protected]>
  10. *
  11. * Brook framework is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Brook framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Brook framework; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *)
  25. program httpauth;
  26. {
  27. Test using cURL:
  28. curl -u abc:123 http://localhost:<PORT>
  29. }
  30. {$IFDEF MSWINDOWS}
  31. {$APPTYPE CONSOLE}
  32. {$ENDIF}
  33. uses
  34. SysUtils,
  35. BrookHTTPAuthentication,
  36. BrookHTTPRequest,
  37. BrookHTTPResponse,
  38. BrookHTTPServer;
  39. type
  40. THTTPServer = class(TBrookHTTPServer)
  41. protected
  42. function DoAuthenticate(ASender: TObject;
  43. AAuthentication: TBrookHTTPAuthentication; ARequest: TBrookHTTPRequest;
  44. AResponse: TBrookHTTPResponse): Boolean; override;
  45. procedure DoRequest(ASender: TObject; ARequest: TBrookHTTPRequest;
  46. AResponse: TBrookHTTPResponse); override;
  47. end;
  48. function THTTPServer.DoAuthenticate(ASender: TObject;
  49. AAuthentication: TBrookHTTPAuthentication; ARequest: TBrookHTTPRequest;
  50. AResponse: TBrookHTTPResponse): Boolean;
  51. begin
  52. AAuthentication.Credentials.Realm := 'My realm';
  53. Result := AAuthentication.Credentials.UserName.Equals('abc') and
  54. AAuthentication.Credentials.Password.Equals('123');
  55. if not Result then
  56. AAuthentication.Deny(
  57. '<html><head><title>Denied</title></head><body><font color="red">Go away</font></body></html>',
  58. 'text/html; charset=utf-8');
  59. end;
  60. procedure THTTPServer.DoRequest(ASender: TObject; ARequest: TBrookHTTPRequest;
  61. AResponse: TBrookHTTPResponse);
  62. begin
  63. AResponse.Send(
  64. '<html><head><title>Secret</title></head><body><font color="green">Secret page</font></body></html>',
  65. 'text/html; charset=utf-8', 200);
  66. end;
  67. begin
  68. with THTTPServer.Create(nil) do
  69. try
  70. Authenticated := True;
  71. NoFavicon := True;
  72. Open;
  73. if not Active then
  74. Exit;
  75. WriteLn('Server running at http://localhost:', Port);
  76. ReadLn;
  77. finally
  78. Free;
  79. end;
  80. end.