CasAuthenticationPlugin.php 5.6 KB

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