EmailAuthenticationPlugin.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero 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. // GNU social 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 Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Plugin that uses the email address as a username, and checks the password as normal
  18. *
  19. * @category Plugin
  20. * @package GNUsocial
  21. * @author Craig Andrews <candrews@integralblue.com>
  22. * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. class EmailAuthenticationPlugin extends Plugin
  27. {
  28. const PLUGIN_VERSION = '2.0.0';
  29. // $nickname for this plugin is the user's email address
  30. public function onStartCheckPassword(
  31. string $nickname,
  32. string $password,
  33. string &$authenticatedUser
  34. ): bool {
  35. $email = filter_var(
  36. $nickname,
  37. FILTER_VALIDATE_EMAIL,
  38. ['flags' => FILTER_FLAG_EMAIL_UNICODE]
  39. );
  40. if ($email === false) {
  41. return true;
  42. }
  43. $user = User::getKV('email', $email);
  44. if ($user instanceof User && $user->email === $email) {
  45. if (common_check_user($user->nickname, $password)) {
  46. $authenticatedUser = $user;
  47. return false;
  48. }
  49. }
  50. return true;
  51. }
  52. public function onPluginVersion(array &$versions): bool
  53. {
  54. $versions[] = array('name' => 'Email Authentication',
  55. 'version' => self::PLUGIN_VERSION,
  56. 'author' => 'Craig Andrews',
  57. 'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/EmailAuthentication',
  58. 'rawdescription' =>
  59. // TRANS: Plugin description.
  60. _m('The Email Authentication plugin allows users to login using their email address.'));
  61. return true;
  62. }
  63. }