CasAuthenticationPlugin.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Plugin to enable Single Sign On via CAS (Central Authentication Service)
  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. // We bundle the phpCAS library...
  33. set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/extlib/CAS');
  34. class CasAuthenticationPlugin extends AuthenticationPlugin
  35. {
  36. public $server;
  37. public $port = 443;
  38. public $path = '';
  39. public $takeOverLogin = false;
  40. public $user_whitelist = null;
  41. function checkPassword($username, $password)
  42. {
  43. global $casTempPassword;
  44. return ($casTempPassword == $password);
  45. }
  46. function onAutoload($cls)
  47. {
  48. switch ($cls)
  49. {
  50. case 'phpCAS':
  51. require_once(INSTALLDIR.'/plugins/CasAuthentication/extlib/CAS.php');
  52. return false;
  53. }
  54. // if it's not our exception, try standard places
  55. return parent::onAutoload($cls);
  56. }
  57. function onArgsInitialize(&$args)
  58. {
  59. if($this->takeOverLogin && $args['action'] == 'login')
  60. {
  61. $args['action'] = 'caslogin';
  62. }
  63. }
  64. function onStartInitializeRouter($m)
  65. {
  66. $m->connect('main/cas', array('action' => 'caslogin'));
  67. return true;
  68. }
  69. function onEndLoginGroupNav($action)
  70. {
  71. $action_name = $action->trimmed('action');
  72. $action->menuItem(common_local_url('caslogin'),
  73. // TRANS: Menu item. CAS is Central Authentication Service.
  74. _m('CAS'),
  75. // TRANS: Tooltip for menu item. CAS is Central Authentication Service.
  76. _m('Login or register with CAS.'),
  77. $action_name === 'caslogin');
  78. return true;
  79. }
  80. function onEndShowPageNotice($action)
  81. {
  82. $name = $action->trimmed('action');
  83. switch ($name)
  84. {
  85. case 'login':
  86. // TRANS: Invitation to users with a CAS account to log in using the service.
  87. // TRANS: "[CAS login]" is a link description. (%%action.caslogin%%) is the URL.
  88. // TRANS: These two elements may not be separated.
  89. $instr = _m('(Have an account with CAS? ' .
  90. 'Try our [CAS login](%%action.caslogin%%)!)');
  91. break;
  92. default:
  93. return true;
  94. }
  95. $output = common_markup_to_html($instr);
  96. $action->raw($output);
  97. return true;
  98. }
  99. function onLoginAction($action, &$login)
  100. {
  101. switch ($action)
  102. {
  103. case 'caslogin':
  104. $login = true;
  105. return false;
  106. default:
  107. return true;
  108. }
  109. }
  110. function onInitializePlugin(){
  111. parent::onInitializePlugin();
  112. if(!isset($this->server)){
  113. // TRANS: Exception thrown when the CAS Authentication plugin has been configured incorrectly.
  114. throw new Exception(_m("Specifying a server is required."));
  115. }
  116. if(!isset($this->port)){
  117. // TRANS: Exception thrown when the CAS Authentication plugin has been configured incorrectly.
  118. throw new Exception(_m("Specifying a port is required."));
  119. }
  120. if(!isset($this->path)){
  121. // TRANS: Exception thrown when the CAS Authentication plugin has been configured incorrectly.
  122. throw new Exception(_m("Specifying a path is required."));
  123. }
  124. //These values need to be accessible to a action object
  125. //I can't think of any other way than global variables
  126. //to allow the action instance to be able to see values :-(
  127. global $casSettings;
  128. $casSettings = array();
  129. $casSettings['server']=$this->server;
  130. $casSettings['port']=$this->port;
  131. $casSettings['path']=$this->path;
  132. $casSettings['takeOverLogin']=$this->takeOverLogin;
  133. $casSettings['user_whitelist']=$this->user_whitelist;
  134. }
  135. function onPluginVersion(array &$versions)
  136. {
  137. $versions[] = array('name' => 'CAS Authentication',
  138. 'version' => GNUSOCIAL_VERSION,
  139. 'author' => 'Craig Andrews',
  140. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/CasAuthentication',
  141. // TRANS: Plugin description. CAS is Central Authentication Service.
  142. 'rawdescription' => _m('The CAS Authentication plugin allows for StatusNet to handle authentication through CAS (Central Authentication Service).'));
  143. return true;
  144. }
  145. }