CasAuthenticationPlugin.php 5.4 KB

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