callback.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2021, Oracle and/or its affiliates.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License, version 2.0, as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is also distributed with certain software (including
  9. * but not limited to OpenSSL) that is licensed under separate terms,
  10. * as designated in a particular file or component or in included license
  11. * documentation. The authors of MySQL hereby grant you an
  12. * additional permission to link the program and your derivative works
  13. * with the separately licensed software that they have included with
  14. * MySQL.
  15. *
  16. * Without limiting anything contained in the foregoing, this file,
  17. * which is part of MySQL Connector/C++, is also subject to the
  18. * Universal FOSS Exception, version 1.0, a copy of which can be found at
  19. * http://oss.oracle.com/licenses/universal-foss-exception.
  20. *
  21. * This program is distributed in the hope that it will be useful, but
  22. * WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  24. * See the GNU General Public License, version 2.0, for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software Foundation, Inc.,
  28. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  29. */
  30. #ifndef _SQL_CALLBACK_H_
  31. #define _SQL_CALLBACK_H_
  32. #include "sqlstring.h"
  33. #include <functional>
  34. namespace sql
  35. {
  36. namespace mysql
  37. {
  38. class MySQL_Connection;
  39. class MySQL_Driver;
  40. }
  41. /*
  42. A callback to be used with `Driver::setCallback()` to define reaction
  43. to the user action request during WebAuthn authentication handshake.
  44. The client library defines default reaction which prints message on stderr.
  45. This callback can be used to change it.
  46. Example usage:
  47. // Use lambda
  48. driver->setCallback(WebAuthn_Callback{[](SQLString msg){
  49. cerr << "User action request: " << msg << endl;
  50. }});
  51. // Disable default behavior (and do nothing upon action request)
  52. driver->setCallback(WebAuthn_Callback{});
  53. // Return to default behavior
  54. driver->setCallbacak(WebAuthn_Callback{nullptr});
  55. // User defined callback
  56. struct My_Callback : WebAuthn_Callback
  57. {
  58. void ActionRequested(SQLString) override;
  59. }
  60. cb;
  61. driver->setCallback(cb);
  62. */
  63. class WebAuthn_Callback
  64. {
  65. std::function<void(SQLString)> callback_func;
  66. public:
  67. /*
  68. Create a callback that will call given lambda upon user action request.
  69. */
  70. WebAuthn_Callback(std::function<void(SQLString)>&& cb)
  71. : callback_func{std::move(cb)}
  72. {}
  73. /*
  74. Create an empty callback that will do nothing upon user action request.
  75. This disables the default callback defined by the client library.
  76. */
  77. WebAuthn_Callback()
  78. : callback_func{[](SQLString){}}
  79. {}
  80. /*
  81. Create a null callback. Setting such callback has the effect of using
  82. the default callback defined by the client library.
  83. */
  84. WebAuthn_Callback(std::nullptr_t)
  85. {}
  86. /*
  87. Derived class can override this method to react to user action request.
  88. */
  89. virtual void ActionRequested(sql::SQLString msg)
  90. {
  91. if (callback_func)
  92. callback_func(msg);
  93. }
  94. // Returns true if this callback is not null.
  95. operator bool() const
  96. {
  97. return (bool)callback_func;
  98. }
  99. void operator()(sql::SQLString msg)
  100. {
  101. ActionRequested(msg);
  102. }
  103. };
  104. /*
  105. * Class that provides functionality allowing user code to set the
  106. * callback functions through inheriting, passing the callback as
  107. * constructor parameters or using lambdas.
  108. */
  109. class Fido_Callback
  110. {
  111. std::function<void(SQLString)> callback_func = nullptr;
  112. bool is_null = false;
  113. public:
  114. /**
  115. * Constructor to set the callback as function or as lambda
  116. */
  117. Fido_Callback(std::function<void(SQLString)> cb) : callback_func(cb)
  118. {}
  119. Fido_Callback()
  120. {}
  121. /**
  122. * Constructor to reset the callback to default
  123. */
  124. Fido_Callback(std::nullptr_t) : is_null(true)
  125. {}
  126. /**
  127. * Override this message to receive Fido Action Requests
  128. */
  129. virtual void FidoActionRequested(sql::SQLString msg)
  130. {
  131. if (callback_func)
  132. callback_func(msg);
  133. }
  134. operator bool() const
  135. {
  136. return !is_null;
  137. }
  138. void operator()(sql::SQLString msg)
  139. {
  140. if (is_null)
  141. return;
  142. FidoActionRequested(msg);
  143. }
  144. friend class mysql::MySQL_Connection;
  145. friend class mysql::MySQL_Driver;
  146. };
  147. } /* namespace sql */
  148. #endif // _SQL_CONNECTION_H_