AuthPlugin.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <?php
  2. /**
  3. * Authentication plugin interface
  4. *
  5. * Copyright © 2004 Brion Vibber <brion@pobox.com>
  6. * https://www.mediawiki.org/
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. * http://www.gnu.org/copyleft/gpl.html
  22. *
  23. * @file
  24. */
  25. /**
  26. * Authentication plugin interface. Instantiate a subclass of AuthPlugin
  27. * and set $wgAuth to it to authenticate against some external tool.
  28. *
  29. * The default behavior is not to do anything, and use the local user
  30. * database for all authentication. A subclass can require that all
  31. * accounts authenticate externally, or use it only as a fallback; also
  32. * you can transparently create internal wiki accounts the first time
  33. * someone logs in who can be authenticated externally.
  34. *
  35. * @deprecated since 1.27
  36. */
  37. class AuthPlugin {
  38. /**
  39. * @var string
  40. */
  41. protected $domain;
  42. /**
  43. * Check whether there exists a user account with the given name.
  44. * The name will be normalized to MediaWiki's requirements, so
  45. * you might need to munge it (for instance, for lowercase initial
  46. * letters).
  47. *
  48. * @param string $username Username.
  49. * @return bool
  50. */
  51. public function userExists( $username ) {
  52. # Override this!
  53. return false;
  54. }
  55. /**
  56. * Check if a username+password pair is a valid login.
  57. * The name will be normalized to MediaWiki's requirements, so
  58. * you might need to munge it (for instance, for lowercase initial
  59. * letters).
  60. *
  61. * @param string $username Username.
  62. * @param string $password User password.
  63. * @return bool
  64. */
  65. public function authenticate( $username, $password ) {
  66. # Override this!
  67. return false;
  68. }
  69. /**
  70. * Modify options in the login template.
  71. *
  72. * @param BaseTemplate &$template
  73. * @param string &$type 'signup' or 'login'. Added in 1.16.
  74. */
  75. public function modifyUITemplate( &$template, &$type ) {
  76. # Override this!
  77. $template->set( 'usedomain', false );
  78. }
  79. /**
  80. * Set the domain this plugin is supposed to use when authenticating.
  81. *
  82. * @param string $domain Authentication domain.
  83. */
  84. public function setDomain( $domain ) {
  85. $this->domain = $domain;
  86. }
  87. /**
  88. * Get the user's domain
  89. *
  90. * @return string
  91. */
  92. public function getDomain() {
  93. return $this->domain ?? 'invaliddomain';
  94. }
  95. /**
  96. * Check to see if the specific domain is a valid domain.
  97. *
  98. * @param string $domain Authentication domain.
  99. * @return bool
  100. */
  101. public function validDomain( $domain ) {
  102. # Override this!
  103. return true;
  104. }
  105. /**
  106. * When a user logs in, optionally fill in preferences and such.
  107. * For instance, you might pull the email address or real name from the
  108. * external user database.
  109. *
  110. * The User object is passed by reference so it can be modified; don't
  111. * forget the & on your function declaration.
  112. *
  113. * @deprecated since 1.26, use the UserLoggedIn hook instead. And assigning
  114. * a different User object to $user is no longer supported.
  115. * @param User &$user
  116. * @return bool
  117. */
  118. public function updateUser( &$user ) {
  119. # Override this and do something
  120. return true;
  121. }
  122. /**
  123. * Return true if the wiki should create a new local account automatically
  124. * when asked to login a user who doesn't exist locally but does in the
  125. * external auth database.
  126. *
  127. * If you don't automatically create accounts, you must still create
  128. * accounts in some way. It's not possible to authenticate without
  129. * a local account.
  130. *
  131. * This is just a question, and shouldn't perform any actions.
  132. *
  133. * @return bool
  134. */
  135. public function autoCreate() {
  136. return false;
  137. }
  138. /**
  139. * Allow a property change? Properties are the same as preferences
  140. * and use the same keys. 'Realname' 'Emailaddress' and 'Nickname'
  141. * all reference this.
  142. *
  143. * @param string $prop
  144. *
  145. * @return bool
  146. */
  147. public function allowPropChange( $prop = '' ) {
  148. if ( $prop == 'realname' && is_callable( [ $this, 'allowRealNameChange' ] ) ) {
  149. return $this->allowRealNameChange();
  150. } elseif ( $prop == 'emailaddress' && is_callable( [ $this, 'allowEmailChange' ] ) ) {
  151. return $this->allowEmailChange();
  152. } elseif ( $prop == 'nickname' && is_callable( [ $this, 'allowNickChange' ] ) ) {
  153. return $this->allowNickChange();
  154. } else {
  155. return true;
  156. }
  157. }
  158. /**
  159. * Can users change their passwords?
  160. *
  161. * @return bool
  162. */
  163. public function allowPasswordChange() {
  164. return true;
  165. }
  166. /**
  167. * Should MediaWiki store passwords in its local database?
  168. *
  169. * @return bool
  170. */
  171. public function allowSetLocalPassword() {
  172. return true;
  173. }
  174. /**
  175. * Set the given password in the authentication database.
  176. * As a special case, the password may be set to null to request
  177. * locking the password to an unusable value, with the expectation
  178. * that it will be set later through a mail reset or other method.
  179. *
  180. * Return true if successful.
  181. *
  182. * @param User $user
  183. * @param string $password Password.
  184. * @return bool
  185. */
  186. public function setPassword( $user, $password ) {
  187. return true;
  188. }
  189. /**
  190. * Update user information in the external authentication database.
  191. * Return true if successful.
  192. *
  193. * @deprecated since 1.26, use the UserSaveSettings hook instead.
  194. * @param User $user
  195. * @return bool
  196. */
  197. public function updateExternalDB( $user ) {
  198. return true;
  199. }
  200. /**
  201. * Update user groups in the external authentication database.
  202. * Return true if successful.
  203. *
  204. * @deprecated since 1.26, use the UserGroupsChanged hook instead.
  205. * @param User $user
  206. * @param array $addgroups Groups to add.
  207. * @param array $delgroups Groups to remove.
  208. * @return bool
  209. */
  210. public function updateExternalDBGroups( $user, $addgroups, $delgroups = [] ) {
  211. return true;
  212. }
  213. /**
  214. * Check to see if external accounts can be created.
  215. * Return true if external accounts can be created.
  216. * @return bool
  217. */
  218. public function canCreateAccounts() {
  219. return false;
  220. }
  221. /**
  222. * Add a user to the external authentication database.
  223. * Return true if successful.
  224. *
  225. * @param User $user Only the name should be assumed valid at this point
  226. * @param string $password
  227. * @param string $email
  228. * @param string $realname
  229. * @return bool
  230. */
  231. public function addUser( $user, $password, $email = '', $realname = '' ) {
  232. return true;
  233. }
  234. /**
  235. * Return true to prevent logins that don't authenticate here from being
  236. * checked against the local database's password fields.
  237. *
  238. * This is just a question, and shouldn't perform any actions.
  239. *
  240. * @return bool
  241. */
  242. public function strict() {
  243. return false;
  244. }
  245. /**
  246. * Check if a user should authenticate locally if the global authentication fails.
  247. * If either this or strict() returns true, local authentication is not used.
  248. *
  249. * @param string $username Username.
  250. * @return bool
  251. */
  252. public function strictUserAuth( $username ) {
  253. return false;
  254. }
  255. /**
  256. * When creating a user account, optionally fill in preferences and such.
  257. * For instance, you might pull the email address or real name from the
  258. * external user database.
  259. *
  260. * The User object is passed by reference so it can be modified; don't
  261. * forget the & on your function declaration.
  262. *
  263. * @deprecated since 1.26, use the UserLoggedIn hook instead. And assigning
  264. * a different User object to $user is no longer supported.
  265. * @param User &$user
  266. * @param bool $autocreate True if user is being autocreated on login
  267. */
  268. public function initUser( &$user, $autocreate = false ) {
  269. # Override this to do something.
  270. }
  271. /**
  272. * If you want to munge the case of an account name before the final
  273. * check, now is your chance.
  274. * @param string $username
  275. * @return string
  276. */
  277. public function getCanonicalName( $username ) {
  278. return $username;
  279. }
  280. /**
  281. * Get an instance of a User object
  282. *
  283. * @param User &$user
  284. *
  285. * @return AuthPluginUser
  286. */
  287. public function getUserInstance( User &$user ) {
  288. return new AuthPluginUser( $user );
  289. }
  290. /**
  291. * Get a list of domains (in HTMLForm options format) used.
  292. *
  293. * @return array
  294. */
  295. public function domainList() {
  296. return [];
  297. }
  298. }
  299. /**
  300. * @deprecated since 1.27
  301. */
  302. class AuthPluginUser {
  303. function __construct( $user ) {
  304. # Override this!
  305. }
  306. public function getId() {
  307. # Override this!
  308. return -1;
  309. }
  310. /**
  311. * Indicate whether the user is locked
  312. * @deprecated since 1.26, use the UserIsLocked hook instead.
  313. * @return bool
  314. */
  315. public function isLocked() {
  316. # Override this!
  317. return false;
  318. }
  319. /**
  320. * Indicate whether the user is hidden
  321. * @deprecated since 1.26, use the UserIsHidden hook instead.
  322. * @return bool
  323. */
  324. public function isHidden() {
  325. # Override this!
  326. return false;
  327. }
  328. /**
  329. * @deprecated since 1.28, use SessionManager::invalidateSessionForUser() instead.
  330. * @return bool
  331. */
  332. public function resetAuthToken() {
  333. # Override this!
  334. return true;
  335. }
  336. }