AuthenticationModule.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. * Superclass for plugins that do authentication and/or authorization
  18. *
  19. * @category Plugin
  20. * @package GNUsocial
  21. *
  22. * @author Craig Andrews <candrews@integralblue.com>
  23. * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
  24. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  25. */
  26. defined('GNUSOCIAL') || die;
  27. /**
  28. * Superclass for plugins that do authentication
  29. *
  30. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  31. */
  32. abstract class AuthenticationModule extends Module
  33. {
  34. public $widgetOpts;
  35. public $scoped;
  36. //is this plugin authoritative for authentication?
  37. public $authoritative = false;
  38. //should accounts be automatically created after a successful login attempt?
  39. public $autoregistration = false;
  40. //can the user change their email address
  41. public $password_changeable = true;
  42. //unique name for this authentication provider
  43. public $provider_name = null;
  44. //------------Auth plugin should implement some (or all) of these methods------------\\
  45. /**
  46. * Check if a nickname/password combination is valid
  47. *
  48. * @param string $username
  49. * @param string $password
  50. *
  51. * @return bool true if the credentials are valid, false if they are invalid.
  52. */
  53. public function checkPassword($username, $password)
  54. {
  55. return false;
  56. }
  57. /**
  58. * Automatically register a user when they attempt to login with valid credentials.
  59. * User::register($data) is a very useful method for this implementation
  60. *
  61. * @param string $username username (that is used to login and find the user in the authentication provider) of the user to be registered
  62. * @param null|string $nickname nickname of the user in the SN system. If nickname is null, then set nickname = username
  63. *
  64. * @return mixed instance of User, or false (if user couldn't be created)
  65. */
  66. public function autoRegister($username, $nickname = null)
  67. {
  68. if (is_null($nickname)) {
  69. $nickname = $username;
  70. }
  71. $registration_data = [];
  72. $registration_data['nickname'] = $nickname;
  73. return User::register($registration_data);
  74. }
  75. /**
  76. * Change a user's password
  77. * The old password has been verified to be valid by this plugin before this call is made
  78. *
  79. * @param string $username
  80. * @param string $oldpassword
  81. * @param string $newpassword
  82. *
  83. * @return bool true if the password was changed, false if password changing failed for some reason
  84. */
  85. public function changePassword($username, $oldpassword, $newpassword)
  86. {
  87. return false;
  88. }
  89. /**
  90. * Given a username, suggest what the nickname should be
  91. * Used during autoregistration
  92. * Useful if your usernames are ugly, and you want to suggest
  93. * nice looking nicknames when users initially sign on
  94. * All nicknames returned by this function should be valid
  95. * implementations may want to use common_nicknamize() to ensure validity
  96. *
  97. * @param string $username
  98. *
  99. * @return string nickname
  100. */
  101. public function suggestNicknameForUsername($username)
  102. {
  103. return common_nicknamize($username);
  104. }
  105. //------------Below are the methods that connect StatusNet to the implementing Auth plugin------------\\
  106. public function onInitializePlugin()
  107. {
  108. if (is_null($this->provider_name)) {
  109. throw new Exception('must specify a provider_name for this authentication provider');
  110. }
  111. }
  112. /**
  113. * Internal AutoRegister event handler
  114. *
  115. * @param string $nickname
  116. * @param string $provider_name
  117. * @param User $user - the newly registered user
  118. */
  119. public function onAutoRegister($nickname, $provider_name, &$user)
  120. {
  121. if ($provider_name == $this->provider_name && $this->autoregistration) {
  122. $suggested_nickname = $this->suggestNicknameForUsername($nickname);
  123. $test_user = User::getKV('nickname', $suggested_nickname);
  124. if ($test_user) {
  125. //someone already exists with the suggested nickname, so used the passed nickname
  126. $suggested_nickname = common_nicknamize($nickname);
  127. }
  128. $test_user = User::getKV('nickname', $suggested_nickname);
  129. if ($test_user) {
  130. //someone already exists with the suggested nickname
  131. //not much else we can do
  132. } else {
  133. $user = $this->autoRegister($nickname, $suggested_nickname);
  134. if ($user instanceof User) {
  135. User_username::register($user, $nickname, $this->provider_name);
  136. return false;
  137. }
  138. }
  139. }
  140. }
  141. public function onStartCheckPassword($nickname, $password, &$authenticatedUser)
  142. {
  143. //map the nickname to a username
  144. $user_username = new User_username();
  145. $user_username->username = $nickname;
  146. $user_username->provider_name = $this->provider_name;
  147. if ($user_username->find() && $user_username->fetch()) {
  148. $authenticated = $this->checkPassword($user_username->username, $password);
  149. if ($authenticated) {
  150. $authenticatedUser = User::getKV('id', $user_username->user_id);
  151. return false;
  152. }
  153. } else {
  154. //$nickname is the username used to login
  155. //$suggested_nickname is the nickname the auth provider suggests for that username
  156. $suggested_nickname = $this->suggestNicknameForUsername($nickname);
  157. $user = User::getKV('nickname', $suggested_nickname);
  158. if ($user) {
  159. //make sure this user isn't claimed
  160. $user_username = new User_username();
  161. $user_username->user_id = $user->id;
  162. $we_can_handle = false;
  163. if ($user_username->find()) {
  164. //either this provider, or another one, has already claimed this user
  165. //so we cannot. Let another plugin try.
  166. return;
  167. } else {
  168. //no other provider claims this user, so it's safe for us to handle it
  169. $authenticated = $this->checkPassword($nickname, $password);
  170. if ($authenticated) {
  171. $authenticatedUser = $user;
  172. User_username::register($authenticatedUser, $nickname, $this->provider_name);
  173. return false;
  174. }
  175. }
  176. } else {
  177. $authenticated = $this->checkPassword($nickname, $password);
  178. if ($authenticated) {
  179. if (!Event::handle('AutoRegister', [$nickname, $this->provider_name, &$authenticatedUser])) {
  180. //unlike most Event::handle lines of code, this one has a ! (not)
  181. //we want to do this if the event *was* handled - this isn't a "default" implementation
  182. //like most code of this form.
  183. if ($authenticatedUser) {
  184. return false;
  185. }
  186. }
  187. }
  188. }
  189. }
  190. if ($this->authoritative) {
  191. return false;
  192. } else {
  193. //we're not authoritative, so let other handlers try
  194. return;
  195. }
  196. }
  197. public function onStartChangePassword(Profile $target, $oldpassword, $newpassword)
  198. {
  199. if ($this->password_changeable) {
  200. $user_username = new User_username();
  201. $user_username->user_id = $target->getID();
  202. $user_username->provider_name = $this->provider_name;
  203. if ($user_username->find(true)) {
  204. $authenticated = $this->checkPassword($user_username->username, $oldpassword);
  205. if ($authenticated) {
  206. $result = $this->changePassword($user_username->username, $oldpassword, $newpassword);
  207. if ($result) {
  208. //stop handling of other handlers, because what was requested was done
  209. return false;
  210. } else {
  211. // TRANS: Exception thrown when a password change fails.
  212. throw new Exception(_('Password changing failed.'));
  213. }
  214. } else {
  215. if ($this->authoritative) {
  216. //since we're authoritative, no other plugin could do this
  217. // TRANS: Exception thrown when a password change fails.
  218. throw new Exception(_('Password changing failed.'));
  219. } else {
  220. //let another handler try
  221. return null;
  222. }
  223. }
  224. }
  225. } else {
  226. if ($this->authoritative) {
  227. //since we're authoritative, no other plugin could do this
  228. // TRANS: Exception thrown when a password change attempt fails because it is not allowed.
  229. throw new Exception(_('Password changing is not allowed.'));
  230. }
  231. }
  232. }
  233. public function onStartAccountSettingsPasswordMenuItem($widget)
  234. {
  235. if ($this->authoritative && !$this->password_changeable) {
  236. //since we're authoritative, no other plugin could change passwords, so do not render the menu item
  237. return false;
  238. }
  239. }
  240. public function onCheckSchema()
  241. {
  242. $schema = Schema::get();
  243. $schema->ensureTable('user_username', User_username::schemaDef());
  244. return true;
  245. }
  246. public function onUserDeleteRelated($user, &$tables)
  247. {
  248. $tables[] = 'User_username';
  249. return true;
  250. }
  251. }