Moodle authentication plugin for Macaroons

auth.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. * The name of the component. Used by the configuration.
  43. */
  44. const COMPONENT_NAME = 'auth_macaroons';
  45. /**
  46. * Constructor.
  47. */
  48. public function __construct() {
  49. $this->authtype = 'macaroons';
  50. $this->config = get_config(self::COMPONENT_NAME);
  51. }
  52. /**
  53. * Old syntax of class constructor. Deprecated in PHP7.
  54. *
  55. * @deprecated since Moodle 3.1
  56. */
  57. public function auth_plugin_macaroons() {
  58. debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  59. self::__construct();
  60. }
  61. function loginpage_hook() {
  62. global $DB, $login, $CFG;
  63. $placeholders[0] = "/{{firstname}}/";
  64. $placeholders[1] = "/{{lastname}}/";
  65. if(!empty($_COOKIE['das-macaroon'])) {
  66. try {
  67. $m = Macaroon::deserialize($_COOKIE['das-macaroon']);
  68. $v = new Verifier();
  69. $v->setCallbacks([
  70. function($a) {
  71. return !strcmp($a, "status = student");
  72. }
  73. ]);
  74. if($v->verify($m, $this->config->secret)) {
  75. $name = explode(";", $m->getIdentifier());
  76. $login = join("", $name);
  77. $user = authenticate_user_login($login, null);
  78. if($user) {
  79. $user->firstname = $name[0];
  80. $user->lastname = $name[1];
  81. $user->email = preg_replace($placeholders, $name, $this->config->email_config);
  82. $DB->update_record('user', $user);
  83. complete_user_login($user);
  84. redirect($CFG->wwwroot);
  85. }
  86. }
  87. } catch(Exception $e) {
  88. $message = $e->getMessage();
  89. }
  90. }
  91. }
  92. /**
  93. * Returns true if the username and password work or don't exist and false
  94. * if the user exists and the password is wrong.
  95. *
  96. * @param string $username The username
  97. * @param string $password The password
  98. * @return bool Authentication success or failure.
  99. */
  100. function user_login ($username, $password) {
  101. global $login;
  102. if($login == $username) {
  103. return true;
  104. }
  105. return false;
  106. }
  107. /**
  108. * Updates the user's password.
  109. *
  110. * called when the user password is updated.
  111. *
  112. * @param object $user User table object
  113. * @param string $newpassword Plaintext password
  114. * @return boolean result
  115. *
  116. */
  117. function user_update_password($user, $newpassword) {
  118. $user = get_complete_user_data('id', $user->id);
  119. // This will also update the stored hash to the latest algorithm
  120. // if the existing hash is using an out-of-date algorithm (or the
  121. // legacy md5 algorithm).
  122. return update_internal_user_password($user, $newpassword);
  123. }
  124. function prevent_local_passwords() {
  125. return false;
  126. }
  127. /**
  128. * Returns true if this authentication plugin is 'internal'.
  129. *
  130. * @return bool
  131. */
  132. function is_internal() {
  133. return false;
  134. }
  135. /**
  136. * Returns true if this authentication plugin can change the user's
  137. * password.
  138. *
  139. * @return bool
  140. */
  141. function can_change_password() {
  142. return true;
  143. }
  144. /**
  145. * Returns the URL for changing the user's pw, or empty if the default can
  146. * be used.
  147. *
  148. * @return moodle_url
  149. */
  150. function change_password_url() {
  151. return null;
  152. }
  153. /**
  154. * Returns true if plugin allows resetting of internal password.
  155. *
  156. * @return bool
  157. */
  158. function can_reset_password() {
  159. return true;
  160. }
  161. /**
  162. * Returns true if plugin can be manually set.
  163. *
  164. * @return bool
  165. */
  166. function can_be_manually_set() {
  167. return true;
  168. }
  169. /**
  170. * Prints a form for configuring this authentication plugin.
  171. *
  172. * This function is called from admin/auth.php, and outputs a full page with
  173. * a form for configuring this plugin.
  174. *
  175. * @param array $page An object containing all the data for this page.
  176. */
  177. function config_form($config, $err, $user_fields) {
  178. include "config.html";
  179. }
  180. /**
  181. * Processes and stores configuration data for this authentication plugin.
  182. */
  183. function process_config($config) {
  184. if(!isset($config->email_config)) {
  185. $config->email_config = '';
  186. }
  187. if(!isset($config->secret)) {
  188. $config->secret = '';
  189. }
  190. set_config('email_config', $config->email_config, self::COMPONENT_NAME);
  191. set_config('secret', $config->secret, self::COMPONENT_NAME);
  192. return true;
  193. }
  194. function is_synchronised_with_external() {
  195. return false;
  196. }
  197. }