Moodle authentication plugin for Macaroons

auth.php 5.1KB

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