authenticationplugin.php 10 KB

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