2
0

digest_auth_example.pp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. (*
  2. This file is part of libmicrohttpd
  3. Copyright (C) 2010 Christian Grothoff (and other contributing authors)
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  15. *)
  16. (**
  17. * @file digest_auth_example.pp (Original: digest_auth_example.c)
  18. * @brief minimal example for how to use digest auth with libmicrohttpd
  19. * @author Amr Ali / Silvio Clécio
  20. *)
  21. program digest_auth_example;
  22. {$mode objfpc}{$H+}
  23. uses
  24. sysutils, BaseUnix, cmem, cutils, libmicrohttpd;
  25. const
  26. PAGE: Pcchar = '<html><head><title>libmicrohttpd demo</title></head><body>Access granted</body></html>';
  27. DENIED: Pcchar = '<html><head><title>libmicrohttpd demo</title></head><body>Access denied</body></html>';
  28. MY_OPAQUE_STR = '11733b200778ce33060f31c9af70a870ba96ddd4';
  29. function ahc_echo(cls: Pointer; connection: PMHD_Connection; url: Pcchar;
  30. method: Pcchar; version: Pcchar; upload_data: Pcchar;
  31. upload_data_size: Psize_t; ptr: PPointer): cint; cdecl;
  32. const
  33. password: Pcchar = 'testpass';
  34. realm: Pcchar = '[email protected]';
  35. var
  36. response: PMHD_Response;
  37. username: Pcchar;
  38. ret: cint;
  39. signal_stale: cint;
  40. begin
  41. username := MHD_digest_auth_get_username(connection);
  42. if username = nil then
  43. begin
  44. response := MHD_create_response_from_buffer(strlen(DENIED), DENIED,
  45. MHD_RESPMEM_PERSISTENT);
  46. ret := MHD_queue_auth_fail_response(connection, realm, MY_OPAQUE_STR,
  47. response, MHD_NO);
  48. MHD_destroy_response(response);
  49. Exit(ret);
  50. end;
  51. ret := MHD_digest_auth_check(connection, realm, username, password, 300);
  52. Free(username);
  53. if (ret = MHD_INVALID_NONCE) or (ret = MHD_NO) then
  54. begin
  55. response := MHD_create_response_from_buffer(strlen(DENIED), DENIED,
  56. MHD_RESPMEM_PERSISTENT);
  57. if nil = response then
  58. Exit(MHD_NO);
  59. if ret = MHD_INVALID_NONCE then
  60. signal_stale := MHD_YES
  61. else
  62. signal_stale := MHD_NO;
  63. ret := MHD_queue_auth_fail_response(connection, realm, MY_OPAQUE_STR,
  64. response, signal_stale);
  65. MHD_destroy_response(response);
  66. Exit(ret);
  67. end;
  68. response := MHD_create_response_from_buffer(strlen(PAGE), PAGE,
  69. MHD_RESPMEM_PERSISTENT);
  70. ret := MHD_queue_response(connection, MHD_HTTP_OK, response);
  71. MHD_destroy_response(response);
  72. Result := ret;
  73. end;
  74. var
  75. fd: cint;
  76. rnd: array[0..7] of AnsiChar;
  77. len: ssize_t;
  78. off: size_t;
  79. d: PMHD_Daemon;
  80. begin
  81. if argc <> 2 then
  82. begin
  83. WriteLn(argv[0], ' PORT');
  84. Halt(1);
  85. end;
  86. fd := FpOpen('/dev/urandom', O_RDONLY);
  87. if -1 = fd then
  88. begin
  89. WriteLn(stderr, Format('Failed to open `%s'': %s', [
  90. '/dev/urandom', strerror(errno^)]));
  91. Halt(1);
  92. end;
  93. off := 0;
  94. while off < 8 do
  95. begin
  96. len := FpRead(fd, rnd, 8);
  97. if len = -1 then
  98. begin
  99. WriteLn(stderr, Format('Failed to read `%s'': %s', [
  100. '/dev/urandom', strerror(errno^)]));
  101. FpClose(fd);
  102. Halt(1);
  103. end;
  104. off += len;
  105. end;
  106. FpClose(fd);
  107. d := MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION or MHD_USE_DEBUG,
  108. StrToInt(argv[1]), nil, nil, @ahc_echo, PAGE,
  109. MHD_OPTION_DIGEST_AUTH_RANDOM, SizeOf(rnd), rnd,
  110. MHD_OPTION_NONCE_NC_SIZE, 300,
  111. MHD_OPTION_CONNECTION_TIMEOUT, cuint(120),
  112. MHD_OPTION_END);
  113. if d = nil then
  114. Halt(1);
  115. ReadLn;
  116. MHD_stop_daemon (d);
  117. end.