AuthenticationModule.php 10 KB

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