Moodle authentication plugin for Macaroons

auth.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Anobody can login with any password.
  18. *
  19. * @package auth_macaroons
  20. * @author Brendan Abolivier
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. require_once($CFG->libdir.'/authlib.php');
  25. require_once($CFG->dirroot.'/auth/macaroons/Macaroons/Macaroon.php');
  26. require_once($CFG->dirroot.'/auth/macaroons/Macaroons/Caveat.php');
  27. require_once($CFG->dirroot.'/auth/macaroons/Macaroons/Packet.php');
  28. require_once($CFG->dirroot.'/auth/macaroons/Macaroons/Utils.php');
  29. require_once($CFG->dirroot.'/auth/macaroons/Macaroons/Verifier.php');
  30. require_once($CFG->dirroot.'/auth/macaroons/Macaroons/Exceptions/CaveatUnsatisfiedException.php');
  31. require_once($CFG->dirroot.'/auth/macaroons/Macaroons/Exceptions/InvalidMacaroonKeyException.php');
  32. require_once($CFG->dirroot.'/auth/macaroons/Macaroons/Exceptions/SignatureMismatchException.php');
  33. use Macaroons\Macaroon;
  34. use Macaroons\Verifier;
  35. /**
  36. * Plugin for no authentication.
  37. */
  38. class auth_plugin_macaroons extends auth_plugin_base {
  39. /**
  40. * Constructor.
  41. */
  42. public function __construct() {
  43. $this->authtype = 'macaroons';
  44. }
  45. function loginpage_hook() {
  46. global $message;
  47. $message = "";
  48. if(!empty($_COOKIE['das-macaroon'])) {
  49. try {
  50. $m = Macaroon::deserialize($_COOKIE['das-macaroon']);
  51. $frm = new stdClass();
  52. $frm->username = $m->getIdentifier();
  53. $frm->password = 'passwdMacaroons';
  54. $v = new Verifier();
  55. $v->setCallbacks([
  56. function($a) {
  57. return !strcmp($a, "status = student");
  58. }
  59. ]);
  60. if($v->verify($m, "pocsecret")) {
  61. $frm = new stdClass();
  62. $frm->username = $m->getIdentifier();
  63. $frm->password = 'passwdMacaroons';
  64. }
  65. } catch(Exception $e) {
  66. $message = $e->getMessage();
  67. }
  68. authenticate_user_login($frm->username, sesskey());
  69. }
  70. }
  71. /**
  72. * Old syntax of class constructor. Deprecated in PHP7.
  73. *
  74. * @deprecated since Moodle 3.1
  75. */
  76. public function auth_plugin_macaroons() {
  77. debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  78. self::__construct();
  79. }
  80. /**
  81. * Returns true if the username and password work or don't exist and false
  82. * if the user exists and the password is wrong.
  83. *
  84. * @param string $username The username
  85. * @param string $password The password
  86. * @return bool Authentication success or failure.
  87. */
  88. function user_login ($username, $password) {
  89. global $message;
  90. if(!empty($message)) {
  91. return false;
  92. } elseif(!empty($_COOKIE['das-macaroon'])) {
  93. return true;
  94. }
  95. }
  96. /**
  97. * Updates the user's password.
  98. *
  99. * called when the user password is updated.
  100. *
  101. * @param object $user User table object
  102. * @param string $newpassword Plaintext password
  103. * @return boolean result
  104. *
  105. */
  106. function user_update_password($user, $newpassword) {
  107. $user = get_complete_user_data('id', $user->id);
  108. // This will also update the stored hash to the latest algorithm
  109. // if the existing hash is using an out-of-date algorithm (or the
  110. // legacy md5 algorithm).
  111. return update_internal_user_password($user, $newpassword);
  112. }
  113. function prevent_local_passwords() {
  114. return false;
  115. }
  116. /**
  117. * Returns true if this authentication plugin is 'internal'.
  118. *
  119. * @return bool
  120. */
  121. function is_internal() {
  122. return false;
  123. }
  124. /**
  125. * Returns true if this authentication plugin can change the user's
  126. * password.
  127. *
  128. * @return bool
  129. */
  130. function can_change_password() {
  131. return true;
  132. }
  133. /**
  134. * Returns the URL for changing the user's pw, or empty if the default can
  135. * be used.
  136. *
  137. * @return moodle_url
  138. */
  139. function change_password_url() {
  140. return null;
  141. }
  142. /**
  143. * Returns true if plugin allows resetting of internal password.
  144. *
  145. * @return bool
  146. */
  147. function can_reset_password() {
  148. return true;
  149. }
  150. /**
  151. * Returns true if plugin can be manually set.
  152. *
  153. * @return bool
  154. */
  155. function can_be_manually_set() {
  156. return true;
  157. }
  158. /**
  159. * Prints a form for configuring this authentication plugin.
  160. *
  161. * This function is called from admin/auth.php, and outputs a full page with
  162. * a form for configuring this plugin.
  163. *
  164. * @param array $page An object containing all the data for this page.
  165. function config_form($config, $err, $user_fields) {
  166. include "config.html";
  167. }
  168. */
  169. /**
  170. * Processes and stores configuration data for this authentication plugin.
  171. */
  172. function process_config($config) {
  173. return true;
  174. }
  175. }