key_auth.class.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. use ChamiloSession as Session;
  3. /**
  4. * Used to authenticate user with an access token. By default this method is disabled.
  5. * Method used primarily to make API calls: Rss, file upload.
  6. *
  7. * Access is granted only for the services that are enabled.
  8. *
  9. * To be secured this method must
  10. *
  11. * 1) be called through httpS to avoid sniffing (note that this is the case anyway with other methods such as cookies)
  12. * 2) the url/access token must be secured
  13. *
  14. * This authentication method is session less. This is to ensure that the navigator
  15. * do not receive an access cookie that will grant it access to other parts of the
  16. * application.
  17. *
  18. *
  19. * Usage:
  20. *
  21. * Enable KeyAuth for a specific service. Add the following lines so that
  22. * the key authentication method is enabled for a specific service before
  23. * calling global.inc.php.
  24. *
  25. * include_once '.../main/inc/autoload.inc.php';
  26. * KeyAuth::enable_services('my_service');
  27. * include_once '.../main/inc/global.inc.php';
  28. *
  29. *
  30. * Enable url access for a short period of time:
  31. *
  32. * token = KeyAuth::create_temp_token();
  33. * url = '...?access_token=' . $token ;
  34. *
  35. * @see AccessToken
  36. * @license see /license.txt
  37. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  38. */
  39. class KeyAuth
  40. {
  41. const PARAM_ACCESS_TOKEN = 'access_token';
  42. protected static $services = array();
  43. public static function create_temp_token($service = null, $duration = 60, $user_id = null)
  44. {
  45. return UserApiKeyManager::create_temp_token($service, $duration, $user_id);
  46. }
  47. /**
  48. * Returns enabled services
  49. *
  50. * @return array
  51. */
  52. public static function get_services()
  53. {
  54. return self::$services;
  55. }
  56. /**
  57. * Name of the service for which we are goint to check the API Key.
  58. * If empty it disables authentication.
  59. *
  60. * !! 10 chars max !!
  61. */
  62. public static function enable_services($_)
  63. {
  64. $args = func_get_args();
  65. $names = array();
  66. foreach ($args as $arg) {
  67. if (is_object($arg)) {
  68. $f = array($arg, 'get_service_name');
  69. $name = call_user_func($f);
  70. } else {
  71. $name = $arg;
  72. }
  73. $name = substr($name, 0, 10);
  74. self::$services[$name] = $name;
  75. }
  76. }
  77. public static function disable_services($_)
  78. {
  79. $args = func_get_args();
  80. $names = array();
  81. foreach ($args as $name) {
  82. $name = substr($name, 0, 10);
  83. unset(self::$services[$name]);
  84. }
  85. }
  86. public static function is_service_enabled($service)
  87. {
  88. $services = self::get_services();
  89. foreach ($services as $s) {
  90. if ($s == $service) {
  91. return true;
  92. }
  93. }
  94. return false;
  95. }
  96. public static function clear_services()
  97. {
  98. self::$services[$name] = array();
  99. }
  100. /**
  101. * Enable key authentication for the default service - i.e. chamilo
  102. */
  103. public static function enable()
  104. {
  105. self::enable_services(UserApiKeyManager::default_service());
  106. }
  107. public static function disable()
  108. {
  109. self::$services[$name] = array();
  110. }
  111. /**
  112. * Returns true if the key authentication method is enabled. False otherwise.
  113. * Default to false.
  114. *
  115. * @return bool
  116. */
  117. public static function is_enabled()
  118. {
  119. return !empty(self::$services);
  120. }
  121. /**
  122. * @return KeyAuth
  123. */
  124. public static function instance()
  125. {
  126. static $result = null;
  127. if (empty($result)) {
  128. $result = new self();
  129. }
  130. return $result;
  131. }
  132. protected function __construct()
  133. {
  134. }
  135. /**
  136. * Returns true if authentication accepts to run otherwise returns false.
  137. *
  138. * @return boolean
  139. */
  140. public function accept()
  141. {
  142. /**
  143. * Authentication method must be enabled
  144. */
  145. if (!self::is_enabled()) {
  146. return false;
  147. }
  148. $token = $this->get_access_token();
  149. if ($token->is_empty()) {
  150. return false;
  151. }
  152. $key = UserApiKeyManager::get_by_id($token->get_id());
  153. if (empty($key)) {
  154. return false;
  155. }
  156. /**
  157. * The service corresponding to the key must be enabled.
  158. */
  159. $service = $key['api_service'];
  160. if (!self::is_service_enabled($service)) {
  161. return false;
  162. }
  163. /**
  164. * User associated with the key must be active
  165. */
  166. $user = api_get_user_info($token->get_user_id());
  167. if (empty($user)) {
  168. return false;
  169. }
  170. if (!$user['active']) {
  171. return false;
  172. }
  173. /**
  174. * Token must be valid.
  175. */
  176. return $token->is_valid();
  177. }
  178. /**
  179. * If accepted tear down session, log in user and returns true.
  180. * If not accepted do nothing and returns false.
  181. *
  182. * @return boolean
  183. */
  184. public function login()
  185. {
  186. if (!$this->accept()) {
  187. return false;
  188. }
  189. /**
  190. * ! important this is to ensure we don't grant access for other parts
  191. */
  192. Session::destroy();
  193. /**
  194. * We don't allow redirection since access is granted only for this call
  195. */
  196. global $no_redirection, $noredirection;
  197. $no_redirection = true;
  198. $noredirection = true;
  199. Session::write('noredirection', $noredirection);
  200. $user_id = $this->get_user_id();
  201. $course_code = $this->get_course_code();
  202. $group_id = $this->get_group_id();
  203. Login::init_user($user_id, true);
  204. Login::init_course($course_code, true);
  205. Login::init_group($group_id, true);
  206. return true;
  207. }
  208. /**
  209. * Returns the request access token
  210. *
  211. * @return AccessToken
  212. */
  213. public function get_access_token()
  214. {
  215. $string = Request::get(self::PARAM_ACCESS_TOKEN);
  216. return AccessToken::parse($string);
  217. }
  218. public function get_user_id()
  219. {
  220. return $this->get_access_token()->get_user_id();
  221. }
  222. public function get_course_code()
  223. {
  224. return Request::get('cidReq', 0);
  225. }
  226. public function get_group_id()
  227. {
  228. return Request::get('gidReq', 0);
  229. }
  230. }