AuthPlugin.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. */
  4. # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
  5. # http://www.mediawiki.org/
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. # http://www.gnu.org/copyleft/gpl.html
  21. /**
  22. * Authentication plugin interface. Instantiate a subclass of AuthPlugin
  23. * and set $wgAuth to it to authenticate against some external tool.
  24. *
  25. * The default behavior is not to do anything, and use the local user
  26. * database for all authentication. A subclass can require that all
  27. * accounts authenticate externally, or use it only as a fallback; also
  28. * you can transparently create internal wiki accounts the first time
  29. * someone logs in who can be authenticated externally.
  30. */
  31. class AuthPlugin {
  32. /**
  33. * Check whether there exists a user account with the given name.
  34. * The name will be normalized to MediaWiki's requirements, so
  35. * you might need to munge it (for instance, for lowercase initial
  36. * letters).
  37. *
  38. * @param $username String: username.
  39. * @return bool
  40. */
  41. public function userExists( $username ) {
  42. # Override this!
  43. return false;
  44. }
  45. /**
  46. * Check if a username+password pair is a valid login.
  47. * The name will be normalized to MediaWiki's requirements, so
  48. * you might need to munge it (for instance, for lowercase initial
  49. * letters).
  50. *
  51. * @param $username String: username.
  52. * @param $password String: user password.
  53. * @return bool
  54. */
  55. public function authenticate( $username, $password ) {
  56. # Override this!
  57. return false;
  58. }
  59. /**
  60. * Modify options in the login template.
  61. *
  62. * @param $template UserLoginTemplate object.
  63. */
  64. public function modifyUITemplate( &$template ) {
  65. # Override this!
  66. $template->set( 'usedomain', false );
  67. }
  68. /**
  69. * Set the domain this plugin is supposed to use when authenticating.
  70. *
  71. * @param $domain String: authentication domain.
  72. */
  73. public function setDomain( $domain ) {
  74. $this->domain = $domain;
  75. }
  76. /**
  77. * Check to see if the specific domain is a valid domain.
  78. *
  79. * @param $domain String: authentication domain.
  80. * @return bool
  81. */
  82. public function validDomain( $domain ) {
  83. # Override this!
  84. return true;
  85. }
  86. /**
  87. * When a user logs in, optionally fill in preferences and such.
  88. * For instance, you might pull the email address or real name from the
  89. * external user database.
  90. *
  91. * The User object is passed by reference so it can be modified; don't
  92. * forget the & on your function declaration.
  93. *
  94. * @param User $user
  95. */
  96. public function updateUser( &$user ) {
  97. # Override this and do something
  98. return true;
  99. }
  100. /**
  101. * Return true if the wiki should create a new local account automatically
  102. * when asked to login a user who doesn't exist locally but does in the
  103. * external auth database.
  104. *
  105. * If you don't automatically create accounts, you must still create
  106. * accounts in some way. It's not possible to authenticate without
  107. * a local account.
  108. *
  109. * This is just a question, and shouldn't perform any actions.
  110. *
  111. * @return bool
  112. */
  113. public function autoCreate() {
  114. return false;
  115. }
  116. /**
  117. * Can users change their passwords?
  118. *
  119. * @return bool
  120. */
  121. public function allowPasswordChange() {
  122. return true;
  123. }
  124. /**
  125. * Set the given password in the authentication database.
  126. * As a special case, the password may be set to null to request
  127. * locking the password to an unusable value, with the expectation
  128. * that it will be set later through a mail reset or other method.
  129. *
  130. * Return true if successful.
  131. *
  132. * @param $user User object.
  133. * @param $password String: password.
  134. * @return bool
  135. */
  136. public function setPassword( $user, $password ) {
  137. return true;
  138. }
  139. /**
  140. * Update user information in the external authentication database.
  141. * Return true if successful.
  142. *
  143. * @param $user User object.
  144. * @return bool
  145. */
  146. public function updateExternalDB( $user ) {
  147. return true;
  148. }
  149. /**
  150. * Check to see if external accounts can be created.
  151. * Return true if external accounts can be created.
  152. * @return bool
  153. */
  154. public function canCreateAccounts() {
  155. return false;
  156. }
  157. /**
  158. * Add a user to the external authentication database.
  159. * Return true if successful.
  160. *
  161. * @param User $user - only the name should be assumed valid at this point
  162. * @param string $password
  163. * @param string $email
  164. * @param string $realname
  165. * @return bool
  166. */
  167. public function addUser( $user, $password, $email='', $realname='' ) {
  168. return true;
  169. }
  170. /**
  171. * Return true to prevent logins that don't authenticate here from being
  172. * checked against the local database's password fields.
  173. *
  174. * This is just a question, and shouldn't perform any actions.
  175. *
  176. * @return bool
  177. */
  178. public function strict() {
  179. return false;
  180. }
  181. /**
  182. * Check if a user should authenticate locally if the global authentication fails.
  183. * If either this or strict() returns true, local authentication is not used.
  184. *
  185. * @param $username String: username.
  186. * @return bool
  187. */
  188. public function strictUserAuth( $username ) {
  189. return false;
  190. }
  191. /**
  192. * When creating a user account, optionally fill in preferences and such.
  193. * For instance, you might pull the email address or real name from the
  194. * external user database.
  195. *
  196. * The User object is passed by reference so it can be modified; don't
  197. * forget the & on your function declaration.
  198. *
  199. * @param $user User object.
  200. * @param $autocreate bool True if user is being autocreated on login
  201. */
  202. public function initUser( &$user, $autocreate=false ) {
  203. # Override this to do something.
  204. }
  205. /**
  206. * If you want to munge the case of an account name before the final
  207. * check, now is your chance.
  208. */
  209. public function getCanonicalName( $username ) {
  210. return $username;
  211. }
  212. /**
  213. * Get an instance of a User object
  214. *
  215. * @param $user User
  216. * @public
  217. */
  218. public function getUserInstance( User &$user ) {
  219. return new AuthPluginUser( $user );
  220. }
  221. }
  222. class AuthPluginUser {
  223. function __construct( $user ) {
  224. # Override this!
  225. }
  226. public function getId() {
  227. # Override this!
  228. return -1;
  229. }
  230. public function isLocked() {
  231. # Override this!
  232. return false;
  233. }
  234. public function isHidden() {
  235. # Override this!
  236. return false;
  237. }
  238. public function resetAuthToken() {
  239. # Override this!
  240. return true;
  241. }
  242. }